Skip to content

fix(cli): write the account credential with private file modes - #1834

Open
ethicnology-agent wants to merge 1 commit into
slopus:mainfrom
ethicnology-agent:fix/cli-credential-file-modes
Open

ethicnology-agent wants to merge 1 commit into
slopus:mainfrom
ethicnology-agent:fix/cli-credential-file-modes

Conversation

@ethicnology-agent

Copy link
Copy Markdown

Fixes the third defect reported in #1830.

Problem

~/.happy and ~/.happy/access.key are created with no explicit mode. Under the common umask 022 that yields directory 0755 and file 0644, measured on macOS. The file holds the account bearer token and, in the dataKey variant, the machine RPC key — so on any host with more than one local account, or on a shared build machine, another account can read it and use it.

Three writers of the same class of material in this repository already do this correctly, notably packages/happy-agent/src/credentials.ts, which uses 0700/0600.

What this changes

  • packages/happy-cli/src/persistence.ts — both credential writers pass mode on mkdir/writeFile and then chmod unconditionally.
  • packages/happy-cli/src/configuration.ts — ~/.happy is created 0700 at startup.
  • packages/happy-cli/src/persistence.test.ts — two cases, one per credential variant.

The unconditional chmod is the part that matters for existing users: writeFile's mode option only applies when it creates the file, so without it every installation already on disk would keep 0644 forever. The test covers exactly that — it loosens the modes to 0755/0644, writes again, and asserts they come back to 0700/0600.

Verified red/green on the audited tree: without the source change the two new cases fail with expected 493 to be 448 (0o755 vs 0o700); with it, src/persistence.test.ts passes 26/26.

What this deliberately does not change

  • ~/.happy/sessions.json also holds per-session encryption keys and is written with no mode. Same class of problem, but it is a different writer and a different call path, so it belongs in its own change rather than widening this diff. Happy to follow up.
  • ~/.happy/logs/ and daemon.state.json are untouched.
  • Windows is unaffected in practice, since POSIX modes are not enforced there; the calls are harmless.

Alternative considered

Setting process.umask() at startup would fix every writer at once, but it silently changes the mode of unrelated files the CLI creates and is invisible at the call site. Explicit modes at the two places that write the credential are easier to review and to keep correct.

~/.happy and ~/.happy/access.key were created with no explicit mode, so
under the common umask 022 they landed as 0755 and 0644. The file holds the
account bearer token and, in the dataKey variant, the machine RPC key, so on
a multi-user host any other local account could read it.

Three other writers of the same class of material in this repository already
set 0700/0600, including packages/happy-agent/src/credentials.ts.

Pass the mode at creation and chmod unconditionally afterwards: writeFile's
mode option only applies when it creates the file, so existing installations
would otherwise keep their current permissions forever.
@ethicnology-agent
ethicnology-agent force-pushed the fix/cli-credential-file-modes branch from 3eb071c to 70eaae6 Compare October 1, 2026 19:02
@ethicnology-agent

Copy link
Copy Markdown
Author

Rebased onto current main (4cf54d1). Clean rebase, diff unchanged.

This branch has not been deployed

No deployments
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.

2 participants