Skip to content

Commit 033e555

Browse files
committed
Consume doubled apostrophes in single-quoted fields
YAML escapes an apostrophe inside a single-quoted scalar by doubling it rather than with a backslash, and XMD documents carry frontmatter, so this is ordinary content. The reader modelled backslash escaping only, so the first apostrophe of a pair closed the value: a frontmatter password truncated to whatever preceded it and produced no finding, directly or inside a serialized event. A doubled apostrophe is now consumed as content. JSON leaves both the delimiters and the doubling untouched, so this reads identically at every depth — unlike a backslash-escaped apostrophe, whose escape doubles on serialization, which is why the two cases need separate handling rather than one shared rule. Refs #199
1 parent 7462496 commit 033e555

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ function readAtDepth(
293293
continue;
294294
}
295295

296+
// YAML escapes an apostrophe inside a single-quoted scalar by doubling
297+
// it, not with a backslash — and JSON leaves both the doubling and the
298+
// delimiters alone, so this reads the same at every depth. Without it the
299+
// first apostrophe of a pair closes the value, and a frontmatter password
300+
// truncates to whatever preceded it.
301+
if (mark === "'" && content[at + 1] === "'") {
302+
at++;
303+
continue;
304+
}
305+
296306
let backslashes = 0;
297307
for (let back = at - 1; back >= bodyStart && content[back] === "\\"; back--) {
298308
backslashes++;

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,73 @@ describe("lowercase passphrases containing placeholder words", () => {
609609
});
610610
}
611611
});
612+
613+
/**
614+
* YAML single-quoted scalars escape an apostrophe by doubling it.
615+
*
616+
* XMD documents carry frontmatter, so this is ordinary content rather than a
617+
* corner case. Backslash escaping is the only convention the reader modelled,
618+
* so the first apostrophe of a pair closed the value and a frontmatter
619+
* password truncated to whatever preceded it.
620+
*
621+
* JSON leaves both the delimiters and the doubling untouched, so the source
622+
* text and its serialized form read identically here — unlike a
623+
* backslash-escaped apostrophe, whose escape doubles on serialization.
624+
*/
625+
describe("YAML doubled apostrophes in single-quoted fields", () => {
626+
const TAIL = A.slice(0, 20);
627+
const CREDENTIAL = `ab''cd${TAIL}`;
628+
const SOURCE = `password: '${CREDENTIAL}'`;
629+
const PLACEHOLDER_SOURCE = `password: 'your-password-here'`;
630+
631+
const asImport = (source: string): DurableEvent => ({
632+
type: "yield",
633+
coroutineId: "root",
634+
description: { type: "import_component", name: "__root__" },
635+
result: { status: "ok", value: { path: "README.md", content: source } },
636+
});
637+
638+
it("detects the credential directly", function* () {
639+
expect((yield* createSecretScanner().scan(SOURCE)).length).toBeGreaterThan(0);
640+
});
641+
642+
it("detects the same source inside a serialized root import", function* () {
643+
const record = serializeDurableEvent(asImport(SOURCE));
644+
645+
// The fixture only proves anything if the doubling survived serialization.
646+
expect(record).toContain("ab''cd");
647+
648+
expect((yield* createSecretScanner().scan(record)).length).toBeGreaterThan(0);
649+
});
650+
651+
it("keeps the credential and its fragments out of the findings", function* () {
652+
const scanner = createSecretScanner();
653+
654+
for (const content of [SOURCE, serializeDurableEvent(asImport(SOURCE))]) {
655+
const serialized = JSON.stringify(yield* scanner.scan(content));
656+
expect(serialized).not.toContain(CREDENTIAL);
657+
expect(serialized).not.toContain(TAIL);
658+
expect(serialized).not.toContain(TAIL.slice(0, 8));
659+
}
660+
});
661+
662+
it("stops at the real closing apostrophe instead of swallowing later YAML", function* () {
663+
const document = `password: '${CREDENTIAL}' owner: someoneelseentirely`;
664+
665+
const findings = (yield* createSecretScanner().scan(document)).filter(
666+
(finding) => finding.ruleId === "@executablemd/secretlint-rule-credentials",
667+
);
668+
669+
expect(findings).toHaveLength(1);
670+
const { column } = findings[0]!.location;
671+
expect(column).toBe(document.indexOf(CREDENTIAL));
672+
expect(document.slice(column, column + CREDENTIAL.length)).toBe(CREDENTIAL);
673+
});
674+
675+
it("leaves a single-quoted placeholder alone, directly and serialized", function* () {
676+
const scanner = createSecretScanner();
677+
678+
expect(yield* scanner.scan(PLACEHOLDER_SOURCE)).toEqual([]);
679+
expect(yield* scanner.scan(serializeDurableEvent(asImport(PLACEHOLDER_SOURCE)))).toEqual([]);
680+
});
681+
});

0 commit comments

Comments
 (0)