Skip to content

fix(api): every refused exec join counts against its network's budget (closes #546) - #555

Merged
samuelmbabhazi merged 3 commits into
mainfrom
fix/546-exec-upgrade-budget
Nov 29, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
fix/546-exec-upgrade-budget

Conversation

@samuelmbabhazi

@samuelmbabhazi samuelmbabhazi commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #546.

The exec WebSocket's upgrade limiter counted only the joins refused at the sign-in: wrong origin, no cookie, or a session that does not authenticate. A signed-in browser could ask for a well-formed but unknown session id again and again, or for one already joined or one it may not use. Each request then answered 404 after reading and touching its sign-in, and never used the budget.

What changed

apps/api/src/modules/deployments/interface/exec-socket.ts:

  • Every refused join counts against the network's budget, the 404s after the sign-in included: an unknown session, a session joined already, another person's session, no container.exec, or a join overtaken by another one. The answers stay the same uniform 404.
  • A join being checked counts too. admit takes a place in the budget before the sign-in lookup, and release gives it back when the check ends. Twenty concurrent joins from one network therefore use the budget up while they are checked, instead of all passing the throttled check before the first refusal is recorded.
  • Past 20 in a minute, a network gets 429 before any lookup, until that minute is over. A join that succeeds costs nothing, so reconnects and normal joins are unaffected unless that network also had 20 refusals in the minute.
  • The limiter state stays bounded. The map of refusals is pruned past 10,000 entries, as before. The new map holds only joins under way, removed when they end. Both are keyed by the client the trusted proxy forwarded (upgradeClientIp, TRUST_PROXY), as before.
  • SPEC 3.1 describes the budget.

How it was tested

exec-socket.spec.ts, which runs a real HTTP server and ws clients, with a counter of sign-in lookups:

  • Repeated unknown-id joins with a valid sign-in: 20 answer 404 with 20 lookups. The 21st, and a join of the person's own session, answer 429 with no new lookup. Once the minute is over (Date.now moved by 61 s), the own session joins again.
  • The other refusals count too: a session already joined, another person's session, and no permission. Each is refused 20 times with 404, then answers 429.
  • Refusals within the budget: 19 refusals still let a normal join through.
  • Concurrent joins: 25 joins while the sign-in is slow make exactly 20 lookups, the other 5 answer 429 at once, and the 20 end in 404. After that, the next join answers 429 with no lookup.
  • Existing tests: the limit on failed sign-ins and the trusted-proxy keying still pass unchanged.
  • Mutations: counting no 404s fails 3 tests, and ignoring joins under way fails the concurrency test.
  • Locally: API unit 999/999; lint, typecheck and format are clean.

Security considerations

  • Resource hardening only: no change to who may join a session or to what a refusal says.
  • The budget applies per network, so browsers behind one NAT share it. They are refused only after 20 refused joins in a minute, never because of successful ones.

Summary by CodeRabbit

  • Bug Fixes
    • WebSocket join requests are now rate-limited across all refused attempts, including invalid, unknown, already-joined, or unauthorized sessions. Checks still in progress also count toward the limit.
    • After 20 attempts per client within a minute, further requests receive a 429 response before authentication is checked. The limit resets after 60 seconds.
  • Documentation
    • Updated the WebSocket join-rate-limit description to reflect the expanded counting rules.

…closes #546)

The exec WebSocket counted only the joins refused at the sign-in. A signed-in browser could ask again and again for a session it cannot join, unknown, joined already or forbidden, and each request still read and touched its sign-in, without ever using the budget.

Every refused join now counts, 404 included, and so does each join still being checked: concurrent joins cannot all pass the check before the first refusal is recorded. Past 20 in a minute, a network gets 429 before any sign-in lookup, until the minute is over. A join that succeeds costs nothing. The joins under way are kept only while they run, next to the existing bounded map of refusals, and both are keyed by the client the trusted proxy forwarded.

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 71814f0 and dbd5d0e.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The WebSocket join limit now counts refused joins and joins undergoing checks. Over-limit requests receive 429 before sign-in lookup. Tests cover refusal cases, expiry, valid joins, and concurrent attempts.

Changes: Exec WebSocket join admission

