Commit Graph

19 Commits

Author SHA1 Message Date
Georges-Antoine Assi
b75c499d8b refactor(assets): make rom_ids the only ROM scope on saves and states
The saves handler carried two ROM filters that ANDed together, and the states
handler had only the singular one, so the same "which ROMs" question had three
answers across two sibling handlers. Collapse both onto rom_ids: a single-ROM
lookup is just a one-element scope, which is what the three internal callers in
the saves endpoint now pass.

That leaves the routes to fold their own public rom_id param into the scope.
Rather than write the fold twice, narrow_rom_id_scope joins utils/validation
beside RomIdScope, keeping the documented "combined with rom_id" semantics: both
filters still apply together, so the effective scope is their intersection.

GET /api/states gains the rom_ids param GET /api/saves got in #4304, declared
with the same shared RomIdScope, so a client syncing a known set of ROMs can
discover states the same way it discovers saves, under the same cap and the same
422 on a bad scope.

One behaviour shift falls out of the handler's old falsy check: rom_id=0 used to
be ignored and return the whole library, and now scopes to nothing, which is
what RomIdScope's positive-ID rule says a zero means.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-29 17:38:08 -04:00
Georges-Antoine Assi
cffe65971e refactor(saves): take rom_ids as a repeated query param
Comma-separated ID params in this codebase are a download-route convention
(/roms/download, and the two file_ids params), built by hand for URLs the
browser navigates to. Every JSON API route that takes a list of IDs repeats
the parameter instead, and roms' platform_ids documents that form explicitly.
GET /api/saves is a JSON route, so it should match those.

Declare the scope once as a shared RomIdScope annotation: the cap and the
positive-ID rule become schema constraints that both routes enforce alike and
that now appear in the OpenAPI schema as maxItems and exclusiveMinimum, so
clients can read the limits instead of discovering them from an error. That
drops the hand-rolled parser and the two validation-to-HTTPException
translations; a bad scope is a 422 on both routes.

One semantic change: the query param can no longer express an explicit empty
scope, since an omitted parameter is the only empty form. Negotiate keeps it,
as a JSON body can still send [].

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-29 16:24:45 -04:00
Georges-Antoine Assi
65819d15b4 refactor(saves): move the rom_ids validators to utils/validation
The scope validator and its cap were sitting in the saves DB handler, which
made two endpoints the first non-test callers to import a behavioral symbol
straight through the handler.database facade, for a helper with no DB content
and that get_saves never calls. They belong beside the repo's other
endpoint-facing validators.

Fold the comma-separated ID parse into a shared parse_comma_separated_ids so
the "comma-separated integers" message the tests assert on has one home, and
call it from the ROM download route that already had it inline. Both scope
validators now raise ValidationError like the rest of the module, which lets
negotiate validate in the endpoint body next to its other request-shape
checks: the two routes reject a bad scope with the same 400 instead of one
returning 400 and the other 422.

Also narrow the sync-record lookup to the newest row per slot (superseded rows
were fetched and discarded), and share one second_rom / second_save fixture
instead of three near-identical builders across the new tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-29 16:09:51 -04:00
Georges-Antoine Assi
30451d5651 fix(security): move SSRF defense into the HTTP client path
The previous validator did a preflight `socket.getaddrinfo` before each
httpx request. Two problems:

  * DNS rebinding / TOCTOU: httpx re-resolves at connect time, so a
    hostname can answer with a public IP for the validator and a
    private IP for the real request. The preflight check did not
    constrain the connection.
  * Event-loop blocking: `socket.getaddrinfo` is synchronous, and the
    media-download callers are async. Slow resolvers stalled
    unrelated requests.

Replace it with two layers, both wired automatically onto every httpx
client built by `utils.context`:

  1. A request event hook running `validate_url_for_http_request`
     (syntactic checks only: scheme, reserved hostnames, literal IPs,
     internal TLDs). No DNS, no call-site responsibility.
  2. `SSRFProtectedAsyncBackend` / `SSRFProtectedSyncBackend`, custom
     httpcore network backends that resolve the hostname inside
     `connect_tcp`, reject any address in a forbidden range, then
     connect to that *same* validated address. The async variant uses
     `loop.getaddrinfo` so it doesn't block the loop. httpcore calls
     `start_tls(server_hostname=<URL host>)` after `connect_tcp`, so
     TLS SNI and cert verification still use the original hostname
     even though the TCP layer connects by IP.

Drop the explicit `validate_url_for_http_request(...)` calls from
`resources_handler.py` — the event hook covers them. Consolidate the
URL validator and its tests under `utils/ssrf.py` /
`tests/utils/test_ssrf.py` so the SSRF surface lives in one module.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 17:58:14 -04:00
Georges-Antoine Assi
2b0ed2296b cleanup 2026-05-27 09:29:21 -04:00
Georges-Antoine Assi
3ae7f998b9 run fmt 2026-05-27 09:20:23 -04:00
Claude
009d358175 fix(security): resolve hostnames in SSRF URL validator
validate_url_for_http_request previously skipped DNS resolution, so
attacker-controlled domains that resolve to private/loopback/link-local
addresses (e.g. 127.0.0.1.nip.io) passed validation and the subsequent
httpx GET hit internal services. Resolve the hostname via getaddrinfo
and reject any result whose IP is private, loopback, link-local,
reserved, multicast, or unspecified. Unresolvable hostnames are
rejected as well.

https://claude.ai/code/session_01T335ZvA825YhuzPctmYzUy
2026-05-27 12:33:36 +00:00
Georges-Antoine Assi
b79bcbcfce remove clud meta ID 2026-04-05 19:50:37 -04:00
Georges-Antoine Assi
7c41fb5bac revert fs_name sibling roms 2026-04-05 17:57:48 -04:00
Georges-Antoine Assi
ef35ecaea9 props rom updte endpoint 2026-04-04 14:16:00 -04:00
Georges-Antoine Assi
3fc8c68c49 last change 2026-03-07 10:23:33 -05:00
Georges-Antoine Assi
13f7586590 changes from grp review 2026-03-07 10:07:50 -05:00
Georges-Antoine Assi
b3659a1226 changes from bot review 2026-03-07 08:58:42 -05:00
Georges-Antoine Assi
46915eee5b fix types and tests 2026-03-06 19:52:09 -05:00
Georges-Antoine Assi
b030b98062 attempt to fix vuln reported in PR 2026-03-06 19:42:56 -05:00
Georges-Antoine Assi
c7d3bb7d80 fixups 2025-10-05 14:56:10 -04:00
Georges-Antoine Assi
3ee14fd23f force ascii on password and email 2025-10-05 14:52:00 -04:00
Georges-Antoine Assi
ae9b01e38d update rules 2025-10-05 14:42:57 -04:00
Georges-Antoine Assi
f6d7f9d1c1 [ROMM-2509] Validate user email and username 2025-10-05 14:11:34 -04:00