Skip to content

Commit e2c56f7

Browse files
committed
Decide closing quotes by escape-aware parsing
A closing quote cannot be recognized by the character that follows it. `\",` and `\" ` occur inside ordinary passwords, so the delimiter lookahead ended the value at the first one and kept `ab` — two characters, under every threshold, silently waved through. Field values are now read rather than matched. The scanner counts the backslashes in front of a candidate quote, which is what actually distinguishes a delimiter from an escaped quote, and handles both encodings that reach it: a document escaped once closes on an even count, while a serialized event escapes the same text again, so a delimiter leaves 1 mod 4 and an inner quote 3 mod 4. Refs #199
1 parent 3c6980c commit e2c56f7

2 files changed

Lines changed: 110 additions & 47 deletions

File tree

‎packages/core/src/secrets/rules.ts‎

Lines changed: 104 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,37 +183,72 @@ const BEARER = new RegExp(
183183
/** `sk-` and `sk-proj-` keys, with or without the preset's `T3BlbkFJ` marker. */
184184
const OPENAI = /(?<![A-Za-z0-9_-])sk-(?:proj-)?[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g;
185185

186+
/** A credential-named field and the separator before its value. */
187+
const FIELD_HEAD = new RegExp(`${Q}\\b(?:${FIELD_NAMES})\\b${Q}\\s*[:=]\\s*`, "gi");
188+
189+
/** An unquoted value ends at the first whitespace or JSON punctuation. */
190+
const BARE_VALUE = /^[^\s,}"'\\]+/;
191+
186192
/**
187-
* The body of a quoted value: escape sequences, or anything that is not a
188-
* quote or a backslash.
193+
* Where a quoted value ends, decided by counting the escapes in front of a
194+
* candidate quote.
189195
*
190-
* A secret may legitimately contain `\"` or `\\`. Treating either as the end
191-
* of the value truncates it to whatever came first — `ab` out of a 26
192-
* character password — which then falls under every threshold and is waved
193-
* through. Treating a backslash as invalid is worse: the field stops matching
194-
* at all.
196+
* A closing quote cannot be recognized by what follows it. `\",` and `\" `
197+
* appear inside perfectly ordinary passwords, so any rule keyed on the next
198+
* character ends the value early and keeps only the leading fragment — which
199+
* is then short enough to fall under every threshold and be waved through.
200+
* What actually distinguishes a delimiter is how many backslashes precede it.
195201
*
196-
* The quantifier is lazy and the closing quote must be followed by a JSON
197-
* delimiter. Greedy matching cannot tell an escaped quote inside the value
198-
* from the closing quote of a *serialized* event, where the value arrives
199-
* escaped twice, and runs past the end of the field.
202+
* Two encodings reach this scanner. In a document read from disk a value is
203+
* escaped once, so a quote closes the string when an even number of
204+
* backslashes precede it. In a serialized event the same text is escaped
205+
* again: the delimiter arrives as `\"` and an inner quote as `\\\"`. Writing
206+
* the level-1 backslash count as `n`, level 2 shows `2n + 1` — so a delimiter
207+
* (`n` even) leaves a count of 1 mod 4, and an escaped quote (`n` odd) leaves
208+
* 3 mod 4.
200209
*/
201-
const quoted = (mark: string) =>
202-
`\\\\?${mark}((?:\\\\.|[^${mark}\\\\])+?)\\\\?${mark}(?=[,}\\s\\\\]|$)`;
210+
function readQuotedValue(
211+
content: string,
212+
openAt: number,
213+
): { value: string; end: number } | undefined {
214+
const escaped = content[openAt] === "\\";
215+
const mark = content[openAt + (escaped ? 1 : 0)];
216+
if (mark !== '"' && mark !== "'") {
217+
return undefined;
218+
}
219+
220+
const bodyStart = openAt + (escaped ? 2 : 1);
221+
222+
for (let at = bodyStart; at < content.length; at++) {
223+
if (content[at] !== mark) {
224+
continue;
225+
}
226+
227+
let backslashes = 0;
228+
for (let back = at - 1; back >= bodyStart && content[back] === "\\"; back--) {
229+
backslashes++;
230+
}
231+
232+
const closes = escaped ? backslashes % 4 === 1 : backslashes % 2 === 0;
233+
if (closes) {
234+
// A doubly-escaped delimiter owns the backslash in front of its quote.
235+
const valueEnd = escaped ? at - 1 : at;
236+
return { value: content.slice(bodyStart, valueEnd), end: at + 1 };
237+
}
238+
}
239+
240+
return undefined;
241+
}
203242

204243
/**
205-
* A credential-named field and its complete value.
244+
* The complete value of a credential-named field.
206245
*
207-
* A quoted value runs to its actual closing quote, spaces and punctuation
208-
* included — stopping at the first space truncates
209-
* `"correct horse Battery 123!"` to `correct`. Only an unquoted value ends at
210-
* whitespace.
246+
* A quoted value runs to its actual closing quote — spaces, punctuation, and
247+
* escape sequences included. Only an unquoted value ends at whitespace.
211248
*/
212-
const FIELD = new RegExp(
213-
`${Q}\\b(?:${FIELD_NAMES})\\b${Q}\\s*[:=]\\s*` +
214-
`(?:${quoted('"')}|${quoted("'")}|([^\\s,}"'\\\\]+))`,
215-
"gi",
216-
);
249+
function readFieldValue(content: string, at: number): string | undefined {
250+
return readQuotedValue(content, at)?.value ?? BARE_VALUE.exec(content.slice(at))?.[0];
251+
}
217252

218253
/** An auth scheme in front of the value it carries. */
219254
const SCHEME = /^(?:Bearer|Token|Basic|ApiKey)\s+/i;
@@ -238,43 +273,65 @@ export const xmdCredentialRule: SecretLintRuleCreator = {
238273

239274
return {
240275
file(source) {
241-
report(source.content, BEARER, "BEARER_TOKEN");
242-
report(source.content, OPENAI, "OPENAI_KEY");
243-
report(source.content, FIELD, "CREDENTIAL_FIELD");
276+
const content = source.content;
277+
278+
reportPattern(BEARER, "BEARER_TOKEN");
279+
reportPattern(OPENAI, "OPENAI_KEY");
280+
reportFields();
244281

245-
function report(content: string, pattern: RegExp, messageId: keyof typeof messages): void {
282+
function reportPattern(pattern: RegExp, messageId: keyof typeof messages): void {
246283
// A /g regex carries lastIndex across calls; each scan starts fresh.
247284
pattern.lastIndex = 0;
248285

249286
for (const match of content.matchAll(pattern)) {
250-
// The value is the first capture that participated, or the whole
251-
// match for a pattern that captures nothing (OpenAI).
252-
const value = match.slice(1).find((group) => group !== undefined) ?? match[0];
287+
const value = match[1] ?? match[0];
253288
if (value === undefined || match.index === undefined) {
254289
continue;
255290
}
291+
report(value, match.index + match[0].lastIndexOf(value), messageId);
292+
}
293+
}
294+
295+
/**
296+
* Field values are read rather than matched: where a quoted value
297+
* ends is a parsing question, not one a pattern can answer.
298+
*/
299+
function reportFields(): void {
300+
FIELD_HEAD.lastIndex = 0;
256301

257-
// `authorization: Bearer <token>` carries its scheme into the
258-
// field value; the credential is what follows it.
259-
const credential = value.replace(SCHEME, "");
260-
261-
if (messageId === "OPENAI_KEY") {
262-
// An OpenAI-shaped value still has to be a value. A documented
263-
// `sk-your-openai-api-key-here` is prose that happens to wear
264-
// the prefix, and rejecting it would block documentation.
265-
if (isPlaceholder(credential.replace(/^sk-(?:proj-)?/, ""))) {
266-
continue;
267-
}
268-
} else if (!looksLikeSecret(credential)) {
302+
for (const head of content.matchAll(FIELD_HEAD)) {
303+
if (head.index === undefined) {
269304
continue;
270305
}
306+
const at = head.index + head[0].length;
307+
const value = readFieldValue(content, at);
308+
if (value !== undefined) {
309+
report(value, at, "CREDENTIAL_FIELD");
310+
}
311+
}
312+
}
313+
314+
function report(value: string, at: number, messageId: keyof typeof messages): void {
315+
// `authorization: Bearer <token>` carries its scheme into the field
316+
// value; the credential is what follows it.
317+
const credential = value.replace(SCHEME, "");
318+
const start = at + (value.length - credential.length);
271319

272-
const start = match.index + match[0].lastIndexOf(credential);
273-
context.report({
274-
message: t(messageId),
275-
range: [start, start + credential.length],
276-
});
320+
if (messageId === "OPENAI_KEY") {
321+
// An OpenAI-shaped value still has to be a value. A documented
322+
// `sk-your-openai-api-key-here` is prose that happens to wear the
323+
// prefix, and rejecting it would block documentation.
324+
if (isPlaceholder(credential.replace(/^sk-(?:proj-)?/, ""))) {
325+
return;
326+
}
327+
} else if (!looksLikeSecret(credential)) {
328+
return;
277329
}
330+
331+
context.report({
332+
message: t(messageId),
333+
range: [start, start + credential.length],
334+
});
278335
}
279336
},
280337
};

‎packages/core/tests/secret-rules.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ describe("escapes inside a quoted credential field", () => {
393393
const CASES: Record<string, string> = {
394394
"an escaped quote": WITH_QUOTE,
395395
"a literal backslash": WITH_BACKSLASH,
396+
// A closing quote cannot be recognized by the character after it: both of
397+
// these put a JSON delimiter directly after an escaped quote *inside* the
398+
// value, so any rule keyed on what follows ends the value here and keeps
399+
// only `ab`.
400+
"an escaped quote before a comma": `ab",cd${TAIL}`,
401+
"an escaped quote before whitespace": `ab" cd${TAIL}`,
396402
};
397403

398404
for (const [name, secret] of Object.entries(CASES)) {

0 commit comments

Comments
 (0)