Skip to content

fix(http): wire rate limiter, harden CORS and fix Swagger authorize - #22

Merged
SrStamm merged 2 commits into
mainfrom
fix/http-hardening
Oct 20, 2026
Merged

SrStamm merged 2 commits into
mainfrom
fix/http-hardening

Conversation

@SrStamm

@SrStamm SrStamm commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Context

B6 of the portfolio fixes — three HTTP integration defects where documented behavior did not match reality: rate limiting was declared but not wired, CORS used an invalid origin/credentials combination, and Swagger's Authorize button pointed at a route that does not exist.

Changes

Rate limiting (over-limit returned 500, documented as 429)

core/limiter.py defines a Limiter, and 38 @limiter.limit(...) decorators are active across 7 routers — but app.state.limiter and the RateLimitExceeded handler were both commented out in main.py. Over-limit requests therefore surfaced as unhandled 500s.

  • Registered app.state.limiter = limiter and app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler).

CORS (invalid "*" + allow_credentials=True)

allow_origins mixed the "*" wildcard with allow_credentials=True, which browsers reject.

  • Removed "*" and pinned origins to the demo frontend, http://localhost:5173.
  • Note: the first attempt used a trailing slash (http://localhost:5173/), which made the preflight fail with 400 — the Origin header never carries a trailing slash. The new CORS test caught it before merge.

Swagger Authorize (404)

OAuth2PasswordBearer(tokenUrl="token") pointed at a non-existent /token route, so the Authorize button could never authenticate.

  • Pointed tokenUrl at the real /login route.

Tests

  • test/test_routers/test_cors.py — preflight allowed for the demo origin, rejected for unknown origins, and no wildcard in allow_origins.
  • test/test_routers/test_rate_limit.py — over-limit returns 429 (never 500) and the body reports Rate limit exceeded; limiter state is reset per test to avoid cross-test bleed.

Verification

pytest -q -> 177 passed (baseline 155).

- Register app.state.limiter and the RateLimitExceeded handler so over-limit
  requests return 429 instead of an unhandled 500
- Remove the invalid "*" origin combined with allow_credentials=True and pin
  CORS to the demo frontend origin (http://localhost:5173, no trailing slash)
- Add tests covering the CORS preflight and the 429 rate-limit response
The OAuth2PasswordBearer tokenUrl was "token", but no /token route exists;
Swagger's Authorize button failed with 404. Point it at /login.
@SrStamm
SrStamm merged commit 7bc5558 into main Oct 20, 2026
1 check passed
@SrStamm
SrStamm deleted the fix/http-hardening branch October 20, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant