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

Commit 765b49b

Browse files
committed
Merge branch 'hotfix/317-https-scheme-detection'
Close #318 Fixes #317
2 parents caebdb4 + b11dfa9 commit 765b49b

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#318](https://github.com/zendframework/zend-diactoros/pull/318) fixes the logic for discovering whether an HTTPS scheme is in play
26+
to be case insensitive when comparing header and SAPI values, ensuring no
27+
false negative lookups occur.
28+
2529
- [#314](https://github.com/zendframework/zend-diactoros/pull/314) modifies error handling around opening a file resource within
2630
`Zend\Diactoros\Stream::setStream()` to no longer use the second argument to
2731
`set_error_handler()`, and instead check the error type in the handler itself;

src/functions/marshal_uri_from_sapi.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,15 @@ function marshalUriFromSapi(array $server, array $headers)
172172

173173
// URI scheme
174174
$scheme = 'http';
175-
$https = array_key_exists('HTTPS', $server) ? $server['HTTPS'] : false;
176-
if (($https && 'off' !== $https)
177-
|| $getHeaderFromArray('x-forwarded-proto', $headers, false) === 'https'
175+
if (array_key_exists('HTTPS', $server)) {
176+
$https = $server['HTTPS'];
177+
} elseif (array_key_exists('https', $server)) {
178+
$https = $server['https'];
179+
} else {
180+
$https = false;
181+
}
182+
if (($https && 'off' !== strtolower($https))
183+
|| strtolower($getHeaderFromArray('x-forwarded-proto', $headers, false)) === 'https'
178184
) {
179185
$scheme = 'https';
180186
}

test/ServerRequestFactoryTest.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,29 @@ public function testMarshalHostAndPortWillDetectPortInIpv6StyleHost()
306306
$this->assertNull($uri->getPort());
307307
}
308308

309-
public function testMarshalUriDetectsHttpsSchemeFromServerValue()
309+
/**
310+
* @return array
311+
*/
312+
public function httpsParamProvider()
313+
{
314+
return [
315+
'lowercase' => ['https'],
316+
'uppercase' => ['HTTPS'],
317+
];
318+
}
319+
320+
/**
321+
* @dataProvider httpsParamProvider
322+
* @param string $param
323+
*/
324+
public function testMarshalUriDetectsHttpsSchemeFromServerValue($param)
310325
{
311326
$request = new ServerRequest();
312327
$request = $request->withUri(new Uri('http://example.com/'));
313328
$request = $request->withHeader('Host', 'example.com');
314329

315330
$server = [
316-
'HTTPS' => true,
331+
$param => true,
317332
];
318333

319334
$uri = marshalUriFromSapi($server, $request->getHeaders());
@@ -322,14 +337,34 @@ public function testMarshalUriDetectsHttpsSchemeFromServerValue()
322337
$this->assertSame('https', $uri->getScheme());
323338
}
324339

325-
public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff()
340+
/**
341+
* @return iterable
342+
*/
343+
public function httpsDisableParamProvider()
344+
{
345+
foreach ($this->httpsParamProvider() as $key => $data) {
346+
$param = array_shift($data);
347+
foreach (['lowercase-off', 'uppercase-off'] as $type) {
348+
$key = sprintf('%s-%s', $key, $type);
349+
$value = false !== strpos($type, 'lowercase') ? 'off' : 'OFF';
350+
yield $key => [$param, $value];
351+
}
352+
}
353+
}
354+
355+
/**
356+
* @dataProvider httpsDisableParamProvider
357+
* @param string $param
358+
* @param string $value
359+
*/
360+
public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff($param, $value)
326361
{
327362
$request = new ServerRequest();
328363
$request = $request->withUri(new Uri('http://example.com/'));
329364
$request = $request->withHeader('Host', 'example.com');
330365

331366
$server = [
332-
'HTTPS' => 'off',
367+
$param => $value,
333368
];
334369

335370
$uri = marshalUriFromSapi($server, $request->getHeaders());
@@ -338,12 +373,16 @@ public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff()
338373
$this->assertSame('http', $uri->getScheme());
339374
}
340375

341-
public function testMarshalUriDetectsHttpsSchemeFromXForwardedProtoValue()
376+
/**
377+
* @dataProvider httpsParamProvider
378+
* @param string $xForwardedProto
379+
*/
380+
public function testMarshalUriDetectsHttpsSchemeFromXForwardedProtoValue($xForwardedProto)
342381
{
343382
$request = new ServerRequest();
344383
$request = $request->withUri(new Uri('http://example.com/'));
345384
$request = $request->withHeader('Host', 'example.com');
346-
$request = $request->withHeader('X-Forwarded-Proto', 'https');
385+
$request = $request->withHeader('X-Forwarded-Proto', $xForwardedProto);
347386

348387
$server = [];
349388

0 commit comments

Comments
 (0)