Skip to content

Commit 6a6bbcb

Browse files
committed
SignUrl: Fix double ??
1 parent fe52677 commit 6a6bbcb

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Plugin/SignedUrl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function signUrl(
8181

8282
$token = $this->getToken($this->buildUrl($signUrl), $allowedHttpMethods, $expire, $mode, $value);
8383

84-
$parsedUrl['query'] = ($parsedUrl['query'] ?? '') . ((($parsedUrl['query'] ?? '') === '') ? '?' : '&')
84+
$parsedUrl['query'] = ltrim(($parsedUrl['query'] ?? '') . '&', '&')
8585
. self::URL_QUERY_TOKEN_KEY . '=' . urlencode($token);
8686

8787
return $this->buildUrl($parsedUrl);
@@ -321,7 +321,7 @@ protected function sendRedirectResponse(string $canonicalUrl): void
321321
*/
322322
protected function normalizeUrl(array $url): array
323323
{
324-
$url['path'] = ($url['path'] ?? '') === '' ? '/' : ($url['path']??'');
324+
$url['path'] = ($url['path'] ?? '') === '' ? '/' : ($url['path'] ?? '');
325325
unset($url['fragment']);
326326
/** @var ParsedUrl $url (bypass PhpStan bug) */
327327
return $url;

tests/Plugin/SignUrlTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ public function testSign(): void
2121
{
2222
$audience = 'test.' . __FUNCTION__;
2323

24+
$plugin = new SignedUrl(self::KEY_HS256, 'HS256', $audience);
25+
$plugin->setTimestamp(1600000000);
26+
$token = $plugin->signUrl('https://host.tld/path', 1600000600);
27+
$expected = 'https://host.tld/path?_debug=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjei5yZWRiaXQuZGVidWcudXJsIiwiYXVkIjoidGVzdC50ZXN0U2lnbiIsImlhdCI6MTYwMDAwMDAwMCwiZXhwIjoxNjAwMDAwNjAwLCJzdWIiOiJodHRwczpcL1wvaG9zdC50bGRcL3BhdGgiLCJtZXRoIjpbImdldCJdLCJtb2QiOjAsInZhbCI6MX0.MTZOii4lQ2WCk1UltRx_e9T5vCT7nq8G3kh4D8EXy7s';
28+
Assert::equal($expected, $token);
29+
}
30+
31+
public function testSignQuery(): void
32+
{
33+
$audience = 'test.' . __FUNCTION__;
34+
2435
$plugin = new SignedUrl(self::KEY_HS256, 'HS256', $audience);
2536
$plugin->setTimestamp(1600000000);
2637
$token = $plugin->signUrl('https://host.tld/path?query=value', 1600000600);
27-
$expected = 'https://host.tld/path?query=value&_debug=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjei5yZWRiaXQuZGVidWcudXJsIiwiYXVkIjoidGVzdC50ZXN0U2lnbiIsImlhdCI6MTYwMDAwMDAwMCwiZXhwIjoxNjAwMDAwNjAwLCJzdWIiOiJodHRwczpcL1wvaG9zdC50bGRcL3BhdGg_cXVlcnk9dmFsdWUiLCJtZXRoIjpbImdldCJdLCJtb2QiOjAsInZhbCI6MX0.E2__15ZLCOvLRA1jG3tq47DctbZ44sLYYDLXGElMrDs';
38+
$expected = 'https://host.tld/path?query=value&_debug=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjei5yZWRiaXQuZGVidWcudXJsIiwiYXVkIjoidGVzdC50ZXN0U2lnblF1ZXJ5IiwiaWF0IjoxNjAwMDAwMDAwLCJleHAiOjE2MDAwMDA2MDAsInN1YiI6Imh0dHBzOlwvXC9ob3N0LnRsZFwvcGF0aD9xdWVyeT12YWx1ZSIsIm1ldGgiOlsiZ2V0Il0sIm1vZCI6MCwidmFsIjoxfQ.RrO7BCmdgldB7OlEIpudBWo8P33xDh-MsNjtZC34CNY';
2839
Assert::equal($expected, $token);
2940
}
3041

0 commit comments

Comments
 (0)