|
30 | 30 | use PHPUnit\Framework\MockObject\Exception; |
31 | 31 | use Psr\Container\ContainerExceptionInterface; |
32 | 32 | use Psr\Container\NotFoundExceptionInterface; |
| 33 | +use SP\Application\Notification\Ports\MailService; |
| 34 | +use SP\Domain\Core\Messages\MailMessage; |
33 | 35 | use SP\Domain\User\Models\User as UserModel; |
34 | 36 | use SP\Domain\Common\Dtos\QueryResult; |
35 | 37 | use SP\Tests\Support\Generators\UserDataGenerator; |
@@ -78,4 +80,62 @@ public function testSaveRequestBuildsTheResetMailWithoutFataling(): void |
78 | 80 |
|
79 | 81 | $this->expectOutputRegex('/"status":"OK","description":"Request sent"/'); |
80 | 82 | } |
| 83 | + |
| 84 | + /** |
| 85 | + * The mailed link points where the installation lives, not where the caller said it does. |
| 86 | + * |
| 87 | + * `saveRequestAction()` needs no session, and the base URI came from |
| 88 | + * `UriContextInterface::getWebUri()`, which prefers `Forwarded` / `X-Forwarded-Host`. Nothing |
| 89 | + * in this application calls `setTrustedProxies()`, so those headers are whatever the caller |
| 90 | + * sent — verified against the running instance, where `X-Forwarded-Host: evil.example.com` |
| 91 | + * comes straight back out. |
| 92 | + * |
| 93 | + * So an unauthenticated caller who knew a login and its email address could choose the host in |
| 94 | + * the message the real user then received: a genuine mail, from the real installation, whose |
| 95 | + * link hands the one-time hash to somebody else's server. |
| 96 | + * |
| 97 | + * @throws ContainerExceptionInterface |
| 98 | + * @throws Exception |
| 99 | + * @throws NotFoundExceptionInterface |
| 100 | + */ |
| 101 | + public function testTheResetLinkIgnoresAForwardedHost(): void |
| 102 | + { |
| 103 | + $login = 'resetme'; |
| 104 | + $email = 'resetme@example.com'; |
| 105 | + |
| 106 | + $userData = UserDataGenerator::factory()->buildUserData()->mutate( |
| 107 | + ['login' => $login, 'email' => $email, 'isDisabled' => false, 'isLdap' => false] |
| 108 | + ); |
| 109 | + |
| 110 | + $this->addDatabaseMapperResolver(UserModel::class, new QueryResult([$userData])); |
| 111 | + |
| 112 | + $sent = null; |
| 113 | + $mailService = $this->createStub(MailService::class); |
| 114 | + $mailService->method('send')->willReturnCallback( |
| 115 | + static function (string $subject, string|array $to, MailMessage $mailMessage) use (&$sent): void { |
| 116 | + $sent = $mailMessage->composeText(); |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + $container = $this->buildContainer( |
| 121 | + IntegrationTestCase::buildRequest( |
| 122 | + 'post', |
| 123 | + 'index.php', |
| 124 | + ['r' => 'userPassReset/saveRequest'], |
| 125 | + ['login' => $login, 'email' => $email], |
| 126 | + [], |
| 127 | + self::CSRF_TOKEN, |
| 128 | + ['HTTP_X_FORWARDED_HOST' => 'evil.example.com', 'HTTP_X_FORWARDED_PROTO' => 'https'] |
| 129 | + ), |
| 130 | + [MailService::class => $mailService] |
| 131 | + ); |
| 132 | + |
| 133 | + IntegrationTestCase::runApp($container); |
| 134 | + |
| 135 | + self::assertNotNull($sent, 'the reset mail has to be sent for this to say anything'); |
| 136 | + self::assertStringNotContainsString('evil.example.com', $sent); |
| 137 | + self::assertStringContainsString('localhost', $sent); |
| 138 | + |
| 139 | + $this->expectOutputRegex('/"status":"OK","description":"Request sent"/'); |
| 140 | + } |
81 | 141 | } |
0 commit comments