Skip to content

Commit 35de45c

Browse files
committed
Report the credential's own offset, not the quote in front of it
Field extraction now returns where the credential begins alongside its text: the body start for a quoted value, the value position for a bare one. The offset is not cosmetic. A finding's fingerprint is derived from the text its reported range covers, so a range starting at the opening quote hashed a different slice — and the same credential carried a different fingerprint depending on whether it happened to be quoted, which defeats the one thing a fingerprint is for. A regression test scans one credential in three encodings through a single scanner and asserts one fingerprint and three correct columns. Also drops a comment that restated the line under it, per review. Refs #199
1 parent e2c56f7 commit 35de45c

2 files changed

Lines changed: 84 additions & 11 deletions

File tree

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const BARE_VALUE = /^[^\s,}"'\\]+/;
210210
function readQuotedValue(
211211
content: string,
212212
openAt: number,
213-
): { value: string; end: number } | undefined {
213+
): { value: string; at: number } | undefined {
214214
const escaped = content[openAt] === "\\";
215215
const mark = content[openAt + (escaped ? 1 : 0)];
216216
if (mark !== '"' && mark !== "'") {
@@ -233,21 +233,33 @@ function readQuotedValue(
233233
if (closes) {
234234
// A doubly-escaped delimiter owns the backslash in front of its quote.
235235
const valueEnd = escaped ? at - 1 : at;
236-
return { value: content.slice(bodyStart, valueEnd), end: at + 1 };
236+
return { value: content.slice(bodyStart, valueEnd), at: bodyStart };
237237
}
238238
}
239239

240240
return undefined;
241241
}
242242

243243
/**
244-
* The complete value of a credential-named field.
244+
* The complete value of a credential-named field, and where it begins.
245245
*
246246
* A quoted value runs to its actual closing quote — spaces, punctuation, and
247247
* escape sequences included. Only an unquoted value ends at whitespace.
248+
*
249+
* The offset is the first character of the credential itself, past any
250+
* opening quote or escape. It is not cosmetic: a finding's fingerprint is
251+
* derived from the text the reported range covers, so an offset pointing at
252+
* the quote would hash a different slice and give the same credential two
253+
* fingerprints depending on whether it happened to be quoted.
248254
*/
249-
function readFieldValue(content: string, at: number): string | undefined {
250-
return readQuotedValue(content, at)?.value ?? BARE_VALUE.exec(content.slice(at))?.[0];
255+
function readFieldValue(content: string, at: number): { value: string; at: number } | undefined {
256+
const quoted = readQuotedValue(content, at);
257+
if (quoted) {
258+
return quoted;
259+
}
260+
261+
const bare = BARE_VALUE.exec(content.slice(at))?.[0];
262+
return bare === undefined ? undefined : { value: bare, at };
251263
}
252264

253265
/** An auth scheme in front of the value it carries. */
@@ -303,17 +315,14 @@ export const xmdCredentialRule: SecretLintRuleCreator = {
303315
if (head.index === undefined) {
304316
continue;
305317
}
306-
const at = head.index + head[0].length;
307-
const value = readFieldValue(content, at);
308-
if (value !== undefined) {
309-
report(value, at, "CREDENTIAL_FIELD");
318+
const found = readFieldValue(content, head.index + head[0].length);
319+
if (found !== undefined) {
320+
report(found.value, found.at, "CREDENTIAL_FIELD");
310321
}
311322
}
312323
}
313324

314325
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.
317326
const credential = value.replace(SCHEME, "");
318327
const start = at + (value.length - credential.length);
319328

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { describe, it } from "@executablemd/test-support/bdd";
1515
import { expect } from "@executablemd/test-support/expect";
1616
import { serializeDurableEvent } from "@executablemd/durable-streams";
1717
import type { DurableEvent } from "@executablemd/durable-streams";
18+
import type { Operation } from "effection";
19+
import type { SecretFinding } from "../src/secrets/findings.ts";
1820
import { createSecretScanner } from "../src/secrets/scanner.ts";
1921
import {
2022
isPlaceholder,
@@ -419,3 +421,65 @@ describe("escapes inside a quoted credential field", () => {
419421
});
420422
}
421423
});
424+
425+
/**
426+
* One credential, three encodings, one scanner.
427+
*
428+
* A finding's fingerprint is derived from the text its reported range covers,
429+
* so the range has to start at the credential itself. An offset left pointing
430+
* at an opening quote hashes a different slice, and the same credential then
431+
* carries two fingerprints depending on whether it happened to be quoted —
432+
* which defeats the one thing a fingerprint is for.
433+
*/
434+
describe("reported offsets locate the credential itself", () => {
435+
const CRED = "aB3xK9mQ7pL2wR5t";
436+
437+
const unquoted = `password=${CRED}`;
438+
const quoted = `{"password":"${CRED}"}`;
439+
const serialized = serializeDurableEvent({
440+
type: "yield",
441+
coroutineId: "root",
442+
description: { type: "import_component", name: "__root__" },
443+
result: { status: "ok", value: { path: "README.md", content: quoted } },
444+
});
445+
446+
/** The XMD field finding, which is the one whose offset is under test. */
447+
function* fieldFinding(
448+
scanner: { scan: (c: string) => Operation<SecretFinding[]> },
449+
content: string,
450+
) {
451+
const findings = (yield* scanner.scan(content)).filter(
452+
(finding) => finding.ruleId === "@executablemd/secretlint-rule-credentials",
453+
);
454+
expect(findings).toHaveLength(1);
455+
return findings[0]!;
456+
}
457+
458+
it("gives one credential one fingerprint across all three encodings", function* () {
459+
// One scanner, so one HMAC key — fingerprints are only comparable within
460+
// a single execution by design.
461+
const scanner = createSecretScanner();
462+
463+
const fromUnquoted = yield* fieldFinding(scanner, unquoted);
464+
const fromQuoted = yield* fieldFinding(scanner, quoted);
465+
const fromSerialized = yield* fieldFinding(scanner, serialized);
466+
467+
expect(fromQuoted.fingerprint).toBe(fromUnquoted.fingerprint);
468+
expect(fromSerialized.fingerprint).toBe(fromUnquoted.fingerprint);
469+
});
470+
471+
it("reports the credential's own index, not the quote or escape in front of it", function* () {
472+
const scanner = createSecretScanner();
473+
474+
for (const content of [unquoted, quoted, serialized]) {
475+
const finding = yield* fieldFinding(scanner, content);
476+
477+
// Every fixture is one line, so the column is the index in the string.
478+
expect(finding.location.line).toBe(1);
479+
expect(finding.location.column).toBe(content.indexOf(CRED));
480+
expect(content.slice(finding.location.column, finding.location.column + CRED.length)).toBe(
481+
CRED,
482+
);
483+
}
484+
});
485+
});

0 commit comments

Comments
 (0)