Django におけるセキュリティ¶
このドキュメントは、Django のセキュリティ機能の概要です。Django を利用したサイトを安全なものにするためのヒントを説明します。
クロス・サイト・スクリプティング (XSS) の防御¶
XSS 攻撃はあるユーザーに対してクライアント用のスクリプトを別のユーザーのブラウザに読み込ませることを可能にしてしまいます。この攻撃は通常他のユーザーに対して選択・表示を行うデータベース内に悪意のあるスクリプトを保存させる、あるいは攻撃者の JavaScript がブラウザで実行されるリンクをユーザーにクリックさせることでなされます。しかしながら、XSS 攻撃はページを読み込む前に十分なサニタイズを行っていなければ、信頼できないクッキーやウェブ・サービスのようなデータが発生の原因となることがあります。
Django のテンプレートを用いる事で多数の XSS 攻撃に対抗することができます。しかしながら、それがどのような防御策が用意でき、またその利用に際する制限について知ることが重要になります。
Django は HTML に対して特に危険とみなされる 特定の文字列のエスケープ を行います。この防御によってユーザーは大半の悪意のある入力から守られますが、いつも簡単で容易に利用できる訳ではありません。たとえば、次に示す例では防御がなされません。
<style class={{ var }}>...</style>
もし var
の値が 'class1 onmouseover=javascript:func()'
にセットされた場合、不完全な HTML をそのブラウザがどのようにレンダリングするかによっては、許可されていない Javascript を実行させる事になります。(このケースであれば属性値のクオートを行えば対処できます)
カスタム・テンプレート・タグと共に ``is_safe``を用いたり、safe
テンプレートタグや、mark_safe
の利用に加えてオートエスケープが無効化されている場合十分な注意を払う必要が有ります。
ここまでの内容に加えて、HTML 以外を出力するのにテンプレートシステムを利用している場合、完全に別のエスケープすべき文字や単語が存在するかもしれません。
HTML をデータベース内に収集、特にその HTML が選択・表示される場合は十分な注意を払う必要が有ります。
クロス・サイト・リクエスト・フォージェリ(CSRF)の防御¶
CSRF 攻撃は悪意ある利用者が、あるユーザーが自身で持っている情報や承認を用いずに、そのユーザーの認証情報での処理を発生させてしまう攻撃です。
Django は大半の CSRF 攻撃に対応したビルトインの防御機構を備え、適切な所で:ref:有効化して利用できます <using-csrf>。しかしながら、いかなる緩和策にも制限が存在します。例えば、CSRF モジュールをグローバルにあるいは特定のビューだけ無効化できます。これは自身が何をしているのか理解している場合のみ行うべきです。もしあなたのサイト内に管理対象外のサブドメインが存在すればそこにも:ref:`制限 <csrf-limitations>`が存在します。
CSRFプロテクション はそれぞれのPOSTリクエストのシークレットを検証することで動作します。これにより、悪意のあるユーザーがあなたのウェブサイトに対して単純にフォームのPOSTを「再送信」することはできず、他のログイン済みユーザーは意識することなくフォームを送信することができます。悪意のあるユーザーはCookieを使用したユーザー固有のシークレットを知る必要があります。
HTTPS ` で接続されている場合、 ``CsrfViewMiddleware` は HTTP referer ヘッダが(サブドメインとポートを含め)同一オリジンのURLにセットされているかをチェックします。HTTPSでは強化されたセキュリティを利用できるため、可能な限り安全でない接続リクエストを転送したり、サポートされたブラウザにはHSTSを使用するなどしてHTTPS接続を使用することが不可欠です。
どうしても必要という場合を除いて、 csrf_exempt
デコレータでビューをマークする際には十分注意してください。
SQL injectionへの防御¶
SQL injectionは、悪意ある攻撃者がデータベース任意のSQLコードを実行する攻撃です。これによって、レコードが削除されたり、情報が漏洩する可能性があります。
Djangoのクエリセットは、クエリのパラメータ化によってクエリを構成するため、SQL injectionから守られています。クエリのSQLコードはそのクエリのパラメータとは独立に定義されています。パラメータはユーザが指定するものも有り、安全ではないので、underlying database driverによってエスケープされます。
Django開発者はまた、生のクエリを書くことやcustom SQLを実行することができます。これらの手段は慎重に用いなければいけません。開発者は常に、ユーザがコントロールできるパラメータを適切にエスケープしなければなりません。加えて、extra()とRawSQLを使うときには警戒を忘れないようにしなければなりません。
Clickjacking に対する防御¶
Clickjacking は、悪意のあるサイトが他のサイトをフレームの中に表示するタイプの攻撃です。攻撃の結果、疑いのないユーザーが騙されてターゲットのサイトに対して意図しない操作を行ってしまいます。
Django contains clickjacking protection in
the form of the
X-Frame-Options middleware
which in a supporting browser can prevent a site from being rendered inside
a frame. It is possible to disable the protection on a per view basis
or to configure the exact header value sent.
サードパーティのサイト上でページをフレーム内に表示する必要がないか、もし合ったとしてもごく少数のセクションしかないようなあらゆるサイトは、このミドルウェアを使用することを強く推奨します。
SSL/HTTPS¶
セキュリティの点ではサイトを HTTPS の下でデプロイするのは常に良い選択です。HTTPS がなければ、悪意のあるネットワークユーザーが、認証情報またはクライアント・サーバー間で転送されるその他あらゆる情報を盗み見たり、場合によっては、活動的な ネットワーク攻撃者が両方向のデータを書き換えることが可能になってしまいます。
HTTPS が提供するプロテクトを使用するには、サーバー上で有効にし、いくつかの追加作業が必要になります。
If necessary, set
SECURE_PROXY_SSL_HEADER
, ensuring that you have understood the warnings there thoroughly. Failure to do this can result in CSRF vulnerabilities, and failure to do it correctly can also be dangerous!Set
SECURE_SSL_REDIRECT
toTrue
, so that requests over HTTP are redirected to HTTPS.Please note the caveats under
SECURE_PROXY_SSL_HEADER
. For the case of a reverse proxy, it may be easier or more secure to configure the main Web server to do the redirect to HTTPS.'secure' クッキーを使用する
If a browser connects initially via HTTP, which is the default for most browsers, it is possible for existing cookies to be leaked. For this reason, you should set your
SESSION_COOKIE_SECURE
andCSRF_COOKIE_SECURE
settings toTrue
. This instructs the browser to only send these cookies over HTTPS connections. Note that this will mean that sessions will not work over HTTP, and the CSRF protection will prevent any POST data being accepted over HTTP (which will be fine if you are redirecting all HTTP traffic to HTTPS).HTTP Strict Transport Security (HSTS) を使用する
HSTS is an HTTP header that informs a browser that all future connections to a particular site should always use HTTPS. Combined with redirecting requests over HTTP to HTTPS, this will ensure that connections always enjoy the added security of SSL provided one successful connection has occurred. HSTS may either be configured with
SECURE_HSTS_SECONDS
,SECURE_HSTS_INCLUDE_SUBDOMAINS
, andSECURE_HSTS_PRELOAD
, or on the Web server.
Host ヘッダーの検証¶
Djangoはいくつかのケースで、URLを組み立てるためにクライアントから送られてきた Host
ヘッダーを使用します。 Host
ヘッダーの値は、クロス・サイト・スクリプティング (XSS) 攻撃を回避するためにサニタイズされますが、それでもまだニセの Host
値はクロス・サイト・リクエスト・フォージェリ (CSRF) やキャッシュポイズニング攻撃、Eメールアドレス内のリンクへのポイズニング等に使用されます。
Because even seemingly-secure web server configurations are susceptible to fake
Host
headers, Django validates Host
headers against the
ALLOWED_HOSTS
setting in the
django.http.HttpRequest.get_host()
method.
このヘッダの検証は get_host()
メソッドの実行時にのみ適用されます。もし request.META
から直接 Host
ヘッダにアクセスするコードを書いてしまうと、このセキュリティプロテクションを回避してしまうので注意してください。
より詳しい情報については、ALLOWED_HOSTS
ドキュメントをご覧ください。
警告
Previous versions of this document recommended configuring your web server to
ensure it validates incoming HTTP Host
headers. While this is still
recommended, in many common web servers a configuration that seems to
validate the Host
header may not in fact do so. For instance, even if
Apache is configured such that your Django site is served from a non-default
virtual host with the ServerName
set, it is still possible for an HTTP
request to match this virtual host and supply a fake Host
header. Thus,
Django now requires that you set ALLOWED_HOSTS
explicitly rather
than relying on web server configuration.
Additionally, Django requires you to explicitly enable support for the
X-Forwarded-Host
header (via the USE_X_FORWARDED_HOST
setting)
if your configuration requires it.
セッションのセキュリティ¶
Similar to the CSRF limitations requiring a site to
be deployed such that untrusted users don't have access to any subdomains,
django.contrib.sessions
also has limitations. See the session
topic guide section on security for details.
ユーザーがアップロードしたコンテンツ¶
注釈
以下に説明する問題を回避するには serving static files from a cloud service or CDN の利用を検討してください。
If your site accepts file uploads, it is strongly advised that you limit these uploads in your Web server configuration to a reasonable size in order to prevent denial of service (DOS) attacks. In Apache, this can be easily set using the LimitRequestBody directive.
If you are serving your own static files, be sure that handlers like Apache's
mod_php
, which would execute static files as code, are disabled. You don't want users to be able to execute arbitrary code by uploading and requesting a specially crafted file.Django's media upload handling poses some vulnerabilities when that media is served in ways that do not follow security best practices. Specifically, an HTML file can be uploaded as an image if that file contains a valid PNG header followed by malicious HTML. This file will pass verification of the library that Django uses for
ImageField
image processing (Pillow). When this file is subsequently displayed to a user, it may be displayed as HTML depending on the type and configuration of your web server.No bulletproof technical solution exists at the framework level to safely validate all user uploaded file content, however, there are some other steps you can take to mitigate these attacks:
- One class of attacks can be prevented by always serving user uploaded
content from a distinct top-level or second-level domain. This prevents
any exploit blocked by same-origin policy protections such as cross
site scripting. For example, if your site runs on
example.com
, you would want to serve uploaded content (theMEDIA_URL
setting) from something likeusercontent-example.com
. It's not sufficient to serve content from a subdomain likeusercontent.example.com
. - Beyond this, applications may choose to define a whitelist of allowable file extensions for user uploaded files and configure the web server to only serve such files.
- One class of attacks can be prevented by always serving user uploaded
content from a distinct top-level or second-level domain. This prevents
any exploit blocked by same-origin policy protections such as cross
site scripting. For example, if your site runs on
Additional security topics¶
While Django provides good security protection out of the box, it is still important to properly deploy your application and take advantage of the security protection of the Web server, operating system and other components.
- Make sure that your Python code is outside of the Web server's root. This will ensure that your Python code is not accidentally served as plain text (or accidentally executed).
- Take care with any user uploaded files.
- Django does not throttle requests to authenticate users. To protect against brute-force attacks against the authentication system, you may consider deploying a Django plugin or Web server module to throttle these requests.
- Keep your
SECRET_KEY
a secret. - It is a good idea to limit the accessibility of your caching system and database using a firewall.
- Take a look at the Open Web Application Security Project (OWASP) Top 10 list which identifies some common vulnerabilities in web applications. While Django has tools to address some of the issues, other issues must be accounted for in the design of your project.