Skip to content

Commit 8bdbd50

Browse files
committed
esc() for 'raw' context (Fixes #8624)
1 parent b8adef6 commit 8bdbd50

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

system/Common.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,22 +426,22 @@ function env(string $key, $default = null)
426426
*/
427427
function esc($data, string $context = 'html', ?string $encoding = null)
428428
{
429+
$context = strtolower($context);
430+
431+
// Provide a way to NOT escape data since
432+
// this could be called automatically by
433+
// the View library.
434+
if ($context === 'raw') {
435+
return $data;
436+
}
437+
429438
if (is_array($data)) {
430439
foreach ($data as &$value) {
431440
$value = esc($value, $context);
432441
}
433442
}
434443

435444
if (is_string($data)) {
436-
$context = strtolower($context);
437-
438-
// Provide a way to NOT escape data since
439-
// this could be called automatically by
440-
// the View library.
441-
if ($context === 'raw') {
442-
return $data;
443-
}
444-
445445
if (! in_array($context, ['html', 'js', 'css', 'url', 'attr'], true)) {
446446
throw new InvalidArgumentException('Invalid escape context provided.');
447447
}

tests/system/CommonFunctionsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,27 @@ public function testEscapeBadContextZero(): void
247247
esc('<script>', '0');
248248
}
249249

250+
public function testEscapeArray(): void
251+
{
252+
$data = [
253+
'a' => [
254+
'b' => 'c&'
255+
],
256+
'd' => 'e>'
257+
];
258+
$expected = $data;
259+
$expected['a']['b'] = 'c&amp;';
260+
$expected['d'] = 'e&gt;';
261+
$this->assertEquals($expected, esc($data));
262+
}
263+
264+
public function testEscapeRecursiveArrayRaw(): void
265+
{
266+
$data = ['a' => 'b', 'c' => 'd'];
267+
$data['e'] = &$data;
268+
$this->assertSame($data, esc($data, 'raw'));
269+
}
270+
250271
/**
251272
* @runInSeparateProcess
252273
* @preserveGlobalState disabled

0 commit comments

Comments
 (0)