Skip to content

Commit a6bc468

Browse files
authored
fix: ignore media ranges without a subtype in Negotiate::media()
`Negotiate::matchTypes()` split each value on "/" and read the subtype without checking that there was one, so an `Accept` header with a value such as "json", or an empty list element, threw an `ErrorException` ("Undefined array key 1"). Such values are not media types and now do not match.
1 parent 345262a commit a6bc468

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎system/HTTP/Negotiate.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ protected function matchParameters(array $acceptable, array $supported): bool
409409
*/
410410
public function matchTypes(array $acceptable, array $supported): bool
411411
{
412+
// A value without a subtype, such as a malformed "json", is not a media type.
413+
if (! str_contains($acceptable['value'], '/') || ! str_contains($supported['value'], '/')) {
414+
return false;
415+
}
416+
412417
// PHPDocumentor v2 cannot parse yet the shorter list syntax,
413418
// causing no API generation for the file.
414419
[$aType, $aSubType] = explode('/', $acceptable['value']);

‎tests/system/HTTP/NegotiateTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ public function testBestMatchOnlyLowQuality(): void
197197
$this->assertSame('popcorn/cheddar', $this->negotiate->media(['popcorn/cheddar', 'apple/mac'], false));
198198
}
199199

200+
public function testBestMatchIgnoresMediaRangesWithoutSubtype(): void
201+
{
202+
$this->request->setHeader('Accept', 'json, application/json');
203+
$this->assertSame('application/json', $this->negotiate->media(['text/html', 'application/json'], true));
204+
205+
$this->request->setHeader('Accept', ', text/html');
206+
$this->assertSame('text/html', $this->negotiate->media(['application/json', 'text/html'], true));
207+
}
208+
200209
public function testParameterMatching(): void
201210
{
202211
$this->request->setHeader('Accept', 'popcorn/cheddar;a=0;b=1');

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bugs Fixed
4242
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive.
4343
- **HTTP:** Fixed a bug where ``IncomingRequest`` did not read the request body when PHP's ``post_max_size`` was set to ``0`` or ``-1`` (unlimited).
4444
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted proxy when the value contained uppercase letters or surrounding whitespace, or was represented as a single-element array.
45+
- **HTTP:** Fixed a bug where ``Negotiate::media()`` threw an ``ErrorException`` when the ``Accept`` header contained a value without a subtype, such as ``json`` or an empty list element. Such values are now ignored.
4546
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
4647
- **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.
4748

0 commit comments

Comments
 (0)