Repository navigation
Fix dataset list --mine auth header - #462
Conversation
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR462Version: 🤖 Updated on each push. |
PR #462 Code ReviewSummary: The fix correctly distinguishes "no header" from "bad token" at the backend, addresses the root cause, and the new error message propagates correctly to the CLI user via CriticalNone. Important1. 2. 3. Malformed Suggestions4. 5. 6. Test for invalid bearer should also verify the response does NOT contain the generic "Authentication required" string Nice-to-haves7. 8. Test comment says "32+ chars so authMiddleware does the DB lookup" — should say False positives / intentional design choices
Verdict: Mergeable after addressing items 1 (type accuracy) and 2 (consider a follow-up issue for the |
- Mark authAttempted as optional in Variables type (was a lie) - Extend stale-token 401 hint to GET /datasets/:id - Note lowercase bearer fallthrough in middleware comment - Fix test comment: authMiddleware -> optionalAuthMiddleware - Add negative assertion and test for /:id stale-token path
Review findings addressed (commit 2f445c3)Important #1 — Important #2 — Same bug class on Important #3 — Lowercase bearer comment Suggestion #6 — Negative assertion on the Suggestion #8 — Test comment said Suggestions #4, #5, #7 deferred as polish-only per the review; no behavior change needed. |
Closes #447.
What changed
nemar dataset list --minefailed withAuthentication required to view your datasetsfor users who were logged in (isAuthenticated()returned true). The CLI sent theAuthorization: Bearer <key>header, but the backend'soptionalAuthMiddlewaresilently dropped it whenever the key didn't resolve to an approved user (revoked, expired, rotated, or matching a different env's DB). The route then sawuser=nulland bounced the caller with the generic "Authentication required" reply — indistinguishable from "no header sent" so the user has no hint to re-login.Fix:
optionalAuthMiddlewarenow setsc.set("authAttempted", true)whenever the caller provided a Bearer header (regardless of whether the lookup succeeded). The flag distinguishes "no header" from "header sent but unresolvable".GET /datasets?mine=truereadsauthAttemptedwhenuseris null and emits a token-specific 401 directing the user tonemar auth login(orregenerate-keyif the original key was revoked). The "no header" branch keeps the original message for callers that never logged in.Variables.authAttempted: booleanfield to the Hono context typing.Why this is the root cause, not a band-aid
The CLI's
isAuthenticated()is presence-only — it can only verify a key was stored, not that the backend still accepts it. We could try to revalidate the key on every command, but that doubles requests for every authenticated call. The cheaper fix is to make the backend's reply unambiguous: presence of an unresolvable Bearer token is a known signal that the caller's session is stale.Test plan
test/api.test.tsunderGET /datasets:bun run typecheckpasses for backendbun run buildfor CLI succeeds