|
| 1 | +from django.conf import settings |
| 2 | + |
| 3 | +from .. import register, Tags, Warning |
| 4 | + |
| 5 | + |
| 6 | +SECRET_KEY_MIN_LENGTH = 50 |
| 7 | +SECRET_KEY_MIN_UNIQUE_CHARACTERS = 5 |
| 8 | + |
| 9 | +W001 = Warning( |
| 10 | + "You do not have 'django.middleware.security.SecurityMiddleware' " |
| 11 | + "in your MIDDLEWARE_CLASSES so the SECURE_HSTS_SECONDS, " |
| 12 | + "SECURE_CONTENT_TYPE_NOSNIFF, " |
| 13 | + "SECURE_BROWSER_XSS_FILTER, and SECURE_SSL_REDIRECT settings " |
| 14 | + "will have no effect.", |
| 15 | + id='security.W001', |
| 16 | +) |
| 17 | + |
| 18 | +W002 = Warning( |
| 19 | + "You do not have " |
| 20 | + "'django.middleware.clickjacking.XFrameOptionsMiddleware' in your " |
| 21 | + "MIDDLEWARE_CLASSES, so your pages will not be served with an " |
| 22 | + "'x-frame-options' header. Unless there is a good reason for your " |
| 23 | + "site to be served in a frame, you should consider enabling this " |
| 24 | + "header to help prevent clickjacking attacks.", |
| 25 | + id='security.W002', |
| 26 | +) |
| 27 | + |
| 28 | +W004 = Warning( |
| 29 | + "You have not set a value for the SECURE_HSTS_SECONDS setting. " |
| 30 | + "If your entire site is served only over SSL, you may want to consider " |
| 31 | + "setting a value and enabling HTTP Strict Transport Security. " |
| 32 | + "Be sure to read the documentation first; enabling HSTS carelessly " |
| 33 | + "can cause serious, irreversible problems.", |
| 34 | + id='security.W004', |
| 35 | +) |
| 36 | + |
| 37 | +W005 = Warning( |
| 38 | + "You have not set the SECURE_HSTS_INCLUDE_SUBDOMAINS setting to True. " |
| 39 | + "Without this, your site is potentially vulnerable to attack " |
| 40 | + "via an insecure connection to a subdomain. Only set this to True if " |
| 41 | + "you are certain that all subdomains of your domain should be served " |
| 42 | + "exclusively via SSL.", |
| 43 | + id='security.W005', |
| 44 | +) |
| 45 | + |
| 46 | +W006 = Warning( |
| 47 | + "Your SECURE_CONTENT_TYPE_NOSNIFF setting is not set to True, " |
| 48 | + "so your pages will not be served with an " |
| 49 | + "'x-content-type-options: nosniff' header. " |
| 50 | + "You should consider enabling this header to prevent the " |
| 51 | + "browser from identifying content types incorrectly.", |
| 52 | + id='security.W006', |
| 53 | +) |
| 54 | + |
| 55 | +W007 = Warning( |
| 56 | + "Your SECURE_BROWSER_XSS_FILTER setting is not set to True, " |
| 57 | + "so your pages will not be served with an " |
| 58 | + "'x-xss-protection: 1; mode=block' header. " |
| 59 | + "You should consider enabling this header to activate the " |
| 60 | + "browser's XSS filtering and help prevent XSS attacks.", |
| 61 | + id='security.W007', |
| 62 | +) |
| 63 | + |
| 64 | +W008 = Warning( |
| 65 | + "Your SECURE_SSL_REDIRECT setting is not set to True. " |
| 66 | + "Unless your site should be available over both SSL and non-SSL " |
| 67 | + "connections, you may want to either set this setting True " |
| 68 | + "or configure a load balancer or reverse-proxy server " |
| 69 | + "to redirect all connections to HTTPS.", |
| 70 | + id='security.W008', |
| 71 | +) |
| 72 | + |
| 73 | +W009 = Warning( |
| 74 | + "Your SECRET_KEY has less than %(min_length)s characters or less than " |
| 75 | + "%(min_unique_chars)s unique characters. Please generate a long and random " |
| 76 | + "SECRET_KEY, otherwise many of Django's security-critical features will be " |
| 77 | + "vulnerable to attack." % { |
| 78 | + 'min_length': SECRET_KEY_MIN_LENGTH, |
| 79 | + 'min_unique_chars': SECRET_KEY_MIN_UNIQUE_CHARACTERS, |
| 80 | + }, |
| 81 | + id='security.W009', |
| 82 | +) |
| 83 | + |
| 84 | +W018 = Warning( |
| 85 | + "You should not have DEBUG set to True in deployment.", |
| 86 | + id='security.W018', |
| 87 | +) |
| 88 | + |
| 89 | +W019 = Warning( |
| 90 | + "You have " |
| 91 | + "'django.middleware.clickjacking.XFrameOptionsMiddleware' in your " |
| 92 | + "MIDDLEWARE_CLASSES, but X_FRAME_OPTIONS is not set to 'DENY'. " |
| 93 | + "The default is 'SAMEORIGIN', but unless there is a good reason for " |
| 94 | + "your site to serve other parts of itself in a frame, you should " |
| 95 | + "change it to 'DENY'.", |
| 96 | + id='security.W019', |
| 97 | +) |
| 98 | + |
| 99 | + |
| 100 | +def _security_middleware(): |
| 101 | + return "django.middleware.security.SecurityMiddleware" in settings.MIDDLEWARE_CLASSES |
| 102 | + |
| 103 | + |
| 104 | +def _xframe_middleware(): |
| 105 | + return "django.middleware.clickjacking.XFrameOptionsMiddleware" in settings.MIDDLEWARE_CLASSES |
| 106 | + |
| 107 | + |
| 108 | +@register(Tags.security, deploy=True) |
| 109 | +def check_security_middleware(app_configs, **kwargs): |
| 110 | + passed_check = _security_middleware() |
| 111 | + return [] if passed_check else [W001] |
| 112 | + |
| 113 | + |
| 114 | +@register(Tags.security, deploy=True) |
| 115 | +def check_xframe_options_middleware(app_configs, **kwargs): |
| 116 | + passed_check = _xframe_middleware() |
| 117 | + return [] if passed_check else [W002] |
| 118 | + |
| 119 | + |
| 120 | +@register(Tags.security, deploy=True) |
| 121 | +def check_sts(app_configs, **kwargs): |
| 122 | + passed_check = not _security_middleware() or settings.SECURE_HSTS_SECONDS |
| 123 | + return [] if passed_check else [W004] |
| 124 | + |
| 125 | + |
| 126 | +@register(Tags.security, deploy=True) |
| 127 | +def check_sts_include_subdomains(app_configs, **kwargs): |
| 128 | + passed_check = ( |
| 129 | + not _security_middleware() or |
| 130 | + not settings.SECURE_HSTS_SECONDS or |
| 131 | + settings.SECURE_HSTS_INCLUDE_SUBDOMAINS is True |
| 132 | + ) |
| 133 | + return [] if passed_check else [W005] |
| 134 | + |
| 135 | + |
| 136 | +@register(Tags.security, deploy=True) |
| 137 | +def check_content_type_nosniff(app_configs, **kwargs): |
| 138 | + passed_check = ( |
| 139 | + not _security_middleware() or |
| 140 | + settings.SECURE_CONTENT_TYPE_NOSNIFF is True |
| 141 | + ) |
| 142 | + return [] if passed_check else [W006] |
| 143 | + |
| 144 | + |
| 145 | +@register(Tags.security, deploy=True) |
| 146 | +def check_xss_filter(app_configs, **kwargs): |
| 147 | + passed_check = ( |
| 148 | + not _security_middleware() or |
| 149 | + settings.SECURE_BROWSER_XSS_FILTER is True |
| 150 | + ) |
| 151 | + return [] if passed_check else [W007] |
| 152 | + |
| 153 | + |
| 154 | +@register(Tags.security, deploy=True) |
| 155 | +def check_ssl_redirect(app_configs, **kwargs): |
| 156 | + passed_check = ( |
| 157 | + not _security_middleware() or |
| 158 | + settings.SECURE_SSL_REDIRECT is True |
| 159 | + ) |
| 160 | + return [] if passed_check else [W008] |
| 161 | + |
| 162 | + |
| 163 | +@register(Tags.security, deploy=True) |
| 164 | +def check_secret_key(app_configs, **kwargs): |
| 165 | + passed_check = ( |
| 166 | + getattr(settings, 'SECRET_KEY', None) and |
| 167 | + len(set(settings.SECRET_KEY)) >= SECRET_KEY_MIN_UNIQUE_CHARACTERS and |
| 168 | + len(settings.SECRET_KEY) >= SECRET_KEY_MIN_LENGTH |
| 169 | + ) |
| 170 | + return [] if passed_check else [W009] |
| 171 | + |
| 172 | + |
| 173 | +@register(Tags.security, deploy=True) |
| 174 | +def check_debug(app_configs, **kwargs): |
| 175 | + passed_check = not settings.DEBUG |
| 176 | + return [] if passed_check else [W018] |
| 177 | + |
| 178 | + |
| 179 | +@register(Tags.security, deploy=True) |
| 180 | +def check_xframe_deny(app_configs, **kwargs): |
| 181 | + passed_check = ( |
| 182 | + not _xframe_middleware() or |
| 183 | + settings.X_FRAME_OPTIONS == 'DENY' |
| 184 | + ) |
| 185 | + return [] if passed_check else [W019] |
0 commit comments