Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit c6ea797

Browse files
oauth2-middlewareweierophinney
authored andcommitted
Bug fix: filter out header sources only if the value is an empty string
A previous change checked for a truthy `$value` in SAPI header sources within the `marshal_headers_from_sapi()` function, which could lead to problems when the value was `0` or `'0'`. This patch adds tests to prevent future changes from causing the issue to re-surface, as well as a fix for the problem.
1 parent 0c108cc commit c6ea797

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/functions/marshal_headers_from_sapi.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ function marshalHeadersFromSapi(array $server) : array
3535
}
3636
}
3737

38-
if ($value && strpos($key, 'HTTP_') === 0) {
38+
if ($value === '') {
39+
continue;
40+
}
41+
42+
if (strpos($key, 'HTTP_') === 0) {
3943
$name = strtr(strtolower(substr($key, 5)), '_', '-');
4044
$headers[$name] = $value;
4145
continue;
4246
}
4347

44-
if ($value && strpos($key, 'CONTENT_') === 0) {
48+
if (strpos($key, 'CONTENT_') === 0) {
4549
$name = 'content-' . strtolower(substr($key, 8));
4650
$headers[$name] = $value;
4751
continue;
4852
}
49-
50-
$headers[$key] = $value;
5153
}
5254

5355
return $headers;

test/ServerRequestFactoryTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ public function testMarshalsExpectedHeadersFromServerArray()
6060
$this->assertSame($expected, marshalHeadersFromSapi($server));
6161
}
6262

63+
public function testMarshalInvalidHeadersStrippedFromServerArray()
64+
{
65+
$server = [
66+
'COOKIE' => 'COOKIE',
67+
'HTTP_AUTHORIZATION' => 'token',
68+
'MD5' => 'CONTENT-MD5',
69+
'CONTENT_LENGTH' => 'UNSPECIFIED',
70+
];
71+
72+
//Headers that don't begin with HTTP_ or CONTENT_ will not be returned
73+
$expected = [
74+
'authorization' => 'token',
75+
'content-length' => 'UNSPECIFIED',
76+
];
77+
$this->assertSame($expected, marshalHeadersFromSapi($server));
78+
}
79+
6380
public function testMarshalsVariablesPrefixedByApacheFromServerArray()
6481
{
6582
// Non-prefixed versions will be preferred
@@ -427,10 +444,27 @@ public function testFromGlobalsUsesCookieHeaderInsteadOfCookieSuperGlobal()
427444
*/
428445
public function testCreateFromGlobalsShouldPreserveKeysWhenCreatedWithAZeroValue()
429446
{
430-
$_SERVER['Accept'] = '0';
447+
$_SERVER['HTTP_ACCEPT'] = '0';
448+
$_SERVER['CONTENT_LENGTH'] = '0';
431449

432450
$request = ServerRequestFactory::fromGlobals();
433-
$this->assertSame('0', $request->getHeaderLine('Accept'));
451+
$this->assertSame('0', $request->getHeaderLine('accept'));
452+
$this->assertSame('0', $request->getHeaderLine('content-length'));
453+
}
454+
455+
/**
456+
* @runInSeparateProcess
457+
* @preserveGlobalState
458+
*/
459+
public function testCreateFromGlobalsShouldNotPreserveKeysWhenCreatedWithAnEmptyValue()
460+
{
461+
$_SERVER['HTTP_ACCEPT'] = '';
462+
$_SERVER['CONTENT_LENGTH'] = '';
463+
464+
$request = ServerRequestFactory::fromGlobals();
465+
466+
$this->assertFalse($request->hasHeader('accept'));
467+
$this->assertFalse($request->hasHeader('content-length'));
434468
}
435469

436470
/**

0 commit comments

Comments
 (0)