Skip to content

Commit 724562f

Browse files
Release 6.5.7 (#3022)
1 parent f092dd7 commit 724562f

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 6.5.7 - 2022-06-09
4+
5+
* Fix failure to strip Authorization header on HTTP downgrade
6+
* Fix failure to strip the Cookie header on change in host or HTTP downgrade
7+
38
## 6.5.6 - 2022-05-25
49

510
* Fix cross-domain cookie leakage

src/RedirectMiddleware.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function (ResponseInterface $response) use ($uri, $statusCode) {
141141
}
142142

143143
/**
144-
* Check for too many redirects
144+
* Check for too many redirects.
145145
*
146146
* @return void
147147
*
@@ -190,7 +190,7 @@ public function modifyRequest(
190190
$modify['body'] = '';
191191
}
192192

193-
$uri = $this->redirectUri($request, $response, $protocols);
193+
$uri = self::redirectUri($request, $response, $protocols);
194194
if (isset($options['idn_conversion']) && ($options['idn_conversion'] !== false)) {
195195
$idnOptions = ($options['idn_conversion'] === true) ? IDNA_DEFAULT : $options['idn_conversion'];
196196
$uri = Utils::idnUriConvert($uri, $idnOptions);
@@ -210,24 +210,50 @@ public function modifyRequest(
210210
$modify['remove_headers'][] = 'Referer';
211211
}
212212

213-
// Remove Authorization header if host is different.
214-
if ($request->getUri()->getHost() !== $modify['uri']->getHost()) {
213+
// Remove Authorization and Cookie headers if required.
214+
if (self::shouldStripSensitiveHeaders($request->getUri(), $modify['uri'])) {
215215
$modify['remove_headers'][] = 'Authorization';
216+
$modify['remove_headers'][] = 'Cookie';
216217
}
217218

218219
return Psr7\modify_request($request, $modify);
219220
}
220221

221222
/**
222-
* Set the appropriate URL on the request based on the location header
223+
* Determine if we should strip sensitive headers from the request.
224+
*
225+
* We return true if either of the following conditions are true:
226+
*
227+
* 1. the host is different;
228+
* 2. the scheme has changed, and now is non-https.
229+
*
230+
* @return bool
231+
*/
232+
private static function shouldStripSensitiveHeaders(
233+
UriInterface $originalUri,
234+
UriInterface $modifiedUri
235+
) {
236+
if (strcasecmp($originalUri->getHost(), $modifiedUri->getHost()) !== 0) {
237+
return true;
238+
}
239+
240+
if ($originalUri->getScheme() !== $modifiedUri->getScheme() && 'https' !== $modifiedUri->getScheme()) {
241+
return true;
242+
}
243+
244+
return false;
245+
}
246+
247+
/**
248+
* Set the appropriate URL on the request based on the location header.
223249
*
224250
* @param RequestInterface $request
225251
* @param ResponseInterface $response
226252
* @param array $protocols
227253
*
228254
* @return UriInterface
229255
*/
230-
private function redirectUri(
256+
private static function redirectUri(
231257
RequestInterface $request,
232258
ResponseInterface $response,
233259
array $protocols

tests/RedirectMiddlewareTest.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,31 +251,36 @@ public function testInvokesOnRedirectForRedirects()
251251
self::assertTrue($call);
252252
}
253253

254-
public function testRemoveAuthorizationHeaderOnRedirect()
254+
public function crossOriginRedirectProvider()
255255
{
256-
$mock = new MockHandler([
257-
new Response(302, ['Location' => 'http://test.com']),
258-
function (RequestInterface $request) {
259-
self::assertFalse($request->hasHeader('Authorization'));
260-
return new Response(200);
261-
}
262-
]);
263-
$handler = HandlerStack::create($mock);
264-
$client = new Client(['handler' => $handler]);
265-
$client->get('http://example.com?a=b', ['auth' => ['testuser', 'testpass']]);
256+
return [
257+
['http://example.com?a=b', 'http://test.com/', false],
258+
['https://example.com?a=b', 'https://test.com/', false],
259+
['http://example.com?a=b', 'https://test.com/', false],
260+
['https://example.com?a=b', 'http://test.com/', false],
261+
['http://example.com?a=b', 'http://example.com/', true],
262+
['https://example.com?a=b', 'https://example.com/', true],
263+
['http://example.com?a=b', 'https://example.com/', true],
264+
['https://example.com?a=b', 'http://example.com/', false],
265+
];
266266
}
267267

268-
public function testNotRemoveAuthorizationHeaderOnRedirect()
268+
/**
269+
* @dataProvider crossOriginRedirectProvider
270+
*/
271+
public function testHeadersTreatmentOnRedirect($originalUri, $targetUri, $shouldBePresent)
269272
{
270273
$mock = new MockHandler([
271-
new Response(302, ['Location' => 'http://example.com/2']),
272-
function (RequestInterface $request) {
273-
self::assertTrue($request->hasHeader('Authorization'));
274+
new Response(302, ['Location' => $targetUri]),
275+
function (RequestInterface $request) use ($shouldBePresent) {
276+
self::assertSame($shouldBePresent, $request->hasHeader('Authorization'));
277+
self::assertSame($shouldBePresent, $request->hasHeader('Cookie'));
278+
274279
return new Response(200);
275280
}
276281
]);
277282
$handler = HandlerStack::create($mock);
278283
$client = new Client(['handler' => $handler]);
279-
$client->get('http://example.com?a=b', ['auth' => ['testuser', 'testpass']]);
284+
$client->get($originalUri, ['auth' => ['testuser', 'testpass'], 'headers' => ['Cookie' => 'foo=bar']]);
280285
}
281286
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy