作成 2022.10.10 13:03
通常、contentEditable な要素に </script><script>alert()</script> と入力してもアラートは表示されないが、PHP などでファイルに書き出してから要素内に出力するとアラートが表示される。
但し、エスケープする場合、h(file_get_contents($txt))
とすると、</script><script>alert()</script> となってしまうので、当座は str_replace('/', '\u002f', file_get_contents($txt)) とすべきか。
追記 2022.10.12
また下のコードだと、単純に ";alert();let a=" と入力してもアラートできる罠。
例) 2022.10.21 エンターキー押下で送信に修正
<?php
$txt='./1.txt';
if ($input = json_decode(file_get_contents('php://input') ?? null, true))
{
file_put_contents($txt, $input);
}
?>
<script>
document.documentElement.appendChild(div=document.createElement("div")),
div.contentEditable=true,
div.style.margin=div.style.padding="1.5em",
div.style.border="thin solid",
div.textContent="<?=(!is_file($txt) ? '' : file_get_contents($txt))?>
",
div.focus(),
div.onkeydown=e=>13!==e.keyCode?"":fetch("",{method:"post",cache:"no-cache",body:JSON.stringify(e.target.textContent)}).then(()=>location.reload())
</script>