水無月ばけらのえび日記

bakera.jp > 水無月ばけらのえび日記 > CSS2 のバックスラッシュの扱い

CSS2 のバックスラッシュの扱い

2003年12月15日(月曜日)

CSS2 のバックスラッシュの扱い

スタイル属性中で、ある処理系が、危険な文字列として、「background」を認識し、これをフィルターしているとします。しかしながら、「bac\kground」は透過してしまうかもしれません。多くのブラウザは、これを「background」として処理します。しかしながら、これはCSSの仕様上、おかしな話しです。いや、規定されていないので、ブラウザのメーカが適宜解釈して実装しているのかもしれません。

以上、スターダストさんのhoshikuzu|stardustの書斎 2003-12-13 Vulnerability Note VU#707100 より

"bac\kground" という文字列も警戒しなきゃいけないよという話で、それは全くその通りだと思います。

ただ、これが "background" として処理されるのは CSS2 の仕様が規定する正しい挙動です。

CSS2 では、バックスラッシュには 3種類の用法があります。

In CSS2, a backslash (\) character indicates three types of character escapes.

First, inside a string, a backslash followed by a newline is ignored (i.e., the string is deemed not to contain either the backslash or the newline).

Second, it cancels the meaning of special CSS characters. Any character (except a hexadecimal digit) can be escaped with a backslash to remove its special meaning. For example, "\"" is a string consisting of one double quote. Style sheet preprocessors must not remove these backslashes from a style sheet since that would change the style sheet's meaning.

Third, backslash escapes allow authors to refer to characters they can't easily put in a document. In this case, the backslash is followed by at most six hexadecimal digits (0..9A..F), which stand for the ISO 10646 ([ISO10646]) character with that number. If a digit or letter follows the hexadecimal number, the end of the number needs to be made clear.

以上、CSS2 4.1.3 Characters and case より

Second のところで "Any character (except a hexadecimal digit)" と書かれていることに注目して頂きたいわけですが、"k" も "Any character except a hexadecimal digit" に該当しますので、"\k" はこの用法になります。仮に "k" が区切り子としての機能を持っていたとすると、"\k" と書くことで "k" という文字列を表現できます。実際には現在の CSS では "k" に区切り子の機能はありませんが、なにしろ "Any character" ですから "k" にもこの表現は使えるのです。

そんなわけで、"\k" と書かれたものは "k" という文字列として扱われることになります。

※ちなみに CSS3 でも同様になる予定。CSS3 Sytntax 4.1. Characters and case (www.w3.org) 参照。

余談ですが、この辺りで意外にくせ者なのが、16進数で表記したモノの後ろの空白ひとつが無視されるという仕様。話の元ネタである Vulnerability Note VU#707100 (www.kb.cert.org) には 「\73CRIPT」という例が出ていますが、\73 の後ろにスペースをひとつ入れて「\73 CRIPT」としても、やはり SCRIPT と解釈されることになっています。この辺も要注意かも知れません。

※実は昔 "MS ゴシック" という文字列をエンコードしようとしてハマったことがあります。

関連する話題: CSS / CSS2 / Internet Explorer

最近の日記

関わった本など