Skip to content

feat(credentials)!: Encrypt stored credentials at rest - #3109

Merged
dcramer merged 9 commits into
mainfrom
feat/encrypt-stored-credentials
Nov 11, 2026
Merged

dcramer merged 9 commits into
mainfrom
feat/encrypt-stored-credentials

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Until now, provider credentials in Redis were stored as plaintext JSON. This change encrypts them at rest with a dedicated keyring that supports rotation.

Keys (chat/encryption/keyring.ts)

  • JUNIOR_ENCRYPTION_KEYS: comma-separated <id>:<key> entries. Each key is 32 random bytes in base64 or base64url.
  • JUNIOR_ENCRYPTION_KEY_ID: the key used for new writes. Optional when there is only one key.
  • The keyring is parsed with the rest of the chat config, so startup fails if a key is missing or the wrong length, an id repeats, or the active id isn't in the list. Error messages never include key material.
  • These keys are separate from JUNIOR_SECRET, so signing and encryption rotate independently.
  • There is no default key, even for local development, so the app and nitro build both fail without one. The error includes the command that generates a key. Tests, dashboard e2e, and the CI example build use fixed test-only keys.

Encryption (chat/encryption/secret.ts, state.ts)

  • AES-256-GCM with a random IV per value. The envelope is jenc:v1:<keyId>:<iv>.<ciphertext>.<tag>.
  • The AAD binds the key id and the storage key. The storage key already contains the user and provider, so an envelope copied to a different key fails to decrypt.
  • There is no extra per-value salt: the keys are random rather than passwords, and the IV already makes every value unique.
  • createEncryptedState(stateAdapter) wraps set/get. readEncryptedState also reports stale, meaning the value is plaintext from before this change or was encrypted with a key that is no longer active.

Records now encrypted: user OAuth tokens, MCP OAuth credentials, MCP OAuth sign-in attempts (which hold the PKCE verifier), MCP service-account credentials and their pending sign-ins, and sandbox egress leases.

Rotation (documented in config-and-env)

  1. Add the new key and deploy, keeping the old id active.
  2. Switch the active id and deploy.
  3. Keep the old key for at least 30 days.
  4. Remove the old key.

The two deploys mean every instance can decrypt the new key before anything writes with it.

What happens to stale values

  • User tokens and service-account credentials are saved again with the active key when read. That only happens if the refresh or update lock is free and the record is re-read under the lock, so it can't overwrite a rotated refresh token.
  • MCP user credentials (30-day TTL) move over on their next save or expire.
  • A stale egress lease counts as a cache miss.
  • A value that can't be decrypted reads as missing and logs encryption.secret_unreadable, so the user gets the normal connect prompt. That covers a key id that isn't configured and a malformed or altered envelope.
  • MCP OAuth sign-in attempts saved before this change can still be read. Their next write encrypts them.
  • The plaintext read path is temporary (TODO(dcramer)).

Deploy note: JUNIOR_ENCRYPTION_KEYS is required. It must be set in every environment before this ships, or the app will not start.

Follow-ups:

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 11, 2026 12:31am UTC

Request Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

214 screenshot changes — 0 changed · 214 added · 0 removed

Review screenshots in Frameshift

@blacksmith-sh

This comment has been minimized.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/junior/tests/fixtures/env.ts Resolved

@dcramer dcramer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if no encryption key is present it should hard refuse to build or something? and we should set a default in test envs

whatever way we make it safe right now, we wanna enforce generating this

@sentry-junior

sentry-junior Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Yes, it now refuses to build without a key. Changed in 198d10c.

  • Build and start: config loads during nitro build, and it throws when JUNIOR_ENCRYPTION_KEYS is missing or invalid. The error includes the command that generates a key. I checked this with apps/example and no key set: the build exits with code 1.
  • Local dev: I removed the fixed dev key. The development defaults also applied when NODE_ENV was unset, so a deployment that never set NODE_ENV could have run quietly on a key that is public in this repo. Local setups now have to generate their own key, the same as production.
  • Tests: the shared test env loader always sets a test keyring. The dashboard e2e harness and the CI example build get fixed test-only keys.

dcramer added a commit that referenced this pull request Nov 11, 2026
Review summaries got no 👀. David's review bodies on #3111, #3109, and
#3091 have no reaction, but his conversation comment on #3108 does.

**Root cause:** `pullRequestFeedbackTarget` marked only
`pull_request.comment.created` and
`pull_request.review_comment.created`. Review events set no `commentId`
or `commentKind`. GitHub also has no REST reactions endpoint for a
review body.

