repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(leaks): tighten provider and environment detection

c8d5ab4def2cbd250cb4a1e10dcf29fd911fe3b5

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

extensions/leaks/__tests__/detectors.test.ts

Modified
59 59 for (const block of blocks) expectRedacts(block, block.slice(0, 16));
62 - it("redacts generic sensitive-key assignments listed in the spec", () => {
63 - const keys = [
62 + it("keeps opaque sensitive-name assignments visible", () => {
63 + for (const key of [
64 64 "KEY",
65 65 "KEY_DATA",
66 66 "PASS",
74 74 "SECRET_KEY_BASE",
75 75 "UNSEAL_KEY",
76 76 "MNEMONIC",
77 - ];
79 - for (const key of keys)
80 - expectRedacts(`${key}=assignment-fixture-secret-123456789`, key);
77 + ]) {
78 + const text = `${key}=assignment-fixture-secret-123456789`;
79 + expectVisible(text, key);
80 + }
83 83 it("keeps explicit negative controls visible", () => {

extensions/leaks/__tests__/dotenv.test.ts

Modified
97 97 expect(__test.isSensitiveKey("lineage")).toBe(false);
100 + it("does not learn environment metadata or context-only names", () => {
101 + const ordinary = ["ordinary", "metadata", "value", "123456"].join("-");
102 + const learned = __test.learnEnvironmentValues({
103 + GPG_KEY_ID: ordinary,
104 + SESSION_MANAGER: ordinary,
105 + KEY: ordinary,
106 + AUTH: ordinary,
107 + PROD_ACCESS_TOKEN: ordinary,
108 + });
110 + expect(learned.values.map((item) => item.value)).toEqual([ordinary]);
111 + });
100 113 it("enforces learned exact value cap and warns once", () => {
101 114 const learned = __test.learnEnvironmentValues(

extensions/leaks/src/detectors.ts

Modified
82 82 "private-key",
83 83 true,
85 - rule(
86 - "putty-private",
87 - "PUTTY_PRIVATE_KEY",
88 - "private-key",
89 - /PuTTY-User-Key-File-[\s\S]*?Private-Lines:\s*\d+[\s\S]*/dg,
90 - 0,
91 - "private-key",
92 - true,
93 - ),
94 85 rule(
95 86 "age-secret",
96 87 "AGE_SECRET_KEY",
119 110 export function findBuiltinSpans(text: string): SecretSpan[] {
120 - const spans: SecretSpan[] = [];
111 + const spans: SecretSpan[] = findPuttySpans(text);
121 112 for (const item of builtinRules) {
122 113 item.regex.lastIndex = 0;
123 114 for (const match of text.matchAll(item.regex)) {
137 128 return spans;
131 +function findPuttySpans(text: string): SecretSpan[] {
132 + const header = /^PuTTY-User-Key-File-[^\r\n]*\r?\n(?:[^\r\n]*\r?\n)*?Private-Lines:\s*(\d+)\r?\n/gm;
133 + const spans: SecretSpan[] = [];
134 + for (const match of text.matchAll(header)) {
135 + const count = Number(match[1]);
136 + if (!Number.isSafeInteger(count) || count < 1 || count > 4096) continue;
137 + let end = (match.index as number) + match[0].length;
138 + let valid = true;
139 + for (let index = 0; index < count; index++) {
140 + const lineEnd = text.indexOf("\n", end);
141 + if (lineEnd === -1) {
142 + end = text.length;
143 + valid = index === count - 1;
144 + break;
146 + if (!/^[A-Za-z0-9+/=]+\r?$/.test(text.slice(end, lineEnd))) {
147 + valid = false;
148 + break;
150 + end = lineEnd + 1;
152 + if (!valid) continue;
153 + spans.push({
154 + start: match.index as number,
155 + end,
156 + value: text.slice(match.index, end),
157 + labels: [label("PUTTY_PRIVATE_KEY")],
158 + kind: "private-key",
159 + confidence: 100,
160 + wholeSpan: true,
161 + });
163 + return spans;
140 166 function rule(
141 167 id: string,
142 168 tag: string,

extensions/leaks/src/dotenv.ts

Modified
6 6 const CAP = 8192;
7 7 const PUBLIC_PREFIX_RE = /^(?:PUBLIC|NEXT_PUBLIC|VITE)[_-]/i;
8 +const METADATA_SUFFIX_RE = /_(?:ID|NAME|PATH|FILE|TYPE|HOST|PORT|MANAGER|SOCKET|ENDPOINT)$/;
9 +const CONTEXT_ONLY = new Set([
10 + "KEY",
11 + "AUTH",
12 + "SESSION",
13 + "COOKIE",
14 + "JWT",
15 + "SEED",
16 + "DATABASE_URL",
17 + "MONGO_URL",
18 + "REDIS_URL",
19 +]);
8 20 const SENSITIVE = new Set([
9 21 "TOKEN",
10 22 "SECRET",
167 179 continue;
169 - const publicPrefixed = PUBLIC_PREFIX_RE.test(key);
170 181 const secretShape = highConfidenceSecretShape(value);
171 - if (
172 - (!publicPrefixed && isSensitiveKey(key) && !isWeakValue(value)) ||
173 - secretShape
174 - ) {
175 - values.push({
176 - value,
177 - labels: [
178 - {
179 - tag: source === "env" ? "ENV_KEY" : sanitizeTag(key),
180 - priority: 10,
181 - source,
182 - },
183 - ],
184 - });
185 - }
182 + if (!secretShape && (!isLearnableKey(key) || isWeakValue(value))) continue;
183 + values.push({
184 + value,
185 + labels: [
187 + tag: source === "env" ? "ENV_KEY" : sanitizeTag(key),
188 + priority: 10,
189 + source,
190 + },
191 + ],
192 + });
187 194 return { values, warnings };
197 +function isLearnableKey(key: string) {
198 + if (PUBLIC_PREFIX_RE.test(key)) return false;
199 + const normalized = normalizeKey(key);
200 + if (METADATA_SUFFIX_RE.test(normalized) || CONTEXT_ONLY.has(normalized))
201 + return false;
202 + return isSensitiveKey(normalized);
190 205 function normalizeKey(key: string) {
191 206 return key.toUpperCase().replace(/[-]+/g, "_").replace(/_\d+$/, "");