| 1 |
--- |
| 2 |
title: Caveats |
| 3 |
description: Things to be aware of when using django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Caveats |
| 7 |
|
| 8 |
There are a few things to be aware of when using django-guardian to manage object permissions. |
| 9 |
If ignored, they can lead to unexpected behavior and potential security issues. |
| 10 |
|
| 11 |
## Orphaned object permissions |
| 12 |
|
| 13 |
Note the following does not apply if using direct foreign keys, as |
| 14 |
documented in `performance-direct-fk`. |
| 15 |
|
| 16 |
Permissions, in particular *per object permissions*, can be tricky to manage. |
| 17 |
For example, how can we manage permissions that are no longer used? |
| 18 |
In some setups, it is possible to reuse database primary keys |
| 19 |
which have been removed from the database. |
| 20 |
When permissions are at the model level only, this is not a problem; |
| 21 |
but can lead to tremendous security issues when using object permissions. |
| 22 |
How can we deal with this? |
| 23 |
|
| 24 |
Let's imagine our table has primary key to the filesystem path. |
| 25 |
We have a record with pk equal to `/home/www/joe.config`. |
| 26 |
User *jane* has read access to joe's configuration and we store that information in database |
| 27 |
by creating guardian's object permissions. |
| 28 |
Now, *joe* user removes account from our site and another user creates account with *joe* as username. |
| 29 |
The problem is that if we haven't removed object permissions explicitly in |
| 30 |
the process of first *joe* account removal, *jane* still |
| 31 |
has read permissions for *joe's* configuration file - but this is another user. |
| 32 |
|
| 33 |
There is no easy way to deal with orphaned permissions as they are not |
| 34 |
foreign keyed with objects directly. Even if they would, there are some |
| 35 |
database engines—or *ON DELETE* rules—which restricts removal of |
| 36 |
related objects. |
| 37 |
|
| 38 |
!!! danger "Important" |
| 39 |
|
| 40 |
It is **extremely** important to remove`UserObjectPermission` and |
| 41 |
`GroupObjectPermission` as we delete objects for which permissions are defined. |
| 42 |
|
| 43 |
Guardian comes with utility function which tries to help to remove |
| 44 |
orphaned object permissions. Remember - those are only helpers. |
| 45 |
Applications should remove those object permissions explicitly by |
| 46 |
itself. |
| 47 |
|
| 48 |
From our previous example, our application should remove a user object (e.g., *joe*). |
| 49 |
However, permissions for the *joe* user assigned to *jane* would**NOT** be removed. |
| 50 |
In this case, it would be easy to remove |
| 51 |
user/group object permissions if we connect proper action with proper |
| 52 |
signal. |
| 53 |
This could be achieved by following snippet: |
| 54 |
|
| 55 |
```python |
| 56 |
from django.contrib.contenttypes.models import ContentType |
| 57 |
from django.db.models import Q |
| 58 |
from django.db.models.signals import pre_delete |
| 59 |
from guardian.models import User |
| 60 |
from guardian.models import UserObjectPermission |
| 61 |
from guardian.models import GroupObjectPermission |
| 62 |
|
| 63 |
|
| 64 |
def remove_obj_perms_connected_with_user(sender, instance, **kwargs): |
| 65 |
filters = Q(content_type=ContentType.objects.get_for_model(instance), |
| 66 |
object_pk=instance.pk) |
| 67 |
UserObjectPermission.objects.filter(filters).delete() |
| 68 |
GroupObjectPermission.objects.filter(filters).delete() |
| 69 |
|
| 70 |
pre_delete.connect(remove_obj_perms_connected_with_user, sender=User) |
| 71 |
``` |
| 72 |
|
| 73 |
This signal handler would remove all object permissions connected with |
| 74 |
user just before user is actually removed. |
| 75 |
|
| 76 |
If we forgot to add such handlers, we may still remove orphaned object |
| 77 |
permissions by using `clean_orphan_obj_perms` If our application uses |
| 78 |
[celery](http://www.celeryproject.org/), it is also straightforward to remove |
| 79 |
orphaned permissions periodically with `guardian.utils.clean_orphan_obj_perms` function. |
| 80 |
We would still **strongly** advise to remove orphaned object permissions explicitly |
| 81 |
(i.e., at view that confirms object removal or using signals as described above). |
| 82 |
|
| 83 |
!!! info |
| 84 |
- `guardian.utils.clean_orphan_obj_perms` |
| 85 |
- `clean_orphan_obj_perms` |
| 86 |
|
| 87 |
## Using multiple databases |
| 88 |
|
| 89 |
This is not supported at present time due to a Django bug. See |
| 90 |
[288](https://github.com/django-guardian/django-guardian/issues/288) and |
| 91 |
[16281](https://code.djangoproject.com/ticket/16281). |
| 92 |
|
| 93 |
## Primary key types |
| 94 |
|
| 95 |
Guardian stores object primary keys as text in the `object_pk` column of its |
| 96 |
permission tables. When filtering querysets (e.g. in `get_objects_for_user` or |
| 97 |
`get_objects_for_group`), guardian must cast primary keys so that the stored |
| 98 |
text values can be compared against the model's actual PK column. |
| 99 |
|
| 100 |
The following PK types are natively supported with optimised casts: |
| 101 |
|
| 102 |
| PK field type | Cast strategy | |
| 103 |
|---|---| |
| 104 |
| `IntegerField`, `AutoField`, `BigIntegerField`, `SmallIntegerField`, `PositiveIntegerField`, `PositiveSmallIntegerField` | Cast to `BigIntegerField` | |
| 105 |
| `UUIDField` | Native UUID cast (or string with hyphens stripped on databases without native UUID support) | |
| 106 |
| `CharField` | No cast needed — already stored as text | |
| 107 |
|
| 108 |
For **any other primary key type** (e.g. `TextField`, PostgreSQL `macaddr` or |
| 109 |
`inet` fields), guardian automatically falls back to casting PKs with |
| 110 |
`Cast("pk", CharField())`. This means models with non-standard PK types work |
| 111 |
out of the box: |
| 112 |
|
| 113 |
```python |
| 114 |
from django.db import models |
| 115 |
|
| 116 |
class Device(models.Model): |
| 117 |
mac = models.TextField(primary_key=True) |
| 118 |
|
| 119 |
class Meta: |
| 120 |
permissions = (("view_device", "Can view device"),) |
| 121 |
``` |
| 122 |
|
| 123 |
```python |
| 124 |
from guardian.shortcuts import assign_perm, get_objects_for_user |
| 125 |
|
| 126 |
device = Device.objects.create(mac="00:1B:44:11:3A:B7") |
| 127 |
assign_perm("view_device", user, device) |
| 128 |
|
| 129 |
# Returns a queryset containing the device — no extra configuration needed |
| 130 |
devices = get_objects_for_user(user, "app.view_device") |
| 131 |
``` |
| 132 |
|
| 133 |
!!! warning "Performance consideration" |
| 134 |
|
| 135 |
The string-casting fallback adds a `Cast()` annotation to every query, |
| 136 |
which prevents the database from using primary key indexes directly. For |
| 137 |
large tables this can be noticeably slower than native type comparisons. |
| 138 |
If performance is critical, consider using |
| 139 |
[direct foreign keys](performance.md#direct-foreign-keys) instead. |
| 140 |
|
| 141 |
!!! note |
| 142 |
|
| 143 |
Permission management functions such as `assign_perm`, `remove_perm`, and |
| 144 |
`get_perms` work with any PK type without special handling. The |
| 145 |
casting behaviour described above only applies to queryset-filtering |
| 146 |
functions like `get_objects_for_user` and `get_objects_for_group`. |