-
Notifications
You must be signed in to change notification settings - Fork 634
Do not abort cross-origin requests with an empty 200 (#1955) #1975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
svpernova09
wants to merge
1
commit into
php:master
Choose a base branch
from
svpernova09:prevent-returning-blank-responses
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace phpweb\Test\EndToEnd; | ||
|
|
||
| use PHPUnit\Framework; | ||
|
|
||
| /** | ||
| * Regression tests for the blank-page bug reported in | ||
| * https://github.com/php/web-php/issues/1955. | ||
| * | ||
| * The CORS guard in include/prepend.inc used to terminate the request with | ||
| * exit(10) whenever the Origin request header did not belong to php.net. | ||
| * exit(10) emits an integer exit status, not a body, so the response was a | ||
| * perfectly valid "200 OK" with a zero byte body. | ||
| * | ||
| * Since Origin was not part of the CDN cache key and no Vary header was sent, | ||
| * a single request carrying a foreign Origin header was enough for the CDN to | ||
| * store that empty 200 and replay it to every subsequent visitor as a blank | ||
| * white page, until the cache was purged. | ||
| * | ||
| * SmokeTest only asserts the status code, so it could not catch this: a blank | ||
| * page is still a 200. | ||
| */ | ||
| #[Framework\Attributes\CoversNothing] | ||
| final class OriginHeaderTest extends Framework\TestCase | ||
| { | ||
| /** | ||
| * A cross-origin request must never produce an empty body, because that | ||
| * body is what the CDN caches and replays to everyone else. | ||
| */ | ||
| #[Framework\Attributes\DataProvider('provideForeignOrigin')] | ||
| public function testForeignOriginDoesNotReturnBlankPage(string $origin): void | ||
| { | ||
| $response = self::get('/', $origin); | ||
|
|
||
| self::assertSame(200, $response['status'], sprintf( | ||
| 'Expected "/" to return 200 for Origin "%s", got "%d".', | ||
| $origin, | ||
| $response['status'], | ||
| )); | ||
|
|
||
| self::assertNotSame('', $response['body'], sprintf( | ||
| 'Blank page: "/" returned a %d response with a zero byte body for Origin "%s". ' | ||
| . 'The CDN caches this empty body and serves it to all visitors.', | ||
| $response['status'], | ||
| $origin, | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Every page is affected, not just the homepage. | ||
| */ | ||
| #[Framework\Attributes\DataProvider('providePath')] | ||
| public function testPathDoesNotReturnBlankPageForForeignOrigin(string $path): void | ||
| { | ||
| $response = self::get($path, 'https://example.com'); | ||
|
|
||
| self::assertNotSame('', $response['body'], sprintf( | ||
| 'Blank page: "%s" returned a %d response with a zero byte body for a foreign Origin.', | ||
| $path, | ||
| $response['status'], | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Guards the fix from the other side: legitimate php.net origins, and | ||
| * requests with no Origin at all, must keep working. | ||
| */ | ||
| #[Framework\Attributes\DataProvider('provideAllowedOrigin')] | ||
| public function testAllowedOriginReturnsContent(?string $origin): void | ||
| { | ||
| $response = self::get('/', $origin); | ||
|
|
||
| self::assertSame(200, $response['status']); | ||
| self::assertNotSame('', $response['body']); | ||
| } | ||
|
|
||
| /** | ||
| * The CORS headers depend on the Origin request header, so shared caches | ||
| * have to be told not to reuse a response across origins. | ||
| */ | ||
| public function testResponseVariesOnOrigin(): void | ||
| { | ||
| $response = self::get('/'); | ||
|
|
||
| self::assertStringContainsStringIgnoringCase('origin', $response['vary'], sprintf( | ||
| 'Expected a "Vary: Origin" response header, got "%s".', | ||
| $response['vary'], | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * The blank page must not be traded for a cross site request forgery hole: | ||
| * the state changing endpoints have no CSRF tokens, so a cross origin POST | ||
| * still has to be rejected. | ||
| */ | ||
| public function testCrossOriginPostIsRejected(): void | ||
| { | ||
| $response = self::post('/manual/vote-note.php', 'https://example.com'); | ||
|
|
||
| self::assertSame(403, $response['status'], sprintf( | ||
| 'Expected a cross-origin POST to be rejected with 403, got "%d".', | ||
| $response['status'], | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Rejections still need a body, otherwise they are just the blank page | ||
| * again wearing a different status code. | ||
| */ | ||
| public function testRejectedCrossOriginPostStillHasABody(): void | ||
| { | ||
| $response = self::post('/manual/vote-note.php', 'https://example.com'); | ||
|
|
||
| self::assertNotSame('', $response['body'], 'A rejected cross-origin POST returned an empty body.'); | ||
| } | ||
|
|
||
| /** | ||
| * Same-origin POSTs must keep working. | ||
| */ | ||
| public function testSameOriginPostIsNotRejected(): void | ||
| { | ||
| $httpHost = getenv('HTTP_HOST'); | ||
|
|
||
| $response = self::post('/manual/vote-note.php', sprintf('http://%s', $httpHost)); | ||
|
|
||
| self::assertNotSame(403, $response['status'], 'A same-origin POST was rejected.'); | ||
| } | ||
|
|
||
| /** | ||
| * @return \Generator<string, array{0: string}> | ||
| */ | ||
| public static function provideForeignOrigin(): \Generator | ||
| { | ||
| $origins = [ | ||
| // Any unrelated site embedding or fetching php.net. | ||
| 'https://example.com', | ||
| // Browsers send a literal "null" origin for sandboxed iframes, | ||
| // documents from data:/file: URLs, and some cross-origin redirects. | ||
| 'null', | ||
| // Must not be treated as php.net just because it contains it. | ||
| 'https://evil-php.net.attacker.com', | ||
| 'https://notphp.net', | ||
| ]; | ||
|
|
||
| foreach ($origins as $origin) { | ||
| yield $origin => [$origin]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return \Generator<string, array{0: ?string}> | ||
| */ | ||
| public static function provideAllowedOrigin(): \Generator | ||
| { | ||
| $origins = [ | ||
| 'no Origin header' => null, | ||
| 'https://www.php.net' => 'https://www.php.net', | ||
| 'https://php.net' => 'https://php.net', | ||
| 'https://qa.php.net' => 'https://qa.php.net', | ||
| ]; | ||
|
|
||
| foreach ($origins as $name => $origin) { | ||
| yield $name => [$origin]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return \Generator<string, array{0: string}> | ||
| */ | ||
| public static function providePath(): \Generator | ||
| { | ||
| $paths = [ | ||
| '/', | ||
| '/downloads.php', | ||
| '/contact.php', | ||
| '/manual/en/function.str-replace.php', | ||
| '/releases/', | ||
| ]; | ||
|
|
||
| foreach ($paths as $path) { | ||
| yield $path => [$path]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return array{status: int, body: string, vary: string} | ||
| */ | ||
| private static function post(string $path, string $origin): array | ||
| { | ||
| return self::get($path, $origin, ['vote' => 'up']); | ||
| } | ||
|
|
||
| /** | ||
| * @param ?array<string, string> $postFields | ||
| * | ||
| * @return array{status: int, body: string, vary: string} | ||
| */ | ||
| private static function get( | ||
| string $path, | ||
| ?string $origin = null, | ||
| ?array $postFields = null, | ||
| ): array | ||
| { | ||
| $httpHost = getenv('HTTP_HOST'); | ||
|
|
||
| if (!is_string($httpHost)) { | ||
| throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.'); | ||
| } | ||
|
|
||
| $headers = []; | ||
|
|
||
| if (is_string($origin)) { | ||
| $headers[] = sprintf('Origin: %s', $origin); | ||
| } | ||
|
|
||
| $vary = ''; | ||
|
|
||
| $handle = curl_init(); | ||
|
|
||
| if (is_array($postFields)) { | ||
| curl_setopt($handle, CURLOPT_POST, true); | ||
| curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($postFields)); | ||
| } | ||
|
|
||
| curl_setopt_array($handle, [ | ||
| CURLOPT_RETURNTRANSFER => true, | ||
| CURLOPT_HTTPHEADER => $headers, | ||
| CURLOPT_URL => sprintf('http://%s%s', $httpHost, $path), | ||
| CURLOPT_HEADERFUNCTION => static function ($handle, string $header) use (&$vary): int { | ||
| if (stripos($header, 'vary:') === 0) { | ||
| $vary = trim(substr($header, strlen('vary:'))); | ||
| } | ||
|
|
||
| return strlen($header); | ||
| }, | ||
| ]); | ||
|
|
||
| $body = curl_exec($handle); | ||
| $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); | ||
|
|
||
| curl_close($handle); | ||
|
|
||
| if (!is_string($body)) { | ||
| throw new \RuntimeException(sprintf('Failed to request "%s".', $path)); | ||
| } | ||
|
|
||
| return [ | ||
| 'status' => $status, | ||
| 'body' => $body, | ||
| 'vary' => $vary, | ||
| ]; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you're allowing these three methods without the CORS origin server to match.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not an access gate — it only decides whether the response carries CORS headers. Safe methods are served as usual, because these pages are public anyway: drop the Origin header and you get the same bytes, on master too. What stops a foreign page reading the response is the missing Access-Control-Allow-Origin, not us refusing to answer.
Unsafe methods are the separate branch below, and those we still reject; with a 403, a body and no-store, rather than the empty 200 that was getting cached.
We could also do something like this if it makes the intentions clearer: