Stefan Esser raksta, ka lielākā daļa regulāro expressiju, kas izmantotas iekš php, un kas izmanto $ lai atzīmētu stringa beigas, ir potenciāli ievainojamas.


<?php
$string = "asd\n";
if (preg_match('/^[a-z]+$/i', $string)) {
	echo 'ir ok';
} else {
	echo 'nav ok';
}
?>

ir ok

PHP dokumentācijā ir teikts, ka $ simbols atbildīs vai nu stringa pēdējam simbolam, vai arī pēdējam simbolam, kam vēl arī seko jaunas līnijas simbols (\n).

Lai tā nebūtu, expressijās jāizmanto D modifikators (Pattern modifier).

D (PCRE_DOLLAR_ENDONLY)

If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl.

<?php
$string = "asd\n";
if (preg_match('/^[a-z]+$/Di', $string)) {
	echo 'ir ok';
} else {
	echo 'nav ok';
}
?>

nav ok