[GH-ISSUE #2332] [Issue]: Invoke command rate-limit headers are overwritten by the coarse pre-extraction bucket in the middleware (server runtime) #1158

Closed
opened 2026-10-21 05:28:07 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2332

Originally assigned to: @Xelvanis on GitHub.

Summary

Severity: Low — Correctness (server runtime, advisory headers). For /api/invoke, the handler sets correct command-specific rate-limit headers, but after next.run(request) returns the middleware re-applies the coarse invoke-pre-extraction outcome with headers.insert, overwriting ratelimit-limit/ratelimit-remaining. Clients always see the coarse 6000 limit for invoke commands, breaking client-side backoff that reads ratelimit-remaining. Enforcement (429 + retry-after) is still correct; only the advisory headers are wrong.

Expected behavior

The advisory ratelimit-* headers on an /api/invoke response should reflect the command-specific bucket the handler enforced (e.g. update_apply=5, backup_*=30), not the coarse pre-extraction bucket.

Actual behavior

src-tauri/src/http_server.rs:1378-1381:

            let mut response = next.run(request).await;
            if let Some(outcome) = &rate_limit {
                apply_rate_limit_headers(response.headers_mut(), outcome);
            }

/api/invoke passes through api_controls_middleware, which computes the coarse invoke-pre-extraction outcome (limit INVOKE_PRE_EXTRACTION_API_RATE_LIMIT = 6000). The invoke handler then computes the command-specific outcome and sets correct headers. But the middleware re-applies the coarse outcome via apply_rate_limit_headers, which uses headers.insert (1905/1908/1911) — overwriting the handler's headers on every invoke response (success and 429). Enforcement survives because the coarse outcome is allowed (retry_after None), so the 429 status + retry-after are not clobbered. The test api_invoke_enforces_command_specific_rate_limit_at_dispatch_boundary calls invoke() directly, bypassing the middleware, so it never catches the overwrite.

Steps to reproduce

  1. Server runtime; any /api/invoke request through the production router.
  2. Inspect the ratelimit-* headers (most visible when a command-specific limit like update_apply/backup_* is exceeded): they report the coarse 6000 bucket, not the command limit.

Impact area

  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: remote/Docker runtime (Axum server) — not the primary Tauri desktop path
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? yes (server-only)
  • Provider, model, or integration, if relevant: n/a

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Advisory-only — server enforcement is correct; impact limited to a client doing adaptive backoff off ratelimit-remaining, hence low.

Additional context

Suggested fix: do not blindly re-apply the coarse outcome over the handler's headers. Simplest: after next.run, only call apply_rate_limit_headers when !response.headers().contains_key("ratelimit-limit") (insert-if-absent), or make the invoke handler the sole header authority for that route.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2332 Originally assigned to: @Xelvanis on GitHub. ## Summary **Severity: Low — Correctness (server runtime, advisory headers).** For `/api/invoke`, the handler sets correct command-specific rate-limit headers, but after `next.run(request)` returns the middleware re-applies the coarse `invoke-pre-extraction` outcome with `headers.insert`, overwriting `ratelimit-limit`/`ratelimit-remaining`. Clients always see the coarse 6000 limit for invoke commands, breaking client-side backoff that reads `ratelimit-remaining`. Enforcement (429 + retry-after) is still correct; only the advisory headers are wrong. ## Expected behavior The advisory `ratelimit-*` headers on an `/api/invoke` response should reflect the command-specific bucket the handler enforced (e.g. `update_apply=5`, `backup_*=30`), not the coarse pre-extraction bucket. ## Actual behavior `src-tauri/src/http_server.rs:1378-1381`: ```rust let mut response = next.run(request).await; if let Some(outcome) = &rate_limit { apply_rate_limit_headers(response.headers_mut(), outcome); } ``` `/api/invoke` passes through `api_controls_middleware`, which computes the coarse `invoke-pre-extraction` outcome (limit `INVOKE_PRE_EXTRACTION_API_RATE_LIMIT = 6000`). The invoke handler then computes the command-specific outcome and sets correct headers. But the middleware re-applies the coarse outcome via `apply_rate_limit_headers`, which uses `headers.insert` (1905/1908/1911) — overwriting the handler's headers on every invoke response (success and 429). Enforcement survives because the coarse outcome is `allowed` (retry_after `None`), so the 429 status + retry-after are not clobbered. The test `api_invoke_enforces_command_specific_rate_limit_at_dispatch_boundary` calls `invoke()` directly, bypassing the middleware, so it never catches the overwrite. ## Steps to reproduce 1. Server runtime; any `/api/invoke` request through the production router. 2. Inspect the `ratelimit-*` headers (most visible when a command-specific limit like `update_apply`/`backup_*` is exceeded): they report the coarse 6000 bucket, not the command limit. ## Impact area - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: remote/Docker runtime (Axum server) — not the primary Tauri desktop path - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? yes (server-only) - Provider, model, or integration, if relevant: n/a ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Advisory-only — server enforcement is correct; impact limited to a client doing adaptive backoff off `ratelimit-remaining`, hence low. ## Additional context Suggested fix: do not blindly re-apply the coarse outcome over the handler's headers. Simplest: after `next.run`, only call `apply_rate_limit_headers` when `!response.headers().contains_key("ratelimit-limit")` (insert-if-absent), or make the invoke handler the sole header authority for that route. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1158
No description provided.