Skip to content

RedirectPlugin: Default to empty path when Location doesn't specify any #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 2.5.1 - 2022-09-??

### Fixed

- Fixes false positive circular detection in RedirectPlugin in cases when target location does not contain path

## 2.5.0 - 2021-11-26

### Added
Expand Down
6 changes: 6 additions & 0 deletions spec/Plugin/RedirectPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ public function it_replace_full_url(

$request->getUri()->willReturn($uri);
$uri->withScheme('https')->willReturn($uriRedirect);
$uri->withPath('/redirect')->willReturn($uri);
$uri->withQuery('query')->willReturn($uri);
$uri->withFragment('fragment')->willReturn($uri);
$uriRedirect->withHost('server.com')->willReturn($uriRedirect);
$uriRedirect->withPort('8000')->willReturn($uriRedirect);
$uriRedirect->withPath('/redirect')->willReturn($uriRedirect);
Expand Down Expand Up @@ -520,6 +523,9 @@ public function it_redirects_http_to_https(
$request->getUri()->willReturn($uri);
$request->withUri($uriRedirect)->willReturn($modifiedRequest);
$uri->__toString()->willReturn('http://my-site.com/original');
$uri->withPath('/original')->willReturn($uri);
$uri->withFragment('')->willReturn($uri);
$uri->withQuery('')->willReturn($uri);

$uri->withScheme('https')->willReturn($uriRedirect);
$uriRedirect->withHost('my-site.com')->willReturn($uriRedirect);
Expand Down
21 changes: 5 additions & 16 deletions src/Plugin/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ private function createUri(ResponseInterface $redirectResponse, RequestInterface
}

$uri = $originalRequest->getUri();
$uri = $uri
->withPath(array_key_exists('path', $parsedLocation) ? $parsedLocation['path'] : '')
->withQuery(array_key_exists('query', $parsedLocation) ? $parsedLocation['query'] : '')
->withFragment(array_key_exists('fragment', $parsedLocation) ? $parsedLocation['fragment'] : '')
;

if (array_key_exists('scheme', $parsedLocation)) {
$uri = $uri->withScheme($parsedLocation['scheme']);
Expand All @@ -244,22 +249,6 @@ private function createUri(ResponseInterface $redirectResponse, RequestInterface
$uri = $uri->withPort($parsedLocation['port']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am actually not sure about this one either. if i was on http://localhost:8080/foo and i get redirect a header https://php-http.org/bar i would not expect to stay on port 8080... the only case where the port should be kept is when there is also no host in the redirection... wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Will change

}

if (array_key_exists('path', $parsedLocation)) {
$uri = $uri->withPath($parsedLocation['path']);
}

if (array_key_exists('query', $parsedLocation)) {
$uri = $uri->withQuery($parsedLocation['query']);
} else {
$uri = $uri->withQuery('');
}

if (array_key_exists('fragment', $parsedLocation)) {
$uri = $uri->withFragment($parsedLocation['fragment']);
} else {
$uri = $uri->withFragment('');
}

return $uri;
}
}
48 changes: 48 additions & 0 deletions tests/Plugin/RedirectPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace Plugin;

use Http\Client\Common\Exception\CircularRedirectionException;
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Promise\FulfilledPromise;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class RedirectPluginTest extends TestCase
{
public function testCircularDetection(): void
{
$this->expectException(CircularRedirectionException::class);
(new RedirectPlugin())->handleRequest(
new Request('GET', 'https://example.com/path?query=value'),
function () {
return new FulfilledPromise(new Response(302, ['Location' => 'https://example.com/path?query=value']));
},
function () {}
)->wait();
}

/**
* @testWith ["https://example.com/path?query=value", "https://example.com?query=value", "https://example.com?query=value"]
* ["https://example.com/path?query=value", "https://example.com/?query=value", "https://example.com/?query=value"]
* ["https://example.com", "https://example.com?query=value", "https://example.com?query=value"]
*/
public function testTargetUriMappingFromLocationHeader(string $originalUri, string $locationUri, string $targetUri): void
{
$response = (new RedirectPlugin())->handleRequest(
new Request('GET', $originalUri),
function () use ($locationUri) {
return new FulfilledPromise(new Response(302, ['Location' => $locationUri]));
},
function (RequestInterface $request) {
return new FulfilledPromise(new Response(200, ['uri' => $request->getUri()->__toString()]));
}
)->wait();
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals($targetUri, $response->getHeaderLine('uri'));
}
}