Skip to content
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
16 changes: 15 additions & 1 deletion src/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,29 @@ public function getLoginRedirect(ServerRequestInterface $request): ?string
) {
return null;
}
$value = (string)$params[$redirectParam];

$parsed = parse_url((string)$params[$redirectParam]);
// In the `Location` header, Browsers normalize \ to /
// (see WHATWG URL Standard).
// We do the same to prevent injection via \ sequences.
$normalized = str_replace('\\', '/', $value);

// A leading run of `//` or `\\` are rejected
if (str_starts_with($normalized, '//')) {
return null;
}

$parsed = parse_url($normalized);
if ($parsed === false) {
return null;
}
if (!empty($parsed['host']) || !empty($parsed['scheme'])) {
return null;
}
$parsed += ['path' => '/', 'query' => ''];
if (str_contains($parsed['path'], '\\')) {
return null;
}
if (strlen($parsed['path']) && $parsed['path'][0] !== '/') {
$parsed['path'] = '/' . $parsed['path'];
}
Expand Down
35 changes: 34 additions & 1 deletion tests/TestCase/AuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public function testGetUnauthenticatedRedirectUrlWithBasePath(): void
);
}

public function testGetLoginRedirect(): void
public function testGetLoginRedirectInvalid(): void
{
$service = new AuthenticationService([
'unauthenticatedRedirect' => '/users/login',
Expand Down Expand Up @@ -863,6 +863,39 @@ public function testGetLoginRedirect(): void
'/path/with?query=string',
$service->getLoginRedirect($request),
);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/login'],
['redirect' => '/\\evil.com'],
);
$this->assertNull(
$service->getLoginRedirect($request),
);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/login'],
['redirect' => '\\/\\evil.com'],
);
$this->assertNull(
$service->getLoginRedirect($request),
);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/login'],
['redirect' => '\\evil.com/path'],
);
$this->assertSame(
'/evil.com/path',
$service->getLoginRedirect($request),
);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/login'],
['redirect' => '\\\\evil.com/path'],
);
$this->assertNull(
$service->getLoginRedirect($request),
);
}

/**
Expand Down
Loading