Skip to content

fix(fs): stop leaking fs credentials through companion lookup, run-with-secret, and the WebDAV bridge - #89

Merged
srstack merged 16 commits into
mainfrom
fix/fs-companion-integrity-and-vault-env
Nov 13, 2026
Merged

srstack merged 16 commits into
mainfrom
fix/fs-companion-integrity-and-vault-env

Conversation

@srstack

@srstack srstack commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Three scanner findings are fixed in this PR; the fourth is a drive9/FS service contract that ti cannot change from the client.

1. Companion resolved by bare name from $PATH with no integrity check (high)

fswrap.Runner.locateCompanion used exec.LookPath("ti-drive9") and ran the result while injecting the TiDB Cloud organization private key, the filesystem bearer token, and delegated vault tokens. Any account able to write an earlier $PATH entry could receive those credentials.

Fix

  • $PATH is not consulted at all. The only implicit candidates are ti's own directory and the installer-managed ~/.ti/bin; TI_ALLOW_STANDALONE_DRIVE9 (the bare drive9 PATH fallback) is removed.
  • Explicit configuration (Runner.CompanionPath, TI_DRIVE9_BIN) is terminal and must be absolute; a missing or insecure explicit path fails with fs.companion_missing / fs.companion_untrusted instead of falling back to another binary.
  • Every candidate must be an absolute, regular, executable file, not world writable, not writable by an unrelated group, owned by the current user or root, inside a directory with the same properties (sticky directories excepted).
  • Symlinks are resolved first and the directory that actually contains the binary is validated too, so a link in a trusted directory cannot point at a binary another account can replace.
  • Group-writable binaries within the caller's own groups stay accepted so a umask of 002 does not break every install.

2. fs-vault run-with-secret leaked the FS bearer token and vault token to the child (high)

ti fs-vault run-with-secret delegated the whole operation to the companion (ti-drive9 vault with <path> -- <cmd>), so the child inherited the companion environment including DRIVE9_API_KEY (filesystem bearer token) and DRIVE9_VAULT_TOKEN; scrubTICredEnv was dead code.

Fix

  • ti reads the secret itself through the companion (vault get <name> --json) and spawns the user command directly from ti.
  • The child environment starts from os.Environ() with TI_* / TDC_* / DRIVE9_* credentials scrubbed, then only the validated secret fields are injected. The filesystem bearer token and the vault token never reach the child.
  • Nonzero child exit codes propagate as vault.command_failed.

3. Native loopback WebDAV bridge with no authentication and its prefix in argv (high)

Service.MountFileSystem routes unconditionally to drive9MountFileSystem, and mountForeground had no callers, so the native bridge was unreachable. Rather than authenticate dead code, it is removed.

Fix

  • internal/fs/webdavfs.go is deleted: the unauthenticated 127.0.0.1 WebDAV server, the in-memory remote filesystem, and the random URL prefix are gone.
  • mountWebDAVForeground, randomWebDAVPrefix, and the WebDAV branch of mountForeground are deleted, so nothing passes a URL/prefix as a process argument.
  • mountdriver keeps only Name() and CheckPrerequisites(); the mount_webdav / umount exec path with the mount URL in argv is deleted, because mounting belongs to the companion.
  • Metadata helpers the dead FUSE code still references were moved to internal/fs/remotefs.go unchanged.

4. Organization private key sent to the drive9.ai service (not a ti-side fix)

Not changed here, with reasons:

  • The FS control plane (tenant create/list/describe/delete, token minting, AI provider configuration) authenticates with the TiDB Cloud public/private key pair in X-TiDBCloud-* headers and digest challenges. That is the deployed service contract; deleting the header breaks every FS control-plane command. ti already uses only the built-in HTTPS endpoint mapping, never logs, persists, or telemetry-records the key, redacts it from errors and debug output, and never retries ambiguous mutations.
  • Removing the organization key from the drive9.ai boundary requires a service/authorization change (delegated per-tenant credentials or an IAM-issued short-lived token the FS service accepts). That is owned by the FS/drive9 service, not the CLI.

Tests

  • internal/fswrap/runner_test.go, locate_test.go: relative TI_DRIVE9_BIN, world-writable, unrelated-group-writable, non-executable, world-writable-parent, and symlink-to-a-writable-directory binaries are rejected with fs.companion_untrusted; a valid absolute override, a ~/.ti/bin-style install, and a umask-002 group-writable install are accepted; a ti-drive9 placed on PATH is never executed and the resolution fails with fs.companion_missing.
  • internal/fs/vault_secret_env_test.go: the child environment keeps safe variables and injected secret fields but drops TI_FS_TOKEN, TI_VAULT_TOKEN, TIDB_CLOUD_PRIVATE_KEY, DRIVE9_API_KEY, TI_FS_AI_PROVIDER_API_KEY and case-variant spellings of all of them; a real child (/bin/sh -c env) is inspected end to end; field names are normalized with the same byte-wise rule the companion uses for read-secret --format env (café → CAF__, é vs _ stay distinct) and colliding names fail with vault.env_collision; Unix keeps case-distinct inherited names such as http_proxy while Windows folds them; exit code 7 propagates as vault.command_failed.
  • e2e/release_build_test.go no longer sets the removed standalone control.

Verification

  • gofmt clean, go vet ./... clean, go build -tags ti_test_controls ./... clean
  • go test -count=1 ./... pass (43 packages)
  • Manual, with a built bin/ti and a fake companion:
    • TI_DRIVE9_BIN=<fake> ti fs list-files --path / runs the explicit companion and succeeds.
    • The same fake companion placed only on PATH is never executed; ti exits 1 with ti requires the Drive9 companion binary.
    • ti fs-vault run-with-secret --secret-path /n/vault/demo -- env injects only DB_URL / PASSWORD; DRIVE9_API_KEY, DRIVE9_VAULT_TOKEN, DRIVE9_PRIVATE_KEY, TIDB_CLOUD_PRIVATE_KEY, and TI_FS_TOKEN are absent from the child environment.

Review follow-ups (1970e9c)

  • Companion POSIX mode/ownership checks moved behind the Unix/non-Unix seam; Windows no longer rejects every install (its permission bits only encode the read-only attribute and access is ACL-controlled). CI now cross-compiles windows/amd64 and darwin/arm64.
  • One canonical credential-env denylist (internal/credentialenv) for TIDB_CLOUD_*, TDC_*, DRIVE9_*, TI_FS_*, TI_VAULT_*, shared by the companion env builder and the run-with-secret child env. This closes TI_FS_AI_PROVIDER_API_KEY, which the previous exact-name list missed.
  • Companion directory validation now enforces the owner rule the binary's doc comment described.
  • Removed the duplicated executable-directory candidate and the duplicate stat in resolveExplicitCompanion; vaultSecretChildEnv composes mergeEnv.
  • Stale --dry-run mount descriptions now describe companion routing instead of a local FUSE/WebDAV runtime.
  • Tests: unrelated-group rejection exercises the group rule through a seam, added directory-owner rejection, provider key added to both child-env assertions.
  • Dead native FUSE runtime deferred to Remove the dead native FUSE foreground mount runtime #90 (this PR stays scoped to the three security fixes).

Copilot AI balanced review requested due to automatic review settings November 10, 2026 09:30
@ti-chi-bot ti-chi-bot Bot added the size/XXL label Nov 10, 2026

Copilot AI 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.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

A critical security issue remains because companion binary validation follows symlinks (os.Stat), potentially allowing a symlinked path to evade intended checks, and there’s also a minor doc/behavior mismatch around allowed group-writable binaries.

2 open issues
What changed in this PR

Hardens Drive9 companion execution and fs-vault run-with-secret to reduce credential exposure by validating companion binaries and ensuring child processes do not inherit Ti/Drive9 tokens.

Changes:

  • Adds trusted companion resolution with explicit absolute-path overrides and safety validation of candidate binaries/directories.
  • Refactors fs-vault run-with-secret to fetch secrets via the companion but spawn the user command directly with a scrubbed environment.
  • Adds focused tests for companion trust rules and run-with-secret environment/exit-code behavior.
File Description
internal/​fswrap/​runner.go Implements hardened companion discovery and validation logic to prevent PATH hijacking and executing untrusted companions with sensitive credentials.
internal/​fswrap/​runner_test.go Adds unit tests covering acceptance/rejection of companion candidates under different path and permission scenarios.
internal/​fswrap/​owner_unix.go Introduces Unix-specific helpers for file ownership and group membership checks used by companion trust validation.
internal/​fswrap/​owner_other.go Provides non-Unix stubs for ownership/group helpers to keep builds portable across platforms.
internal/​fs/​vault.go Updates run-with-secret execution to build a scrubbed child environment, validate injected fields, and propagate child failures consistently.
internal/​fs/​vault_secret_env_test.go Adds end-to-end tests verifying credential env vars are scrubbed, secret fields are injected safely, and exit codes propagate.
internal/​fs/​drive9_companion.go Changes run-with-secret to read secrets via the companion and execute the user command directly from ti rather than delegating the full run to the companion.
AGENTS.md Documents stricter companion override requirements and the expected trust-validation behavior for companion binaries.

Review effort: Balanced

Comment thread internal/fswrap/runner.go Resolved
Comment thread internal/fswrap/runner.go Resolved
@srstack
srstack force-pushed the fix/fs-companion-integrity-and-vault-env branch 2 times, most recently from a5cfe41 to 4aeb1ee Compare November 10, 2026 09:38
@srstack
srstack force-pushed the fix/fs-companion-integrity-and-vault-env branch from 4aeb1ee to 5d78bc9 Compare November 10, 2026 09:41
@srstack
srstack requested a review from qiffang November 10, 2026 09:42
@srstack
srstack force-pushed the fix/fs-companion-integrity-and-vault-env branch from 5d78bc9 to 25d45c2 Compare November 10, 2026 09:48
@srstack srstack changed the title fix(fs): harden companion resolution and isolate run-with-secret child env fix(fs): stop leaking fs credentials through companion lookup, run-with-secret, and the WebDAV bridge Nov 10, 2026

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

This PR closes three real credential-exposure paths, and both findings from the earlier review pass (symlink resolution in companion validation, the group-writable doc mismatch) are verified fixed at this HEAD with targeted tests. Under a strict bar, two issues remain that sit inside the PR's own stated contract: the new companion trust validation is unconditionally broken on Windows — Go reports every normal writable file as mode 0666 and every directory as 0777 there, so the world-writable check rejects every default install — and the run-with-secret environment scrub misses TI_FS_AI_PROVIDER_API_KEY, a documented plaintext credential, contradicting the PR's stated TI_* scrubbing. Both are small, targeted fixes; the structural follow-ups below are strong but not merge-stopping.

What looks good

  • Companion resolution never consults $PATH, treats explicit configuration as terminal (fail rather than silently fall back), and validates the symlink-resolved binary in its real containing directory — the right posture for a credential-bearing exec, with a dense rejection test matrix.
  • run-with-secret now reads the secret through the companion and spawns the child directly from ti with a deterministic scrubbed + secret-injected environment, verified end-to-end against a real child process, including exit-code propagation. This removes the leak structurally instead of sanitizing a nested exec.
  • Deleting the unauthenticated loopback WebDAV bridge rather than authenticating it is the right call; the dead-code claim checks out, no references remain, and --driver webdav keeps working through the companion.

Blocking (please address before merge)

1. Companion validation rejects every default Windows install
internal/fswrap/runner.go:417 — the perm&0o002 world-writable checks in validateCompanionBinary (and validateCompanionDir at :458) run on all platforms, but on Windows os.Stat derives permission bits only from the read-only attribute: every normal writable file reports mode 0666 and every directory 0777 (see os/types_windows.go). So on Windows the check always trips: the default ~/.ti/bin/ti-drive9.exe staged by scripts/install.ps1 fails as "world writable (mode 0666)", and ~/.ti/bin itself fails as a world-writable parent. locateCompanion can never succeed, breaking every companion-routed command (ti fs, fs-vault, fs-git, fs-journal) on a platform .goreleaser.yaml ships and that CI (ubuntu-only) never exercises. The executable-bit check two lines below already carries runtime.GOOS != "windows", and owner_other.go already stubs ownership/group — the write-bit checks were left applying Unix semantics to bits that carry no such meaning on Windows.

Fix: move the permission/ownership validation behind the same platform seam the PR already created (Unix: perm + owner + group; non-Unix: not enforced, Windows ACLs are the real control there), keeping only the absolute/regular-file checks platform-neutral. Add GOOS=windows compile/test coverage in CI so the platform cannot silently regress again.

2. run-with-secret still passes TI_FS_AI_PROVIDER_API_KEY to the child
internal/fs/vault.go:556 — scrubTICredEnv is an exact-match list of 13 names and omits TI_FS_AI_PROVIDER_API_KEY, a documented plaintext provider secret read straight from the environment (internal/cli/commands.go:3265). A shell that has it exported — the documented way to configure extraction/embedding providers — hands the key to any run-with-secret child, contradicting both the PR description ("TI_* / TDC_* / DRIVE9_* credentials scrubbed") and the vaultSecretChildEnv comment ("ti's credential variables are removed"). Note that fswrap.drive9Env strips the DRIVE9_ family by prefix while this list scrubs exact names only — two scrub lists in two packages that have already drifted apart in this same PR.

Fix: scrub by prefix (TI_, TDC_, DRIVE9_, plus the TIDB_CLOUD_* pair) or share one canonical credential-env denylist between the two packages, and extend TestVaultSecretChildEnvScrubsCredentials with the provider key.

Should fix (non-blocking, strong)

3. Directory trust never checks ownership, while the docs claim "the same treatment"
internal/fswrap/runner.go:449 — validateCompanionDir checks only write-permission bits, never the owner uid, although the validateCompanionBinary doc says the containing directory "must pass the same treatment" — which per the binary checks includes owned-by-current-user-or-root. An unrelated user's 0755 directory containing a victim-owned binary passes validation, and its owner can still unlink/replace entries, leaving only the validate-to-exec race as protection (what is validated is not byte-for-byte what exec later resolves). Either enforce the same owner rule on Unix directories or scope the doc comment to what is actually enforced; walking the ancestor chain for the implicit candidates would also close the parent-rename swap window.

4. The dead-code deletion stops one third of the way
internal/fs/mount.go:501 — with the WebDAV branch gone, mountForeground has zero callers, which makes mountFUSEForeground and the entire in-process FUSE foreground runtime it anchors (~3k lines across fuse_mount.go, fuse_git.go, fuse_cache.go, fuse_writeback.go) unreachable — credential-bearing code that must keep compiling and keep being audited for nothing. mountVaultForeground, mountResult, autoUnpackMount, and pathErr (relocated into the new remotefs.go, which exists largely to keep this graph compiling) are equally dead. The PR's own justification for deleting the WebDAV bridge — "rather than authenticate dead code, it is removed" — removes this tree too. Deleting it here or as an immediate follow-up banks the simplification this change already paid for.

5. Stale --dry-run descriptions reference deleted components
internal/fs/mount.go:184 — the default description still says "normal execution starts a local ti fs FUSE runtime" and the webdav branch says "starts a local WebDAV bridge": this PR deletes the bridge, and normal execution always routes through the ti-drive9 companion. Dry-run output is ti's accuracy contract for planned mutations; replace both strings with one accurate companion-routed description.

Nits

  • internal/fswrap/runner.go:332 — the exe-directory candidate is validated twice (the inline os.Executable block, then again as the first entry of trustedCompanionDirs()), so an untrusted install reports the same candidate twice in the error; resolveExplicitCompanion also stats the path and then validateCompanionBinary stats it again.
  • internal/fs/vault.go:694 — vaultSecretChildEnv re-implements the merge body of mergeEnv (~15 lines) instead of composing mergeEnv(scrubTICredEnv(base), secretEnv).
  • internal/fswrap/runner_test.go:136 — the "group-writable" rejection fixture uses mode 0o664, which is rejected by the not-executable check before the group check runs (and the fixture's group is the caller's own group), so the unrelated-group branch is never actually exercised anywhere.

Recommendation

Request changes. Two verified blockers — the Windows-breaking permission check and the TI_FS_AI_PROVIDER_API_KEY scrub gap — are small fixes inside this PR's own stated contract and should land before merge.

@srstack

srstack commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — both blockers were real. Pushed 1970e9c on top of the PR.

1. Windows (blocking) — fixed. The POSIX mode and ownership rules now live behind the same Unix/non-Unix seam as the ownership helpers: validateCompanionFileMode / validateCompanionDirMode in owner_unix.go, no-ops in owner_other.go (Windows controls access with ACLs; its permission bits only encode the read-only attribute). The absolute/regular-file checks stay platform-neutral. Added a Cross-compile other platforms CI step (GOOS=windows GOARCH=amd64, GOOS=darwin GOARCH=arm64) so a platform regression fails the build; verified locally that both targets compile.

2. TI_FS_AI_PROVIDER_API_KEY (blocking) — fixed, and unified. New internal/credentialenv owns one denylist for the TIDB_CLOUD_*, TDC_*, DRIVE9_*, TI_FS_* and TI_VAULT_* families. Both fswrap.drive9Env and vaultSecretChildEnv scrub through it, so the two lists can no longer drift; the old exact-name list is gone. Non-credential TI_* variables (TI_PROFILE, TI_REGION_CODE, TI_TEST_*) stay inherited by the companion so its test controls keep working. TI_FS_AI_PROVIDER_API_KEY is now in TestVaultSecretChildEnvScrubsCredentials and in the real-child /bin/sh -c env assertion; manually re-ran run-with-secret … -- env with the provider key exported and it is absent from the child.

3. Directory ownership — fixed. validateCompanionDirMode now enforces the owner rule (current user or root) on Unix, matching the comment; TestLocateCompanionRejectsDirectoryOwnedByAnotherUser covers it through the ownership seam. I did not add the ancestor-chain walk: for the installer-owned ~/.ti/bin and the binary's own directory it would mostly re-validate the same trusted path, and the remaining validate-to-exec race needs the same-UID attacker that can already read ~/.ti/credentials. Happy to add it if you want the extra window closed.

4. Dead FUSE runtime — deferred, tracked. Opened #90 with the deadcode evidence and the acceptance criteria. I kept this PR scoped to the three security fixes as asked; #90 is the immediate follow-up.

5. Stale --dry-run text — fixed. One description now: "normal execution starts the ti-drive9 companion mount and returns once the requested path is readable".

Nits: executable-directory candidate is validated once (the trustedCompanionDirs loop starts with the exe directory), resolveExplicitCompanion validates in one call and maps os.ErrNotExist to fs.companion_missing, vaultSecretChildEnv composes mergeEnv, and the unrelated-group rejection now uses a 0o775 fixture with a group-membership seam so the group rule actually executes.

Local checks at 1970e9c: gofmt clean, go vet ./... clean, go test -count=1 ./... pass (44 packages), windows/darwin cross-compile clean.

@srstack
srstack requested a review from mornyx November 10, 2026 10:28

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

Re-review at the new head. Everything from the previous round is verified resolved: the POSIX mode/ownership rules now live behind the unix/!unix seam (validateCompanionFileMode/validateCompanionDirMode) with the Unix checks preserved verbatim and Windows a deliberate no-op; the new internal/credentialenv package is a genuinely complete credential denylist (every environment variable the CLI reads was enumerated — no credential name falls outside the five families, and TI_FS_AI_PROVIDER_API_KEY is covered and tested) shared by both spawn paths so the two lists cannot drift again; directory ownership is enforced with real seam tests; the stale dry-run text and all nits are fixed; and the deferred native FUSE runtime is tracked in #90. No regressions were found in the fix commit — the scrub does not strip anything the companion path needs, and the unit/e2e suites pass. The declined ancestor-chain walk is accepted: with directory ownership enforced, the residual requires an attacker already positioned against the user's own home path, which already exposes ~/.ti/credentials. Two non-blocking items remain, both about Windows test gating, plus nits.

What looks good

  • One canonical prefix-based denylist in internal/credentialenv, consumed by both the companion environment and the run-with-secret child environment, is the structurally right fix for the drift that let a credential variable slip through — future TI_FS_*/TDC_*/DRIVE9_* names are covered automatically, and keeping TI_TEST_* inheritable shows the tradeoff was reasoned.
  • The platform seam is placed correctly: platform-neutral checks (absolute, regular file, symlink resolution, both parent directories) stay shared, only POSIX mode/ownership moved behind the build tags, and the Unix checks were strengthened (directory ownership) rather than weakened to make Windows pass.
  • The ownership/group test seams exercise the real rules without chown privileges and without weakening assertions — fixtures are chosen so only the rule under test can reject (the old 0o664 fixture that was rejected by the wrong check is gone).

Should fix (non-blocking, strong)

1. The world-writable rejection assertion will fail on Windows
internal/fswrap/runner_test.go:132 — the world-writable subcase of TestLocateCompanionRejectsUntrustedCandidates asserts fs.companion_untrusted for a 0o777 fixture with no platform guard. On Windows the new seam deliberately accepts that fixture (mode checks are a no-op there), so locateCompanion returns success and the assertion fails the moment anyone runs go test on Windows. The adjacent nonExecutable subcase and four other assertions in this file already carry the correct runtime.GOOS guard; this one was missed. It is latent only because CI runs tests on ubuntu and merely cross-compiles for Windows — and it encodes the exact always-reject behavior the platform fix removed. Wrap it in the same guard (or skip the subcase on Windows).

2. The cross-compile CI step never type-checks test files
.github/workflows/ci.yml:59 — the new "Cross-compile other platforms" step runs GOOS=windows GOARCH=amd64 go build ./... (and darwin), but go build does not compile _test.go files, so the Windows-guarded test code added in this PR is never even type-checked for those targets. Switching to GOOS=windows GOARCH=amd64 go vet ./... (darwin likewise) type-checks the tests too at the same cost. Note the interaction with item 1: compile gating cannot catch behavioral mismatches like that one — a future windows-latest test job (not required for this PR) is what would actually gate the no-op seam.

Nits

  • internal/fswrap/owner_unix.go:14 — fileOwnerUID/fileGroupID/currentProcessGroup became mutable package-global function vars as test seams; pragmatic here, but the seam-swapping tests are silently incompatible with t.Parallel (nothing enforces that today). An ownerInfo struct injected into Runner would remove the globals if it ever needs hardening.
  • internal/fswrap/runner.go:365 — errCompanionPathNotAbsolute is effectively unreachable (resolveExplicitCompanion returns its own typed error first; implicit candidates are built from absolute directories), and the read.(VaultReadSecretResult) assertion in drive9RunWithVaultSecret is always true given Format: "json" with no field. Drop them or leave a one-line comment that they guard future callers.

Recommendation

Comment. All previous blockers are verified fixed with no regressions; the two remaining items are Windows test-gating hardening that should not hold up this security fix.

@srstack

srstack commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Both Windows test-gating items are fixed in 9c5f41d.

  1. The world-writable subcase in TestLocateCompanionRejectsUntrustedCandidates now sits under the same runtime.GOOS != "windows" guard as the non-executable case — it encoded exactly the always-reject behavior the seam removed. I also added TestValidateCompanionBinaryRequiresAbsolutePath so the relative-path guard is covered by a direct caller instead of being dead defensive code.
  2. The cross-compile step is now GOOS=windows GOARCH=amd64 go vet ./... and GOOS=darwin GOARCH=arm64 go vet ./..., so _test.go files are type-checked for both targets. Verified locally: both pass, go test -count=1 ./... still passes (44 packages).

Nits: the mutable fileOwnerUID/fileGroupID/currentProcessGroup seams only exist for the chown-less tests and none of those tests use t.Parallel; if we ever want an ownerInfo injected into Runner I'd rather do it when the seam actually needs hardening. The vault read type assertion got a comment explaining the future-caller guard.

Agreed that a windows-latest test job would be the real gate for the no-op seam; I left it out of this PR as you suggested.

@qiffang qiffang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed exact head 9c5f41d87db1fe1415de04cbd97071c9acec57df against feb08c24f29f832b505a79df28336c45509f1fb4.

One blocking credential-isolation issue remains: Windows case variants bypass the new shared credential filter, so run-with-secret still passes ambient credentials to its child. See the inline finding.

The previously discussed Windows POSIX-mode rejection, provider-key omission, directory ownership, symlink target-directory check, and Windows test type-checking issues are addressed at this head. The removed WebDAV bridge has no remaining callers; relocated metadata helpers are unchanged. I also traced companion discovery, credential construction, vault read/validation/child execution, and runtime preflight.

Validation was static source and call-chain review, including the Go 1.26.5 environment implementation; no local target code or runtime tests were executed. git diff --check passed. Current CI test and license/cla checks are successful, but the CI runtime is Ubuntu and the new Windows vet step cannot catch this behavioral leak.

Comment thread internal/credentialenv/credentialenv.go Resolved
@srstack

srstack commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Good catch — fixed in cd97c00.

credentialenv.IsCredential now upper-cases the key before matching, so ti_fs_token, Tidb_Cloud_Private_Key, drive9_private_key, tdc_vault_token, and any other spelling Windows resolves to the same variable are dropped by both spawn paths (the run-with-secret child environment and the companion probe environment). On Unix the case-insensitive match only widens the denylist to spellings ti never emits, so nothing legitimate is affected.

Two related touches:

  • The companion HOME/USERPROFILE filter got the same treatment, so an ambient home cannot shadow the scoped HOME= appended afterwards.
  • Tests now cover lowercase and mixed-case spellings for every family and assert that non-credential names (ti_profile, ti_region_code, ti_test_runtime_record) still pass through.

Verification at cd97c00: go test -count=1 ./... pass (44 packages), go vet ./... clean, and GOOS=windows GOARCH=amd64 go vet ./... / GOOS=darwin GOARCH=arm64 go vet ./... clean (the Windows target type-checks the new tests; the behavior itself is covered by the case-variant unit assertions since CI has no Windows runner).

Also noted for the record: the case-sensitivity bug would have been caught by a windows-latest test job; if the team wants one I can add it as a follow-up, but it is not in this PR.

