更新 2022.11.6 12:36閲覧 940

Javascript の変数に $_GET して alert する実験と結果

条件 /test フォルダに .htaccess を作成して ?test= にリライト。 index.php の script タグ内に filter_input(INPUT_GET, 'test') を記述し、ブラウザで確認した。 結論 PHP の GET 変数を Javascript の変数に代入する場合、ダブルクオートで囲み、クォートエンコードしても安全とは限らない。 .htaccess RewriteEngine on RewriteRule ^([^/]*)/$ ?test=$1 [L]

ダブルクオートなし + エンコードなし

index.php <script>let a=
<?=filter_input(INPUT_GET, 'test')?>
;
</script>
URL http://localhost/test/alert()/ 結果 アラートあり

ダブルクオートなし + クォートエンコードあり

index.php <script>let a=
<?=filter_input(INPUT_GET, 'test', FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>
;
</script>
URL http://localhost/test/alert()/ 結果 アラートあり

ダブルクオートあり + エンコードなし

index.php <script>let a="
<?=filter_input(INPUT_GET, 'test')?>
";
</script>
URL http://localhost/test/alert()/ 結果 アラートなし

ダブルクオートあり + エンコードなし

index.php <script>let a="
<?=filter_input(INPUT_GET, 'test')?>
";
</script>
URL http://localhost/test/";alert();z="/ 結果 アラートあり

ダブルクオートあり + クォートエンコードあり

index.php <script>let a="
<?=filter_input(INPUT_GET, 'test', FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>
";
</script>
URL http://localhost/test/";alert();z="/ 結果 アラートなし

ダブルクオートなし + JSON エンコードあり

index.php <script>let a=
<?=json_encode(filter_input(INPUT_GET, 'test'))?>
;
</script>
URL http://localhost/test/alert()/ 結果 アラートなし

ダブルクオートあり + JSON エンコードあり

index.php <script>let a="
<?=json_encode(filter_input(INPUT_GET, 'test'))?>
";
</script>
URL http://localhost/test/alert()/ 結果 アラートなし

ダブルクオートなし + JSON エンコードあり

index.php <script>let a=
<?=json_encode(filter_input(INPUT_GET, 'test'))?>
;
</script>
URL http://localhost/test/";alert();z="/ 結果 アラートなし

ダブルクオートあり + JSON エンコードあり

index.php <script>let a="
<?=json_encode(filter_input(INPUT_GET, 'test'))?>
";
</script>
URL http://localhost/test/;alert();z=/ 結果 アラートあり

ダブルクオートあり + JSON エンコードあり + クォートエンコードあり

index.php <script>let a="
<?=json_encode(filter_input(INPUT_GET, 'test', FILTER_SANITIZE_FULL_SPECIAL_CHARS))?>
";
</script>
URL http://localhost/test/;alert();z=/ 結果 アラートあり

コメント

当フォームより収集される個人情報は、返信を要する際に使用されるものであり、法令に基づく行政機関等への提供を除き、ご本人の同意を得ずに第三者に提供することはありません。また、コメントが掲載される場合であってもメールアドレスが本サイト内に記載されることはありません。