**Fix:** every comment on a pull request now gets the same behavior:
conversation comments, inline review comments, and review bodies.
- A submitted review with a non-blank body sets `commentId` (the review
id) and `commentKind: "review_body"` on its event data.
- The webhook marks the first pull request event that has a feedback
target. An approval with a body is also sent as `review.commented`, and
it gets one reaction. For `review_body`, the webhook reads the review's
`node_id` and sends GraphQL `addReaction` (`EYES`).
- All comment events share one feedback guidance: call
`github_updatePullRequestFeedback` with `repo`, `pull_request`,
`comment_id`, and `comment_kind` from Verified details.
- `github_updatePullRequestFeedback` requires `pull_request` for every
kind. It checks that the bot authored that pull request and that the
comment is on it, then replaces Junior's state reaction. Review bodies
use GraphQL `reactionGroups.viewerHasReacted`, `removeReaction`, and
`addReaction`. `reaction_id` is now optional, because GraphQL gives no
id for a reaction that already exists.
- The egress policy allows `AddReviewReaction` and
`RemoveReviewReaction` only for the host operation
`github.pull.review-reaction.update:<repo>`.

**Remaining risk:** the GraphQL calls are tested only against fakes.
`viewerHasReacted` depends on the installation credential that the
egress read grant picks today. The webhook adds one REST read for each
review with a body.

<!-- junior-request-attribution:start -->
via @dcramer.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A0f0c2da6c27982200b4d351cdb09043e -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A0f0c2da6c27982200b4d351cdb09043e)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A0f0c2da6c27982200b4d351cdb09043e/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: David Cramer <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3d886b1. Configure here.

@dcramer dcramer changed the title feat(credentials): Encrypt stored credentials at rest feat(credentials)!: Encrypt stored credentials at rest Nov 11, 2026
@dcramer
dcramer merged commit fa94165 into main Nov 11, 2026
43 checks passed
@dcramer
dcramer deleted the feat/encrypt-stored-credentials branch November 11, 2026 00:54
dcramer added a commit that referenced this pull request Nov 11, 2026
Moves every read of Junior's signing secrets into
`packages/junior/src/chat/secrets.ts`, and adds an ast-grep rule
(`no-secret-env-reads-outside-secrets-module`) that rejects reads
anywhere else in `packages/junior/src`. To find out what code can touch
keys, you now only have to read one file. That backs the blast-radius
rule in #3137.

The rule covers `JUNIOR_SECRET`, `BETTER_AUTH_SECRET`,
`JUNIOR_SCHEDULER_SECRET`, `CRON_SECRET`, and
`JUNIOR_WEB_PUSH_PRIVATE_KEY`. Each caller keeps its own error message
and its own behavior when a secret is missing. The sign-in fallback
still uses `??` on the raw values.

Out of scope:
- `JUNIOR_ENCRYPTION_KEYS` / `JUNIOR_ENCRYPTION_KEY_ID` aren't on main
yet. Whichever of this PR and #3109 merges second should add them to the
module and the rule.
- Plugin webhook secrets (`LINEAR_`, `SENTRY_`, `VERCEL_WEBHOOK_SECRET`)
and `GOOGLE_CLIENT_SECRET` belong to plugins or provider OAuth config.
- Test configs and `cli/env.ts` set these variables for local runs. The
rule only checks reads of `process.env` in non-test source.

<!-- junior-request-attribution:start -->
via @dcramer.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A0972f6eada08f9c48a19ff20324ffbe0 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A0972f6eada08f9c48a19ff20324ffbe0)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A0972f6eada08f9c48a19ff20324ffbe0/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: David Cramer <[email protected]>
dcramer pushed a commit that referenced this pull request Nov 11, 2026
#3141 and #3109 both merged. #3141 confines secret env reads to
`chat/secrets.ts`. #3109 adds `JUNIOR_ENCRYPTION_KEYS` and
`JUNIOR_ENCRYPTION_KEY_ID`, which the rule does not cover yet.

The rule now covers both keys. No code reads them from `process.env`
directly. `chat/config.ts` passes the startup env to
`readEncryptionKeyring` once, and `chat/encryption/keyring.ts` parses
them. The comment in `secrets.ts` names that path, so it stays the one
documented exception and does not drift into a second place that reads
keys.

Moving the keyring read into `secrets.ts` would change the env-injection
contract of `readChatConfig`, which tests depend on. This PR does not do
that.

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0B595QDZLL%3A1794357294.209209 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0B595QDZLL%3A1794357294.209209)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0B595QDZLL%3A1794357294.209209/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant