クロスサイトスクリプティングの危険性を解説した
In addition, web pages should explicitly set a character set to an appropriate value in all dynamically generated pages.
加えて、動的に生成されたすべてのWebページは適切な文字コードセットを明示的に設定されなければならない
としています。
<meta>タグを利用すると<meta http-equiv="content-type" content="text/html; charset=iso-2022-jp">
などと文字エンコーディングをページ中に記載可能ですが、
RFC2616のセクション3.
HTTP/1.1 recipients MUST respect the charset label provided by the sender; and those user agents that have a provision to "guess" a charset MUST use the charset from the content-type field if they support that charset, rather than the recipient's preference, when initially displaying a document.
と、
また、
When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP. Data in character sets other than "ISO-8859-1" or its subsets MUST be labeled with an appropriate charset value.
と、
インターネットで利用されるプログラムがRFCに準拠していないことは珍しくありませんが、
ブラウザがRFCに準拠していないとしても、
ini_set('default_charset', 'Shift_JIS');
以下のようなContent-Typeヘッダが送信されます。
Content-Type: text/html;charset=Shift_JIS
しかし、
Content-Type: text/html
default_
対策のまとめ
- プログラム中から必ずdefault_
charset設定を利用してHTTPヘッダで文字エンコーディングを指定する - プログラムで設定漏れがあった場合にそなえ、
php. iniなどの設定ファイルでもdefault_ charsetを指定する - HTTPヘッダで指定した文字エンコーディングとコンテンツの文字エンコーディングには同じものを使用する
- ユーザが送信したデータなどが原因で複数の文字エンコーディングの文字列が同一ページ上に表示されないように注意する