| 1 |
from itertools import chain |
| 2 |
from types import MethodType |
| 3 |
|
| 4 |
from django.apps import apps |
| 5 |
from django.conf import settings |
| 6 |
from django.core import checks |
| 7 |
from django.utils.module_loading import import_string |
| 8 |
|
| 9 |
from .management import _get_builtin_permissions |
| 10 |
|
| 11 |
|
| 12 |
def _subclass_index(class_path, candidate_paths): |
| 13 |
""" |
| 14 |
Return the index of dotted class path (or a subclass of that class) in a |
| 15 |
list of candidate paths. If it does not exist, return -1. |
| 16 |
""" |
| 17 |
cls = import_string(class_path) |
| 18 |
for index, path in enumerate(candidate_paths): |
| 19 |
try: |
| 20 |
candidate_cls = import_string(path) |
| 21 |
if issubclass(candidate_cls, cls): |
| 22 |
return index |
| 23 |
except (ImportError, TypeError): |
| 24 |
continue |
| 25 |
return -1 |
| 26 |
|
| 27 |
|
| 28 |
def check_user_model(app_configs=None, **kwargs): |
| 29 |
if app_configs is None: |
| 30 |
cls = apps.get_model(settings.AUTH_USER_MODEL) |
| 31 |
else: |
| 32 |
app_label, model_name = settings.AUTH_USER_MODEL.split(".") |
| 33 |
for app_config in app_configs: |
| 34 |
if app_config.label == app_label: |
| 35 |
cls = app_config.get_model(model_name) |
| 36 |
break |
| 37 |
else: |
| 38 |
# Checks might be run against a set of app configs that don't |
| 39 |
# include the specified user model. In this case we simply don't |
| 40 |
# perform the checks defined below. |
| 41 |
return [] |
| 42 |
|
| 43 |
errors = [] |
| 44 |
|
| 45 |
# Check that REQUIRED_FIELDS is a list |
| 46 |
if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)): |
| 47 |
errors.append( |
| 48 |
checks.Error( |
| 49 |
"'REQUIRED_FIELDS' must be a list or tuple.", |
| 50 |
obj=cls, |
| 51 |
id="auth.E001", |
| 52 |
) |
| 53 |
) |
| 54 |
|
| 55 |
# Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS. |
| 56 |
if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS: |
| 57 |
errors.append( |
| 58 |
checks.Error( |
| 59 |
"The field named as the 'USERNAME_FIELD' " |
| 60 |
"for a custom user model must not be included in 'REQUIRED_FIELDS'.", |
| 61 |
hint=( |
| 62 |
"The 'USERNAME_FIELD' is currently set to '%s', you " |
| 63 |
"should remove '%s' from the 'REQUIRED_FIELDS'." |
| 64 |
% (cls.USERNAME_FIELD, cls.USERNAME_FIELD) |
| 65 |
), |
| 66 |
obj=cls, |
| 67 |
id="auth.E002", |
| 68 |
) |
| 69 |
) |
| 70 |
|
| 71 |
# Check that the username field is unique |
| 72 |
if not cls._meta.get_field(cls.USERNAME_FIELD).unique and not any( |
| 73 |
constraint.fields == (cls.USERNAME_FIELD,) |
| 74 |
for constraint in cls._meta.total_unique_constraints |
| 75 |
): |
| 76 |
if settings.AUTHENTICATION_BACKENDS == [ |
| 77 |
"django.contrib.auth.backends.ModelBackend" |
| 78 |
]: |
| 79 |
errors.append( |
| 80 |
checks.Error( |
| 81 |
"'%s.%s' must be unique because it is named as the " |
| 82 |
"'USERNAME_FIELD'." % (cls._meta.object_name, cls.USERNAME_FIELD), |
| 83 |
obj=cls, |
| 84 |
id="auth.E003", |
| 85 |
) |
| 86 |
) |
| 87 |
else: |
| 88 |
errors.append( |
| 89 |
checks.Warning( |
| 90 |
"'%s.%s' is named as the 'USERNAME_FIELD', but it is not unique." |
| 91 |
% (cls._meta.object_name, cls.USERNAME_FIELD), |
| 92 |
hint=( |
| 93 |
"Ensure that your authentication backend(s) can handle " |
| 94 |
"non-unique usernames." |
| 95 |
), |
| 96 |
obj=cls, |
| 97 |
id="auth.W004", |
| 98 |
) |
| 99 |
) |
| 100 |
|
| 101 |
if isinstance(cls().is_anonymous, MethodType): |
| 102 |
errors.append( |
| 103 |
checks.Critical( |
| 104 |
"%s.is_anonymous must be an attribute or property rather than " |
| 105 |
"a method. Ignoring this is a security issue as anonymous " |
| 106 |
"users will be treated as authenticated!" % cls, |
| 107 |
obj=cls, |
| 108 |
id="auth.C009", |
| 109 |
) |
| 110 |
) |
| 111 |
if isinstance(cls().is_authenticated, MethodType): |
| 112 |
errors.append( |
| 113 |
checks.Critical( |
| 114 |
"%s.is_authenticated must be an attribute or property rather " |
| 115 |
"than a method. Ignoring this is a security issue as anonymous " |
| 116 |
"users will be treated as authenticated!" % cls, |
| 117 |
obj=cls, |
| 118 |
id="auth.C010", |
| 119 |
) |
| 120 |
) |
| 121 |
return errors |
| 122 |
|
| 123 |
|
| 124 |
def check_models_permissions(app_configs=None, **kwargs): |
| 125 |
if app_configs is None: |
| 126 |
models = apps.get_models() |
| 127 |
else: |
| 128 |
models = chain.from_iterable( |
| 129 |
app_config.get_models() for app_config in app_configs |
| 130 |
) |
| 131 |
|
| 132 |
Permission = apps.get_model("auth", "Permission") |
| 133 |
permission_name_max_length = Permission._meta.get_field("name").max_length |
| 134 |
permission_codename_max_length = Permission._meta.get_field("codename").max_length |
| 135 |
errors = [] |
| 136 |
|
| 137 |
for model in models: |
| 138 |
opts = model._meta |
| 139 |
builtin_permissions = dict(_get_builtin_permissions(opts)) |
| 140 |
# Check builtin permission name length. |
| 141 |
max_builtin_permission_name_length = ( |
| 142 |
max(len(name) for name in builtin_permissions.values()) |
| 143 |
if builtin_permissions |
| 144 |
else 0 |
| 145 |
) |
| 146 |
if max_builtin_permission_name_length > permission_name_max_length: |
| 147 |
verbose_name_max_length = permission_name_max_length - ( |
| 148 |
max_builtin_permission_name_length - len(opts.verbose_name_raw) |
| 149 |
) |
| 150 |
errors.append( |
| 151 |
checks.Error( |
| 152 |
"The verbose_name of model '%s' must be at most %d " |
| 153 |
"characters for its builtin permission names to be at " |
| 154 |
"most %d characters." |
| 155 |
% (opts.label, verbose_name_max_length, permission_name_max_length), |
| 156 |
obj=model, |
| 157 |
id="auth.E007", |
| 158 |
) |
| 159 |
) |
| 160 |
# Check builtin permission codename length. |
| 161 |
max_builtin_permission_codename_length = ( |
| 162 |
max(len(codename) for codename in builtin_permissions.keys()) |
| 163 |
if builtin_permissions |
| 164 |
else 0 |
| 165 |
) |
| 166 |
if max_builtin_permission_codename_length > permission_codename_max_length: |
| 167 |
model_name_max_length = permission_codename_max_length - ( |
| 168 |
max_builtin_permission_codename_length - len(opts.model_name) |
| 169 |
) |
| 170 |
errors.append( |
| 171 |
checks.Error( |
| 172 |
"The name of model '%s' must be at most %d characters " |
| 173 |
"for its builtin permission codenames to be at most %d " |
| 174 |
"characters." |
| 175 |
% ( |
| 176 |
opts.label, |
| 177 |
model_name_max_length, |
| 178 |
permission_codename_max_length, |
| 179 |
), |
| 180 |
obj=model, |
| 181 |
id="auth.E011", |
| 182 |
) |
| 183 |
) |
| 184 |
codenames = set() |
| 185 |
for codename, name in opts.permissions: |
| 186 |
# Check custom permission name length. |
| 187 |
if len(name) > permission_name_max_length: |
| 188 |
errors.append( |
| 189 |
checks.Error( |
| 190 |
"The permission named '%s' of model '%s' is longer " |
| 191 |
"than %d characters." |
| 192 |
% ( |
| 193 |
name, |
| 194 |
opts.label, |
| 195 |
permission_name_max_length, |
| 196 |
), |
| 197 |
obj=model, |
| 198 |
id="auth.E008", |
| 199 |
) |
| 200 |
) |
| 201 |
# Check custom permission codename length. |
| 202 |
if len(codename) > permission_codename_max_length: |
| 203 |
errors.append( |
| 204 |
checks.Error( |
| 205 |
"The permission codenamed '%s' of model '%s' is " |
| 206 |
"longer than %d characters." |
| 207 |
% ( |
| 208 |
codename, |
| 209 |
opts.label, |
| 210 |
permission_codename_max_length, |
| 211 |
), |
| 212 |
obj=model, |
| 213 |
id="auth.E012", |
| 214 |
) |
| 215 |
) |
| 216 |
# Check custom permissions codename clashing. |
| 217 |
if codename in builtin_permissions: |
| 218 |
errors.append( |
| 219 |
checks.Error( |
| 220 |
"The permission codenamed '%s' clashes with a builtin " |
| 221 |
"permission for model '%s'." % (codename, opts.label), |
| 222 |
obj=model, |
| 223 |
id="auth.E005", |
| 224 |
) |
| 225 |
) |
| 226 |
elif codename in codenames: |
| 227 |
errors.append( |
| 228 |
checks.Error( |
| 229 |
"The permission codenamed '%s' is duplicated for " |
| 230 |
"model '%s'." % (codename, opts.label), |
| 231 |
obj=model, |
| 232 |
id="auth.E006", |
| 233 |
) |
| 234 |
) |
| 235 |
codenames.add(codename) |
| 236 |
|
| 237 |
return errors |
| 238 |
|
| 239 |
|
| 240 |
def check_middleware(app_configs, **kwargs): |
| 241 |
errors = [] |
| 242 |
|
| 243 |
login_required_index = _subclass_index( |
| 244 |
"django.contrib.auth.middleware.LoginRequiredMiddleware", |
| 245 |
settings.MIDDLEWARE, |
| 246 |
) |
| 247 |
|
| 248 |
if login_required_index != -1: |
| 249 |
auth_index = _subclass_index( |
| 250 |
"django.contrib.auth.middleware.AuthenticationMiddleware", |
| 251 |
settings.MIDDLEWARE, |
| 252 |
) |
| 253 |
if auth_index == -1 or auth_index > login_required_index: |
| 254 |
errors.append( |
| 255 |
checks.Error( |
| 256 |
"In order to use django.contrib.auth.middleware." |
| 257 |
"LoginRequiredMiddleware, django.contrib.auth.middleware." |
| 258 |
"AuthenticationMiddleware must be defined before it in MIDDLEWARE.", |
| 259 |
id="auth.E013", |
| 260 |
) |
| 261 |
) |
| 262 |
return errors |