Findings from the audit (50-check harness across guest/user/uploader/staff/ admin) and their fixes: - SSRF: 'Download to Library' and the staged-upload resolve path fetched any http(s) URL. services.validate_remote_url now enforces the e621 media allowlist and open_remote re-validates every redirect hop; the download-task create endpoint and the guest proxy use them, so internal addresses (127.0.0.1, LAN, metadata) are rejected with 400. - Privilege escalation: staff could promote users to staff and demote other staff. Role changes across the staff boundary now require an admin, matching the account-deletion rules; the Users page hides what the backend would refuse. - SPA-only gating: /api/storage/ and /api/duplicates/* were readable by any authenticated account (absolute paths, duplicate groups) while the SPA only shows them to uploaders. They now require CanUpload. - Throttling (REST_FRAMEWORK, env-overridable, counted in Redis): anon 120/min, user 600/min, login 5/min, register 20/hour, guest e621 proxy 60/hour. Login now goes through a throttled view. - e621 API keys are encrypted at rest with a Fernet key derived from SECRET_KEY (apps/accounts/crypto.py); a data migration encrypts existing rows and the column widens first. Reads decrypt transparently, legacy plaintext still works, and a changed SECRET_KEY reads as 'not configured' instead of leaking. Rotating SECRET_KEY now invalidates stored keys as well as signed media URLs. - Hardening: the server refuses to start with DEBUG=False while SECRET_KEY is still the development default. Verified: corrected harness 50/50 (guest visibility, IDOR, signed-URL tamper/expiry, staged-upload/similarity privacy, role matrix, SSRF), login throttles at the 6th attempt with 429, anon polling unaffected, the guest proxy still reaches allowlisted hosts, live e621 auth works with the decrypted key, and DB rows hold only ciphertext.
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from django.contrib.auth.models import AbstractUser
|
|
from django.db import models
|
|
|
|
|
|
class User(AbstractUser):
|
|
"""Project user with optional e621 API credentials and an app role."""
|
|
|
|
ROLE_USER = "user"
|
|
ROLE_UPLOADER = "uploader"
|
|
ROLE_STAFF = "staff"
|
|
ROLE_CHOICES = [
|
|
(ROLE_USER, "User"),
|
|
(ROLE_UPLOADER, "Uploader"),
|
|
(ROLE_STAFF, "Staff"),
|
|
]
|
|
|
|
role = models.CharField(max_length=20, choices=ROLE_CHOICES, default=ROLE_USER)
|
|
avatar = models.ForeignKey(
|
|
"library.MediaItem",
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
related_name="+",
|
|
)
|
|
e621_username = models.CharField(max_length=100, blank=True, default="")
|
|
# Stored encrypted (see apps/accounts/crypto.py): never the plaintext key.
|
|
e621_api_key = models.CharField(max_length=400, blank=True, default="")
|
|
e621_base_url = models.CharField(max_length=200, default="https://e621.net")
|
|
# Per-user browse preferences (landing page, default filters, grid size).
|
|
preferences = models.JSONField(default=dict, blank=True)
|
|
|
|
@property
|
|
def e621_api_key_plain(self):
|
|
"""The decrypted e621 key ("" when it cannot be decrypted)."""
|
|
from .crypto import decrypt_secret
|
|
|
|
return decrypt_secret(self.e621_api_key)
|
|
|
|
@property
|
|
def e621_configured(self):
|
|
return bool(self.e621_username and self.e621_api_key_plain)
|
|
|
|
@property
|
|
def can_upload(self):
|
|
return self.is_superuser or self.role in {self.ROLE_UPLOADER, self.ROLE_STAFF}
|
|
|
|
@property
|
|
def is_app_staff(self):
|
|
"""Staff in this app: superusers, Django staff, or the staff role."""
|
|
return bool(
|
|
self.is_superuser or self.is_staff or self.role == self.ROLE_STAFF
|
|
)
|