Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.
TypeScript claude-code-mod
herdr: only the client variables the mod reads are kept; no NUL string
razorback16568e670a9e66
diff --git a/hooks/lib/terminal.ts b/hooks/lib/terminal.tsindex 310875c..d95dc11 100644--- a/hooks/lib/terminal.ts+++ b/hooks/lib/terminal.ts@@ -88,6 +88,11 @@ export function herdrHostOf(env: { cmux?: boolean; ghosttyResources?: string; gh// readers use (host.ts outerEnv): under herdr, the client's.export const OUTER_NAMES = ['COLORFGBG', 'GHOSTTY_BIN_DIR', 'GHOSTTY_RESOURCES_DIR', 'ITERM_PROFILE', 'KITTY_CONFIG_DIRECTORY', 'RIO_CONFIG_HOME'] as const satisfies readonly EnvName[]export type OuterName = (typeof OUTER_NAMES)[number]+// The variables kept of a herdr client: OUTER_NAMES and those outerKindOf reads.+export const CLIENT_NAMES = [+ ...OUTER_NAMES, 'CMUX_SOCKET_PATH', 'CMUX_SURFACE_ID', 'CMUX_WORKSPACE_ID', 'ITERM_SESSION_ID', 'KITTY_INSTALLATION_DIR', 'KITTY_PID', 'KITTY_WINDOW_ID', 'LC_TERMINAL',+ 'TERM', 'TERM_PROGRAM',+] as const// A herdr client (HERDR_CLIENT_SH): its pid, its tty and the tty's access// time (the last key typed there), its command, the nearest ancestor with no@@ -125,7 +130,8 @@ export function outerKindOf(env: Readonly<Record<string, string>>, app: string):export function envOfText(text: string, cmd: string): Record<string, string> {const env: Record<string, string> = {}let words: string[]- if (text.includes('\0')) words = text.split('\0')+ // A NUL as a regular expression: no control character in a string.+ if (/\x00/.test(text)) words = text.split(/\x00/)else {const line = text.replace(/\n+$/, '')const bare = !!cmd && (line === cmd || line.startsWith(`${cmd} `))@@ -152,8 +158,10 @@ export function herdrClientOf(stdout: string): HerdrClient | null {if (!Number.isInteger(pid) || pid <= 0) return nullconst cmd = field('cmd')const app = field('app')- const env = envOfText(stdout.slice(at + 5), cmd)- const kind = outerKindOf(env, app)+ const all = envOfText(stdout.slice(at + 5), cmd)+ const kind = outerKindOf(all, app)+ // Only the variables the mod reads are kept (not a token of the client's shell).+ const env = Object.fromEntries(Object.entries(all).filter(([name]) => (CLIENT_NAMES as readonly string[]).includes(name)))const bin = /^(.*)\/ghostty(?= |$)/.exec(app)?.[1]if (kind === 'ghostty' && !env.GHOSTTY_BIN_DIR && bin) env.GHOSTTY_BIN_DIR = binreturn { pid, atime: Number(field('atime')) || 0, tty: field('tty'), cmd, app, env, kind }diff --git a/tests/terminals.test.ts b/tests/terminals.test.tsindex 189f277..e939b8a 100644--- a/tests/terminals.test.ts+++ b/tests/terminals.test.ts@@ -275,7 +275,8 @@ test('a herdr client on macOS (ps eww): its command is not a variable, its variaexpect(kitty).toMatchObject({ pid: 54346, atime: 1792712692, tty: 'ttys021', cmd: '/Users/u/.local/bin/herdr', kind: 'kitty' })expect(kitty.env.TERM).toBe('xterm-kitty')expect(kitty.env.KITTY_INSTALLATION_DIR).toBe('/Applications/kitty.app/Contents/Resources/kitty')- expect(kitty.env.USER).toBe('u')+ // A variable the mod does not read is not kept.+ expect(kitty.env.USER).toBeUndefined()expect(Object.keys(kitty.env).some(k => k.includes('herdr'))).toBe(false)const ghostty = herdrClientOf(GHOSTTY_MAC)!expect(ghostty).toMatchObject({ pid: 50390, kind: 'ghostty', cmd: 'herdr' })@@ -299,9 +300,20 @@ test('ps eww text: a value with spaces stays whole; the arguments (--session x)test('a herdr client on Linux (/proc/<pid>/environ): NUL-separated variables; a value can hold = and newlines', () => {const out = 'pid 4242\natime 1792700000\ntty pts/3\ncmd herdr session attach work\napp /usr/bin/kitty\nenv\n' +'TERM=xterm-kitty\0KITTY_CONFIG_DIRECTORY=/home/u/kc\0A=b=c\0B=one\ntwo\0'+ expect(envOfText(out.slice(out.indexOf('\nenv\n') + 5), '')).toEqual({ TERM: 'xterm-kitty', KITTY_CONFIG_DIRECTORY: '/home/u/kc', A: 'b=c', B: 'one\ntwo' })const c = herdrClientOf(out)!expect(c).toMatchObject({ pid: 4242, tty: 'pts/3', kind: 'kitty', app: '/usr/bin/kitty' })- expect(c.env).toEqual({ TERM: 'xterm-kitty', KITTY_CONFIG_DIRECTORY: '/home/u/kc', A: 'b=c', B: 'one\ntwo' })+ expect(c.env).toEqual({ TERM: 'xterm-kitty', KITTY_CONFIG_DIRECTORY: '/home/u/kc' })+})++test("a herdr client keeps only the variables the mod reads: never a token of the client's shell", () => {+ const secret = 'GITHUB_TOKEN=ghp_secret AWS_SECRET_ACCESS_KEY=abc ANTHROPIC_API_KEY=sk-x'+ const mac = herdrClientOf(['pid 7', 'atime 5', 'tty ttys003', 'cmd herdr', 'app /Applications/Ghostty.app/Contents/MacOS/ghostty', 'env',+ `herdr TERM=xterm-ghostty COLORFGBG=15;0 ${secret}`].join('\n'))!+ expect(mac.env).toEqual({ TERM: 'xterm-ghostty', COLORFGBG: '15;0', GHOSTTY_BIN_DIR: '/Applications/Ghostty.app/Contents/MacOS' })+ const linux = herdrClientOf(`pid 8\natime 5\ntty pts/1\ncmd herdr\napp /usr/bin/kitty\nenv\nTERM=xterm-kitty\0${secret.split(' ').join('\0')}\0`)!+ expect(linux.env).toEqual({ TERM: 'xterm-kitty' })+ for (const c of [mac, linux]) expect(JSON.stringify(c)).not.toMatch(/secret|ghp_|sk-x|abc/)})test('no herdr client: no output, no pid or no env line is null; no variables read gives the app', () => {