Skip to content

Commit a0aea71

Browse files
[5.x] Fix PHP sanitization edge cases (#14300)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent ea0fa7f commit a0aea71

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/View/Antlers/Language/Utilities/StringUtilities.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,10 @@ public static function containsSymbolicCharacters($text)
7979
*/
8080
public static function sanitizePhp($text)
8181
{
82-
$text = str_replace('<?php', '&lt;?php', $text);
83-
84-
// Also replace short tags if they're enabled.
85-
if (ini_get('short_open_tag')) {
86-
$xmlPlaceholder = '__XML_PLACEHOLDER'.Str::uuid();
87-
$text = str_replace('<?xml', $xmlPlaceholder, $text);
88-
$text = str_replace('<?', '&lt;?', $text);
89-
$text = str_replace($xmlPlaceholder, '<?xml', $text);
90-
}
82+
$xmlPlaceholder = '__XML_PLACEHOLDER'.Str::uuid();
83+
$text = str_replace('<?xml', $xmlPlaceholder, $text);
84+
$text = str_replace('<?', '&lt;?', $text);
85+
$text = str_replace($xmlPlaceholder, '<?xml', $text);
9186

9287
return $text;
9388
}

tests/Antlers/Runtime/PhpEnabledTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,19 @@ public function test_disabled_php_node_inside_user_values()
608608

609609
GlobalRuntimeState::$allowPhpInContent = false;
610610
}
611+
612+
public function test_sanitize_php_is_case_insensitive()
613+
{
614+
$this->assertSame('&lt;?php echo "test"; ?>', StringUtilities::sanitizePhp('<?php echo "test"; ?>'));
615+
$this->assertSame('&lt;?PHP echo "test"; ?>', StringUtilities::sanitizePhp('<?PHP echo "test"; ?>'));
616+
$this->assertSame('&lt;?Php echo "test"; ?>', StringUtilities::sanitizePhp('<?Php echo "test"; ?>'));
617+
$this->assertSame('&lt;?pHp echo "test"; ?>', StringUtilities::sanitizePhp('<?pHp echo "test"; ?>'));
618+
}
619+
620+
public function test_sanitize_php_handles_short_tags()
621+
{
622+
$this->assertSame('&lt;?= $var ?>', StringUtilities::sanitizePhp('<?= $var ?>'));
623+
$this->assertSame('&lt;?="test"?>', StringUtilities::sanitizePhp('<?="test"?>'));
624+
$this->assertSame("&lt;? echo 'test' ?>", StringUtilities::sanitizePhp("<? echo 'test' ?>"));
625+
}
611626
}

0 commit comments

Comments
 (0)