@qiffang qiffang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed exact head cd97c009028eb7685ab170f7417d5c2e6bbebea6, including the complete four-file increment from 9c5f41d87db1fe1415de04cbd97071c9acec57df.

The previous P1 credential-leak finding is fixed and its thread is resolved. Both the vault child and companion/probe environment now use case-insensitive credential-family matching. Regression assertions cover lowercase/mixed-case credentials and preserve non-credential variables; the scoped HOME/USERPROFILE handling remains consistent. No remaining blocking findings.

Validation: static review of the changed source, both callers, and regression assertions; git diff --check passed. Current test and license/cla checks are successful. No local tests were rerun. CI executes tests on Ubuntu and type-checks Windows/Darwin; native Windows runtime testing remains an optional follow-up, not an approval blocker.

@srstack
srstack requested a review from mornyx November 10, 2026 12:55

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

Third pass at the new head. Both items from the previous round are verified fixed — the platform-sensitive assertions in TestLocateCompanionRejectsUntrustedCandidates now sit behind runtime.GOOS guards with TestValidateCompanionBinaryRequiresAbsolutePath covering the relative-path branch, and the CI step is now GOOS=windows GOARCH=amd64 go vet ./... plus GOOS=darwin GOARCH=arm64 go vet ./..., which genuinely type-checks the _test.go files (both re-run clean at this head). The case-insensitive credential scrub is verified sound on both spawn paths: upper-casing keys in credentialenv.IsCredential closes the Windows case-variant leak with no reachable folding bypass, no legitimately inherited variable is newly scrubbed, and the case-insensitive HOME/USERPROFILE filter cannot collide with the scoped append. No blocking issues remain. Three non-blocking items should still be addressed before the new injection behavior solidifies.

What looks good

  • One shared prefix-based denylist fixing both spawn paths in one place — and the case-insensitivity change additionally closes an ambient case-variant DRIVE9_*/HOME shadowing vector on the companion path, with a doc comment explaining the Windows os.Environ semantics that motivate it.
  • Reading the secret in ti and spawning the child directly with a scrubbed, deterministic environment deletes the trust problem instead of papering over it, and it ships with a real-child leak test and exit-code propagation tests.
  • Test discipline tracked the review exactly: the cross-vet step type-checks the platform-gated tests, the previously unreachable relative-path sentinel now has a direct caller, and the case-folding behavior is pinned at the credentialenv boundary.

Should fix (non-blocking, strong)

1. On Windows, an ambient case-variant variable silently masks an injected secret field
internal/fs/vault.go:682 — vaultSecretChildEnv merges the scrubbed base environment with the secret fields via mergeEnv, which keys and sorts case-sensitively, and the merge is intended to let secret fields win over same-named inherited variables. On Windows, where environment names fold case-insensitively, an ambient db_url next to a secret field DB_URL survives the credential scrub and coexists in the merged slice; sort.Strings places the lowercase spelling after the uppercase secret, and Go's exec deduplication of cmd.Env on Windows preserves the last occurrence of a case-insensitively equal key — so the child resolves DB_URL to the ambient value, silently masking the secret. This is not a credential leak (the ambient value is the user's own), but it breaks the documented injection guarantee on Windows, and this PR is the change that introduced ti-side injection. Fix: drop base entries whose key strings.EqualFold-matches an override key before merging, so exactly one spelling — the secret's — reaches the child.

2. One field-to-env-key rule, three behaviors
internal/fs/vault.go:381 — create-secret accepts any non-empty field key (parseVaultFieldAssignments), read-secret --format env normalizes names into the env charset (normalizeVaultEnvKey: db_url → DB_URL), but run-with-secret now hard-fails the same secret with vault.invalid_env_key (validateVaultEnvFields enforces [A-Z_][A-Z0-9_]*), where the pre-PR companion delegation imposed no ti-side gate. A secret that two commands accept and one rejects — with two conversion rules for the same concept in one file — will drift further. Either route the injection through the same normalization (with collision detection), or enforce the env charset at create-secret/replace-secret time so non-conforming field names cannot be stored in the first place.

3. The rewritten run-with-secret service flow has no ordinary-CI coverage
internal/fs/drive9_companion.go:1215 — drive9RunWithVaultSecret grew from a four-line companion delegation into ~40 lines of orchestration (path parsing, companion vault get --json, field decode, vault.empty_secret guard, env-key validation wiring, exit-code propagation), and only the opt-in live e2e (e2e/live_test.go, requires cloud credentials) executes it. The env contract itself is well tested with a real child in vault_secret_env_test.go; it is the service glue that is untested, and the package already has a script-companion harness (drive9_companion_test.go) that covers other companion flows. Add one test through that harness: normal field map reaches the child with the scrubbed env; {} → vault.empty_secret; non-conforming keys → vault.invalid_env_key; child exit 7 → vault.command_failed.

Nits

  • internal/fs/remotefs.go:114 — pathErr was relocated from the deleted WebDAV bridge but has zero remaining callers; it can go now even if the rest of the file waits for the #90 native-FUSE cleanup, and mapWebDAVError is a vestigial name for what is now a generic API-error mapping.
  • internal/fswrap/runner_test.go:16 — the drive9Env boundary test uses only canonical spellings; one mixed-case ambient (ti_fs_token, Drive9_Server=…) would pin the case-insensitive strip at the boundary where it actually matters.

Recommendation

Comment. The credential-leak objective is verified fixed on both spawn paths with no reachable bypass, and both prior-round items are resolved; the three remaining items are Windows injection correctness, rule consistency, and test coverage — real, but not merge-stopping.

@srstack

srstack commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Third round addressed in 7cd69c8; thanks for the qiffang approval.

  1. Windows case-fold shadowing — fixed. vaultSecretChildEnv now drops inherited entries whose key strings.EqualFold-matches a secret field before merging, so exactly one spelling (DB_URL) reaches the child. Covered twice: TestVaultSecretChildEnvDropsCaseVariantInheritedKey (ambient db_url + injected DB_URL) and the real-child test, which asserts db_url=ambient is absent from the child environment.

  2. One field-to-env-key rule — fixed by normalizing, not rejecting. Injection now goes through normalizeVaultEnvKey, the same rule read-secret --format env already used, and two fields that normalize to the same key fail with vault.env_collision instead of overwriting. A secret stored as db_url is injectable as DB_URL; the separate vault.invalid_env_key gate is gone. I chose normalization over tightening create-secret because existing secrets must stay readable/injectable.

  3. Service-flow coverage — added. The script companion grew a vault get branch, and TestDrive9RunWithVaultSecretServiceFlow runs the whole rewritten path through it: normal field map → child environment has the fields and no DRIVE9_API_KEY/FS token; {} → vault.empty_secret; colliding normalizations → vault.env_collision; child exit 7 → vault.command_failed; the companion invocation is asserted from the record file.

Nits: pathErr is deleted (no callers). The mapWebDAVError rename waits for #90 since its last callers are deleted there. The drive9Env boundary test now also pins Drive9_Server and ti_fs_token stripping.

Verification at 7cd69c8: go test -count=1 ./... pass (44 packages), go vet ./... clean, GOOS=windows GOARCH=amd64 go vet ./... and GOOS=darwin GOARCH=arm64 go vet ./... clean.

This PR still shows CHANGES_REQUESTED because the first-round review was never superseded by an approval; if the remaining items look good, an approval from you would unblock the merge.

@srstack
srstack requested a review from mornyx November 10, 2026 15:36

@qiffang qiffang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed exact head 7cd69c842e48a1035209dd7c4d59481651a2ca24, including the complete five-file increment from the previously approved cd97c009028eb7685ab170f7417d5c2e6bbebea6.

The previous credential-isolation P1 remains fixed. The new service-flow coverage, case-variant companion assertions, and removal of the unused helper are sound. However, the new environment merge and field-name conversion introduce two P2 correctness issues, detailed inline:

  1. Windows case-folding is applied on Unix too, deleting distinct, non-credential inherited variables.
  2. Injection does not actually share the normalization used by read-secret --format env: ti iterates Unicode runes, whereas the companion iterates UTF-8 bytes, producing different keys and collision decisions.

These are new findings on this increment, not a reopening of the resolved credential leak. Please address both before approval. The deliberate change from strict field-name rejection to normalization should also be described in current user-facing documentation; the PR's test description still says invalid keys are rejected.

Validation: static review of the full increment, surrounding execution paths, regression assertions, and the companion source at the repository's pinned ref/drive9 commit ee40c65c6c966aff222f3aeff48d68127c4dc54e. git diff --check passed. Current CI test and license/cla checks are successful. No local target code or runtime tests were executed.

Comment thread internal/fs/vault.go Resolved
Comment thread internal/fs/vault.go Resolved
@srstack

srstack commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Both P2s are fixed in 87554f8.

