Repository navigation
fix(analytics): keep the shared config out of the working directory and off links - #587
Conversation
eddy-heygen
left a comment
There was a problem hiding this comment.
(Eddy's agent) Reviewed by Eddy's Claude agent at commit 13e81c8.
Verdict: approved, good to ship.
Summary. With HOME (or USERPROFILE on Windows) unset or relative, the CLI read and wrote its shared analytics identity file .hyperframes/config.json under the current folder, adopting any planted anonymousId and writing through a symlink planted there. The PR adds paths.HomeDir() (absolute home or an error) and uses it for both the shared config path and paths.ConfigDir(), so with no absolute home the shared config is neither read nor written. It also adds paths.ReplaceFile() (temp file in the same directory, then rename) and uses it for the shared config, the credentials file, the update-check cache and the telemetry-notice marker, so a link at the final path is replaced instead of followed. Both attack paths in SECOPS-1932 are closed, with regression tests that fail on the old code; I found no new weakness and no production-breaking bug.
Approved. I read the path helper and the credentials change myself. paths.HomeDir returns a home folder only when it is absolute (internal/paths/paths.go:32-38); with none, the shared analytics config is neither read nor written, so nothing lands in the working folder. Writes go through paths.ReplaceFile: a temp file beside the target, mode 0600, renamed over it, so a link at the config path is replaced and not written through.
internal/auth/credentials_file.go now uses the same helper for the credentials file. I compared it line by line with the code it replaces: parent directory 0700, temp file in the same directory, chmod 0600, rename. The behaviour is the same; only the per-step error wording is lost. All 12 checks pass.
Blockers
None.
Non-blockers
cmd/heygen/main.go:152: No regression test for the telemetry-notice marker or for paths.ReplaceFile / paths.HomeDir directly. The third commit changes the telemetry marker from os.WriteFile to paths.ReplaceFile to stop writing through a link, but the only new tests are in internal/analytics. Reverting main.go:152 to os.WriteFile, or breaking ReplaceFile's mode handling, would only be caught indirectly through the analytics symlink test and the existing credentials tests. To resolve: Add a small internal/paths test for ReplaceFile (link at target is replaced, resulting mode 0600, parent created 0700) and for HomeDir with relative and empty home. Not needed for the SECOPS-1932 criteria.internal/paths/paths.go:40: ReplaceFile comment overstates: only a link at the final path component is replaced, a linked parent directory is still followed. The comment says a link at path is 'replaced, never written through'. os.MkdirAll (line 44) and os.CreateTemp (line 47) follow a symlinked parent, so if ~/.hyperframes or ~/.heygen is itself a link, the temp file and final file land in the link's target directory. With an absolute home that directory is the user's own, so this is outside the finding's attack path (the attacker controlled the working directory, which is no longer used), but the wording could mislead a later caller. To resolve: Reword the comment to say it covers the final path component only. No code change required for this finding.internal/auth/credentials_file.go:88: Credentials write errors lost their per-step wording. Five distinct messages (create config directory, create temp, write temp, chmod, finalize) are now one 'failed to write credentials file: '. The wrapped OS error still names the operation and path, and no code or test matches on the old strings (git grep found none), so nothing breaks; it is only slightly less specific for a user with a read-only home. To resolve: Optional: leave as is, or have ReplaceFile wrap each step. Does not affect security.
Fix criteria from the finding
- SECOPS-1932: (1a) The shared analytics config path is never derived from the working directory: met (internal/analytics/analytics.go:291-296 sharedConfigPath takes home from paths.HomeDir(); internal/paths/paths.go:32-37 returns ErrNoHome unless os.UserHomeDir succeeds and filepath.IsAbs(home). It is the only os.UserHomeDir call left in non-test code (git grep).)
- SECOPS-1932: (1b) With home unset or not absolute the shared config is treated as absent: no read, no directory creation, no write: met (internal/analytics/analytics.go:302-305 readSharedConfig returns an empty map when the path is empty, and :324-327 writeSharedConfig returns before any MkdirAll/write; the MkdirAll now lives only inside paths.ReplaceFile (paths.go:44), which is not reached.)
- SECOPS-1932: (1c) A test mirroring TestNoConfigDirLeavesTheWorkingDirectoryAlone shows the constructor, with HOME and USERPROFILE relative, does not read a pre-seeded .hyperframes/config.json and creates no entry: met (internal/analytics/shared_config_test.go:37-68 runs newWithCapture with home "", "." and "relative/home", a planted config under cwd, and compares a full directory listing with contents before and after; the "." and relative cases would adopt the planted id on the old code. Read, not run; CI test jobs pass on all three OSes.)
- SECOPS-1932: (2a) The write replaces the directory entry (temp file plus rename in the same directory) rather than writing through it; a link at the config path is never followed for writing: met (internal/analytics/analytics.go:333 calls paths.ReplaceFile; internal/paths/paths.go:42-62 uses os.CreateTemp in filepath.Dir(path) (O_EXCL, mode 0600), writes, closes, chmods 0600 and os.Rename over path, which replaces a symlink at the final path component.)
- SECOPS-1932: (2b) When the config path is a link to another file, that file's content is unchanged after the CLI starts: met (internal/analytics/shared_config_test.go:70-98 links config.json to a victim file, calls distinctID(), asserts the victim is unchanged and the config path is now a regular file holding the id; it would fail on the old os.WriteFile. Skips only where symlinks cannot be created.)
Open outside this PR
- internal/config/file_provider.go:73 (untouched) still writes config.toml with os.OpenFile(O_CREATE|O_WRONLY|O_TRUNC), which truncates through a symlink at that path; the PR body's claim that the telemetry marker was 'the one other plain os.WriteFile' is literally true but this is the same write-through pattern. It sits under ConfigDir (absolute home or user-set HEYGEN_CONFIG_DIR), so it is not the SECOPS-1932 path; worth a follow-up to use paths.ReplaceFile.
- cmd/heygen/update_notice.go:298 (untouched) opens update_check.json.lock with O_CREATE|O_RDWR, following a link (creates/opens but does not truncate). Same directory as above.
- internal/paths/paths.go:16-18 (untouched logic): HEYGEN_CONFIG_DIR is returned as given, including a relative value, in which case credentials, update cache and marker resolve under the working directory. This is an explicit user-set override, not a default, and not part of the finding; owner to decide whether to require it be absolute.
- A HOME that is absolute but points at a world-writable or attacker-controlled folder is still trusted (reads follow links there, a planted anonymousId would be adopted). Out of scope for this finding: the attacker would have to control the victim's environment or home.
- A symlinked parent directory (~/.hyperframes or ~/.heygen being a link) is followed by MkdirAll/CreateTemp; under the user's own absolute home this is by the user's choice.
- With no absolute home and no legacy id, analytics mints a fresh anonymous id on every run (nothing is persisted). This is what the fix criteria ask for; analytics owners may want to know about the effect on unique-install counts.
- Shared-config behaviour change for the hyperframes CLI and media-use, which share ~/.hyperframes/config.json: the file is now replaced (new inode, mode 0600; a deliberate dotfiles symlink becomes a regular file). Intended by the finding; not verified against those tools' code, which is outside this repository.
- Claude Security finding find_015fgkbpTEEaQWtpPjhGWFao is still 'open'; resolving it after merge is the owner's step.
CI: pass - all 12 checks green (test on ubuntu/macos/windows, lint, govulncheck, secrets, goreleaser-check, workflow-lint, release-scripts, pr-template, surface-report, WIP). None failing or pending. Mergeable.
How this was reviewed: a code-review pass and a security-review pass against the Mythos finding, by a reviewer agent in an isolated checkout of this commit; I then read the core of the fix and the cited lines myself. Nothing was built or run.
What changed
The CLI's shared analytics config (
~/.hyperframes/config.json) is now found only under an absolute home directory, and it is written by replacing the file rather than writing through it.HOME(orUSERPROFILE) unset or relative, such as., the CLI used to read and write.hyperframes/config.jsonunder the current folder. It took anyanonymousIdit found there as this install's analytics id, and created the file when it was missing. It now treats the shared config as absent: no read, no folder created, no write.paths.ConfigDir()already refused a relative home. Both now go through onepaths.HomeDir(), so the rule has one owner.writeSharedConfigusedos.WriteFile, which truncates the existing file and follows a symbolic link to its target. It now writes a temp file beside the config and renames it over the path, so a link there is replaced and its target is left alone.paths.ReplaceFilewith the shared config, and so does the telemetry-notice marker, the one other plainos.WriteFilein the CLI. Behaviour is unchanged for those (mode 0600, parent 0700), apart from the credentials error text.Testing
internal/analytics/shared_config_test.go:TestNoAbsoluteHomeLeavesTheWorkingDirectoryAloneruns the client constructor with home"",.andrelative/home, each with a pre-seeded.hyperframes/config.jsonin the working folder. It mirrors the existingTestNoConfigDirLeavesTheWorkingDirectoryAlone.TestSharedConfigWriteReplacesALinkInsteadOfWritingThroughItchecks that the link's target keeps its content and the config path becomes a regular file holding the id.., and the second rewrites the link's target with the new id. Both pass with this change, 3 runs in a row.go test ./...andgo veton./internal/...and./cmd/...pass (Go 1.26, Linux).gofmt -lis clean on the changed packages.Not covered