Skip to content

feat(backend): encrypt stored GitHub access tokens - #536

Draft
marcalexiei wants to merge 3 commits into
stats-organization:masterfrom
marcalexiei:crypto
Draft

marcalexiei wants to merge 3 commits into
stats-organization:masterfrom
marcalexiei:crypto

Conversation

@marcalexiei

@marcalexiei marcalexiei commented Aug 28, 2026

Copy link
Copy Markdown

Warning

Merge this after #537


  • authenticated_users.access_token is AES-256-GCM encrypted when TOKEN_ENCRYPTION_KEY is set (AccessTokenCipher), with user_id as AAD;
    without a key it stays plaintext and a warning is logged on boot.
  • Reads accept legacy plaintext. An unreadable token comes back as token: null:
    /api/user-access and /api/downgrade drop the row so the user logs in again, cards fall back to the public PAT.
    A bad key fails on the token paths only, not on import.
  • TOKEN_ENCRYPTION_KEY=<new>,<old> rotates: first key encrypts, all decrypt.
  • pnpm --filter ./apps/backend run encrypt-access-tokens re-encrypts the rows not yet on the first key, with compare-and-swap, and exits non-zero on unreadable ones or a missing key.

@vercel

vercel Bot commented Aug 28, 2026

Copy link
Copy Markdown

@marcalexiei is attempting to deploy a commit to the martin-mfg's projects Team on Vercel.

A member of the Team first needs to authorize it.

@marcalexiei
marcalexiei marked this pull request as draft August 28, 2026 12:48
Comment thread apps/backend/src/common/tokenEncryption.js
martin-mfg added a commit that referenced this pull request Sep 13, 2026
…d router (#537)

The wizard ran the whole `apps/backend` router in the browser. 
#536 added `node:crypto` to `src/common/database.js`, Vite's browser
stub for a Node builtin throws on the first named-export causing e2e
tests timeouts.
`pg` already needed a `rolldownOptions.external` escape hatch for the
same reason.

- `src/wizard/renderCard.ts` maps a card URL's pathname to the matching
`packages/core` handler and `SvgInline` calls it.
`mock-http.ts` and the the `pg` external are removed alongside backend
dependency.
  Resolves the `// will be solved by npm package`

https://github.com/stats-organization/github-stats-extended/blob/2e8537db4eb6ce932b8d072f764960e436f612af/apps/frontend/src/wizard/components/Card/SvgInline.tsx#L76-L78
- ~~`e2e/stubCardApi.ts` fulfils the card endpoints, so the `Display`
stage's fetch of `https://<host>/api…` no longer logs network errors
during e2e.~~ If `process.env.STUB_CARD_API` is set, vite will now serve
a stub card. This avoids network error logs during e2e tests without the
backend server running.

---------

Co-authored-by: martin-mfg <2026226+martin-mfg@users.noreply.github.com>
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.

Access tokens stored in plaintext in Postgres

1 participant