Preserve case-distinct inherited variables on Unix. The fold is now platform-aware (envKeysFoldCase, Windows only): dropShadowedEnvKeys compares exactly on Unix and case-insensitively on Windows. Tests pin both sides — TestVaultSecretChildEnvPreservesCaseDistinctKeysOnUnix keeps the inherited http_proxy next to an injected HTTP_PROXY, TestVaultSecretChildEnvDropsCaseVariantInheritedKey covers the Windows fold, and the real-child test now asserts the Unix preservation it previously (wrongly) asserted as deletion.

Match the env-read normalization. normalizeVaultEnvKey now walks UTF-8 bytes with the companion's rules (upper-case ASCII letters, leading digit prefixed with _, every other byte replaced with _), so café exports as CAF__ in both commands and é / _ stay distinct (__ vs _) instead of colliding. Covered at the helper boundary and through the script-companion service flow ({"café":"accented"} reaches the child as CAF__=accented, and {"é":"a","_":"b"} is accepted with distinct keys). Thanks for pointing at the pinned companion source — the byte-vs-rune difference was the whole bug.

Docs. README now describes the injection contract for ti fs-vault run-with-secret: the child receives only the secret fields plus a credential-scrubbed environment; field names use the same conversion as read-secret --format env (one underscore per UTF-8 byte for accented names); colliding names fail with a usage error; Windows replaces inherited names that differ only by case. The PR body's test description is updated the same way, and it no longer claims invalid keys are rejected.

Verification at 87554f8: go test -count=1 ./... pass (44 packages), go vet ./... clean, GOOS=windows GOARCH=amd64 go vet ./... and GOOS=darwin GOARCH=arm64 go vet ./... clean.

@srstack
srstack force-pushed the fix/fs-companion-integrity-and-vault-env branch from 87554f8 to a4c1b3b Compare November 10, 2026 16:17

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

Re-review at the new head 6053136. All three should-fixes and all three nits from the previous round are verifiably fixed: the three new observed vectors were recomputed from an independent reimplementation of the derivation formula and all eight match exactly, with /aéb genuinely discriminating the per-rune label rule from a byte-wise one; the mount-audit extraction is a pure move (byte-identical except the three deliberate teardown changes and the new mountStopHint), reversing the growth of drive9_companion.go (1914 → 1727); mountStopHint resolves its advice from observed state — the ti-unmount advice appears only when a locator exists, and the no-locator advice computes the exact resource-scoped companion HOME the daemon actually runs under, so the guidance works on every reachable path; the dead defaultMountLocalRoot is deleted and the relative-XDG_CACHE_HOME error names the variable. The security posture of the whole PR holds at this head. One hardening gap remains — the load-bearing half of the new guidance has no test pinning it — plus three nits; nothing blocking.

What looks good

  • The new vectors are chosen with real discrimination power: /aéb places a multi-byte rune between ASCII characters where the trim cannot erase the evidence, so a byte-wise refactor of the label sanitizer now fails the pin loudly; /.hidden and /x.y pin the leading-trim vs interior-retention distinction.
  • mountStopHint derives its advice from observed state (locator read) rather than assumptions, and its companion-HOME advice provably matches the daemon's environment derivation — the advice on every reachable path can actually work.
  • The file split is a genuinely pure move, verified line-by-line, landing the cohesive mount-security unit beside mountroot.go/mount.go instead of rearranging the giant companion file.

Should fix (non-blocking, strong)

1. The no-locator half of the new guidance is unpinned by tests

internal/fs/drive9_mount_audit.go:200 — the only umount-failure test (TestDrive9MountAuditCleanupFailureKeepsLocator) runs on the post-readiness path where the locator exists, and asserts only the "may still be running" + "unmount-file-system" message. Nothing covers the two new pre-locator double-failure branches (foreground mount failed with a clean audit + umount failed; locator write failed + umount failed) or mountStopHint itself, so a regression back to unconditional ti fs unmount-file-system advice — which verifiably cannot work without a locator (fs.mount_locator_not_found) — would pass the entire suite. That is the same green-suite-under-regression class this thread has consistently graded as a should-fix, and the fake companion already has the required knobs (TI_FAKE_DRIVE9_MOUNT_FAIL + TI_FAKE_UMOUNT_FAIL, plus TI_FAKE_MOUNT_ESCAPE_SYMLINK for the unsafe-audit variant). Add the two fake-companion tests (asserting the error contains the HOME=… ti-drive9 umount advice, not the ti unmount command), and a small table test for mountStopHint (locator present / absent) is a cheap third pin.

Nits

  • internal/fs/mountroot.go:24 — the doc comment still says "ti's own default (defaultMountLocalRoot) is a different path", but this commit deleted that function; the sentence now names a symbol that no longer exists, in the file whose job is keeping the two derivations straight. Reword to reference-free phrasing.
  • README.md:206 — "If the mount cannot be stopped, the error says so and the mount locator is kept so ti fs unmount-file-system can retry" is now true only for post-locator failures; in the new pre-locator paths there is no locator to keep and the error advises the companion's own umount with the scoped HOME. Add the clause.
  • internal/fs/drive9_companion.go:1251 — drive9MountVault's locator-write-failure branch still discards its umount error, so a vault mount whose locator write and umount both fail is left running with no stop guidance — the same shape just fixed for fs mounts, minus the unpack-audit security riding on it. Pre-existing and fine as a follow-up; noted so the pattern does not survive as the only swallowed teardown in the file.

Recommendation

Comment. Everything from the previous round is verifiably fixed and the security boundary holds — the code itself is in mergeable shape — but the locator-aware guidance added this round deserves its discriminating test (item 1) before this thread closes, per the same bar applied throughout.

@srstack

srstack commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

All items are in 546763a.

1. The no-locator guidance is now pinned. mountStopHint is generic over the ti unmount command and profile, and TestMountStopHintMatchesLocatorState pins both branches (locator present → ti unmount; absent → HOME=<companion home> ti-drive9 umount). Three fake-companion tests cover the pre-locator double failures you listed:

  • TestDrive9MountPreLocatorTeardownFailureAdvisesCompanion — foreground mount failed with a clean audit and the umount also failed;
  • TestDrive9MountLocatorWriteFailureTeardownAdvisesCompanion — the locator write failed (a file where ~/.ti/mounts belongs) and the umount failed;
  • TestDrive9MountUnsafeAuditPreLocatorTeardownAdvisesCompanion — the runErr + audit-violation branch whose teardown failed.
    Each asserts the message contains ti-drive9 umount and does not point at the locator-only command, so a regression back to unconditional ti fs unmount-file-system now fails the suite.

Nits.

  • mountroot.go no longer names the deleted defaultMountLocalRoot; it says "the mount runtime's own default overlay path was a different one".
  • README/AGENTS now add the pre-locator clause: before the locator exists the error names the companion's umount with the scoped HOME instead of suggesting the ti command.
  • drive9MountVault's locator-write-failure branch no longer swallows the umount error: if both fail it reports that the vault mount may still be running and gives the locator-aware stop hint (ti fs-vault unmount-vault when a locator exists, companion umount otherwise), so the last swallowed teardown in that file is gone.

One unrelated doc correction while I was there: the AGENTS companion-parity paragraph still mentioned the removed plan9 cache branch, so it now lists only the shipped platforms and states the deliberate relative-XDG deviation.

Verification at 546763a: go test -count=1 ./... (44 packages), go vet ./..., windows/darwin go vet clean.

@srstack
srstack requested a review from mornyx November 12, 2026 19:07

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

Re-review at the new head 546763a. The previous round's should-fix lands exactly as claimed on the fs side: the four new tests are genuinely discriminating pins — mutating the stop advice to unconditional ti-unmount or unconditional companion-unmount fails the suite in both directions — and they drive the real MountFileSystem path through the fake-companion subprocess rather than unit-calling internals. The mountStopHint parameterization is pure argument plumbing with no fs behavior change, the non-test hunks match the described items only, and the docs match the code. However, one of this round's claimed fixes does not materially work: the new vault teardown branch composes its message with fmt.Errorf("%w; …"), and the CLI renders errors through apperr.MessageFor, which extracts only the inner *apperr.Error message — so the "may still be running" warning and the stop hint never reach the user. That is the one merge-blocker; it is a one-line fix plus a test.

What looks good

  • The four new tests are discriminating in both directions (verified by mutation, not assertion count), closing the green-suite-under-regression gap this thread has tracked since the locator-aware advice was introduced.
  • mountStopHint resolves the companion HOME through the same Service.homeDir + fscred.CompanionHome source the runner actually uses, for both fs and vault mounts — the advice provably matches the daemon's environment.
  • Documentation discipline holds: the README/AGENTS pre-locator clause and the cache-root rewrite (plan9 branch removed from both code and AGENTS, deliberate relative-XDG_CACHE_HOME deviation stated) match the code precisely.

Blocking (please address before merge)

1. The vault teardown message is composed so that the CLI never prints it

