Skip to content

Harden JWT storage/rendering against XSS and fix silent logout on network blips #4

Closed

Description

@chonilius

Problem

src/lib/auth.ts stores the session JWT in window.localStorage under mergefi_token, read by apiRequest in src/lib/api.ts and attached as a bearer token to every authenticated call (claim bounty, fund escrow, deposit to maintenance pool). This is a classic XSS-to-account-takeover vector: any script injection anywhere in the app (a maintainer-controlled issue body is rendered from GitHub-sourced content via raw.issue?.body in adaptBounty — check how/where description is rendered on /issues/[id]) could exfiltrate the token and impersonate the user for wallet-adjacent financial actions. There is no CSP configured in next.config.ts, no token expiry/refresh flow visible in AuthContext.tsx (refresh() just clears the token on any /auth/me failure, with no distinction between "expired, try refresh" and "invalid, log out"), and no rotation on privilege-sensitive actions like wallet connection.

Why this is hard

  • You need to actually verify whether GitHub issue bodies are rendered as raw HTML/markdown anywhere in the issue detail page (src/app/issues/[id]/page.tsx) or if they're currently escaped — this determines whether the XSS vector is theoretical or live, and the fix differs materially in each case.
  • Migrating off localStorage to httpOnly cookies requires backend cooperation (the mergefi-backend repo issues the JWT) — scope what's achievable frontend-only (CSP hardening, token scoping, short-lived tokens with silent refresh) versus what must be filed as a cross-repo dependency, and design the frontend to work with either.
  • Next.js 16 App Router CSP setup via next.config.ts headers needs to account for Turbopack dev mode's inline scripts/HMR websocket, Freighter's content-script injection model, and any inline styles Tailwind v4's JIT might need — getting this wrong breaks dev or breaks the wallet extension bridge.
  • AuthContext.refresh() currently has no retry/backoff and clears the token on ANY failure from /auth/me, including transient network errors — meaning a flaky connection silently logs the user out. Distinguishing "token invalid" (401) from "network blip" (fetch failure) requires inspecting apiRequest's ApiRequestError vs a generic throw, which the current code doesn't do.

Scope

  • Audit and fix any unescaped rendering of GitHub-sourced content (issue titles/bodies/labels) across /issues, /issues/[id], /milestones.
  • Add a CSP header in next.config.ts scoped as tightly as possible while preserving Turbopack dev and Freighter compatibility.
  • Fix AuthContext.refresh() to only clear the token on a genuine 401/403, retrying with backoff on network failures.
  • Document (in the PR) the localStorage-to-cookie migration path as a follow-up, including what backend changes it requires.

Acceptance criteria

  • A crafted issue body/title cannot execute script in the rendered page.
  • A CSP header is present and doesn't break npm run dev or Freighter connect/sign flows.
  • Transient network failure during /auth/me no longer logs the user out.

Activity

  1. added
    very hard
    security
    bug
    on Jul 15, 2026
  2. added
    GrantFox OSS
    Maybe Rewarded
    Official Campaign | FWC26
    on Jul 15, 2026
  3. added
    Stellar Wave
    on Aug 25, 2026
  4. Biokes commented on Aug 25, 2026

    @Biokes

    @Biokes has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Hi maintainer, i will like to work on this issue.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Biokes to this issue.

  5. luciachizaram commented on Aug 25, 2026

    @luciachizaram
    Contributor

    @luciachizaram has applied to work on this issue as part of the Stellar Wave Program's 8th wave.

    Please assign this issue to me

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @luciachizaram to this issue.

  6. drips-wave commented on Aug 25, 2026

    @drips-wave

    Congratulations, @luciachizaram! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on August 31, 2026.

    🧑‍💻 @luciachizaram: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  7. added 2 commits that reference this issue on Aug 25, 2026
    d476d2d
    8c25540
  8. grantfox-oss commented on Aug 25, 2026

    @grantfox-oss

    🎉 This issue has been marked as completed on GrantFox as part of the Third Campaign campaign!

    @luciachizaram's PR #333 was approved and merged by @chonilius.

    🏆 @luciachizaram: You earned 35 FoxPoints for this contribution! Your current tier: Explorer (990 total points). Track your full progress on GrantFox.

    👏 Great work, @luciachizaram! Keep contributing to MergeFi.

  9. drips-wave commented on Aug 25, 2026

    @drips-wave

    This issue has been completed by @luciachizaram as part of the Stellar Wave Program's 8th Wave 🥳

    😎 @luciachizaram: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

  10. removed
    Stellar Wave
    on Oct 15, 2026
  11. added
    Stellar Wave
    on Oct 15, 2026
  12. removed
    Stellar Wave
    on Oct 15, 2026
  13. added
    Stellar Wave
    on Oct 15, 2026
  14. removed
    Stellar Wave
    on Oct 15, 2026
  15. added
    Stellar Wave
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GrantFox OSSMaybe RewardedOfficial Campaign | FWC26Stellar Wavebugsecurityvery hard

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions