Skip to content
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

URLs should not contains reserved characters according to RFC 3986 #35885

Merged
merged 2 commits into from
Sep 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ protected function setUp(): void

public function testGetCurrentBase64Url()
{
$this->assertEquals('aHR0cDovL2xvY2FsaG9zdDo4MS8,', $this->_helper->getCurrentBase64Url());
$this->assertEquals('aHR0cDovL2xvY2FsaG9zdDo4MS8~', $this->_helper->getCurrentBase64Url());
}

public function testGetEncodedUrl()
{
$this->assertEquals('aHR0cDovL2xvY2FsaG9zdDo4MS8,', $this->_helper->getEncodedUrl());
$this->assertEquals('aHR0cDovL2V4YW1wbGUuY29tLw,,', $this->_helper->getEncodedUrl('http://example.com/'));
$this->assertEquals('aHR0cDovL2xvY2FsaG9zdDo4MS8~', $this->_helper->getEncodedUrl());
$this->assertEquals('aHR0cDovL2V4YW1wbGUuY29tLw~~', $this->_helper->getEncodedUrl('http://example.com/'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function getTrackingPopupUrlBySalesModelDataProvider()
'setId',
42,
'abc',
'http://localhost/index.php/shipping/tracking/popup?hash=c2hpcF9pZDo0MjphYmM%2C'
'http://localhost/index.php/shipping/tracking/popup?hash=c2hpcF9pZDo0MjphYmM%7E'
],
[\Magento\Sales\Model\Order\Shipment\Track::class,
'setEntityId',
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/Magento/Framework/Url/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
class Encoder implements EncoderInterface
{
/**
* base64_encode() for URLs encoding
* Method base64_encode() for URLs encoding
*
* @param string $url
* @return string
*/
public function encode($url)
{
return strtr(base64_encode($url), '+/=', '-_,');
return strtr(base64_encode($url), '+/=', '-_~');
}
}
76 changes: 76 additions & 0 deletions lib/internal/Magento/Framework/Url/Test/Unit/EncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Url\Test\Unit;

use Magento\Framework\Url\Encoder;
use PHPUnit\Framework\TestCase;

class EncoderTest extends TestCase
{
/**
* @var Encoder|null
*/
private ?Encoder $encoder = null;

protected function setUp(): void
{
parent::setUp();

$this->encoder = new Encoder();
}

public function testEncode(): void
{
$url = 'http://magento2.adobe/encoding';

self::assertEquals('aHR0cDovL21hZ2VudG8yLmFkb2JlL2VuY29kaW5n', $this->encoder->encode($url));
}

/**
* Equals should be replaced by a non-reserved character.
*/
public function testEncodeWithEndingSlash(): void
{
$url = 'http://magento2.adobe/encoding/with/longer/url/';

self::assertEquals(
'aHR0cDovL21hZ2VudG8yLmFkb2JlL2VuY29kaW5nL3dpdGgvbG9uZ2VyL3VybC8~',
$this->encoder->encode($url)
);
}

/**
* @dataProvider rfc3986Urls
*
* @see https://www.rfc-editor.org/rfc/rfc3986.html#section-2.2
*/
public function testEncodeNotContainingRfc3986ReservedCharacters(string $url): void
{
$genDelims = [':', '/', '?', '#', '[', ']', '@'];
$subDelims = ['!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='];

$encodedUrl = $this->encoder->encode($url);

array_map(static function (string $value) use ($encodedUrl): void {
self::assertStringNotContainsString($value, $encodedUrl);
}, $genDelims);

array_map(static function (string $value) use ($encodedUrl): void {
self::assertStringNotContainsString($value, $encodedUrl);
}, $subDelims);
}

public function rfc3986Urls(): array
{
return [
['http://magento2.adobe/encoding/with/longer/url/'],
['http://magento2.adobe/some/other/random/url?currency=eur&price=2'],
['http://magento2.adobe/yet/not/done/url#anchor']
];
}
}