どんなことが起こるのかな?と、色々試したので紹介します。
こんなことが起きます。
以下のJSON文字列をブラウザが受け取った際、ヘッダーに含まれている"Content-Type"によって、動作にどんな違いが有るのか試します。
{"sctipt":"<body onload=alert(1)>","miss img":"<img src=img.jpg>"}"Content-Type"は以下の2種類です。
- "application/json"
- "text/html"(ダイアログと画像が出ます)
安心して見ていられます。
"text/html"の方はやばいですね。
ダイアログが開き、画像が表示されています。
ヘタしたら個人情報が抜き取られるかも…
ブラウザが受け取ったデータをHTMLであるか、JSONであるか、あるいはまた別の何かであるかで挙動が変わってきます。
WebAPIを作成する際には、レスポンスのヘッダに含めるContent-Typeに何を使うかを正しく検証する必要があると思います。
フレームワークとか使うと、その辺をうまい具合にやってくれるので安心だと思います。
上で使ったソースコードを貼っておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: application/json; charset=urf-8'); | |
$json = Array(); | |
$json['sctipt'] = '<body onload=alert(1)>'; | |
$json['miss img'] = '<img src=img.jpg>'; | |
echo json_encode($json); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/html; charset=urf-8'); | |
$json = Array(); | |
$json['sctipt'] = '<body onload=alert(1)>'; | |
$json['miss img'] = '<img src=img.jpg>'; | |
echo json_encode($json); | |
?> |
0 件のコメント:
コメントを投稿