internal/fs/drive9_companion.go:1252 — the new vault locator-write-failure branch returns fmt.Errorf("%w; the vault mount at %q may still be running and could not be stopped (%v); %s", err, opts.MountPath, umountErr, s.mountStopHint(...)). The CLI boundary (cmd/ti/main.go:29) prints apperr.MessageFor(err), and MessageFor uses errors.As to return only the first embedded *apperr.Error's message — here the wrapped locator-write error, whose message is just write ti fs mount locator. Exercising the real MountVault path (fake companion, ~/.ti/mounts as a regular file to break the locator write, TI_FAKE_UMOUNT_FAIL=1) confirms it: MessageFor returns the bare locator-write message while the full composed text — including stop it with HOME=… ti-drive9 umount … — exists only in err.Error(). So the vault teardown surfacing claimed this round is not fixed in user-visible output: a running vault mount daemon holding a delegated vault token is left with no working stop advice, and the ti unmount command a user would guess fails with fs.mount_locator_not_found. Every sibling teardown branch in internal/fs/drive9_mount_audit.go builds its message with apperr.New(apperr.CodeFor(err), "runtime", 1, <full text>) precisely so the advice renders; this branch deviates. No test covers it — which is exactly how it slipped through, since the fs-side tests all assert through MessageFor on apperr-built errors.

Fix: mirror the sibling pattern — apperr.New(apperr.CodeFor(err), "runtime", 1, fmt.Sprintf("%v; the vault mount at %q may still be running and could not be stopped (%v); %s", err, opts.MountPath, umountErr, s.mountStopHint(opts.Profile, opts.MountPath, "ti fs-vault unmount-vault"))) — and add the vault-side analog of TestDrive9MountLocatorWriteFailureTeardownAdvisesCompanion, asserting through apperr.MessageFor that the message contains ti-drive9 umount and does not point at the locator-only command. That test would have caught this.


Recommendation

