Skip to content

fix(csrf): merge CSRF token into effectiveInit for Request inputs (bot-fix for #1991)#1999

Open
hognek wants to merge 1 commit into
jaylfc:devfrom
hognek:fix/csrf-fetch-wrapper
Open

fix(csrf): merge CSRF token into effectiveInit for Request inputs (bot-fix for #1991)#1999
hognek wants to merge 1 commit into
jaylfc:devfrom
hognek:fix/csrf-fetch-wrapper

Conversation

@hognek

@hognek hognek commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the 2 inline bot findings on PR #1991 (Kilo + CodeRabbit) in auth-guard.ts.

Changes

auth-guard.ts — Request-object CSRF handling:

  • Compute effective method from init?.method || input.method (was: input.method only)
  • Merge CSRF token into effectiveInit headers instead of rebuilding the Request with new Request(input, { headers })
  • This fixes 3 issues:
    1. Kilo finding: Request inputs were previously excluded from CSRF when init was provided (init.headers would override the rebuilt request's headers)
    2. CodeRabbit fix: security hardening — bind, validation, atomic writes, deploy rollback #1: init.method overrides were ignored — a GET Request with init: { method: 'POST' } would skip CSRF
    3. CodeRabbit Fresh install test — clean Orange Pi 5 Plus #2: new Request(input, ...) consumes the original body stream

auth-guard.test.ts — Updated Request-object assertion:

  • Now checks spy.mock.calls[1] (init arg) instead of spy.mock.calls[0] (Request arg) since the token lives in effectiveInit

Tests

  • tsc: clean
  • auth-guard.test.ts: 1/1 pass
  • csrf.test.ts: 17/17 pass

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@hognek, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 6 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c034c922-1315-414b-9f4d-ab460d276cad

📥 Commits

Reviewing files that changed from the base of the PR and between c801a22 and 3a4505f.

📒 Files selected for processing (2)
  • desktop/src/lib/auth-guard.test.ts
  • desktop/src/lib/auth-guard.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@hognek
hognek marked this pull request as ready for review July 18, 2026 12:32
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@gitar-bot

gitar-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

Gitar is working

Gitar

@kilo-code-bot

kilo-code-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
desktop/src/lib/auth-guard.ts 80 Headers on the input Request object are dropped when init.headers is also provided; merge both instead of choosing one
Files Reviewed (2 files)
  • desktop/src/lib/auth-guard.ts - 1 issue
  • desktop/src/lib/auth-guard.test.ts - 0 issues

Fix these issues in Kilo Cloud

@jaylfc
jaylfc changed the base branch from fix/csrf-global-fetch to dev July 18, 2026 18:16
…t rebuild

- Compute effective method from init?.method || input.method
- Merge token into effectiveInit headers instead of rebuilding Request
  (avoids body stream consumption and respects init-provided headers/method)
- Update test to inspect init.headers instead of Request.headers

Addresses Kilo finding (Request inputs excluded from CSRF) and
CodeRabbit finding (broken token injection when init overrides
method/headers + body stream consumption).

Refs: PR jaylfc#1991
@hognek
hognek force-pushed the fix/csrf-fetch-wrapper branch from d852c5c to 3a4505f Compare July 20, 2026 00:57
const headers = new Headers(input.headers);
headers.set("X-CSRF-Token", token);
effectiveInput = new Request(input, { headers });
const baseHeaders = init?.headers || input.headers;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Headers set on the input Request object are silently dropped when init.headers is also provided.

baseHeaders is init?.headers || input.headers, so if the caller passes headers via init (e.g. fetch(new Request(url, { headers: { Authorization } }), { method: 'POST', headers: { ... } })), the Request object's own headers are discarded entirely. The previous new Request(input, { headers }) approach preserved input's headers. Consider merging both sources, e.g. new Headers(input.headers) then overlay init?.headers, before deciding whether to set the token.

Suggested change
const baseHeaders = init?.headers || input.headers;
const baseHeaders = new Headers(input.headers);
if (init?.headers) new Headers(init.headers).forEach((v, k) => baseHeaders.set(k, v));

Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@hognek

hognek commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/dev (a3ef7f7). 3 upstream commits auto-dropped (already in dev). Hogne's fix-for-Request-inputs commit re-applied cleanly. Desktop build + vitest pass.

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