Skip to content

chore(security): move master encryption key out of source tree#239

Open
0xtsotsi wants to merge 2 commits into
CoreBunch:mainfrom
0xtsotsi:chore/move-master-key
Open

chore(security): move master encryption key out of source tree#239
0xtsotsi wants to merge 2 commits into
CoreBunch:mainfrom
0xtsotsi:chore/move-master-key

Conversation

@0xtsotsi

Copy link
Copy Markdown

Why

The dev master encryption key (AES-256-GCM, chmod 0600) used to be auto-generated at ./.tmp/secret.key whenever INSTATIC_SECRET_KEY was unset. Even though the file is gitignored, it lived under the working tree and contained the root secret that protects every reversible secret in the dev DB (AI provider credentials, plugin secrets, TOTP seeds).

This commit moves the dev fallback to a platform-native per-user config directory and adds an opt-in env flag.

Changes

  • New server/secrets/paths.ts with perUserConfigDir() and defaultMasterKeyPath() (Linux: $XDG_CONFIG_HOME/instatic/secret.key, macOS: ~/Library/Application Support/instatic/secret.key, Windows: %APPDATA%\instatic\secret.key).
  • server/secrets/masterKey.ts new priority:
    1. INSTATIC_SECRET_KEY env var (unchanged).
    2. INSTATIC_SECRET_KEY_FILE, when set to an absolute path.
    3. The platform-native per-user key file, but only when INSTATIC_ALLOW_DEV_KEY_AUTOGEN=1.
  • The ./.tmp/secret.key fallback is removed. With the opt-in flag unset, bun run dev fails closed with a clear error message.
  • Production behaviour (INSTATIC_SECRET_KEY required when NODE_ENV=production) is unchanged.
  • scripts/dev.ts fails fast with a clear error when no key source is configured.
  • .env.example and .env.production.example document the new env vars.
  • .gitignore adds a clarifying comment.

Files

  • server/secrets/paths.ts (new)
  • server/secrets/masterKey.ts
  • scripts/dev.ts
  • .env.example
  • .env.production.example
  • .gitignore

Verification

  • bun run lint clean.
  • tsc -b clean.
  • With INSTATIC_ALLOW_DEV_KEY_AUTOGEN=1 unset and INSTATIC_SECRET_KEY="", bun run dev exits with a clear error.
  • With INSTATIC_ALLOW_DEV_KEY_AUTOGEN=1, the dev key file is created at the per-user path (verified at ~/Library/Application Support/instatic/secret.key on macOS) and re-used on later boots.
  • INSTATIC_SECRET_KEY_FILE=/abs/path bun run dev reads from that file.
  • The existing ./.tmp/secret.key has been removed from the working tree.

David Babinec added 2 commits July 22, 2026 22:05
Adds `bun run security:rotate-mcp` to swap the connector in `.env`
for a fresh bearer. Reads the old hash from `ai_mcp_connectors`, runs
a transaction that creates the replacement row, deletes the old one,
and verifies both inside the same transaction. Restores the old
`INSTATIC_MCP_TOKEN` value on rollback only when the database still
proves the rollback happened.

This is the local-dev rotation helper; the admin endpoint that returns
the plaintext once is the production path. The .env file remains
gitignored.
Adds `server/secrets/paths.ts` with platform-native per-user config
directory resolution ($XDG_CONFIG_HOME/instatic on Linux,
~/Library/Application Support/instatic on macOS, %APPDATA%\instatic
on Windows).

`server/secrets/masterKey.ts` now reads keys in this priority:

  1. INSTATIC_SECRET_KEY env var (unchanged).
  2. INSTATIC_SECRET_KEY_FILE, when set to an absolute path.
  3. The platform-native per-user key file, but only when
     INSTATIC_ALLOW_DEV_KEY_AUTOGEN=1.

The .tmp/secret.key fallback is removed. With the opt-in flag unset,
`bun run dev` now fails closed with a clear message instead of writing
a key into the working tree. Production behavior (require
INSTATIC_SECRET_KEY) is unchanged.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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