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

Fix #20322: Correct escaping of hex ranges in Html::escapeJsRegularEx… #20330

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------

- Enh #20309: Add custom attributes support to style tags (nzwz)
- Bug #20322: Correct escaping of hex ranges in Html::escapeJsRegularExpression (kowap)


2.0.52 February 13, 2025
Expand Down
2 changes: 1 addition & 1 deletion framework/helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2392,7 +2392,7 @@ public static function getInputId($model, $attribute)
*/
public static function escapeJsRegularExpression($regexp)
{
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $regexp);
$pattern = preg_replace('/\\\\x([0-9a-fA-F]{2})/', '\\x{$1}', $regexp);
$deliminator = substr($pattern, 0, 1);
$pos = strrpos($pattern, $deliminator, 1);
$flag = substr($pattern, $pos + 1);
Expand Down
23 changes: 23 additions & 0 deletions tests/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ protected function setUp(): void
]);
}

/**
* @covers \yii\helpers\Html::escapeJsRegularExpression
*/
public function testEscapeJsRegularExpressionHexRange()
{
$testCases = [
'/^[\x00-\xFF]{8,72}$/',
'/^[\xA1-\xFE]{2}$/',
'/^\xFF\x00$/',
];

$expectedResults = [
'/^[\x{00}-\x{FF}]{8,72}$/',
'/^[\x{A1}-\x{FE}]{2}$/',
'/^\x{FF}\x{00}$/',
];

foreach ($testCases as $index => $original) {
$escaped = Html::escapeJsRegularExpression($original);
$this->assertSame($expectedResults[$index], $escaped, "Test case #$index failed.");
}
}

public function testEncode()
{
$this->assertEquals('a&lt;&gt;&amp;&quot;&#039;�', Html::encode("a<>&\"'\x80"));
Expand Down
Loading