Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All notable changes to cloudpin are listed here. The format follows

### Fixed

- Variables an agent sets earlier in its command line now count (#86). `export AWS_PROFILE=other; aws ...` and
PowerShell `$env:AWS_PROFILE = 'other'; aws ...` were checked against the default account and ran on another; and
`GH_TOKEN=$(cat other.token) gh ...` read the token as empty. cloudpin now follows `export`, `unset` and `$env:`
through the line, and stops a command whose account depends on a value only known when it runs, saying why. Found
in the threat-model review.

- Cursor: a command on your Cursor allowlist runs again when the account matches (#2). cloudpin answered `ask`, which
stopped even allowlisted commands; it now answers `allow`, which Cursor treats exactly like having no hook (measured
in Cursor CLI 2026.10.01: commands not on the allowlist still need your approval). Wrong accounts are still blocked.
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ serves paid API keys and enterprise licenses, so both are supported.
cloudpin reads the whole command line the agent is about to run, so it also catches
`cd ../other && npx vercel deploy`, `bash -c "..."`, pipelines, `$( )`, `xargs`, `find -exec`, heredocs fed to a shell
(`bash <<EOF`), commands held in a variable (`CLI=vercel; $CLI deploy`) and PowerShell assignments
(`$prs = gh pr list`). It can't see inside a script file the agent writes and runs later (`bash deploy.sh`).
(`$prs = gh pr list`). Variables set earlier in the line count too (`export AWS_PROFILE=other; aws ...`,
`$env:AWS_PROFILE = 'other'`), and when one that decides the account is set from something only known when it runs
(`GH_TOKEN=$(cat file) gh ...`), the command is stopped with the reason. It can't see inside a script file the agent
writes and runs later (`bash deploy.sh`).

## Configuration

Expand Down
9 changes: 6 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ function dirStamp(path: string): string[] {
}
}

/** Whether `key` is one of `names`, where a trailing "*" matches a prefix (as in CacheInputs.env). */
export function envNameMatches(names: string[], key: string): boolean {
return names.some((n) => (n.endsWith("*") ? key.startsWith(n.slice(0, -1)) : key === n));
}

function relevantEnv(names: string[], env: NodeJS.ProcessEnv): [string, string][] {
return Object.entries(env)
.filter(([key, value]) =>
value !== undefined && names.some((n) => (n.endsWith("*") ? key.startsWith(n.slice(0, -1)) : key === n)),
)
.filter(([key, value]) => value !== undefined && envNameMatches(names, key))
.map(([key, value]) => [key, value as string] as [string, string])
.sort(([a], [b]) => a.localeCompare(b));
}
Expand Down
7 changes: 3 additions & 4 deletions src/explain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ActiveEnvironment } from "./config.js";
import { commandName, guard, type GuardDeps, type Trace, type Verdict } from "./guard.js";
import { AGENTS, type AgentName, withoutUserOnlyVars } from "./hooks/agents.js";
import { AGENTS, type AgentName } from "./hooks/agents.js";
import { home } from "./paths.js";
import { findInvocations, type Invocation } from "./shellwords.js";
import { envFor, findInvocations, type Invocation } from "./shellwords.js";
import { describe } from "./status.js";
import type { Identity } from "./types.js";

Expand Down Expand Up @@ -109,10 +109,9 @@ export async function explain(command: string | string[], opts: ExplainOptions,
if (wrapped.length === 0) continue;
call = { ...call, bin: wrapped[0]!, args: wrapped.slice(1) };
}
const own = opts.mode === "agent" ? withoutUserOnlyVars(call.env) : call.env;
const cwd = call.cwd ?? opts.cwd;
const trace: Trace = {};
const verdict = await guard({ bin: call.bin, args: call.args, env: { ...opts.env, ...own }, 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" }), cwd, mode: opts.mode }, deps, trace);
const res = trace.resolution;
out.push({
argv: [call.bin, ...call.args],
Expand Down
21 changes: 19 additions & 2 deletions src/guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { basename } from "node:path";
import { type ActiveEnvironment, ConfigError, findConfig, type FoundConfig, type Provider } from "./config.js";
import { envNameMatches } from "./cache.js";
import { isReadOnly } from "./readonly.js";
import type { Exec, ProviderDef, Resolution } from "./types.js";
import { type Exec, type ProviderDef, type Resolution, UNKNOWN_VALUE } from "./types.js";

export interface GuardRequest {
/** The executable as typed or resolved, e.g. "az" or "C:\\...\\gh.exe". */
Expand Down Expand Up @@ -126,7 +127,23 @@ export async function guard(req: GuardRequest, deps: GuardDeps, trace: Trace = {
if (result.notFound) cliMissing = true;
return result;
};
const res = await provider.resolve({ args: req.args, env: req.env, cwd: req.cwd, bin: name }, exec);
// 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(req.env).env;
const unknown = Object.keys(req.env).filter((k) => req.env[k] === UNKNOWN_VALUE && envNameMatches(identityVars, k));
if (unknown.length > 0) {
return {
...base,
uncertain: true,
problems: [
`${unknown.join(", ")} ${unknown.length > 1 ? "are" : "is"} set by the command line from something cloudpin can't read in advance ($(...), backticks or another variable), so it can't tell which account this command would use`,
],
fix: `set ${unknown[0]} to a literal value, or run the command yourself`,
};
}
// Other unknown values don't decide the account; the CLI's identity check runs without them.
const env = Object.fromEntries(Object.entries(req.env).filter(([, v]) => v !== UNKNOWN_VALUE));
const res = await provider.resolve({ args: req.args, env, cwd: req.cwd, bin: name }, exec);
trace.resolution = res;
if (res.kind === "error" && cliMissing) return { action: "allow", reason: "not-installed" };
switch (res.kind) {
Expand Down
13 changes: 2 additions & 11 deletions src/hooks/agents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from "node:path";
import { formatBlock, formatConfirm } from "../format.js";
import { commandName, guard, type GuardDeps } from "../guard.js";
import { findInvocations } from "../shellwords.js";
import { envFor, findInvocations } from "../shellwords.js";

/** How one agent hands a shell command to its hook and reads the answer. */
export interface AgentHook {
Expand Down Expand Up @@ -144,15 +144,6 @@ export const AGENTS = {
},
} satisfies Record<string, AgentHook>;

// Set by the user before starting the agent, these count; set by the agent
// inside its own command, they would let it pick a laxer environment, confirm
// for the user or skip the check, so they are dropped.
const USER_ONLY_VARS = ["CLOUDPIN_ENV", "CLOUDPIN_CONFIRM", "CLOUDPIN_SKIP"];

export function withoutUserOnlyVars(vars: Record<string, string>): Record<string, string> {
return Object.fromEntries(Object.entries(vars).filter(([k]) => !USER_ONLY_VARS.includes(k)));
}

/** A request for the user's OK: the text an agent asks with, or the one it is denied with. */
interface Confirmation {
ask: string;
Expand Down Expand Up @@ -247,7 +238,7 @@ async function checkCommand(
{
bin: inv.bin,
args: inv.args,
env: { ...env, ...withoutUserOnlyVars(inv.env) },
env: envFor(env, inv, { agent: true }),
cwd: inv.cwd ?? call.cwd,
mode: "agent",
},
Expand Down
Loading
Loading