Skip to content

fix(auth): return JSON 401 for all expired /setup-api/* requests (closes #304) - #316

Merged
KrasimirKralev merged 1 commit into
betafrom
fix/setup-api-expired-auth-401
Aug 8, 2026
Merged

KrasimirKralev merged 1 commit into
betafrom
fix/setup-api-expired-auth-401

Conversation

@KrasimirKralev

Copy link
Copy Markdown
Contributor

Closes #304 β€” the follow-up to #231/#303, which each patched a single caller.

The latent bug

A raw fetch("/setup-api/...") sends Accept: */*. The middleware gate was:

if (accept.includes("application/json") || pathname.startsWith("/api/")) { return 401 }
// else -> HTML redirect to /login

So a fetch without an explicit Accept: application/json header fell through to the login-page redirect, and the caller's .json() then threw on the HTML body when the session had expired. ~30 callers across ~15 files (i18n.tsx, SettingsApp.tsx, mascot-client.ts, useOllamaModels, FilesApp, DoneStep, AIModelsStep, …) carried this.

Fix (the issue's preferred "deepest fix")

Return a JSON 401 for the whole /setup-api/* (and /api/*) prefix regardless of the Accept header. Every caller now gets a structured 401 it can detect β€” no client changes. Top-level page navigations (no API prefix) still redirect to /login.

Deliberate trade-off: the handful of /setup-api/* routes loaded by direct browser embedding (webapps?app= iframes, apps/icon/[appId] <img>, file downloads) now get a 401 instead of a login page. That's fine β€” a login page inside an <img> or a download stream is useless anyway; the desktop shell drives the real re-login.

Tests

  • Updated the middleware suite: /setup-api/* with no Accept header now asserts 401 (was the buggy 307).
  • Added a regression test asserting the 401 body is parseable JSON (the actual failure mode).
  • 47/47 middleware tests passing on a real Jetson.

Generalizes the per-caller fixes from #231/#303. A raw fetch("/setup-api/...")
sends `Accept: */*`, so the middleware's `accept.includes("application/json")`
gate missed it and fell through to the HTML login *redirect* β€” whose body then
made the caller's `.json()` throw on an expired session. ~30 callers across
~15 files carried this latent bug.

Fix at the source: return a JSON 401 for the whole /setup-api/* (and /api/*)
prefix regardless of the Accept header, so every caller gets a structured 401
it can detect β€” no client changes needed. Genuine top-level page navigations
(no API prefix) still redirect to /login.

Deliberate trade-off: the few /setup-api/* routes loaded by direct browser
embedding (webapps?app= iframes, apps/icon/[appId] <img>, file downloads) now
get a 401 too instead of a login page β€” which is fine, since a login page
inside an <img> or a download stream is useless anyway.

Tests: middleware suite updated to assert 401 (not 307) for /setup-api/*,
plus a regression test that the 401 body is parseable JSON. 47/47 passing
on-device.

Closes #304.
@KrasimirKralev
KrasimirKralev requested a review from a team as a code owner August 8, 2026 17:31
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 47 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

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: ASSERTIVE

Plan: Pro Plus

Run ID: 1c430b65-1fa4-4220-87a7-6fe4eb4341c5

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 40f1210 and fa29fed.

πŸ“’ Files selected for processing (2)
  • src/middleware.ts
  • src/tests/middleware/middleware.test.ts

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.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

πŸ¦€ ClawReview

Fresh PR washed in with the tide β€” here's the gist.

This PR fixes a long-standing session-expiry bug in the auth middleware: raw fetch() calls (which send Accept: /) were falling through to an HTML login-redirect instead of a JSON 401, causing .json() to throw on ~30 callers across ~15 files. The fix is a single condition change β€” /setup-api/* and /api/* paths now always return a JSON 401 regardless of Accept header, requiring no client-side updates.

At a glance

  • πŸ”§ Fix Β· touches authentication middleware (src/middleware.ts)
  • Base branch: beta Β· +23 source / +24 tests across 2 files
  • βœ… base beta matches the beta-first convention
  • βœ… conventional PR title
  • βœ… source changes come with test changes
  • ℹ️ touches security-sensitive paths (src/middleware.ts) β€” review with extra care

Good to know

  • 🟑 Touches src/middleware.ts β€” security-sensitive path; the policy check flagged this for extra-careful review.
  • ℹ️ Deliberate trade-off: /setup-api/* routes loaded by direct browser embedding (iframes, icon, file downloads) now receive a 401 instead of a login redirect. PR author argues this is acceptable since those contexts can't usefully display a login page.
  • ℹ️ Tests verified on real Jetson hardware (47/47 passing) β€” good signal given this runs under systemd on customer devices.

β€” ClawReview πŸ¦€. I set the scene; CodeRabbit reviews the code; you decide. Conventions: docs.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

CI Summary

βœ… Tests

  • Result: passed
  • View run
  • Coverage: statements 70.51%, branches 60.89%, functions 66.31%, lines 72.42%

βœ… E2E

βœ… E2E Install

@KrasimirKralev
KrasimirKralev merged commit 2a0585c into beta Aug 8, 2026
8 checks passed
@KrasimirKralev
KrasimirKralev deleted the fix/setup-api-expired-auth-401 branch August 8, 2026 17:46
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