Djangoのセキュリティチェック

2022/03 の作業


Django の設定値をチェックする機能があるので、チェックしてみる。

prod.py の方をチェックする。


現在の状態をチェック

(work_venv) $ python manage.py check --deploy --settings worksite.settings.prod
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.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.

System check identified 3 issues (0 silenced).

$ curl -I https://w1.massolezume.ga/memo/
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 28 Mar 2022 02:24:56 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 2181
Connection: keep-alive
Expires: Mon, 28 Mar 2022 02:24:56 GMT
Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private
X-Frame-Options: DENY
Vary: Cookie
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Set-Cookie: sessionid=xxxxxxx; HttpOnly; Path=/; SameSite=Lax


・security.W004

HSTSと言って、ブラウザ側にこのサイトは https でアクセスする様に http ヘッダで通知する設定らしい。

サブドメインへのポリシーの適用お設定を一緒にしておくのがいいらしい。

ブラウザに記憶されるので、短めに設定して確認。本当は半年とかを設定するらしい。

SECURE_HSTS_SECONDS に秒数を設定。


関連する項目

security.W005

サブドメインも含めるかどうか。

SECURE_HSTS_INCLUDE_SUBDOMAINS = True


security.W021

HSTS を有効にするかどうか。

SECURE_HSTS_PRELOAD = True


・security.W008

http で来たら、https にリダイレクトするかどうか。

SECURE_SSL_REDIRECT = True


・security.W012

セッションのクッキーにセキュア属性を入れるかどうか。

SESSION_COOKIE_SECURE = True


X-Frame-Options: DENY
X-Content-Type-Options: nosniff
は、昔は設定の必要があったみたいだが、今はデフォルトで設定されている。



設定を追加

下の方に設定を追加。

(work_venv) $ vi worksite/worksite/settings/prod.py 
・・・・
# security.W004
SECURE_HSTS_SECONDS = 1800
# security.W005
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
# security.W021
SECURE_HSTS_PRELOAD = True

# I want to access from local via HTTP
# security.W008
SECURE_SSL_REDIRECT = True

# security.W012
SESSION_COOKIE_SECURE = True

# この下は何で設定したか忘れた。
# security.W016
CSRF_USE_SESSIONS = True
# security.W017
CSRF_COOKIE_HTTPONLY = True
・・・・



設定を確認

(work_venv) $ python manage.py check --deploy --settings worksite.settings.prod
System check identified no issues (0 silenced).

$ curl -I https://w1.massolezume.ga/memo/
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 28 Mar 2022 03:29:38 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 2181
Connection: keep-alive
Expires: Mon, 28 Mar 2022 03:29:38 GMT
Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private
X-Frame-Options: DENY
Vary: Cookie
Strict-Transport-Security: max-age=1800; includeSubDomains; preload
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Set-Cookie: sessionid=xxxxxxx; HttpOnly; Path=/; SameSite=Lax; Secure



これでチェックOK。

ただ、ローカルから http でアクセスする口も用意も作ったりするので、hsts と ssl へのリダイレクトは、NGINX 側に任せる事にするので、コメントアウトする。

・・・・
# to nginx
## security.W004
#SECURE_HSTS_SECONDS = 10886400
## security.W005
#SECURE_HSTS_INCLUDE_SUBDOMAINS = True
## security.W021
#SECURE_HSTS_PRELOAD = True

# I want to access from local via HTTP
## security.W008
#SECURE_SSL_REDIRECT = True
・・・・