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
43 changes: 33 additions & 10 deletions src/utils/scrub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* still says what failed and what to do next.
*/

import { realpathSync } from 'fs';
export const REDACTED = '[redacted]';

/** Values shorter than this are not secrets anyone could act on, and replacing them would garble ordinary text. */
Expand Down Expand Up @@ -78,19 +79,41 @@ function spellings(value: string): string[] {
return [...out].filter((s) => s.length >= MIN_KNOWN_LENGTH);
}

/** One known spelling removed from `text`: whole words only when it is short. */
const removeSpelling = (text: string, s: string): string => {
if (!text.includes(s)) return text;
return s.length < WHOLE_WORD_BELOW
? text.replace(new RegExp(`(?<![A-Za-z0-9_])${escapeRegExp(s)}(?![A-Za-z0-9_])`, 'g'), REDACTED)
: text.split(s).join(REDACTED);
};

/**
* CAP-985: the folders this process works in are its own, authoritative locations, never secrets. An `.env` value
* can be spelled like a folder name (`DD_SERVICE=ferry-ops` in a folder named ferry-ops); removing it inside the
* folder's own path turned `answer.wait --folder .../ferry-ops` into `.../[redacted]`, a command no agent can run.
*/
function ownPaths(): readonly string[] {
const cwd = ((): string | null => { try { return process.cwd(); } catch { return null; } })();
if (cwd === null) return [];
const real = ((): string | null => { try { return realpathSync(cwd); } catch { return null; } })();
return [...new Set([cwd, ...(real === null ? [] : [real])])].filter((p) => p.length > 1);
}

/** `text` split around every occurrence of the protected paths: odd parts are the paths themselves. */
function splitAround(text: string, paths: readonly string[]): readonly string[] {
const ordered = [...paths].sort((a, b) => b.length - a.length);
if (ordered.length === 0) return [text];
const pattern = new RegExp(`(${ordered.map(escapeRegExp).join('|')})`, 'g');
return text.split(pattern);
}

function replaceKnown(text: string, known: Iterable<string>): string {
const all = new Set<string>();
for (const value of known) for (const s of spellings(value)) all.add(s);
const all = new Set<string>([...known].flatMap((value) => spellings(value)));
// Longest first, so a value that contains another is removed whole.
const ordered = [...all].sort((a, b) => b.length - a.length);
let out = text;
for (const s of ordered) {
if (!out.includes(s)) continue;
out = s.length < WHOLE_WORD_BELOW
? out.replace(new RegExp(`(?<![A-Za-z0-9_])${escapeRegExp(s)}(?![A-Za-z0-9_])`, 'g'), REDACTED)
: out.split(s).join(REDACTED);
}
return out;
const scrubPart = (part: string): string => ordered.reduce(removeSpelling, part);
// The process's own folder paths are kept as they are; everything around them is scrubbed.
return splitAround(text, ownPaths()).map((part, index) => (index % 2 === 1 ? part : scrubPart(part))).join('');
}

/** Shapes of secret material, each replaced whole (or, where a group is named, only that part). */
Expand Down
12 changes: 12 additions & 0 deletions tests/utils/scrub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ describe('scrub (CAP-980)', () => {
expect((out as { cause: { code: string } }).cause.code).toBe('SERVICE_ERROR');
});
});

describe('CAP-985: the process\'s own folder path is never scrubbed', () => {
test('an .env value spelled like the folder name is kept inside the folder path and removed everywhere else', () => {
const folder = process.cwd();
const name = folder.split('/').filter((p) => p.length > 0).at(-1)!;
// A slug long enough to be remembered, as DD_SERVICE=ferry-ops was.
rememberSecrets([name]);
const wait = `capy setup --wait --folder ${folder} --flow abc --json`;
expect(scrub(wait)).toBe(wait);
expect(scrubKnown(`service is ${name}`)).toBe(`service is ${REDACTED}`);
});
});
Loading