Security fixes: SSRF, staff role escalation, SPA-only gating, throttling, encrypted keys

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.
This commit is contained in:
2026-09-18 00:21:14 -05:00
parent a904abdf20
commit f86eccf9a3
15 changed files with 265 additions and 27 deletions
@@ -0,0 +1,34 @@
from django.db import migrations, models
def encrypt_existing_keys(apps, schema_editor):
"""Store existing e621 API keys encrypted (idempotent)."""
from apps.accounts.crypto import encrypt_secret
User = apps.get_model("accounts", "User")
for user in User.objects.exclude(e621_api_key="").iterator():
if user.e621_api_key.startswith("enc:"):
continue
user.e621_api_key = encrypt_secret(user.e621_api_key)
user.save(update_fields=["e621_api_key"])
def noop(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
("accounts", "0005_user_preferences"),
]
operations = [
# Ciphertext is longer than the plaintext key, so widen the column
# before encrypting existing rows.
migrations.AlterField(
model_name="user",
name="e621_api_key",
field=models.CharField(blank=True, default="", max_length=400),
),
migrations.RunPython(encrypt_existing_keys, noop),
]