-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backup): User import sanitization
Importing User models onto SaaS could be dangerous: the imported user might have overpowered flags (`is_staff`, `is_superuser`, etc), excessive `UserPermission`s, or naughty `UserRole`s assigned. These changes modify the import logic remove sanitize those potentially bad inputs. Such sanitization only needs to happen sometimes, though: if you are using this tool to restore a full self-hosted instance, you actually DO want all of this potentially dangerous data to be imported unchanged from your own exports. To resolve this, this change introduces the concept of `ImportScope`s, which maps very closely to `RelocationScope`. Using `import_in_global_scope` therefore does not perform sanitization, while the other, narrower `User` and `Organization` scopes do. Issue: getsentry/team-ospo#166 Issue: getsentry/team-ospo#181
- Loading branch information
1 parent
d7df828
commit bde42d0
Showing
15 changed files
with
482 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
[ | ||
{ | ||
"model": "sentry.user", | ||
"pk": 1, | ||
"fields": { | ||
"password": "pbkdf2_sha256$150000$iEvdIknqYjTr$+QsGn0tfIJ1FZLxQI37mVU1gL2KbL/wqjMtG/dFhsMA=", | ||
"last_login": null, | ||
"username": "[email protected]", | ||
"name": "", | ||
"email": "[email protected]", | ||
"is_staff": true, | ||
"is_active": true, | ||
"is_superuser": true, | ||
"is_managed": false, | ||
"is_sentry_app": null, | ||
"is_password_expired": false, | ||
"last_password_change": "2023-06-22T22:59:57.023Z", | ||
"flags": "0", | ||
"session_nonce": null, | ||
"date_joined": "2023-06-22T22:59:55.488Z", | ||
"last_active": "2023-06-22T22:59:55.489Z", | ||
"avatar_type": 0, | ||
"avatar_url": null | ||
} | ||
}, | ||
{ | ||
"model": "sentry.authenticator", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"created_at": "2023-07-27T16:30:53.325Z", | ||
"last_used_at": null, | ||
"type": 1, | ||
"config": "\"\"" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.useremail", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"email": "[email protected]", | ||
"validation_hash": "mCnWesSVvYQcq7qXQ36AZHwosAd6cghE", | ||
"date_hash_added": "2023-06-22T22:59:55.521Z", | ||
"is_verified": true | ||
} | ||
}, | ||
{ | ||
"model": "sentry.userip", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"ip_address": "127.0.0.2", | ||
"country_code": null, | ||
"region_code": null, | ||
"first_seen": "2012-04-05T03:29:45.000Z", | ||
"last_seen": "2012-04-05T03:29:45.000Z" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.useroption", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"project_id": null, | ||
"organization_id": null, | ||
"key": "timezone", | ||
"value": "\"Europe/Vienna\"" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.userpermission", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"permission": "users.admin" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.userrole", | ||
"pk": 1, | ||
"fields": { | ||
"date_updated": "2023-06-22T23:00:00.123Z", | ||
"date_added": "2023-06-22T22:54:27.960Z", | ||
"name": "Super Admin", | ||
"permissions": "['broadcasts.admin', 'users.admin', 'options.admin']" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.userroleuser", | ||
"pk": 1, | ||
"fields": { | ||
"date_updated": "2023-06-22T23:00:00.123Z", | ||
"date_added": "2023-06-22T22:59:57.000Z", | ||
"user": 1, | ||
"role": 1 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
[ | ||
{ | ||
"model": "sentry.user", | ||
"pk": 1, | ||
"fields": { | ||
"password": "pbkdf2_sha256$150000$iEvdIknqYjTr$+QsGn0tfIJ1FZLxQI37mVU1gL2KbL/wqjMtG/dFhsMA=", | ||
"last_login": null, | ||
"username": "[email protected]", | ||
"name": "", | ||
"email": "[email protected]", | ||
"is_staff": false, | ||
"is_active": true, | ||
"is_superuser": false, | ||
"is_managed": false, | ||
"is_sentry_app": null, | ||
"is_password_expired": false, | ||
"last_password_change": "2023-06-22T22:59:57.023Z", | ||
"flags": "0", | ||
"session_nonce": null, | ||
"date_joined": "2023-06-22T22:59:55.488Z", | ||
"last_active": "2023-06-22T22:59:55.489Z", | ||
"avatar_type": 0, | ||
"avatar_url": null | ||
} | ||
}, | ||
{ | ||
"model": "sentry.authenticator", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"created_at": "2023-07-27T16:30:53.325Z", | ||
"last_used_at": null, | ||
"type": 1, | ||
"config": "\"\"" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.useremail", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"email": "[email protected]", | ||
"validation_hash": "mCnWesSVvYQcq7qXQ36AZHwosAd6cghE", | ||
"date_hash_added": "2023-06-22T22:59:55.521Z", | ||
"is_verified": true | ||
} | ||
}, | ||
{ | ||
"model": "sentry.userip", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"ip_address": "127.0.0.2", | ||
"country_code": null, | ||
"region_code": null, | ||
"first_seen": "2012-04-05T03:29:45.000Z", | ||
"last_seen": "2012-04-05T03:29:45.000Z" | ||
} | ||
}, | ||
{ | ||
"model": "sentry.useroption", | ||
"pk": 1, | ||
"fields": { | ||
"user": 1, | ||
"project_id": null, | ||
"organization_id": null, | ||
"key": "timezone", | ||
"value": "\"Europe/Vienna\"" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional, Tuple | ||
|
||
from django.core.serializers.base import DeserializedObject | ||
|
||
from sentry.backup.dependencies import PrimaryKeyMap | ||
from sentry.backup.scopes import ImportScope | ||
|
||
|
||
class SanitizeUserImportsMixin: | ||
""" | ||
The only realistic reason to do a `Global`ly-scoped import is when restoring some full-instance | ||
backup to a clean install. In this case, one may want to import so-called "superusers": users | ||
with powerful various instance-wide permissions generally reserved for admins and instance | ||
maintainers. Thus, for security reasons, running this import in any `ImportScope` other than | ||
`Global` will sanitize user imports by ignoring imports of the `UserPermission`, `UserRole`, and | ||
`UserRoleUser` models. | ||
""" | ||
|
||
def write_relocation_import( | ||
self, pk_map: PrimaryKeyMap, obj: DeserializedObject, scope: ImportScope | ||
) -> Optional[Tuple[int, int]]: | ||
if scope != ImportScope.Global: | ||
return None | ||
|
||
return super().write_relocation_import(pk_map, obj, scope) # type: ignore[misc] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.