codex-security icon indicating copy to clipboard operation
codex-security copied to clipboard

~ in CLI path arguments is not expanded outside of scan

Open Dheerajsom opened this issue 4 months ago • 5 comments

Version

@openai/codex-security 0.1.1

OS

Windows 11 (root cause applies to any shell/OS that doesn't pre-expand ~ for a program's arguments)

Steps to reproduce

  1. Run: codex-security bulk-scan repos.csv --output-dir ~/security-scans

Expected behavior

~ expands to the user's home directory, matching the behavior of codex-security scan, which already expands ~ in its own path arguments.

Observed behavior

~ is treated as a literal folder name. The command writes output to ./~/security-scans instead of $HOME/security-scans, leaving a stray directory named ~ in the current working directory. The same issue affects:

  • bulk-scan <input-csv> and --output-dir
  • export <scan-dir>, --output, --source-root
  • scans list <repository>, --scan-root
  • install-hook <repository>
  • the output directory prompted for in the interactive bulk-scan wizard

Besides writing to the wrong location, resuming a bulk scan from a different working directory fails to find the earlier ledger, since the literal ~ path doesn't resolve consistently.

Suggested fix

Every affected call site in sdk/typescript/src/cli.ts resolves the path with a bare resolve(), which never expands ~. A small wrapper around the existing expandHome helper (already used by scan) fixes it at every call site:

Before:

inputPath = resolve(currentDirectory, args.input);
outputDir = resolve(currentDirectory, options.outputDir);

After:

function resolveCliPath(directory: string, value: string): string {
  return resolve(directory, expandHome(value));
}
...
inputPath = resolveCliPath(currentDirectory, args.input);
outputDir = resolveCliPath(currentDirectory, options.outputDir);

The same resolveCliPath swap applies to the other affected arguments: scans list's --repository/--scan-root, export's --output/--source-root, install-hook's repository argument, and the bulk-scan wizard's output-directory prompt.

Dheerajsom avatar Jul 29 '26 03:07 Dheerajsom

I independently audited the current public projection and confirmed that the bug is broader than the two bulk-scan call sites, but still has a clean, bounded fix.

Path-typed call sites that should use a shared resolveCliPath(directory, value) helper:

  • scans list: repository plus both uses of --scan-root (cli.ts)
  • install-hook: repository passed to git -C (cli.ts)
  • bulk-scan: CSV input and output directory (cli.ts)
  • export: scan directory, explicit output, and source root (cli.ts); the --output - stdout sentinel should keep bypassing path resolution
  • interactive bulk-scan output selection (bulk-scan-discovery.ts)

The existing expandHome behavior is a good compatibility boundary: expand only ~, ~/..., and ~\\...; keep ~user literal.

One guardrail: I would not replace every remaining bare resolve() globally. The validation/patch input path at cli.ts:1918 accepts either a filename or literal finding text, so eager tilde expansion there could reinterpret user text as a file.

Suggested regression coverage (no model or network call needed):

  1. Pure path cases: ~, ~/child, ~\\child, ~user/child, ordinary relative, and absolute paths.
  2. CLI tests with isolated HOME and USERPROFILE for each path-typed command above.
  3. A bulk-scan wizard test whose prompt returns ~/custom-results.
  4. Assert that no literal <cwd>/~ directory is created.
  5. Preserve export --output - unchanged.

This should make the change cross-platform while avoiding accidental expansion of non-path positional input.

huanglianggit avatar Jul 29 '26 06:07 huanglianggit

Hey Huang, thanks for the comment and further thoughts. For the detailed audit. I implemented the suggested bounded fix in my fork:

  • Added a shared resolveCliPath() for the path-typed CLI inputs you identified.
  • Expanded ~, ~/…, and ~\… while preserving ~user/….
  • Kept non-path inputs and export --output - unchanged.
  • Added regression coverage for CLI paths, the bulk-scan wizard, and direct expandHome() behavior.
  • Verified that no literal <cwd>/~ directory is created.

I'll link the changes I made on my fork below if you want to see it.

Main fix:
Implementation: https://github.com/Dheerajsom/codex-security/commit/61786cf Regression coverage: https://github.com/Dheerajsom/codex-security/commit/563c47b0c31524708add217f2fc8967b1045b189

Dheerajsom avatar Jul 30 '26 00:07 Dheerajsom

Related closed issue #42 fixed tilde expansion for CODEX_HOME inside configuration preflight only. This issue concerns a different call site: path-valued CLI and bulk-scan arguments. #143 attempted that broader CLI-path fix but was closed without merging, so #42's resolution does not resolve this report and there is currently no active replacement PR.

mldangelo-oai avatar Aug 03 '26 03:08 mldangelo-oai

Fixed on main by #492, which expands home-relative values for the reported CLI and bulk-scan paths. The related knowledge-base path is covered by #254.

rohanpoudel2 avatar Aug 21 '26 20:08 rohanpoudel2

Thanks for reporting this. I believe this is resolved by #492, which makes ~ expansion consistent across CLI commands. Please feel free to reopen if you still run into it on the latest version.

mldangelo-oai avatar Aug 31 '26 14:08 mldangelo-oai