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", ()
=> {
62 + it(
"keeps opaque sensitive-name assignments visible", ()
=> {
79 - for (
const key
of keys)
80 - expectRedacts(
`${key}=assignment-fixture-secret-123456789`, key);
78 + const text
= `${key}=assignment-fixture-secret-123456789`;
79 + expectVisible(text, key);
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,
107 + PROD_ACCESS_TOKEN: ordinary,
110 + expect(learned.values.
map((item)
=> item.
value)).
toEqual([ordinary]);
100 113 it(
"enforces learned exact value cap and warns once", ()
=> {
101 114 const learned
= __test.
learnEnvironmentValues(
extensions/leaks/src/detectors.ts
Modified 87 - "PUTTY_PRIVATE_KEY",
89 - /PuTTY-User-Key-File-[\s\S]*?Private-Lines:\s*\d+[\s\S]*/dg,
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)) {
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;
139 + for (
let index
= 0; index
< count; index
++) {
140 + const lineEnd
= text.
indexOf(
"\n", end);
141 + if (lineEnd
=== -1) {
143 + valid
= index
=== count
- 1;
146 + if (
!/^[A-Za-z0-9+/=]+\r?$/.
test(text.
slice(end, lineEnd))) {
152 + if (
!valid)
continue;
154 + start: match.
index as number,
156 + value: text.
slice(match.
index, end),
157 + labels: [
label(
"PUTTY_PRIVATE_KEY")],
158 + kind:
"private-key",
extensions/leaks/src/dotenv.ts
Modified 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([
8 20 const SENSITIVE
= new Set([
169 - const publicPrefixed
= PUBLIC_PREFIX_RE.
test(key);
170 181 const secretShape
= highConfidenceSecretShape(value);
172 - (
!publicPrefixed
&& isSensitiveKey(key)
&& !isWeakValue(value))
|| 179 - tag: source
=== "env" ? "ENV_KEY" : sanitizeTag(key),
182 + if (
!secretShape
&& (
!isLearnableKey(key)
|| isWeakValue(value)))
continue;
187 + tag: source
=== "env" ? "ENV_KEY" : sanitizeTag(key),
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))
202 + return isSensitiveKey(normalized);
190 205 function normalizeKey(key
: string) {
191 206 return key.
toUpperCase().
replace(
/[-]+/g,
"_").
replace(
/_\d+$/,
"");