AnonymousSpottedToad/securityPatternsRecovery / ground_truth /python /limit_request_rate__luxman89_TradeArena.json
| { | |
| "role_mappings": [ | |
| { | |
| "patternRole": "Entity", | |
| "entity": "TradeArenaClient", | |
| "file": "sdk/client.py", | |
| "lines": "67", | |
| "notes": "The repo ships its own API client SDK whose emit() (sdk/client.py:112) POSTs to {base_url}/signal with an X-API-Key header — exactly the identity the middleware and SignalRateLimiter key on. It is the in-repo caller that sends the repeated requests the limiter budgets; real third-party callers are of course outside the repo.", | |
| "status": "present", | |
| "nodeId": null | |
| }, | |
| { | |
| "patternRole": "Enforcer", | |
| "entity": "dispatch", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "lines": "150", | |
| "notes": "Starlette BaseHTTPMiddleware hook registered in the app factory via app.add_middleware(RateLimitMiddleware) (src/tradearena/api/main.py:661); it runs before every route handler, derives the entity id (client IP via _client_ip, or the SHA-256 of the X-API-Key header via _hash_api_key), asks _check for a verdict, and on block returns a 429 JSONResponse with Retry-After instead of calling call_next. Enforcer and Limiter share the RateLimitMiddleware class (line 96), so each is mapped to its own method; secondary enforcement points are signal_rate_limiter.check(creator_id) in emit_signal (src/tradearena/api/routes/signals.py:42) and check_signup_ip_cap(ip) in register (src/tradearena/api/routes/auth.py:318).", | |
| "status": "present", | |
| "nodeId": null | |
| }, | |
| { | |
| "patternRole": "Limiter", | |
| "entity": "_check", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "lines": "127", | |
| "notes": "The allow/block decision: for a given namespace+key it delegates to the Redis sliding-window backend when available, otherwise prunes the in-memory timestamp list to the window and returns (False, 0) when len(hits) >= rate, appending the current timestamp only when allowed. It returns (allowed, remaining) to dispatch, which is what turns the verdict into a 429 or a forwarded request.", | |
| "status": "present", | |
| "nodeId": null | |
| }, | |
| { | |
| "patternRole": "HistoryProvider", | |
| "entity": "_redis_check", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "lines": "65", | |
| "notes": "The storage backend that keeps the per-identity request history: a Redis sorted set per key (rl:<namespace>:<id>) trimmed with ZREMRANGEBYSCORE, counted with ZCARD, expired with EXPIRE, and appended to with ZADD on an allowed request. The in-memory fallback store is the defaultdict of timestamp lists self._hits/_key_hits/_auth_hits (line 123) pruned by _prune (line 91); the backend abstraction is cited here in preference to those raw attributes, and the module-level _signup_hits (line 264) and SignalRateLimiter._hits (line 212) hold the same history for the two route-level limiters.", | |
| "status": "present", | |
| "nodeId": null | |
| }, | |
| { | |
| "patternRole": "PolicyProvider", | |
| "entity": "DEFAULT_RATE", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "lines": "19", | |
| "notes": "The limits are bare module constants with no separable provider object: DEFAULT_RATE/DEFAULT_WINDOW (200 per 60s per IP), KEY_RATE/KEY_WINDOW (60/min per API key, lines 23-24), AUTH_RATE/AUTH_WINDOW (10/min on the _AUTH_PATHS set, lines 27-34), SIGNAL_RATE/SIGNAL_WINDOW (10/hour per creator, lines 31-32) and SIGNUP_IP_RATE/SIGNUP_IP_WINDOW (10/24h per IP, lines 261-262). They are consumed as the default arguments of RateLimitMiddleware.__init__ (line 105), which is the only scope/override mechanism — nothing is read from settings or a per-route registry.", | |
| "status": "present", | |
| "nodeId": null | |
| }, | |
| { | |
| "patternRole": "System", | |
| "entity": "emit_signal", | |
| "file": "src/tradearena/api/routes/signals.py", | |
| "lines": "31", | |
| "notes": "The protected business logic reached once the checks pass: the POST /signal handler, which is guarded twice — globally by RateLimitMiddleware.dispatch and again per creator by signal_rate_limiter.check(creator_id) at line 42, before any DB work. Every other router mounted in main.py (auth, leaderboard, battles, tournaments, ...) sits behind the same middleware; this handler is cited as the concrete one whose rate check is explicit in the route body.", | |
| "status": "present", | |
| "nodeId": null | |
| } | |
| ], | |
| "interaction_order": { | |
| "available": true, | |
| "language": "python", | |
| "repo": "luxman89_TradeArena", | |
| "reason": "", | |
| "entryRole": "Enforcer", | |
| "entryRule": "behaviour", | |
| "interactions": [ | |
| { | |
| "rank": 1, | |
| "interaction": "Enforcer → Limiter", | |
| "from": "Enforcer", | |
| "to": "Limiter", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "line": 159, | |
| "via": [], | |
| "mechanism": "call", | |
| "path": [ | |
| { | |
| "caller": "src/tradearena/api/rate_limit.py:<module>.RateLimitMiddleware.dispatch", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "line": 159, | |
| "code": "self._check(\"auth\", ip, self.auth_rate, self.auth_window, self._auth_hits, now)", | |
| "callee": "src/tradearena/api/rate_limit.py:<module>.RateLimitMiddleware._check", | |
| "mechanism": "call" | |
| } | |
| ], | |
| "parent": "" | |
| }, | |
| { | |
| "rank": 2, | |
| "interaction": "Limiter → HistoryProvider", | |
| "from": "Limiter", | |
| "to": "HistoryProvider", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "line": 139, | |
| "via": [], | |
| "mechanism": "call", | |
| "path": [ | |
| { | |
| "caller": "src/tradearena/api/rate_limit.py:<module>.RateLimitMiddleware._check", | |
| "file": "src/tradearena/api/rate_limit.py", | |
| "line": 139, | |
| "code": "_redis_check(f\"rl:{namespace}:{key}\", rate, window, now)", | |
| "callee": "src/tradearena/api/rate_limit.py:<module>._redis_check", | |
| "mechanism": "call" | |
| } | |
| ], | |
| "parent": "Enforcer → Limiter" | |
| } | |
| ], | |
| "anchoredRoles": [ | |
| { | |
| "role": "Enforcer", | |
| "kind": "METHOD" | |
| }, | |
| { | |
| "role": "HistoryProvider", | |
| "kind": "METHOD" | |
| }, | |
| { | |
| "role": "Limiter", | |
| "kind": "METHOD" | |
| }, | |
| { | |
| "role": "PolicyProvider", | |
| "kind": "LOCAL" | |
| } | |
| ], | |
| "skippedRoles": [ | |
| "Entity", | |
| "System" | |
| ] | |
| } | |
| } |
Xet Storage Details
- Size:
- 6.42 kB
- Xet hash:
- 12f997a689349f295b5675420c4bf255a33825d353b821d9e1c9fd2cd063cd55
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.