Skip to content

Commit c54c65e

Browse files
authored
fix(HTTP): prevent false positive header matches in Cors::hasResponseHeaders() (#10731)
1 parent 59e5eea commit c54c65e

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Release Date: Unreleased
1616

1717
## Bugs Fixed
1818

19+
- **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers.
1920
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
2021
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
2122
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.

‎system/HTTP/Cors.php‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,23 @@ public function hasResponseHeaders(RequestInterface $request, ResponseInterface
240240
return false;
241241
}
242242

243-
return ! ($this->config['exposedHeaders'] !== [] && (! $response->hasHeader('Access-Control-Expose-Headers') || ! str_contains(
244-
$response->getHeaderLine('Access-Control-Expose-Headers'),
245-
implode(', ', $this->config['exposedHeaders']),
246-
)));
243+
if ($this->config['exposedHeaders'] !== []) {
244+
if (! $response->hasHeader('Access-Control-Expose-Headers')) {
245+
return false;
246+
}
247+
248+
$exposedHeaders = array_map(
249+
strtolower(...),
250+
array_map(trim(...), explode(',', $response->getHeaderLine('Access-Control-Expose-Headers'))),
251+
);
252+
253+
foreach ($this->config['exposedHeaders'] as $header) {
254+
if (! in_array(strtolower($header), $exposedHeaders, true)) {
255+
return false;
256+
}
257+
}
258+
}
259+
260+
return true;
247261
}
248262
}

‎tests/system/HTTP/CorsTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,67 @@ public function testHasResponseHeadersTrue(): void
566566
$cors->hasResponseHeaders($request, $response),
567567
);
568568
}
569+
570+
public function testHasResponseHeadersReturnsFalseWhenExposedHeaderIsSubstringOfExistingHeader(): void
571+
{
572+
$config = $this->getDefaultConfig();
573+
$config['allowedOrigins'] = ['http://localhost:8080'];
574+
$config['allowedMethods'] = ['GET'];
575+
$config['exposedHeaders'] = ['X-Custom'];
576+
$cors = $this->createCors($config);
577+
578+
$request = $this->createRequest()
579+
->withMethod('GET')
580+
->setHeader('Origin', 'http://localhost:8080');
581+
582+
$response = service('redirectresponse', null, false);
583+
$response->setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
584+
$response->setHeader('Access-Control-Expose-Headers', 'X-Custom-Header');
585+
586+
$this->assertFalse(
587+
$cors->hasResponseHeaders($request, $response),
588+
);
589+
}
590+
591+
public function testHasResponseHeadersWithExposedHeadersInDifferentOrder(): void
592+
{
593+
$config = $this->getDefaultConfig();
594+
$config['allowedOrigins'] = ['http://localhost:8080'];
595+
$config['allowedMethods'] = ['GET'];
596+
$config['exposedHeaders'] = ['X-Header-One', 'X-Header-Two'];
597+
$cors = $this->createCors($config);
598+
599+
$request = $this->createRequest()
600+
->withMethod('GET')
601+
->setHeader('Origin', 'http://localhost:8080');
602+
603+
$response = service('redirectresponse', null, false);
604+
$response->setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
605+
$response->setHeader('Access-Control-Expose-Headers', 'X-Header-Two, X-Header-One');
606+
607+
$this->assertTrue(
608+
$cors->hasResponseHeaders($request, $response),
609+
);
610+
}
611+
612+
public function testHasResponseHeadersWithExposedHeadersCaseInsensitive(): void
613+
{
614+
$config = $this->getDefaultConfig();
615+
$config['allowedOrigins'] = ['http://localhost:8080'];
616+
$config['allowedMethods'] = ['GET'];
617+
$config['exposedHeaders'] = ['X-Custom-Header'];
618+
$cors = $this->createCors($config);
619+
620+
$request = $this->createRequest()
621+
->withMethod('GET')
622+
->setHeader('Origin', 'http://localhost:8080');
623+
624+
$response = service('redirectresponse', null, false);
625+
$response->setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
626+
$response->setHeader('Access-Control-Expose-Headers', 'x-custom-header');
627+
628+
$this->assertTrue(
629+
$cors->hasResponseHeaders($request, $response),
630+
);
631+
}
569632
}

0 commit comments

Comments
 (0)