cli icon indicating copy to clipboard operation
cli copied to clipboard

`secrets set` ignores `--workdir` and reads config.toml from the working directory

Open xpressbase opened this issue 1 month ago • 1 comments

Describe the bug

supabase secrets set resolves supabase/config.toml by searching up from process.cwd(), so --workdir has no effect on which config it reads. The command merges that file's [edge_runtime.secrets] into its request body, so running it from a directory whose config declares local-only secrets pushes those values to the target project — even when --workdir points at a different, secret-free project directory.

config push, config diff and config pull do honour --workdir, which makes the behaviour of secrets set surprising.

To reproduce

mkdir -p leaky/supabase clean/supabase

cat > leaky/supabase/config.toml <<'EOF'
project_id = "leaky"

[edge_runtime.secrets]
LOCAL_ONLY = "http://127.0.0.1:54324"
EOF

printf 'project_id = "clean"\n' > clean/supabase/config.toml

cd leaky
supabase secrets set --project-ref <ref> --workdir ../clean --output-format json

No secrets are passed on the command line, so anything the command sends can only have come from a config file.

Expected behavior

--workdir ../clean is used, clean/supabase/config.toml declares no [edge_runtime.secrets], and the command fails with SecretsNoArgumentsError without sending a request.

Actual behavior

It reads leaky/supabase/config.toml and posts LOCAL_ONLY to the project:

{"_tag":"Error","error":{"code":"SecretsSetNetworkError","message":"failed to set secrets: TransportError"}}

(a request was built and attempted — here with outbound traffic blocked, to keep the test off a real project)

Running the identical command from clean/ instead gives:

{"_tag":"Error","error":{"code":"SecretsNoArgumentsError","message":"No arguments found. Use --env-file to read from a .env file."}}

So only the working directory decides which config is read.

Version

Reproduced on 2.118.0 and present in 2.122.0.

Where it comes from

apps/cli/src/commands/secrets/set/set.handler.ts calls loadCliConfig(runtimeInfo.cwd, …) and loadCliProjectEnvironment({ cwd: runtimeInfo.cwd, … }). runtimeInfo.cwd is process.cwd() in apps/cli/src/shared/runtime/runtime-info.layer.ts and never reflects --workdir. By contrast apps/cli/src/commands/config/config.load.ts loads from cliSettings.workdir and respects shouldSearchAncestors.

Why it matters

A natural way to keep local-only Edge Function secrets out of a hosted project is to keep them in one config.toml and run secrets set with --workdir pointed at another directory whose config has no secrets table. That doesn't work, and the merge is silent: nothing in the output names the secrets the config contributed, so the extra values are only visible afterwards in secrets list.

A fix could either honour --workdir here as the config commands do, or — separately useful — list the config-sourced names the command is about to send.

xpressbase avatar Oct 27 '26 19:10 xpressbase

Hi team,

Thanks @xpressbase for the clear reproduction case. I investigated this defect and confirmed the split-brain root cause between target project resolution and secret loading:

Root Cause

  1. In apps/cli/src/commands/secrets/set/set.handler.ts (lines 114 & 171), the handler hardcodes runtimeInfo.cwd (process.cwd()) when invoking loadCliConfig and loadCliProjectEnvironment, bypassing CommandSettings.workdir and SUPABASE_WORKDIR.
  2. While RemoteTarget correctly honors CommandSettings.workdir to resolve <workdir>/supabase/.temp/project-ref, the secret loader scrapes [edge_runtime.secrets] from the shell's CWD. In monorepos or multi-service setups with local config overrides, this inadvertently publishes local dev credentials to remote production environments.
  3. Additionally, without explicitly forwarding search: shouldSearchAncestors(commandSettings) to loadCliProjectEnvironment, directory traversal climbed parent monorepo trees and read root .env files even when an explicit --workdir was supplied.

The Fix

  • Honor --workdir for Config & Env: Pass commandSettings.workdir to both loadCliConfig and loadCliProjectEnvironment.
  • Enforce Ancestor Boundaries: Pass search: shouldSearchAncestors(commandSettings) to both calls to stop ancestor climbing when an explicit workdir is specified.
  • Preserve --env-file Ergonomics: Retained runtimeInfo.cwd for resolving --env-file paths per the contract documented in SIDE_EFFECTS.md.
  • Tests Added: Added integration tests in set.integration.test.ts verifying:
    1. Regression control: CWD secrets are ignored and not leaked when --workdir points to a clean subproject (fails clean with SecretsNoArgumentsError).
    2. Positive resolution: --workdir config secrets are loaded and transmitted.
    3. Environment isolation: Subproject .env overrides parent .env under explicit workdir.
    4. Ergonomics: Relative --env-file paths continue resolving against CWD alongside --workdir.

The fix and test suite are implemented, fully formatted, and verified 100% green on my fork: https://github.com/jadibdev/cli/tree/jadibdev/secrets/fix-workdir-secrets-set-7354

Could you please label this issue open-for-contribution so I can open the pull request against develop? Thanks!

jadibdev avatar Oct 28 '26 00:10 jadibdev