Request changes. Item 1 is a verified user-boundary defect in a fix this head claims to deliver (the branch's internal logic — one umount attempt, correct scoped HOME, correct pre-locator hint — is right; only the message never renders), and the fix is one line plus the missing vault-side test. Everything else from every prior round is verified intact at this head.

@srstack

srstack commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in aac13de.

Blocking: vault teardown advice never rendered. You are right — fmt.Errorf("%w; …") only puts the composed text in err.Error(), while the CLI prints apperr.MessageFor, which extracts the first embedded *apperr.Error message. The branch now composes with apperr.New(apperr.CodeFor(err), "runtime", 1, <full text>), matching every sibling teardown branch, so the running warning and the stop hint reach the user.

Added the vault-side analog you asked for: TestDrive9VaultMountLocatorWriteFailureTeardownAdvisesCompanion drives the real MountVault path with the fake companion, makes the locator write fail (~/.ti/mounts as a regular file), fails the umount, and asserts through apperr.MessageFor that the message contains ti-drive9 umount plus the running warning and does not point at ti fs-vault unmount-vault. That is the assertion that would have caught the original defect.

Verification at aac13de: go test -count=1 ./... (44 packages), go vet ./..., windows/darwin go vet clean.

@srstack
srstack requested a review from mornyx November 13, 2026 02:44

@mornyx mornyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

Re-review at the new head aac13de. The previous round's single blocker is fixed exactly as specified: the vault locator-write-failure teardown now composes its message with apperr.New(apperr.CodeFor(err), "runtime", 1, <full text>), identical in shape to all four sibling teardown branches, so the "may still be running" warning and the locator-aware stop hint render through apperr.MessageFor at the CLI boundary. The new TestDrive9VaultMountLocatorWriteFailureTeardownAdvisesCompanion drives the real MountVault path through the fake companion and asserts through MessageFor — the exact boundary that was broken — pinning both the presence of the ti-drive9 umount advice and the absence of the locator-dependent ti command. Reverting the one-line fix makes that test fail with precisely the originally reported symptom (only write ti fs mount locator rendered), confirming it would have caught the defect. The increment contains nothing else, the full suite and cross-platform vet are clean, and every fix from all prior rounds is verified intact at this head, including the whole security chain: hardened companion resolution, the shared credential denylist on both spawn paths, the WebDAV bridge deletion, the post-readiness overlay audit with its discriminating ordering test, the observed-vector-pinned companion root replica, and the now fully test-pinned teardown guidance on both fs and vault paths.

What looks good

  • The fix is the minimal correct one — one line to the established sibling pattern, plus the exact test that was missing, with a comment explaining why the wrapper choice matters at the rendering boundary.
  • Both user-facing failure boundaries (message composition and stop-advice selection) are now pinned by tests that were each verified to fail on the defect they guard.
  • Across the whole review history the PR kept deleting and unifying rather than patching: the WebDAV bridge removed, eligibility collapsed to one owner, the mount-audit cluster extracted, and the derivation replica pinned by observed constants instead of trusted code.

Nits

  • The teardown-composition shape now exists at five sites; this round's bug was a new site copying it with the wrong wrapper. A small shared helper beside mountStopHint would make the correct composition unavoidable — good follow-up material, not worth reopening this PR for.
  • internal/fs/drive9_companion_test.go has accumulated ~2000 lines of security pins; a domain split would keep it navigable. Also fine as a follow-up.

Recommendation

Approve. The one-line rendering fix is verified correct and mutation-tested, every finding from every prior round is resolved with test coverage at the exact boundaries that failed, and only optional follow-up nits remain.

@srstack

srstack commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Took both nits directly in 2a6816e rather than deferring them, since you called the pattern out as the source of the last defect.

Shared composition helper. mountError(cause, format, args...) now owns apperr.New(apperr.CodeFor(cause), "runtime", 1, fmt.Sprintf(...)), and all six teardown/stop sites use it (fs mount failure, locator-write failure, audit rejection, ready-timeout teardown, the vault locator-write failure, and the stopped-on-timeout note). A new site can no longer pick the wrong wrapper without going out of its way. TestMountErrorKeepsCauseCodeAndFullMessage pins that the cause code survives and that apperr.MessageFor returns the full composed text.

Test file split. drive9_companion_test.go went from ~2000 to 925 lines:

  • drive9_fake_companion_test.go (367) — buildFakeDrive9 and the record/flag/invocation helpers;
  • drive9_mount_audit_test.go (759) — mount/vault mount ordering, teardown guidance, derived-root vectors, cache-root matrix, stop-hint table;
  • the remaining file keeps data-plane, layer, vault command, dry-run, and pack/unpack tests.
    No test body changed apart from imports; the fake companion source string was left byte-identical.

Verification at 2a6816e: go test -count=1 ./... (44 packages), go vet ./..., windows/darwin go vet clean.

I had opened #91 for exactly these two; it can be closed once this lands.

@srstack
srstack requested a review from mornyx November 13, 2026 03:14
…and WebDAV bridge

Three secret-protection findings and one dead-code finding from the scanner.

**Companion integrity (high).** The ti-drive9 companion runs with the TiDB
Cloud private key, the filesystem bearer token, and delegated vault tokens, but
it was resolved by bare name from $PATH with no integrity check, so any account
able to write an earlier PATH entry could receive those credentials. $PATH is
no longer consulted at all: the only implicit candidates are ti's own directory
and the installer-managed ~/.ti/bin, and an explicit CompanionPath or
TI_DRIVE9_BIN is terminal. A missing or insecure explicit path is an error
instead of a cue to fall back to a different binary. Every candidate must be an
absolute, regular, executable file owned by the current user or root, must not
be world writable and must not be writable by a group the caller is not in; the
directory that actually contains the binary is validated too, after resolving
symlinks, so a link inside a trusted directory cannot point at a binary another
account can replace. Group-writable binaries inside the caller's own groups
stay accepted so a umask of 002 does not break every install, and refusals
surface as fs.companion_untrusted with the offending path. The
TI_ALLOW_STANDALONE_DRIVE9 test control is removed with the PATH fallback.

**run-with-secret child environment (high).** ti asked the companion to spawn
the child (`vault with ... -- <cmd>`), so the child inherited the companion
environment including DRIVE9_API_KEY (the filesystem bearer token) and
DRIVE9_VAULT_TOKEN; the existing scrubTICredEnv helper was never called. ti now
reads the secret through the companion itself and spawns the user command from
ti with an environment built from os.Environ() minus TI_*/TDC_*/DRIVE9_*
credentials, plus the validated secret fields. Child exit codes propagate as
vault.command_failed.

**WebDAV bridge (high).** The native loopback WebDAV bridge was unreachable:
Service.MountFileSystem routes unconditionally to the Drive9 companion and
mountForeground had no callers. The bridge (webdavfs.go, the 127.0.0.1 server,
the random URL prefix, and the mount_webdav exec that received that prefix in
argv) is deleted rather than authenticated, and the driver abstraction keeps
only validation because mounting belongs to the companion. The metadata
helpers the FUSE code shares were kept in remotefs.go.

Tests: companion resolution rejects relative, world-writable,
unrelated-group-writable, non-executable, world-writable-directory, and
symlink-to-writable-directory binaries while accepting a valid absolute
override and a umask-002 group-writable install; a ti-drive9 planted on PATH is
never executed. The run-with-secret helpers assert credentials are absent from
a real child environment, secret fields are injected, invalid field keys are
rejected, and exit codes propagate. The native mount tests were removed with
the bridge.
Platform seam for companion trust. The POSIX mode and ownership checks now
live behind the existing Unix/non-Unix seam. Windows derives permission bits
from the read-only attribute (a normal file reports 0666 and a directory 0777),
so the unconditional world-writable check rejected every legitimate install,
including the default ~/.ti/bin/ti-drive9.exe staged by scripts/install.ps1.
Unix keeps the full rule set, now including the directory owner the binary's
doc comment already promised.

One credential-env denylist. internal/credentialenv owns the
TIDB_CLOUD_*, TDC_*, DRIVE9_*, TI_FS_* and TI_VAULT_* families, and both the
companion environment builder and the run-with-secret child environment scrub
through it. This closes TI_FS_AI_PROVIDER_API_KEY, which the previous exact-name
list missed, and removes the two lists that had already drifted apart.
Non-credential TI_ variables (TI_PROFILE, TI_REGION_CODE, test controls) are
still inherited by the companion.

Smaller review follow-ups: the executable-directory candidate is validated once
instead of twice, resolveExplicitCompanion no longer stats the path twice, the
dry-run mount descriptions now describe companion routing instead of a local
FUSE/WebDAV runtime, and vaultSecretChildEnv composes mergeEnv.

Tests: the unrelated-group rejection now runs the group rule (0o775 fixture with
the group-membership seam) instead of being short-circuited by the
not-executable check, a directory-owner rejection case is added, the child env
and real-child assertions include TI_FS_AI_PROVIDER_API_KEY, and CI
cross-compiles windows/amd64 and darwin/arm64 so the platform seam cannot
silently regress.
…platform

Two follow-ups from the re-review, both about Windows coverage rather than the
security fix itself.

The world-writable companion assertion now carries the same runtime.GOOS guard
as the neighbouring non-executable case: POSIX mode bits are deliberately not
enforced on Windows, so on that platform the seam accepts the fixture and the
assertion encoded the always-reject behavior the platform fix removed.

The cross-compile CI step now runs go vet instead of go build, so _test.go files
are type-checked for windows/amd64 and darwin/arm64 at the same cost. A new
direct test covers the relative-path guard that only a direct caller of
validateCompanionBinary can reach, and the vault read type assertion documents
why it stays.
Windows environment variable names are case-insensitive: a user can export the
filesystem token as ti_fs_token (or have an ambient Tidb_Cloud_Private_Key), Go
resolves it for os.LookupEnv, and os.Environ() preserves that spelling. The
shared credentialenv predicate compared prefixes case-sensitively, so both the
run-with-secret child environment and the companion probe environment kept those
entries and still handed the real bearer token or organization key to the child.

IsCredential now upper-cases the key before matching, which is safe on Unix
(case variants are not valid ti names) and closes the Windows spelling gap. The
companion HOME/USERPROFILE filter gets the same treatment so a differently cased
ambient entry cannot shadow the scoped HOME.

Tests cover lowercase and mixed-case spellings for the token families, and assert
that non-credential names such as ti_profile and ti_test_runtime_record still
pass through.
Third review round, all three items are in this commit.

Windows case folding in the merge. mergeEnv keys case-sensitively, so an ambient
db_url survived next to the injected DB_URL; Windows folds environment names and
keeps the last duplicate, so the ambient value would shadow the secret. The child
environment now drops inherited entries that match a secret field under
strings.EqualFold before merging, leaving exactly the injected spelling. There is
a unit case with an ambient db_url plus an injected DB_URL and the real-child test
asserts the folded ambient name is gone.

One field-to-env-key rule. Injection now normalizes field names through
normalizeVaultEnvKey, the same rule `read-secret --format env` already used, and
fails with vault.env_collision when two fields normalize to the same key. A secret
stored as db_url is therefore injectable as DB_URL instead of being rejected by a
second, stricter rule; the old vault.invalid_env_key gate is gone.

Service-flow coverage. The script-companion harness gained a `vault get` branch
and TestDrive9RunWithVaultSecretServiceFlow drives the whole rewritten path: the
companion returns a field map, the child receives it with the scrubbed environment,
`{}` maps to vault.empty_secret, colliding field names to vault.env_collision, and
child exit 7 to vault.command_failed.

Nits: dropped the relocated pathErr (no callers left); added mixed-case ambient
Drive9_Server and ti_fs_token to the drive9Env boundary test. The mapWebDAVError
rename waits for #90, where its last callers are deleted.
…key export

Two P2 findings on the previous increment.

Unix case-distinct variables. The Windows shadowing fix used
strings.EqualFold unconditionally, so on Linux/macOS an injected HTTP_PROXY
deleted an inherited http_proxy (the form curl reads) and any other case-distinct
name disappeared whenever a secret supplied a different casing. The comparison is
now platform-aware: Windows folds case, Unix compares exactly. Tests cover both
sides: the Windows fold drops the ambient db_url, Unix keeps http_proxy next to
the injected HTTP_PROXY, and the real-child assertion now checks the preservation
it previously asserted as deletion.

Companion-identical key conversion. read-secret --format env is exported by the
companion, whose normalizeSecretEnvKey walks UTF-8 bytes; ti's helper walked
runes, so "café" exported as CAF__ while injection produced CAF_, and "é" versus
"_" collided here but not in the companion. normalizeVaultEnvKey now walks bytes
with the same rules (upper-case ASCII letters, prefix a leading digit with '_',
every other byte becomes '_'), so both public commands agree on keys and on
collision decisions.

The new normalization behavior is documented in README: injected fields use the
same conversion as read-secret --format env, accented names produce one
underscore per UTF-8 byte, colliding fields fail with a usage error, and Windows
case-insensitive shadowing is described.

Tests: non-ASCII field coverage exists both at the helper boundary and through the
script-companion service flow ("café" -> CAF__ and "é" vs "_" staying distinct),
plus the Unix case-preservation cases.
- README now states the field-name rule exactly: digits and '_' are kept, a
  leading digit is prefixed with '_', and only bytes outside [A-Za-z0-9_] become
  '_'. The previous "every other byte" wording wrongly implied interior digits
  are replaced.
- renderVaultEnv and validateVaultEnvFields share normalizedVaultFieldMap, so the
  normalize-and-detect-collisions skeleton has one implementation and the export
  and injection paths cannot drift.
- The vault.env_collision message reports the colliding pair in sorted order, so
  the error text no longer depends on map iteration order.
- Dropped the unreachable empty-key guard: every input byte writes at least one
  output byte, so a non-empty field can never normalize to an empty key.
- The service-flow test pins the companion invocation arguments
  (`vault get demo --json`), and a new test repeats a colliding map to pin the
  deterministic message.
mount-file-system with a coding-agent/portable profile, --pack-path, or
--unpack-archive-path lets the ti-drive9 companion unpack a remote archive into
the local overlay, but it never ran the overlay symlink audit that
unpack-file-system enforces. A pack whose links escape the overlay could
therefore be unpacked at mount time and later write through to an arbitrary
local file.

ti now resolves the overlay the companion will write before the mount starts
(explicit --local-root wins, otherwise the same derived default root the mount
uses), passes that exact root to the companion so both sides agree, snapshots it,
and audits it after the companion returns - including when the mount failed,
because unpack is not atomic. Escaping links are removed, the mount is stopped
before the error is reported, and an unpack target ti cannot resolve is refused
with fs.mount_unpack_requires_profile instead of silently skipping the audit.
Ordinary mounts keep their previous flag shape and pass no --local-root.

Tests use the script companion to install an escaping symlink into the overlay
and assert the command fails with fs.unsafe_pack_symlink, the link is removed,
the escape target is untouched, and the mount is unmounted; a derived-root case
proves auto-unpack without --local-root is audited; a no-profile unpack is
refused; and a plain mount still passes no --local-root.
…ility

Review follow-ups on the mount overlay audit.

The audit now runs at the point unpack is observably finished. Mounts are
background-only, so the foreground mount command can return before the companion
daemon finishes unpacking; auditing right after it could miss a link installed
during startup, and the ready-timeout path never audited at all while leaving the
mount running. The audit moved after the readiness wait and also runs on the
timeout path, with the same umount-and-remove-locator teardown on violation.
Tests cover both the readable and the timed-out paths.

Cleanup failures are no longer silent. If the unmount after an audit rejection
fails, the error says the mount may still be running, names the unmount command,
and the mount locator is kept so ti fs unmount-file-system can retry.

Eligibility has one owner again. mountOverlayAudit no longer re-implements profile
resolution: it calls mountOverlayInputs, so the live path and --dry-run reject the
same combinations (for example --mount-profile none --pack-path), and
--no-auto-unpack is part of the unpack predicate instead of being ignored. An
explicit --unpack-archive-path still unpacks and is still audited.

Docs now match the predicate: the audit covers mounts that actually unpack
(auto-unpack with pack paths when --no-auto-unpack is not set, or an explicit
--unpack-archive-path), and a mount that only names --local-root is not audited.

Nits: the companion HOME/USERPROFILE case fold is Windows-only again, and
credentialenv.Scrub drops non-KEY=VALUE entries to match mergeEnv instead of
preserving dead behavior.
Review follow-ups on the mount overlay audit.

The audit-ordering test now discriminates: the fake companion's mount command
starts a detached worker that installs the escaping link only after the
foreground command returned, and the readiness probe reports active only once
that link exists. Under the previous ordering the mount would report success
with the link installed; the test fails there now.

Failure paths regained their guarantees. A companion mount failure plus an audit
violation routes through stopMountAfterUnsafeAudit, so an ambiguously failed
foreground command cannot leave a running daemon that the locator-only unmount
cannot reach. The locator-write failure path audits before tearing down, so a
link from a partial unpack is removed instead of being recorded as pre-existing
state by the next mount's baseline.

A clean audit at the ready-timeout point no longer leaves an unpack-capable mount
running: readiness never completed, so unpack may still be in flight, and the
mount is stopped as a documented exception to the leave-running contract. The
rejection stays in mountOverlayInputs, so live and --dry-run agree on the
none-profile unpack rule; --unpack-archive-path is trimmed at the boundary so the
predicate, argv, and result see the same value.

Docs narrow the coverage to the ti-side trigger (effective pack paths, which
portable supplies by default) and record the three companion-parity assumptions
plus the snapshot-attribution rule.
…g ti's

The companion-parity questions are answered from the drive9 source and binary,
and one answer changes the fix: drive9 derives a mount's default overlay root as
os.UserCacheDir()/drive9/mounts/<safe-label(remote)>-<sha256(server + "\n" +
remoteRoot + "\n" + credentialKey)[:8]> with the companion HOME, while ti's own
defaultMountLocalRoot is ~/.ti/local/fs/<hash>. Passing ti's path as
--local-root for an auto-unpack mount would relocate every existing overlay.

ti now audits the companion's own default root and only forwards --local-root
when the user set it. companionMountLocalRoot replicates drive9's derivation
(including the safe label and the credential fingerprint), and the replica is
pinned by a test using the value observed from the installed companion:
server https://drive9.example, remote /, credential sk-test produce
root-26d2b529bbcb6733. mountOverlayPolicy is the single eligibility owner for
both the native inputs and the audit, so the audit path no longer creates ti's
own overlay directory as a side effect.

The other two assumptions hold in drive9: autoUnpack is
overlayProfile && len(effectivePackPaths) > 0 && !*noAutoUnpack, so
--no-auto-unpack is honored exactly, and the builtin coding-agent profile has no
pack paths while portable supplies "/" (verified by running the companion with a
temporary HOME: bare coding-agent did not fetch an archive, portable did).
Blocking review finding: companionCacheRoot applied XDG_CACHE_HOME on every
platform and filtered it with TrimSpace/IsAbs, while os.UserCacheDir (which the
companion inherits) honours it only on the default Unix branch, verbatim. On
macOS with XDG_CACHE_HOME set, ti audited $XDG_CACHE_HOME/drive9/mounts/... while
the companion unpacked into companionHome/Library/Caches/..., and because a
missing overlay scans as clean the audit silently passed.

The cache root now follows the stdlib decision tree: darwin always
companionHome/Library/Caches, Windows os.UserCacheDir (LOCALAPPDATA untouched),
plan9 companionHome/lib/cache, and the default Unix branch uses a non-empty
XDG_CACHE_HOME verbatim (no trim, no absolute filter) so a relative value makes
scanOverlayEntries reject the non-absolute root instead of auditing a wrong
directory. Tests cover the XDG set/unset matrix per GOOS, the relative-XDG
failure, and the observed derivation vectors are now hermetic.

Also from the same review: companionMountLocalRoot returns fs.missing_profile
instead of a silent empty string; the dry-run path no longer derives or creates
ti's own overlay directory (mountOverlayInputs is gone, mountOverlayPolicy is the
only eligibility owner); a failed foreground mount with a clean audit also stops
the daemon when an unpack is possible; the timeout teardown errors no longer
claim a stopped mount could not be stopped and no longer embed the stale stop
hint; the unpack-file-system mount-path error points at the local_root reported
by mount-file-system; and the derived-root test comment matches what it asserts.
…nt audit

Review follow-ups on the mount overlay audit.

The vector table now pins the per-rune label rule: a mid-segment accented rune
(/aéb -> a-b) distinguishes the rune walk from a byte walk (a--b), which the
previous trimmed vectors could not, and a leading-dot segment pins the hidden
label handling. Both were captured from the installed companion.

Teardown failures are no longer swallowed. The failed-foreground branch and the
locator-write-failure branch report when the mount could not be stopped and say
an unpack may still be in progress, and the stop hint is locator-aware: ti's
unmount resolves through the locator, so before it exists the message names the
companion's own umount with the resource-scoped HOME.

The mount audit cluster (drive9MountFileSystem, the teardown helpers, and
unpackAuditLocalRoot) moved to internal/fs/drive9_mount_audit.go, next to
mountroot.go, leaving drive9_companion.go to the data-plane command adapters.

Nits: ti's orphaned defaultMountLocalRoot is deleted so it cannot be confused
with the companion replica; companionCacheRoot documents its deliberate
XDG_CACHE_HOME deviation and only carries shipped-platform branches; a relative
XDG_CACHE_HOME now fails at derivation with an actionable error naming the
variable instead of reaching a generic overlay-audit message.
Review follow-up: the no-locator half of the stop guidance had no test, so a
regression back to the ti unmount command (which fails with
fs.mount_locator_not_found before a locator exists) would have stayed green.

mountStopHint is now generic over the ti unmount command and profile, pinned by
a table test covering both states (locator present -> ti unmount, absent ->
companion umount with the resource-scoped HOME). Three fake-companion tests
cover the pre-locator double failures: foreground mount failed with a clean
audit and a failed umount, locator write failed with a failed umount, and the
runErr + audit-violation branch whose teardown also failed. Each asserts the
error names `ti-drive9 umount` and not the locator-only command.

The same shaped teardown bug in drive9MountVault is fixed too: a vault mount
whose locator write and umount both fail now reports that it may still be
running and how to stop it, instead of discarding the umount error.

Docs: the mountroot comment no longer names the deleted defaultMountLocalRoot;
README and AGENTS describe the pre-locator advice.
The vault locator-write-failure branch composed its message with
fmt.Errorf("%w; ..."), but the CLI renders apperr.MessageFor, which returns only
the embedded apperr message. The running warning and the companion stop hint
therefore never reached the user, leaving a vault mount daemon holding a
delegated token with no working stop advice.

Compose the error with apperr.New(apperr.CodeFor(err), "runtime", 1, <full text>)
like every sibling teardown branch. TestDrive9VaultMountLocatorWriteFailure...
TeardownAdvisesCompanion drives the real MountVault path with the fake companion,
breaks the locator write, fails the umount, and asserts through apperr.MessageFor
that the message contains the running warning and `ti-drive9 umount` and does not
point at the locator-only unmount command.
…on tests

Applies the two follow-up nits from the approving review instead of leaving them
as issues.

mountError(cause, format, args...) now owns the composition every teardown and
stop message uses: apperr.New(apperr.CodeFor(cause), "runtime", 1,
fmt.Sprintf(...)). The last review round found a new site that used
fmt.Errorf("%w; ...") instead, which the CLI's apperr.MessageFor silently hides;
routing all six sites through one helper makes that mistake structurally
avoidable. TestMountErrorKeepsCauseCodeAndFullMessage pins the code preservation
and the rendered text.

drive9_companion_test.go is split by domain: the fake companion harness
(buildFakeDrive9 and its record/flag helpers) moved to
drive9_fake_companion_test.go, and the mount/vault mount audit pins moved to
drive9_mount_audit_test.go. The remaining file keeps the data-plane, layer,
vault command, dry-run, and pack/unpack tests. No test body changed apart from
imports.
@srstack
srstack force-pushed the fix/fs-companion-integrity-and-vault-env branch from 2a6816e to 37a1606 Compare November 13, 2026 03:30
@srstack
srstack merged commit 182fdc8 into main Nov 13, 2026
2 checks passed
@srstack
srstack deleted the fix/fs-companion-integrity-and-vault-env branch November 13, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants