Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
519bf91
Cloudflare: account variables with unknown values block as can't tell…
paureis Oct 23, 2026
64e29d4
Windows: providers read variable names case-insensitively (#119)
paureis Oct 23, 2026
85648c0
Test: an unknown value in one spelling gives the unknown reason (#119)
paureis Oct 23, 2026
9b58b6d
CONTRIBUTING: what cacheInputs().env must list, and Windows names (#119)
paureis Oct 23, 2026
3f884f3
Windows: the identity cache gets the folded names too (#119)
paureis Oct 23, 2026
5c3d3a5
Test: Windows case and folder gaps, failing before the fix (#136)
paureis Oct 25, 2026
f78f566
Windows: CLOUDPIN_CONFIRM and CLOUDPIN_SKIP read case-insensitively (…
paureis Oct 25, 2026
60ffb08
Windows: the read-only check gets the folded variable names (#136)
paureis Oct 25, 2026
546f1d2
Providers get the platform guard is given (#136)
paureis Oct 25, 2026
40869ed
Wrangler: the identity cache watches the login file on Windows and ma…
paureis Oct 25, 2026
2306d4d
CONTRIBUTING: providers read the platform they are given (#136)
paureis Oct 25, 2026
5322307
Move envFor and the user-only variables into user-env.ts (#136)
paureis Oct 25, 2026
2c4e82a
Windows: CLOUDPIN_ENV and CLOUDPIN_BRANCH read case-insensitively, ag…
paureis Oct 25, 2026
3b41717
AGENTS: list src/user-env.ts in the source layout (#136)
paureis Oct 25, 2026
bcf1f23
Merge origin/main into fix/136-windows-case-gaps (#136)
paureis Nov 2, 2026
92f6460
Test: the Wrangler login folder test checks main's cache inputs (#136)
paureis Nov 2, 2026
f8b5b36
Windows: CLOUDPIN_CONFIRM and CLOUDPIN_SKIP read case-insensitively (…
paureis Nov 2, 2026
e1055f0
Windows: the read-only check gets the folded variable names (#136)
paureis Nov 2, 2026
f9aa403
Providers get the platform guard is given (#136)
paureis Nov 2, 2026
900776e
Wrangler: its folders and .env names follow the platform guard is giv…
paureis Nov 2, 2026
931f27f
Windows: CLOUDPIN_ENV and CLOUDPIN_BRANCH read case-insensitively, ag…
paureis Nov 2, 2026
5b38694
CHANGELOG and ROADMAP: Windows case gaps (#136)
paureis Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Versions 0.1.0 to 0.4.2 (2026-10-08 to 2026-10-10) are in [docs/changelog-0.x.md
- Windows: variable names are read without regard to case, as Windows and the CLIs do (#119), so a lowercase
`gh_token`, `aws_profile`, `azure_config_dir` or `cloudflare_account_id` counts. One variable set in two spellings
to different values stops as "can't tell".
- Windows: cloudpin's own variables are read without regard to case too (#136): a lowercase `cloudpin_env`,
`cloudpin_branch`, `cloudpin_confirm` or `cloudpin_skip` you set counts, and an agent still can't set or unset
`CLOUDPIN_ENV` or `CLOUDPIN_BRANCH` in any spelling. The read-only check on a protected environment reads folded
names, so a lowercase `aws_config_file` counts there as well.

## [1.2.1] - 2026-10-29

Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ environment variables count. When it can't tell, it returns an error, and cloudp
reason. `cacheInputs` must list every variable and file that can change the answer of a CLI call that `resolve` makes,
and its `env` every variable that can change `resolve`'s own answer: when a command line sets one of those from
something cloudpin can't read in advance (`$(...)`), the command is stopped. On Windows, `resolve` gets every
variable name in upper case, as the CLIs read them there.
variable name in upper case, as the CLIs read them there. Both get the platform the command runs on (`platform` in
`resolve`'s context, `cacheInputs`' second argument): read it there, never `process.platform`, so tests can set it.

## Pull requests

Expand Down
2 changes: 2 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ issues it ships. Open an [issue](https://github.com/paureis/cloudpin/issues) to
the way each CLI does, in every provider.
- **Cloudflare account variables** ([#119](https://github.com/paureis/cloudpin/issues/119)): done. An account
variable whose value cloudpin can't know blocks, and Windows reads variable names without regard to case.
- **Windows case gaps** ([#136](https://github.com/paureis/cloudpin/issues/136)): done. cloudpin's own variables
and the read-only check read names without regard to case (Wrangler's per-platform login folder came with #103).

## Ideas (not scheduled yet)

Expand Down
2 changes: 1 addition & 1 deletion src/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function explain(command: string | string[], opts: ExplainOptions,
}
const cwd = call.cwd ?? opts.cwd;
const trace: Trace = {};
const verdict = await guard({ bin: call.bin, args: call.args, env: envFor(opts.env, call, { agent: opts.mode === "agent" }), cwd, mode: opts.mode }, deps, trace);
const verdict = await guard({ bin: call.bin, args: call.args, env: envFor(opts.env, call, { agent: opts.mode === "agent", platform: deps.platform }), cwd, mode: opts.mode }, deps, trace);
const res = trace.resolution;
out.push({
argv: [call.bin, ...call.args],
Expand Down
26 changes: 15 additions & 11 deletions src/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
const provider = deps.providers.find((p) => p.bins.includes(name));
if (!provider) return { action: "allow", reason: "not-guarded" };
trace.provider = provider.name;
// On Windows the provider reads names the way the CLI does, case-insensitively
// (#119), and so does cloudpin for its own variables, CLOUDPIN_ENV included
// (#136; envFor keeps an agent from setting those in any spelling).
const platform = deps.platform ?? process.platform;
const folded = foldEnvNames(req.env, platform);

let config: FoundConfig | null;
try {
config = deps.findConfig(req.cwd, req.env);
config = deps.findConfig(req.cwd, folded.env);
} catch (err) {
// A broken pin file must not silently switch protection off. Exempt
// commands (login, whoami, help, ...) never act on an account, and are
Expand All @@ -146,7 +151,7 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
if (!pin) return { action: "allow", reason: "not-pinned" };
trace.pin = pin as Record<string, string>;
if (provider.isExempt(req.args, name)) return { action: "allow", reason: "exempt" };
if (req.mode === "shell" && req.env.CLOUDPIN_SKIP === "1") return { action: "allow", reason: "skipped" };
if (req.mode === "shell" && folded.env.CLOUDPIN_SKIP === "1") return { action: "allow", reason: "skipped" };

const base = {
action: "block" as const,
Expand All @@ -157,8 +162,6 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
// Track whether the CLI itself is missing: then the command would fail on
// its own, so there is no account to protect (DESIGN.md, edge cases).
let cliMissing = false;
// On Windows the provider reads names the way the CLI does, case-insensitively (#119).
const folded = foldEnvNames(req.env, deps.platform ?? process.platform);
const inner = deps.wrapExec ? deps.wrapExec(provider, folded.env, deps.exec) : deps.exec;
const exec: Exec = async (bin, args, env) => {
const result = await inner(bin, args, env);
Expand All @@ -170,7 +173,7 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
if (dynamic) return { ...base, uncertain: true, problems: [dynamic.problem], fix: dynamic.fix };
// A variable the line sets from something only known when it runs (#86): if it
// decides this CLI's account, cloudpin can't tell which account will be used.
const identityVars = provider.cacheInputs(folded.env).env;
const identityVars = provider.cacheInputs(folded.env, platform).env;
const unknown = Object.keys(folded.env).filter((k) => folded.env[k] === UNKNOWN_VALUE && envNameMatches(identityVars, k));
if (unknown.length > 0) {
return {
Expand All @@ -195,7 +198,7 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
}
// Other unknown values don't decide the account; the CLI's identity check runs without them.
const env = Object.fromEntries(Object.entries(folded.env).filter(([, v]) => v !== UNKNOWN_VALUE));
const res = await provider.resolve({ args: req.args, env, cwd: req.cwd, bin: name }, exec);
const res = await provider.resolve({ args: req.args, env, cwd: req.cwd, bin: name, platform }, exec);
trace.resolution = res;
if (res.kind === "error" && cliMissing) return { action: "allow", reason: "not-installed" };
switch (res.kind) {
Expand All @@ -215,22 +218,23 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
const p = provider as ProviderDef<typeof provider.name>;
const problems = p.compare(pin as never, res.identity);
if (problems.length > 0) return { ...base, problems, fix: p.switchHint(pin as never) };
return protectedEnvironment(req, provider.name, config, trace);
return protectedEnvironment(req, folded.env, provider.name, config, trace);
}
}
}

/**
* The account is right; on a protected environment a command that may change
* something still needs the user's yes (issue #12). Only a human can give it
* ahead of time, with CLOUDPIN_CONFIRM=<environment>.
* ahead of time, with CLOUDPIN_CONFIRM=<environment>. `env` is the command's
* environment with Windows names folded (#136).
*/
function protectedEnvironment(req: GuardRequest, provider: Provider, config: FoundConfig, trace: Trace): Verdict {
function protectedEnvironment(req: GuardRequest, env: NodeJS.ProcessEnv, provider: Provider, config: FoundConfig, trace: Trace): Verdict {
const environment = config.environment;
if (!environment?.protected) return { action: "allow", reason: "match" };
trace.readOnly = isReadOnly(provider, req.args, req.env, config.readOnly ?? {}, commandName(req.bin));
trace.readOnly = isReadOnly(provider, req.args, env, config.readOnly ?? {}, commandName(req.bin));
if (trace.readOnly) return { action: "allow", reason: "match" };
if (req.mode === "shell" && req.env.CLOUDPIN_CONFIRM === environment.name) {
if (req.mode === "shell" && env.CLOUDPIN_CONFIRM === environment.name) {
return { action: "allow", reason: "confirmed" };
}
return { action: "confirm", provider, configPath: config.path, environment };
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async function checkCommand(
{
bin: inv.bin,
args: inv.args,
env: envFor(env, inv, { agent: true }),
env: envFor(env, inv, { agent: true, platform: deps.platform }),
cwd: inv.cwd ?? call.cwd,
mode: "agent",
},
Expand Down
15 changes: 8 additions & 7 deletions src/providers/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,20 @@ export const cloudflare: ProviderDef<"cloudflare"> = {
// The flags resolve reads (#160).
targetFlags: ["--cwd", "--config", "-c", "--env", "-e", "--env-file", "--envFile", "--profile"],

cacheInputs(env) {
cacheInputs(env, platform) {
// Only `wrangler whoami` is cached. Its OAuth logins live in the global
// Wrangler config folder (~/.wrangler if it exists, else the XDG one):
// config/<profile>.toml or .enc, and the folder bindings that pick one.
// WRANGLER_API_ENVIRONMENT renames the default profile's file. XDG_CONFIG_HOME
// and APPDATA move the folder, also when a .env file sets them.
// and APPDATA move the folder, also when a .env file sets them. The XDG
// folder differs per platform (wranglerConfigDirs), so it is `platform`'s (#136).
return {
// Every variable that decides the account, not only whoami's credentials:
// guard blocks when a command line sets one to a value it can't know (#86,
// #119). DOTENV_KEY picks a .env.vault over .env (wrangler-dotenv.ts). Names
// whoami ignores can only make its cache miss, never hit wrongly.
env: [...DOTENV_VARS, "DOTENV_KEY"],
...profileCacheInputs(env),
...profileCacheInputs(env, platform),
};
},

Expand All @@ -214,25 +215,25 @@ export const cloudflare: ProviderDef<"cloudflare"> = {
return false;
},

async resolve({ args, env: commandEnv, cwd }, exec): Promise<Resolution> {
async resolve({ args, env: commandEnv, cwd, platform }, exec): Promise<Resolution> {
if (hasAnyFlag(args, ["--temporary"])) {
return { kind: "error", message: "deploy --temporary uploads to a temporary preview account, not a pinned one" };
}
const start = resolvePath(cwd, flagValue(args, ["--cwd"]) ?? ".");
// Wrangler adds what its .env files set to its own environment (#116); from
// here on, env is that environment, and whoami below is asked with it.
const dotenv = wranglerDotenv(args, commandEnv, start);
const dotenv = wranglerDotenv(args, commandEnv, start, platform);
if ("error" in dotenv) return { kind: "error", message: dotenv.error };
const { env } = dotenv;
const inFile = (name: string) => (dotenv.from[name] ? ` in ${dotenv.from[name]}` : "");
const given = flagValue(args, ["--config", "-c"]);
const configPath = given !== undefined ? resolvePath(start, given) : CONFIG_NAMES.map((n) => findUp(n, start)).find(Boolean);
const envName = flagValue(args, ["--env", "-e"]) ?? (env.CLOUDFLARE_ENV || undefined);
// The folder whose binding picks the profile: the --config file's, else the command's.
const folder = bindingFolder(start);
const folder = bindingFolder(start, platform);
const lookup = given !== undefined ? dirname(resolvePath(folder, given)) : folder;
let known: ReturnType<typeof commandProfile> | undefined;
const profile = () => (known ??= commandProfile(args, env, lookup));
const profile = () => (known ??= commandProfile(args, env, lookup, platform));

if (configPath !== undefined) {
if (!existsSync(configPath)) return { kind: "error", message: `the Wrangler config file ${basename(configPath)} does not exist` };
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface CommandContext {
cwd: string;
/** The command name, e.g. "kubectl" or "helm", for providers that guard several CLIs. */
bin?: string;
/** The platform the command runs on; providers read it, never process.platform, so tests can set it (#136). */
platform?: NodeJS.Platform;
}

/** The identity a command will act as, keyed like the provider's pin section. */
Expand Down Expand Up @@ -63,8 +65,8 @@ export interface ProviderDef<P extends Provider = Provider> {
switchHint(pin: NonNullable<Pins[P]>): string;
/** Command that shows the CLI's login state, suggested when resolving fails. */
statusCommand: string;
/** Environment variables and files that decide the identity (cache invalidation). */
cacheInputs(env: NodeJS.ProcessEnv): CacheInputs;
/** Environment variables and files that decide the identity (cache invalidation), on `platform` (default: this process's). */
cacheInputs(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform): CacheInputs;
/**
* Every flag `resolve` reads (the ones that pick the account, project, scope, context, ...). A value
* cloudpin can't read in advance in one of them means it can't tell which account the command uses,
Expand Down
13 changes: 10 additions & 3 deletions src/user-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ const KEPT_BY_ENV_I = ["PATH", "SYSTEMROOT", "WINDIR"];
* The environment a call runs with: `base` (the environment the line starts
* in, or almost nothing after `env -i`), then the line's plain assignments to
* names already exported, its `unset`s, and its exported and prefix variables.
* With `agent`, the line can't set or unset the user-only variables.
* With `agent`, the line can't set or unset the user-only variables, in any
* spelling on Windows (`platform`, default this process's), where guard reads
* `cloudpin_env` as CLOUDPIN_ENV (#136).
*/
export function envFor(base: NodeJS.ProcessEnv, inv: Invocation, opts: { agent?: boolean } = {}): NodeJS.ProcessEnv {
const allowed = (name: string) => !(opts.agent && USER_ONLY_VARS.includes(name));
export function envFor(
base: NodeJS.ProcessEnv,
inv: Invocation,
opts: { agent?: boolean; platform?: NodeJS.Platform } = {},
): NodeJS.ProcessEnv {
const win = (opts.platform ?? process.platform) === "win32";
const allowed = (name: string) => !(opts.agent && USER_ONLY_VARS.includes(win ? name.toUpperCase() : name));
const kept = ([name]: [string, unknown]) => !allowed(name) || KEPT_BY_ENV_I.includes(name.toUpperCase());
const out: NodeJS.ProcessEnv = inv.clearEnv ? Object.fromEntries(Object.entries(base).filter(kept)) : { ...base };
for (const [name, value] of Object.entries(inv.ifExported ?? {})) if (name in base && allowed(name)) out[name] = value;
Expand Down
Loading
Loading