Djangoサイトのセキュリティ見直しメモ
[Django]セキュリティの見直し
はじめに
はじめてDjangoを使ったので、セキュリティ回りがどうなっているのかが気になったので調べつつ設定をしていった。
./manage.py check --deploy
System check identified some issues:
WARNINGS:
?: (security.W004) You have not set a value for the SECURE_HSTS_SECONDS setting. If your entire site is served only over SSL, you may want to consider setting a value and enabling HTTP Strict Transport Security. Be sure to read the documentation first; enabling HSTS carelessly can cause serious, irreversible problems.
?: (security.W006) Your SECURE_CONTENT_TYPE_NOSNIFF setting is not set to True, so your pages will not be served with an 'x-content-type-options: nosniff' header. You should consider enabling this header to prevent the browser from identifying content types incorrectly.
?: (security.W007) Your SECURE_BROWSER_XSS_FILTER setting is not set to True, so your pages will not be served with an 'x-xss-protection: 1; mode=block' header. You should consider enabling this header to activate the browser's XSS filtering and help prevent XSS attacks.
?: (security.W008) Your SECURE_SSL_REDIRECT setting is not set to True. Unless your site should be available over both SSL and non-SSL connections, you may want to either set this setting True or configure a load balancer or reverse-proxy server to redirect all connections to HTTPS.
?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True. Using a secure-only CSRF cookie makes it more difficult for network traffic sniffers to steal the CSRF token.
?: (security.W018) You should not have DEBUG set to True in deployment.
?: (security.W019) You have 'django.middleware.clickjacking.XFrameOptionsMiddleware' in your MIDDLEWARE, but X_FRAME_OPTIONS is not set to 'DENY'. The default is 'SAMEORIGIN', but unless there is a good reason for your site to serve other parts of itself in a frame, you should change it to 'DENY'.
System check identified 8 issues (0 silenced).
issues
https://docs.djangoproject.com/ja/2.2/topics/security/
security.W004 httpに接続された場合、httpにリダイレクトするヘッドを挿入してはどうかという警告。 不用意にオンにすると取り返しの使ないことになるらしい。
SECURE_HSTS_SECONDS = 60
# SECURE_HSTS_SECONDS = 36000
# SECURE_HSTS_INCLUDE = True
下記を参考設定するのが良い。 https://docs.djangoproject.com/ja/2.1/ref/middleware/#http-strict-transport-security
- 今はGAEがsslにしてくれているようだからオンにしても大丈夫なのかな?
security.W005 HSTSにサブドメインが含まれていないので、サブドメイン接続からの攻撃が想定され危ないよ!っという警告
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
security.W006 コンテンツを自動判定して、指定している”Content-Type”と違う場合でも実行されてしまうため、”Content-Type-Options: nosniff”を指定しようぜっという警告。
SECURE_CONTENT_TYPE_NOSNIFF = True
security.W007
ブラウザにあるXSSフィルタx-xss-protection:
"``1; mode=block``"``;
を有効にして、XSS攻撃の防止に役立てようぜっという警告
SECURE_BROWSER_XSS_FILTER = True
security.W008 HTTPとHTTPSを両方使う場合、でない限りリダイレクトするようにするといいよっという警告
SECURE_SSL_REDIRECT = True
security.W012 HTTPSのみにCookieを転送可能なようにすることで、セッションハイジャックを困難にすると良いぞという警告
SESSION_COOKIE_SECURE = True
security.W016 HTTPSのみにCSRS Cookieを転送可能なようにすることで、セッションハイジャックを困難にすると良いぞという警告
CSRF_COOKIE_SECURE = True
security.W018 デプロイでDEBUGモードは危ないぞ!!っという警告
DEBUG = False
security.W019 デフォルトはsameoriginになっており、同じサイト内のページでフレームに読み込まれた場合にのみ表示される。 denyでは全てに置いてページをフレーム内に表示することはできない。
X-OPTION = "DENY"
security.W021 設定してないとブラウザのプリロードリストに登録されないぞ!っという警告 プリロードリストにホストが登録されている場合、最初に接続する段階からHTTPSでの接続します。
SECURE_HSTS_PRELOAD = True
次にやること
- django-environでの設定の切り分け
- ローカルと本番で同じ設定を使っては開発できないので切り分けする
- ↓読む
参考サイト
Django におけるセキュリティ | Django ドキュメント | Django
現場で使える Django のセキュリティ対策 / Django security measures for business (DjangoCon JP 2019) - Speaker Deck