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
+46 -3
View File
@@ -255,6 +255,51 @@ class DownloadCancelled(Exception):
"""Raised when a streamed download is cancelled by the user."""
class RemoteUrlError(ValueError):
"""The URL is not an allowed e621 media URL."""
def validate_remote_url(url):
"""Only http(s) URLs on the known e621 media hosts may be fetched.
Without this the download paths are an SSRF hole: any uploader could make
the server fetch internal addresses (127.0.0.1, LAN services, cloud
metadata) and read the response back through the library.
"""
from urllib.parse import urlparse
parsed = urlparse(str(url or "").strip())
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
raise RemoteUrlError("Only http(s) URLs can be fetched.")
if parsed.hostname not in settings.E621_MEDIA_HOSTS:
raise RemoteUrlError("That host is not an allowed e621 media host.")
return url
def open_remote(url, *, max_redirects=3, **kwargs):
"""GET an allowlisted URL, re-validating every redirect hop.
Returns a streaming ``requests`` response. Redirects are followed
manually so a hop cannot jump to an internal host.
"""
import requests
from urllib.parse import urljoin
current = url
for _ in range(max_redirects + 1):
validate_remote_url(current)
response = requests.get(current, allow_redirects=False, **kwargs)
if response.is_redirect or response.is_permanent_redirect:
location = response.headers.get("Location")
response.close()
if not location:
raise RemoteUrlError("The remote server redirected without a target.")
current = urljoin(current, location)
continue
return response
raise RemoteUrlError("Too many redirects from the remote server.")
def download_file(
url,
destination,
@@ -268,10 +313,8 @@ def download_file(
worker: without it a hung socket would keep a job "downloading" forever
and the cancel flag could never be observed.
"""
import requests
headers = {"User-Agent": settings.USER_AGENT}
with requests.get(
with open_remote(
url, headers=headers, stream=True, timeout=(10, read_timeout)
) as response:
response.raise_for_status()