A credential reaches the command that needs it without its value reaching the agent: one compiled binary, qsm-secrets, that resolves a key from a Bitwarden…

Lets the agent use a credential without ever reading it: the value goes from your secret store to the command that needs it.
A token that enters the agent's context is a token in a transcript, a log, and every model call after it. qsm-secrets exists so the agent can use a credential without ever reading it: the secret flows from the store into the command's environment or pipe, and the agent sees only that it was there. It takes the stance that the agent has no need to know a secret's value, only that the task ran. It refuses to be a password manager or a place where secrets are typed. When the harness carries secrets to commands on its own, this plugin can go.
| Kind | Name | What it does |
|---|---|---|
| Skill | qsm-secrets | Fires when a task needs a secret, and routes it through the wrapper. |
| Skill | content-denylist | Builds a machine-local gitleaks rule list of employer, client and internal names, after you approve it. |
| Hook | tool.call (guard) | Denies a Bash command that reads a stored value outside the wrapper, read from its syntax tree through sudo, env, xargs, flock, find -exec, qsm-secrets env -- <cmd>, sh -c, subshells and pipelines, with a code and the verb to use instead. It also denies a placeholder inside single quotes. |
| Hook | tool.call (Read, Grep) | Denies reading the keyring files, the vault's data directory and the rendered config directory. |
| Hook | tool.call (use by id) | Runs a command holding {{secret:<id>}} with the value in one environment variable. |
| Hook | session.append | Masks every stored value in every row the conversation keeps (tool results, file reads, responses) before it is stored; a row it cannot mask is withheld. |
| Hook | prompt.submit | Stores a pasted secret under cap_<hex> (or under <name> for a pasted name=value) and gives the model a placeholder; a prompt it cannot check is not sent. |
| Hook | session.end | Scrubs every stored value from the transcript and its subagents' and removes rendered files. |
| CLI | qsm-secrets | 20 verbs, see USAGE.md. |
| Pane | qml/askpass/ | A Quickshell pane that asks the person for a secret, a sudo -A, ssh or git answer, or an allow or deny, and returns it to the asker. |
claude plugin install qsm-secrets@qsm-marketplace
bun plugins/qsm-secrets/scripts/install.ts
qsm-setup activate qsm-secrets -- --backend bitwarden # or libsecret
qsm-secrets doctor
Install compiles qsm-secrets into $QSM_BIN_DIR (default ~/.local/bin) and runs cargo install rbw when rbw is missing (minutes). The backend is Bitwarden through rbw, or libsecret through secret-tool, strictly local. Nothing works until you choose one. See INSTALL.md.
qsm-secrets set github-token # you type the value at the backend's prompt
qsm-secrets set github-token --gui # you type it in the askpass pane instead
qsm-secrets askpass enable --sudo --git # prints the fish lines for SUDO_ASKPASS and GIT_ASKPASS
qsm-secrets pipe github-token -- gh auth login --with-token # value on stdin
qsm-secrets env npm-token=NPM_TOKEN -- npm publish # value in one variable
qsm-secrets list # key names of every namespace, never a value
qsm-secrets check npm-token=NPM_TOKEN -- npm whoami # does the stored token still work: ok or failed
qsm-secrets rename old-name new-name # value never printed, old name removed last
qsm-secrets prune --dry-run # captures no transcript names; drop --dry-run to remove
qsm-secrets render ./app.env.tpl --out app.env # fills {{secret:<id>}}, prints the path
Paste a token into a prompt and the hook stores it. The model reads {{secret:cap_<hex>}} instead of the value. Paste deploy-key=<token> and it is stored as the key deploy-key in the qsm namespace, and the model reads {{secret:qsm/deploy-key}}; a name that is already stored is never overwritten, the value gets a cap_<hex> and the toast says why.
qsm-secrets guard once per Bash call, about 90 ms with the compiled binary. A command it cannot parse (zsh-only syntax such as *(.)) is refused with the cause; rewrite it in bash syntax. A guard that cannot answer denies every Bash call except qsm-secrets doctor and the install script, so a broken install can be repaired.token=, secret:).prune trusts the transcripts: run it after your sessions end, since a capture made in a live session is named in its transcript only once the prompt is rewritten.get <key> 3>FILE is a last resort and leaves a secret on disk until you remove it.set needs a terminal, or --gui, and stores one line only.qsm (--folder or QSM_SECRETS_FOLDER); list with neither shows every namespace. A libsecret entry stored before namespaces existed fails list and mask until qsm-secrets rename <key> <new> --legacy moves it into one.qs on PATH) as an instance of its own, started on demand and addressed by its config path. Without qs or the installed pane the call answers missing; there is no terminal fallback. 120 seconds without an answer, or Escape, is failed with the reason.~/.config/qsm-marketplace/colors.json, which install writes once from the defaults and never replaces; an open pane takes an edit at once.qs ipc call, which any process of your user can read, so one outside the guard can write a forged answer to the reply socket. The guard refuses an agent's Bash that calls the pane's target, runs a qsm-askpass-<kind> link or qsm-secrets askpass <kind> itself, or names the socket directory; a determined agent can still route around it. Peer credentials on the reply socket are not checked.SSH_ASKPASS_PROMPT=confirm ssh gets a yes or no (Allow is exit 0), with none the pane shows the prompt as a notice.missing. It never falls back to Bitwarden.ghp_, glpat-, sk-, AKIA, xox, JWT), a value after a credential name (a password from 6 characters), or a 32-character token that is random for its alphabet and is not a path, a name, an id, a hash or base64 of text. On a labelled corpus the test holds precision 1.0 and recall of at least 0.95; that guards a regression and is not the precision on your own text. The model also sees the last four characters when the secret has 20 or more.render writes to $XDG_RUNTIME_DIR/qsm-secrets/<session>/<name> (directory 0700, file 0600). The session end removes it.${XDG_CONFIG_HOME:-~/.config}/qsm/content-denylist.toml, never in a repository.hooks/register.tsx 452 lines1// qsm-secrets' function-hooks module: the noun, `$.secrets`, one method per verb, and the guard that
2// refuses a Bash command that would print a stored credential into the tool result (the decision is
3// `qsm-secrets guard`, which reads the command's syntax tree with the WASM parser compiled into the
4// binary, because this sandbox cannot load it), a Read or Grep of the files that hold credentials, and the capture of a pasted secret
5// before the prompt enters the session (the decision is `qsm-secrets capture`), and the use of a
6// captured id: replaced in the command by substitute, masked in the result by `qsm-secrets mask`.
7import type { ApiContentBlock, EngineInterface, On, SessionAppendInput } from "claude-code";
8import { type PathEnv, protectedRoots, reaches } from "../lib/protected.ts";
9import { MIN_MASKED, mapStrings } from "../lib/masking.ts";
10import { substitute } from "../lib/substitute.ts";
11import { argvOf, failure, registerNoun, unwrap, type VerbRow, verbs } from "./noun.generated.ts";
12
13// The guard runs before every Bash call, so a parse that hangs must not hold the call for long.
14const GUARD_TIMEOUT_MS = 5000;
15
16// A backend that wants an unlock prompt must not hold the prompt for long.
17const CAPTURE_TIMEOUT_MS = 10_000;
18// Where the hooks run the bin: it reads stdin only, and the session's directory can be gone (a
19// removed worktree), which fails every spawn with ENOENT and withholds every row of the chat.
20const RUN_DIR = "/";
21
22// What a person typed or pasted: the terminal, the remote bridge, the SDK host, a channel's human. Every
23// other origin is text the engine, another session or a model wrote (a task notification, a subagent
24// hand-back, a schedule): it holds ids and paths that look like tokens, and a capture would replace them.
25const PERSON_ORIGINS: ReadonlySet<string> = new Set(["composer", "bridge", "sdk", "channel"]);
26
27/** What a failed hook says about itself in the log: the kind and the message, which names the verb,
28 * the exit code, the outcome code or stderr; never a stdout, which may hold the prompt. */
29function cause(error: { readonly kind: string; readonly message?: string }): string {
30 return `${error.kind}: ${(error.message ?? "no message").slice(0, 400)}`;
31}
32
33// differs: hooks sandbox, which imports no root lib
34const isRecord = (value: unknown): value is Record<string, unknown> =>
35 typeof value === "object" && value !== null && !Array.isArray(value);
36
37const isStrings = (value: unknown): value is string[] =>
38 Array.isArray(value) && value.every((item: unknown) => typeof item === "string");
39
40const isBlocks = (value: unknown): value is ApiContentBlock[] =>
41 Array.isArray(value) &&
42 value.every((item: unknown) => isRecord(item) && typeof item.type === "string");
43
44interface MaskData {
45 readonly texts: string[];
46}
47
48interface CaptureData {
49 readonly text: string;
50 readonly captured: readonly { readonly id: string }[];
51 readonly taken: string[];
52}
53
54const isMaskData = (value: unknown): value is MaskData => isRecord(value) && isStrings(value.texts);
55
56const isCaptureData = (value: unknown): value is CaptureData =>
57 isRecord(value) &&
58 typeof value.text === "string" &&
59 Array.isArray(value.captured) &&
60 value.captured.every((item: unknown) => isRecord(item) && typeof item.id === "string") &&
61 isStrings(value.taken);
62
63interface Envelope {
64 readonly code: string;
65 readonly message: string | undefined;
66 readonly reason: string | undefined;
67}
68
69/** The outcome envelope a binary printed on stdout, or undefined when stdout is not one. */
70function envelopeOf(stdout: string): Envelope | undefined {
71 let parsed: unknown;
72 try {
73 parsed = JSON.parse(stdout);
74 } catch {
75 return undefined;
76 }
77 if (!isRecord(parsed) || typeof parsed.exit !== "number" || typeof parsed.code !== "string") {
78 return undefined;
79 }
80 return {
81 code: parsed.code,
82 message: typeof parsed.message === "string" ? parsed.message : undefined,
83 reason:
84 isRecord(parsed.data) && typeof parsed.data.reason === "string"
85 ? parsed.data.reason
86 : undefined,
87 };
88}
89
90/** A verb's data from a `process.run` result, checked by `isData`. unwrap quotes a stdout that is no
91 * envelope, which for capture and mask can be the prompt or the result, so that case is rebuilt from
92 * exit code and stderr. */
93function dataOf<T>(
94 argv: string[],
95 ran: { exitCode: number; stdout: string; stderr: string },
96 isData: (value: unknown) => value is T,
97): T {
98 if (envelopeOf(ran.stdout) === undefined) {
99 const stderr = ran.stderr.trim().slice(0, 300);
100 throw new Error(
101 `${argv.slice(0, 2).join(" ")} printed no outcome envelope (exit ${ran.exitCode}): ${stderr.length > 0 ? stderr : "no stderr"}`,
102 );
103 }
104 const data = unwrap(argv, ran);
105 if (!isData(data)) {
106 throw new Error(`${argv.slice(0, 2).join(" ")} answered data of an unexpected shape`);
107 }
108 return data;
109}
110
111/** The row of a verb the noun was generated with. */
112function verbOf(name: string): VerbRow {
113 const row = verbs[name];
114 if (row === undefined) {
115 throw new Error(`qsm.json has no verb ${name}`);
116 }
117 return row;
118}
119
120/** What `qsm-secrets guard` said: the reason to deny with, or null when the command may run. A verb
121 * that did not answer is an error, and the hook's catch denies the call. */
122function refusalOf(ran: { exitCode: number; stdout: string; stderr: string }): string | null {
123 const envelope = envelopeOf(ran.stdout);
124 if (envelope === undefined) {
125 const stderr = ran.stderr.trim().slice(0, 300);
126 throw new Error(
127 `qsm-secrets guard printed no outcome envelope (exit ${ran.exitCode}): ${stderr.length > 0 ? stderr : "no stderr"}`,
128 );
129 }
130 if (envelope.code === "ok") {
131 return null;
132 }
133 if (envelope.code === "blocked" && envelope.reason !== undefined) {
134 return envelope.reason;
135 }
136 throw new Error(
137 `qsm-secrets guard answered ${envelope.code}: ${(envelope.message ?? "").slice(0, 200)}`,
138 );
139}
140
141// The one command a broken guard lets through, so the cause can be read: it changes nothing and
142// reads no value. Reinstalling is the person's step in their own terminal, never the agent's: an
143// allowed path to an install script is a path the agent could write its own script to.
144const RECOVERY = /^\s*qsm-secrets doctor\s*$/;
145
146/** The guard runs first, on the command as written, so it sees a placeholder before the substitution
147 * rewrites it. */
148function registerGuard(on: On): void {
149 on("tool.call", { tool: "Bash" }, async ($, e, next) => {
150 const argv = argvOf(verbOf("guard"));
151 const ran = await $.process.run(argv, {
152 stdin: e.command,
153 timeoutMs: GUARD_TIMEOUT_MS,
154 cwd: RUN_DIR,
155 });
156 const reason = refusalOf(ran);
157 if (reason === null) {
158 return next(e);
159 }
160 $.ui.log(`refused a Bash command: ${reason.slice(0, 80)}`);
161 return { deny: reason };
162 }).catch(($, e, next) => {
163 failure($, "tool.call", next.error);
164 const reason = cause(next.error);
165 if (RECOVERY.test((e as { command?: string }).command ?? "")) {
166 $.ui.log("the credential guard failed; a recovery command was let through");
167 return next(e);
168 }
169 $.ui.log("the credential guard failed; the call was denied");
170 return { deny: `qsm-secrets: the credential guard failed (${reason}); the call was not run` };
171 });
172}
173
174/** Read and Grep put a file's lines into the tool result as surely as `cat` does, and a Glob names
175 * the files of the store. A Grep also walks everything under its path, so a path above a
176 * protected directory is refused too; a Glob lists names only, like `ls`, so only a path inside one is. */
177function registerFileGuard(on: On): void {
178 // The shipped types list no Grep or Glob (a build without the tool has nothing to guard), so their
179 // names are cast; a build that has the tool routes its calls here.
180 const guarded = [
181 { tool: "Read" as const, ancestors: false, globs: false },
182 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the engine's declared tool names list no Grep
183 { tool: "Grep" as "Read", ancestors: true, globs: false },
184 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the engine's declared tool names list no Glob
185 { tool: "Glob" as "Read", ancestors: false, globs: true },
186 ];
187 for (const { tool, ancestors, globs } of guarded) {
188 on("tool.call", { tool }, async ($, e, next) => {
189 const input = e as { file_path?: string; path?: string; pattern?: string };
190 // A Grep or a Glob with no path searches the working directory, so that is its path.
191 const target =
192 input.file_path ?? input.path ?? (ancestors || globs ? await $.session.cwd() : undefined);
193 if (target === undefined) {
194 return next(e);
195 }
196 const [home, dataHome, runtimeDir, passwordStore] = await Promise.all([
197 $.env.get("HOME"),
198 $.env.get("XDG_DATA_HOME"),
199 $.env.get("XDG_RUNTIME_DIR"),
200 $.env.get("PASSWORD_STORE_DIR"),
201 ]);
202 const env: PathEnv = { home, dataHome, runtimeDir, passwordStore };
203 const roots = protectedRoots(env);
204 let real: string | undefined;
205 try {
206 real = (await $.fs.stat(target, { resolve: true })).realPath;
207 } catch {
208 // a path that is not there is judged by its spelling
209 }
210 // A Glob's own pattern may start at a protected directory (`~/.password-store/**`): its part
211 // before the first wildcard is a path too.
212 const written = globs ? input.pattern?.split(/[*?[{]/)[0] : undefined;
213 const lead =
214 written !== undefined && written !== "" && !/^[/~$]/.test(written)
215 ? `${target}/${written}`
216 : written;
217 const spellings = [target, real, lead].filter(
218 (path): path is string => path !== undefined && path !== "",
219 );
220 if (!spellings.some((path) => reaches(path, roots, env, ancestors) !== undefined)) {
221 return next(e);
222 }
223 $.ui.log(`refused a ${tool} of ${target}`);
224 return {
225 deny: `[qsm-secrets keyring-files] ${tool} of ${target} would put stored credentials, or the names of the store's files, in this tool result. Use \`qsm-secrets pipe <key> -- <cmd>\` or \`qsm-secrets env <key>=<VAR> -- <cmd>\`; for a rendered config, read its template, which holds placeholders.`,
226 };
227 }).catch(($, _e, next) => {
228 failure($, "tool.call", next.error);
229 const reason = cause(next.error);
230 $.ui.log("the file guard failed; the call was denied");
231 return { deny: `qsm-secrets: the file guard failed (${reason}); the call was not run` };
232 });
233 }
234}
235
236/** A command naming an id runs with the value in its environment; its result is masked on the way
237 * into the conversation, by the session.append hook. */
238function registerSubstitution(on: On): void {
239 on("tool.call", { tool: "Bash" }, (_$, e, next) => {
240 const { command, ids } = substitute(e.command);
241 return ids.length === 0 ? next(e) : next({ ...e, command });
242 }).catch(($, _e, next) => {
243 failure($, "tool.call", next.error, next.error.kind);
244 $.ui.log("the id substitution failed; the call was denied");
245 return {
246 deny: `qsm-secrets: the id substitution failed (${cause(next.error)}); the call was not run`,
247 };
248 });
249}
250
251/**
252 * Rewrites every text of a row's content, in a fixed order: a text block, a tool_result's string
253 * content, and the text blocks inside a tool_result's content. Media and other blocks are kept.
254 */
255function mapTexts(content: ApiContentBlock[], map: (text: string) => string): ApiContentBlock[] {
256 const mapBlock = (block: ApiContentBlock): ApiContentBlock => {
257 if (block.type === "text" && typeof block.text === "string") {
258 return { ...block, text: map(block.text) };
259 }
260 if (block.type === "tool_result" && typeof block.content === "string") {
261 return { ...block, content: map(block.content) };
262 }
263 if (block.type === "tool_result" && isBlocks(block.content)) {
264 return { ...block, content: block.content.map(mapBlock) };
265 }
266 return block;
267 };
268 return content.map(mapBlock);
269}
270
271function withheld(reason: string): string {
272 return `qsm-secrets: this text could not be masked and was withheld (${reason.slice(0, 300)})`;
273}
274
275function rewritten(e: SessionAppendInput, content: ApiContentBlock[]): SessionAppendInput {
276 return { ...e, message: { ...e.message, content } };
277}
278
279/**
280 * Masks every text a walk visits through one `qsm-secrets mask --all`, by running the walk twice:
281 * once to read the texts, once to put the masked ones back in the same order. Undefined when the
282 * mask changed nothing, so the caller keeps what it had.
283 */
284async function maskWalk<T>(
285 $: EngineInterface,
286 walk: (map: (text: string) => string) => T,
287): Promise<T | undefined> {
288 const texts: string[] = [];
289 walk((text) => {
290 texts.push(text);
291 return text;
292 });
293 // A text shorter than the shortest masked value cannot hold one.
294 if (texts.every((text) => text.length < MIN_MASKED)) {
295 return undefined;
296 }
297 const argv = argvOf(verbOf("mask"), { all: true });
298 const ran = await $.process.run(argv, {
299 stdin: JSON.stringify(texts),
300 timeoutMs: CAPTURE_TIMEOUT_MS,
301 cwd: RUN_DIR,
302 });
303 const done = [...dataOf(argv, ran, isMaskData).texts];
304 if (done.length !== texts.length) {
305 throw new Error(`the mask returned ${done.length} texts for ${texts.length}`);
306 }
307 if (done.every((text, i) => text === texts[i])) {
308 return undefined;
309 }
310 return walk(() => done.shift() ?? "");
311}
312
313/** A tool's own record is stored in the transcript as made, beside the row the session.append hook
314 * masks, so the record is masked here, before the engine keeps it. */
315function registerResultMask(on: On): void {
316 on("tool.call", async ($, e, next) => {
317 const out = await next(e);
318 // A refusal quotes only the input the model wrote, so it holds no value the model has not
319 // already sent.
320 if (out.deny !== undefined) {
321 return out;
322 }
323 if (out.isError === true) {
324 // An errored call is kept as its error text in `result`, and `text` is what the model reads.
325 // A hook cannot answer an error, because the engine checks its result against the tool's
326 // output shape, so a masked error comes back as a refusal that carries the masked text.
327 const masked = await maskWalk($, (map) => ({
328 text: map(out.text ?? ""),
329 result: mapStrings(out.result, map),
330 }));
331 if (masked === undefined) {
332 return out;
333 }
334 const result = typeof masked.result === "string" ? masked.result : "";
335 return { deny: masked.text === "" ? result : masked.text };
336 }
337 // The context other hooks added is passed on whole, because the engine refuses a hook that
338 // rewrites an entry its next gave.
339 const result = await maskWalk($, (map) => mapStrings(out.result, map));
340 return result === undefined ? out : { result, context: out.context };
341 }).catch(($, _e, next) => {
342 // Fails closed: a record that could not be masked may hold a value, so it is withheld.
343 failure($, "tool.call", next.error, "a tool result could not be masked and was withheld");
344 $.ui.log("the result mask failed; the tool result was withheld");
345 return { deny: withheld(cause(next.error)) };
346 });
347}
348
349/** Every row the conversation keeps passes through one mask before it is stored. A tool's result is
350 * made from the record the tool.call hook already masked, so its row is not masked twice. */
351function registerMask(on: On): void {
352 on("session.append", async ($, e, next) => {
353 if (e.door === "tool-result") {
354 return next(e);
355 }
356 const content = await maskWalk($, (map) => mapTexts(e.message.content, map));
357 return next(content === undefined ? e : rewritten(e, content));
358 }).catch(($, e, next) => {
359 // Fails closed: a row that could not be masked may hold a value, so its text is replaced.
360 failure($, "session.append", next.error, "a row could not be masked and was withheld");
361 $.ui.log("the mask failed; the row's text was withheld");
362 const notice = withheld(cause(next.error));
363 return next(
364 rewritten(
365 e,
366 mapTexts(e.message.content, (text) => (text === "" ? text : notice)),
367 ),
368 );
369 });
370}
371
372/** The session is over: its rendered files go, and a transcript holds the pasted prompt raw (the
373 * engine writes it before the capture runs), so the value in it is swapped for its placeholder. */
374function registerScrub(on: On): void {
375 on("session.end", async ($, e, next) => {
376 const session = await $.session.id();
377 // The scrub first: a failed clean must not leave the transcript holding a pasted value. A failed
378 // scrub must not leave the rendered files behind either, so each step runs and reports alone.
379 const failed: string[] = [];
380 for (const step of [
381 () => $.secrets.scrub({ transcript: session, detach: true }),
382 () => $.secrets.clean({ session }),
383 ]) {
384 try {
385 await step();
386 } catch (error) {
387 failed.push(
388 cause({
389 kind: "failed",
390 // differs: hooks sandbox, root lib's messageOf
391 message: error instanceof Error ? error.message : String(error),
392 }),
393 );
394 }
395 }
396 if (failed.length > 0) {
397 throw new Error(failed.join("; "));
398 }
399 return next(e);
400 }).catch(($, e, next) => {
401 failure($, "session.end", next.error, "the cleanup or the scrub did not finish");
402 $.ui.log("the session-end cleanup failed; the session ended");
403 return next(e);
404 });
405}
406
407export function register(on: On): void {
408 // Adds $.secrets: one method per qsm-secrets verb, generated by lib/generate-noun.ts. Never
409 // hand-edit noun.generated.ts or types/index.d.ts.
410 registerNoun(on);
411 registerGuard(on);
412 registerFileGuard(on);
413 registerSubstitution(on);
414 registerResultMask(on);
415 registerMask(on);
416 registerScrub(on);
417
418 on("prompt.submit", async ($, e, next) => {
419 if (!PERSON_ORIGINS.has(e.origin.kind)) {
420 $.ui.log(`a ${e.origin.kind} prompt is not the person's own; it was not captured`, {
421 to: "debug",
422 });
423 return next(e);
424 }
425 const argv = argvOf(verbOf("capture"));
426 const ran = await $.process.run(argv, {
427 stdin: e.text,
428 timeoutMs: CAPTURE_TIMEOUT_MS,
429 cwd: RUN_DIR,
430 });
431 const done = dataOf(argv, ran, isCaptureData);
432 if (done.captured.length === 0) {
433 return next(e);
434 }
435 const ids = done.captured.map((c) => c.id).join(", ");
436 const why = done.taken.map((name) => `; ${name} is already stored, so it got a generated id`);
437 $.ui.toast(`qsm-secrets: captured ${done.captured.length} secret(s) as ${ids}${why.join("")}`);
438 return next({ ...e, text: done.text });
439 }).catch(($, e, next) => {
440 // Fails closed: a prompt that could not be checked may hold a value, so it is not sent. The
441 // log names the cause, never the text.
442 failure($, "prompt.submit", next.error, "capture failed, the prompt was not sent");
443 const reason = cause(next.error);
444 $.ui.log("capture failed; the prompt was dropped");
445 return next.called
446 ? next(e)
447 : {
448 drop: `qsm-secrets: capture failed (${reason}); the prompt was not sent. Fix the cause (qsm-secrets doctor names it), then send the prompt again`,
449 };
450 });
451}
452lib/protected.ts 205 lines1// The places a stored credential lives on disk, as a pure function of the environment. Import-free,
2// because hooks/register.tsx imports it inside the engine sandbox and the guard verb imports it too.
3
4export interface PathEnv {
5 readonly home?: string | undefined;
6 /** `XDG_DATA_HOME` */
7 readonly dataHome?: string | undefined;
8 /** `XDG_RUNTIME_DIR` */
9 readonly runtimeDir?: string | undefined;
10 /** `PASSWORD_STORE_DIR`, where `pass` keeps its gpg files when that is not `~/.password-store` */
11 readonly passwordStore?: string | undefined;
12}
13
14export interface Root {
15 readonly path: string;
16 /** `store` holds credentials themselves; `rendered` holds config files with values filled in,
17 * which a command may be handed by path but never read; `askpass` holds the reply sockets of the
18 * askpass pane, which no command may name. */
19 readonly kind: "store" | "rendered" | "askpass";
20}
21
22/** The variables a path is resolved through; the same set `expand` substitutes and the guard watches. */
23export const PATH_VARIABLES: ReadonlySet<string> = new Set([
24 "HOME",
25 "XDG_DATA_HOME",
26 "XDG_RUNTIME_DIR",
27 "PASSWORD_STORE_DIR",
28]);
29
30/** The path variables of a process environment, as a `PathEnv`. */
31export function pathEnvOf(env: Readonly<Record<string, string | undefined>>): PathEnv {
32 return {
33 home: env.HOME,
34 dataHome: env.XDG_DATA_HOME,
35 runtimeDir: env.XDG_RUNTIME_DIR,
36 passwordStore: env.PASSWORD_STORE_DIR,
37 };
38}
39
40/** The keyring files, the rbw data directory, the pass store and the rendered config directory. */
41export function protectedRoots(env: PathEnv): Root[] {
42 const roots: Root[] = [];
43 const data = env.dataHome ?? (env.home === undefined ? undefined : `${env.home}/.local/share`);
44 if (data !== undefined) {
45 roots.push({ path: `${data}/keyrings`, kind: "store" }, { path: `${data}/rbw`, kind: "store" });
46 }
47 if (env.home !== undefined) {
48 roots.push({ path: `${env.home}/.password-store`, kind: "store" });
49 }
50 if (env.passwordStore !== undefined && env.passwordStore !== "") {
51 roots.push({ path: normalizePath(expand(env.passwordStore, env)), kind: "store" });
52 }
53 if (env.runtimeDir !== undefined && env.runtimeDir !== "") {
54 roots.push({ path: `${env.runtimeDir}/qsm-secrets`, kind: "rendered" });
55 }
56 return roots;
57}
58
59/** `~`, `$HOME`, `${HOME}` and the two XDG variables as the shell would expand them; a variable
60 * that is not set stays as written. */
61export function expand(text: string, env: PathEnv): string {
62 let out = env.home === undefined ? text : text.replace(/^~(?=\/|$)/, env.home);
63 const variables: [string, string | undefined][] = [
64 ["HOME", env.home],
65 ["XDG_DATA_HOME", env.dataHome],
66 ["XDG_RUNTIME_DIR", env.runtimeDir],
67 ["PASSWORD_STORE_DIR", env.passwordStore],
68 ];
69 for (const [name, value] of variables) {
70 if (value !== undefined) {
71 out = out
72 .replaceAll(`\${${name}}`, value)
73 .replaceAll(new RegExp(`\\$${name}\\b`, "g"), value);
74 }
75 }
76 return out;
77}
78
79/** `a//b`, `a/./b` and `a/x/../b` folded, so a spelling cannot hide a path. */
80export function normalizePath(path: string): string {
81 const absolute = path.startsWith("/");
82 const kept: string[] = [];
83 for (const part of path.split("/")) {
84 if (part === "" || part === ".") {
85 continue;
86 }
87 if (part === ".." && kept.length > 0 && kept.at(-1) !== "..") {
88 kept.pop();
89 } else if (part !== ".." || !absolute) {
90 kept.push(part);
91 }
92 }
93 return `${absolute ? "/" : ""}${kept.join("/")}`;
94}
95
96/** The root a text mentions, in the spelling it was written or folded. For a word of a command, so a
97 * root may sit anywhere in it (`open('<root>/x')` in a `python -c` body). */
98export function mentioned(text: string, roots: readonly Root[], env: PathEnv): Root | undefined {
99 const spelled = expand(text, env);
100 const folded = normalizePath(spelled);
101 return roots.find((root) => spelled.includes(root.path) || folded.includes(root.path));
102}
103
104/** The root a path lies inside, or, with `ancestors`, the root a search from that path would walk into. */
105export function reaches(
106 path: string,
107 roots: readonly Root[],
108 env: PathEnv,
109 ancestors: boolean,
110): Root | undefined {
111 const folded = normalizePath(expand(path, env));
112 return roots.find(
113 (root) =>
114 folded === root.path ||
115 folded.startsWith(`${root.path}/`) ||
116 (ancestors && (folded === "/" || root.path.startsWith(`${folded}/`))),
117 );
118}
119
120interface GlobAtom {
121 readonly pattern: RegExp;
122 readonly repeated: boolean;
123}
124
125/** Compile the component spelling into single-character atoms. Only an atom, never its repetition,
126 * becomes a regex, so character classes keep their meaning without backtracking over wildcards. */
127function partAtoms(part: string): GlobAtom[] | null {
128 const source = Array.from(part)
129 .map((char) => {
130 if (char === "*") {
131 return "[^/]*";
132 }
133 if (char === "?") {
134 return "[^/]";
135 }
136 return /[A-Za-z0-9_\-[\]]/.test(char) ? char : `\\${char}`;
137 })
138 .join("");
139 const atoms: GlobAtom[] = [];
140 for (let at = 0; at < source.length;) {
141 let end = at + 1;
142 if (source[at] === "\\") {
143 end += 1;
144 } else if (source[at] === "[") {
145 while (end < source.length && source[end] !== "]") {
146 end += source[end] === "\\" ? 2 : 1;
147 }
148 if (end === source.length) return null;
149 end += 1;
150 }
151 try {
152 const pattern = new RegExp(`^(?:${source.slice(at, end)})$`);
153 const repeated = source[end] === "*";
154 atoms.push({ pattern, repeated });
155 at = repeated ? end + 1 : end;
156 } catch {
157 return null;
158 }
159 }
160 return atoms;
161}
162
163/** Each table cell is visited once. A repeated atom consumes nothing or extends its own row by one
164 * character, rather than trying every split of the input among the wildcards. */
165function matchesGlobPart(part: string, text: string): boolean {
166 const atoms = partAtoms(part);
167 // Invalid components match only the empty component.
168 if (atoms === null) return text === "";
169 let row = Array.from({ length: text.length + 1 }, (_, at) => at === 0);
170 for (const atom of atoms) {
171 const next = row.map((matched) => atom.repeated && matched);
172 for (let at = 1; at <= text.length; at += 1) {
173 if (atom.pattern.test(text[at - 1] ?? "")) {
174 const prefix = atom.repeated ? next[at - 1] : row[at - 1];
175 next[at] = next[at] === true || prefix === true;
176 }
177 }
178 row = next;
179 }
180 return row[text.length] === true;
181}
182
183/** The root a glob word could expand to: `~/.local/share/keyr*\/login.keyring` reaches the keyring
184 * directory, because each leading part of the pattern may match the root's part at that depth. */
185export function globMentioned(
186 text: string,
187 roots: readonly Root[],
188 env: PathEnv,
189): Root | undefined {
190 if (!["*", "?", "["].some((char) => text.includes(char))) {
191 return undefined;
192 }
193 const parts = normalizePath(expand(text, env)).split("/");
194 return roots.find((root) => {
195 const rootParts = root.path.split("/");
196 return (
197 parts.length >= rootParts.length &&
198 rootParts.every((rootPart, at) => {
199 const part = parts[at];
200 return part !== undefined && matchesGlobPart(part, rootPart);
201 })
202 );
203 });
204}
205lib/masking.ts 46 lines1// What the mask and the scrub share. Import-free, because hooks/register.tsx imports it inside the
2// engine sandbox and lib/run.ts imports it too.
3
4/** A stored value shorter than this is not masked: it would match inside ordinary words. */
5export const MIN_MASKED = 6;
6
7/** Keys of a transcript line that link the record to its session, its parent or its place: never message text. */
8const STRUCTURAL_KEYS: ReadonlySet<string> = new Set([
9 "sessionId",
10 "uuid",
11 "parentUuid",
12 "cwd",
13 "transcript_path",
14 "agentId",
15 "toolUseID",
16 "tool_use_id",
17]);
18
19// Bytes are no text, and a short value could match inside them: a `base64` field, and the `data` of
20// a base64 source or of an image or audio block. A document's text source is text, and is masked.
21const BYTES_HOLDERS: ReadonlySet<unknown> = new Set(["base64", "image", "audio"]);
22
23function holdsBytes(node: object, key: string): boolean {
24 return key === "base64" || (key === "data" && "type" in node && BYTES_HOLDERS.has(node.type));
25}
26
27/** Parsed JSON with every string rewritten by `map`, in a fixed order; a structural key and a field
28 * of bytes are left as they are. */
29export function mapStrings(node: unknown, map: (text: string) => string): unknown {
30 if (typeof node === "string") {
31 return map(node);
32 }
33 if (Array.isArray(node)) {
34 return node.map((item: unknown) => mapStrings(item, map));
35 }
36 if (node !== null && typeof node === "object") {
37 return Object.fromEntries(
38 Object.entries(node).map(([key, child]) => [
39 key,
40 STRUCTURAL_KEYS.has(key) || holdsBytes(node, key) ? child : mapStrings(child, map),
41 ]),
42 );
43 }
44 return node;
45}
46lib/substitute.ts 62 lines1// The command a hook runs in place of one that names a stored secret by id. Pure and import-free,
2// because hooks/register.tsx imports it inside the engine sandbox.
3
4/** Where captured pastes are stored, apart from the keys a person set. */
5export const CAPTURE_NAMESPACE = "capture";
6
7// A placeholder is `{{secret:<id>}}` or, for a key the person named, `{{secret:<namespace>/<key>}}`;
8// for a long secret the last four characters follow as `…abcd`.
9const PLACEHOLDER = /\{\{secret:((?:[\w-]+\/)?[\w-]+)\}\}(?:…[\w-]{4})?/g;
10
11/** Each distinct id a text holds a placeholder for, in order of appearance. */
12export function placeholderIds(text: string): string[] {
13 return [
14 ...new Set([...text.matchAll(PLACEHOLDER)].flatMap((m) => (m[1] === undefined ? [] : [m[1]]))),
15 ];
16}
17
18export interface Substitution {
19 /** The command to run; the input unchanged when it names no id. */
20 readonly command: string;
21 /** Each distinct id the command names, in order of appearance. */
22 readonly ids: string[];
23}
24
25const variableOf = (id: string): string =>
26 `QSM_SECRET_${id.toUpperCase().replaceAll(/[^A-Z0-9]/g, "_")}`;
27
28/** The namespace and key an id stands for: `<namespace>/<key>` as written, else a captured paste. */
29export function locate(id: string): { namespace: string; key: string } {
30 const slash = id.indexOf("/");
31 return slash === -1
32 ? { namespace: CAPTURE_NAMESPACE, key: id }
33 : { namespace: id.slice(0, slash), key: id.slice(slash + 1) };
34}
35
36const quoted = (text: string): string => `'${text.replaceAll("'", "'\\''")}'`;
37
38/**
39 * `qsm-secrets env <key>=QSM_SECRET_<ID> --folder <namespace> -- sh -c '<command>'`, one `env` per id, so
40 * the value reaches that one command through its environment and is never in the command text.
41 */
42export function substitute(command: string): Substitution {
43 const ids = placeholderIds(command);
44 if (ids.length === 0) {
45 return { command, ids };
46 }
47 const owners = new Map<string, string>();
48 for (const id of ids) {
49 const taken = owners.get(variableOf(id));
50 if (taken !== undefined) {
51 throw new Error(`${taken} and ${id} would share ${variableOf(id)}; rename one of them`);
52 }
53 owners.set(variableOf(id), id);
54 }
55 const body = command.replace(PLACEHOLDER, (_, id: string) => `"$${variableOf(id)}"`);
56 const envs = ids.map((id) => {
57 const { namespace, key } = locate(id);
58 return `qsm-secrets env ${key}=${variableOf(id)} --folder ${namespace} --`;
59 });
60 return { command: `${envs.join(" ")} sh -c ${quoted(body)}`, ids };
61}
62hooks/noun.generated.ts 139 lines1// Generated by lib/generate-noun.ts from qsm.json. Do not hand-edit; run
2// `bun lib/generate-noun.ts plugins/<name>` again after changing qsm.json.
3import type { Caught, EngineInterface, On } from "claude-code";
4import type { Secrets } from "../types/index.d.ts";
5
6/** How one option becomes argv: a positional value (`flag` null), a bare
7 * boolean flag, or a flag and its value; a list repeats it per element. */
8export interface VerbArg {
9 readonly name: string;
10 readonly flag: string | null;
11 readonly boolean?: true;
12}
13
14export interface VerbRow {
15 readonly words: readonly string[];
16 readonly args: readonly VerbArg[];
17 readonly passthrough?: true;
18}
19
20export const verbs: Readonly<Record<string, VerbRow>> = {
21 set: { words: ["qsm-secrets","set"], args: [{ name: "key", flag: null }, { name: "folder", flag: "--folder" }, { name: "gui", flag: "--gui", boolean: true }] },
22 askpassEnable: { words: ["qsm-secrets","askpass","enable"], args: [{ name: "sudo", flag: "--sudo", boolean: true }, { name: "ssh", flag: "--ssh", boolean: true }, { name: "git", flag: "--git", boolean: true }, { name: "decide", flag: "--decide", boolean: true }] },
23 has: { words: ["qsm-secrets","has"], args: [{ name: "key", flag: null }, { name: "folder", flag: "--folder" }] },
24 list: { words: ["qsm-secrets","list"], args: [{ name: "folder", flag: "--folder" }] },
25 inspect: { words: ["qsm-secrets","inspect"], args: [{ name: "folder", flag: "--folder" }] },
26 rm: { words: ["qsm-secrets","rm"], args: [{ name: "key", flag: null }, { name: "folder", flag: "--folder" }] },
27 rename: { words: ["qsm-secrets","rename"], args: [{ name: "from", flag: null }, { name: "to", flag: null }, { name: "folder", flag: "--folder" }, { name: "toFolder", flag: "--to-folder" }, { name: "legacy", flag: "--legacy", boolean: true }] },
28 prune: { words: ["qsm-secrets","prune"], args: [{ name: "dryRun", flag: "--dry-run", boolean: true }] },
29 guard: { words: ["qsm-secrets","guard"], args: [] },
30 capture: { words: ["qsm-secrets","capture"], args: [] },
31 mask: { words: ["qsm-secrets","mask"], args: [{ name: "id", flag: "--id" }, { name: "all", flag: "--all", boolean: true }] },
32 scrub: { words: ["qsm-secrets","scrub"], args: [{ name: "transcript", flag: null }, { name: "detach", flag: "--detach", boolean: true }] },
33 render: { words: ["qsm-secrets","render"], args: [{ name: "template", flag: null }, { name: "out", flag: "--out" }, { name: "session", flag: "--session" }] },
34 clean: { words: ["qsm-secrets","clean"], args: [{ name: "session", flag: "--session" }] },
35 doctor: { words: ["qsm-secrets","doctor"], args: [{ name: "folder", flag: "--folder" }] },
36};
37
38/** One verb's argv: its words, each option in declaration order, the
39 * passthrough rest after `--`, and `--json` last. */
40export function argvOf(row: VerbRow, options: Readonly<Record<string, unknown>> = {}): string[] {
41 const argv = [...row.words];
42 for (const arg of row.args) {
43 const value = options[arg.name];
44 const values = Array.isArray(value) ? value : [value];
45 for (const one of values) {
46 if (one === undefined || one === false) {
47 continue;
48 }
49 argv.push(...(arg.flag === null ? [] : [arg.flag]), ...(arg.boolean ? [] : [String(one)]));
50 }
51 }
52 const rest = options.rest;
53 if (row.passthrough && Array.isArray(rest)) {
54 argv.push("--", ...rest.map(String));
55 }
56 argv.push("--json");
57 return argv;
58}
59
60/** The binary prints one outcome envelope on stdout under --json:
61 * { code, exit, reason, message, warnings, data }. Data on ok; otherwise the
62 * code and message become the error. A stale or misbehaving binary that
63 * prints something else (not JSON, or JSON with no numeric `exit`) throws
64 * loud here instead of a raw JSON.parse SyntaxError or a silent `undefined`. */
65export function unwrap(
66 argv: readonly string[],
67 result: { readonly exitCode: number; readonly stdout: string; readonly stderr: string },
68 label = argv.slice(0, 2).join(" "),
69): unknown {
70 const text = result.stdout.trim();
71 const stderr = result.stderr.trim().slice(0, 200);
72 const cause = `(exit ${result.exitCode}): ${stderr.length > 0 ? stderr : "no stderr"}`;
73 if (text.length === 0) {
74 throw new Error(`${argv.join(" ")}: printed no envelope ${cause}`);
75 }
76 let parsed: unknown;
77 try {
78 parsed = JSON.parse(text);
79 } catch {
80 parsed = undefined;
81 }
82 if (typeof parsed !== "object" || parsed === null || typeof (parsed as { exit?: unknown }).exit !== "number") {
83 throw new Error(`${argv.join(" ")}: stdout is not an outcome envelope ${cause}: ${text.slice(0, 200)}`);
84 }
85 const envelope = parsed as { code: string; exit: number; message: string; data: unknown };
86 if (envelope.exit !== 0) {
87 throw new Error(`${label}: ${envelope.code}: ${envelope.message}`);
88 }
89 return envelope.data;
90}
91
92/** What every `.catch` does first: a toast and a log for the person. The toast says `fixedReason`
93 * when the cause may carry something the toast must not show, else the cause; the cause goes to
94 * the log and comes back for the caller's own policy. */
95export function failure(
96 $: EngineInterface,
97 event: string,
98 error: Caught["error"],
99 fixedReason?: string,
100): string {
101 const said = error.message ?? error.kind;
102 $.ui.toast(`qsm-secrets ERROR: ${event}: ${(fixedReason ?? said).slice(0, 120)}`);
103 $.ui.log(`${event} failed (${error.kind}): ${said}`);
104 return said;
105}
106
107/** Adds $.secrets to the engine. Call once from the plugin's own
108 * `register(on, options)` — never call the engine.create hook's own
109 * `next(e)` result from anywhere but here. */
110export function registerNoun(on: On): void {
111 on("engine.create", async (_$, e, next) => {
112 const beneath = await next(e);
113 const run = async (row: VerbRow, options?: Readonly<Record<string, unknown>>): Promise<unknown> => {
114 const argv = argvOf(row, options);
115 let result: Awaited<ReturnType<typeof beneath.process.run>>;
116 try {
117 result = await beneath.process.run(argv);
118 } catch (error) {
119 // A spawn in a deleted directory fails as ENOENT naming the binary; name the directory. A
120 // check that cannot run leaves the spawn's own error as the cause.
121 const gone = await beneath.session
122 .cwd()
123 .then(async (cwd) => ((await beneath.fs.exists(cwd)) ? null : cwd))
124 .catch(() => null);
125 if (gone !== null) {
126 throw new Error(`${row.words.join(" ")}: the session's working directory ${gone} no longer exists; cd to one that does`);
127 }
128 throw error;
129 }
130 return unwrap(argv, result, row.words.join(" "));
131 };
132 const methods = Object.entries(verbs).map(([method, row]) => [
133 method,
134 (options?: Readonly<Record<string, unknown>>) => run(row, options),
135 ]);
136 return { ...beneath, secrets: Object.fromEntries(methods) as Secrets };
137 });
138}
139types/index.d.ts 68 lines1// Generated by lib/generate-noun.ts from qsm.json. Do not hand-edit; run
2// `bun lib/generate-noun.ts plugins/<name>` again after changing qsm.json.
3
4export type Secrets = {
5 set(options: {
6 key: string;
7 folder?: string;
8 gui?: boolean;
9 }): Promise<unknown>;
10 askpassEnable(options: {
11 sudo?: boolean;
12 ssh?: boolean;
13 git?: boolean;
14 decide?: boolean;
15 }): Promise<unknown>;
16 has(options: {
17 key: string;
18 folder?: string;
19 }): Promise<unknown>;
20 list(options: {
21 folder?: string;
22 }): Promise<unknown>;
23 inspect(options: {
24 folder?: string;
25 }): Promise<unknown>;
26 rm(options: {
27 key: string;
28 folder?: string;
29 }): Promise<unknown>;
30 rename(options: {
31 from: string;
32 to: string;
33 folder?: string;
34 toFolder?: string;
35 legacy?: boolean;
36 }): Promise<unknown>;
37 prune(options: {
38 dryRun?: boolean;
39 }): Promise<unknown>;
40 guard(): Promise<unknown>;
41 capture(): Promise<unknown>;
42 mask(options: {
43 id?: (string)[];
44 all?: boolean;
45 }): Promise<unknown>;
46 scrub(options: {
47 transcript: string;
48 detach?: boolean;
49 }): Promise<unknown>;
50 render(options: {
51 template: string;
52 out: string;
53 session?: string;
54 }): Promise<unknown>;
55 clean(options: {
56 session?: string;
57 }): Promise<unknown>;
58 doctor(options: {
59 folder?: string;
60 }): Promise<unknown>;
61};
62
63declare module 'claude-code' {
64 interface EngineInterface {
65 secrets: Secrets;
66 }
67}
68