Layer / File(s) Summary
Admission, refusal handling, and validation
apps/api/src/modules/deployments/interface/exec-socket.ts, apps/api/src/modules/deployments/interface/exec-socket.spec.ts, docs/SPEC.md
The upgrade path reserves a slot before checking a join and releases it after the check. Authentication, unknown-session, unjoinable-session, and access-denied refusals count toward the per-network limit. Tests cover the refusal cases, limit expiry, valid joins, and concurrent attempts. The specification describes the updated limit.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Upgrade as Exec WebSocket upgrade path
  participant Admission as Per-network admission
  participant SignIn as Sign-in lookup
  participant JoinCheck as Session and access checks
  Client->>Upgrade: Request a WebSocket join
  Upgrade->>Admission: Reserve a slot
  Admission-->>Upgrade: Allow or deny admission
  Upgrade->>SignIn: Read sign-in after admission
  Upgrade->>JoinCheck: Check session and access
  JoinCheck-->>Upgrade: Return join result
  Upgrade->>Admission: Release the in-progress slot
Loading

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · previous review 71814

This change makes refused and in-progress WebSocket joins count against the per-client budget, so over-limit requests get 429 before sign-in lookup. No merge-blocking risk remains. The change is tested and easy to revert.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: refused exec WebSocket joins now count against the network's per-network join budget, which matches the summarized changes to exec-socket.ts, its spec, and SPEC.md. Th…
Linked Issues check Passed Directly linked issue #546 requires an effective admission budget for exec WebSocket upgrade refusals, including refusals after sign-in. The reviewed head implements this in exec-socket.ts. (1) `upgra…
Out of Scope Changes check Passed All changed files relate to #546. exec-socket.ts holds the budget logic: admission, release, refusal counting and the bounded state. exec-socket.spec.ts holds tests for those behaviors, including a si…
Full details: Title check

Explanation

The title names the main change: refused exec WebSocket joins now count against the network's per-network join budget, which matches the summarized changes to exec-socket.ts, its spec, and SPEC.md. The 'fix(api):' prefix and the issue reference are conventional and do not reduce relevance. The title is slightly long at 83 characters but remains specific and readable.

Full details: Linked Issues check

Explanation

Directly linked issue #546 requires an effective admission budget for exec WebSocket upgrade refusals, including refusals after sign-in. The reviewed head implements this in exec-socket.ts. (1) upgrade calls admit(ip) before join. admit counts recent refusals plus joins under way, and answers 429 before any sign-in lookup when the sum reaches FAILED_JOINS_PER_MINUTE (20). (2) join calls failed(ip) on every refusal: 401 from the origin, cookie or sign-in check, and 404 for unknown, already-joined, unauthorized (container.exec check) or overtaken sessions. This covers the post-authentication refusals the issue names. (3) release(ip) runs in a finally, so the in-progress reservation ends on success, refusal or error. (4) The budget window is 60 seconds, so use resumes after expiry. Successful joins do not call failed, so normal joins and reconnects are not charged. (5) failures is still pruned past 10,000 entries, and checking holds only joins under way. Both use upgradeClientIp with the existing TRUST_PROXY policy. Acceptance tests are described in the PR and raw summary (exec-socket.spec.ts: repeated unknown-ID refusals, 61-second expiry, concurrent joins, unauthorized and already-joined refusals, retained failed-sign-in and trusted-proxy tests). I did not read the spec file itself; this verdict relies on the implementation and the summarized test coverage. The objective is met by the code at head.

Full details: Out of Scope Changes check

Explanation

All changed files relate to #546. exec-socket.ts holds the budget logic: admission, release, refusal counting and the bounded state. exec-socket.spec.ts holds tests for those behaviors, including a sign-in lookup counter. docs/SPEC.md (SPEC 3.1) documents the new 429 rule, a one-line change. No unrelated behavior change appears in the reviewed code. Unchanged semantics: the uniform 404 bodies, the trusted-proxy keying and the failed-sign-in limit. The ws upgrade and join flow is otherwise untouched.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 213932f and 71814f0.

Files selected for processing: 3

  • apps/api/src/modules/deployments/interface/exec-socket.spec.ts
  • apps/api/src/modules/deployments/interface/exec-socket.ts
  • docs/SPEC.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@samuelmbabhazi
samuelmbabhazi merged commit 7ad3e6b into main Nov 29, 2026
29 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the fix/546-exec-upgrade-budget branch November 29, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apply an admission quota to authenticated exec WebSocket upgrade failures

1 participant