Django の 管理コマンドに check という プロジェクトの構成を検証するためのコマンドがあります。
このコマンドを実行すると、アプリケーション起動時にエラーとなる記述を、アプリケーションを実行せずに確認できますが、--deployいうオプションがあり、このオプションをつけると、本番環境向けのセキュリティ関連設定のチェックが実行できます。

このチェックを実施し、チェック警告を修正してみましたので、実施したことを記載します。


参考


前提

以下の環境で実行しています。

  • OS

    cat /etc/redhat-release
    CentOS Linux release 7.5.1804 (Core) 
    

  • Python Version

    python3.6 -V
    Python 3.6.4
    

  • Django Version

    python3.6 -m pip list
    Django                     1.10.8     
    


check コマンドの結果

  • コマンド

    python3.6 manage.py check --deploy
    

  • 実行結果
    以下、警告が出力されました。 計 8 つ出力されています。

    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.W017) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_HTTPONLY to True. Using an HttpOnly CSRF cookie makes it more difficult for cross-site scripting attacks to steal the CSRF token.
    ?: (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'.
    


警告への対処

  • security.W004
    SECURE_HSTS_SECONDS未設定の旨を示す警告になります。 これを設定すると、strict-transport-security付与されます。
    関連項目に、SECURE_HSTS_INCLUDE_SUBDOMAINSあり、これも合わせて設定します。

    # security.W004
    SECURE_HSTS_SECONDS = 31536000
    SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    

  • security.W006
    SECURE_CONTENT_TYPE_NOSNIFF未設定の旨を示す警告になります。True にすると、x-content-type-options: nosniff付与されます。

    # security.W006
    SECURE_CONTENT_TYPE_NOSNIFF = True
    

  • security.W007
    SECURE_BROWSER_XSS_FILTER未設定の旨を示す警告になります。True にすると、x-xss-protection: 1; mode=block付与されます。

    # security.W007
    SECURE_BROWSER_XSS_FILTER = True
    

  • security.W008
    SECURE_SSL_REDIRECT未設定の旨を示す警告になります。True にすると、HTTP通信の場合、強制的にHTTPSにリダイレクトされます。

    # security.W008
    SECURE_SSL_REDIRECT = True
    

  • security.W012
    SESSION_COOKIE_SECURE未設定の旨を示す警告になります。True にすると、Session Cookie の secure 属性が True になります。
    session_id という Cookie の secure 属性が切り替わります。

    # security.W012
    SESSION_COOKIE_SECURE = True
    

  • security.W016、security.W017
    django.middleware.csrf.CsrfViewMiddleware付与される CRSF token の Cookie に Secure 属性が付与されていない、Httponly 属性がついていないという警告になります。CSRF_COOKIE_SECURECSRF_COOKIE_HTTPONLY設定を True にします。

    # security.W016、security.W017
    CSRF_COOKIE_SECURE = True
    CSRF_COOKIE_HTTPONLY = True
    

  • security.W019
    django.middleware.clickjacking.XFrameOptionsMiddleware の、X_FRAME_OPTIONS値が未設定の旨を示す警告になります。
    デフォルト値は、SAMEORIGIN で、DENY でも問題ないため、DENY設定します。

    # security.W019
    X_FRAME_OPTIONS = 'DENY'
    

上記の修正で、Observatory by MozillaスコアはB ランク 75 点になりました。
HTTP サーバ側で設定したほうがパフォーマンスが出る項目もあるかと思いますが、アプリケーション側でなんとかできるのはメリットとなることもあるかと思います。

以上です。

コメント