Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,31 @@ export const HOOK_001 = {
};

// Rule 5 — API keys/secrets committed (outside *.example templates).
// AWS-Dokumentations-Beispiele (Access-Key-IDs auf "…EXAMPLE") sind keine
// committeten Credentials: Agent-Configs dokumentieren häufig Cloud-Setup
// (z. B. ein AGENTS.md für einen AWS-Emulator).
//
// Bewusst KEINE Liste konkreter Beispiel-Keys: solche Literale sehen selbst wie
// ein Secret aus und triggern ältere Scanner-Versionen (im CI-Smoke-Test passiert:
// die veröffentlichte Auto-Action flaggte diese Datei). Stattdessen strukturell:
// echte Access-Key-IDs enden praktisch nie auf "EXAMPLE" (36^7 ≈ 7,8e10 Fälle).
const AWS_DOC_EXAMPLE_SUFFIX = "EXAMPLE";

function stripAwsDocExamples(content) {
return content.replace(/AKIA[0-9A-Z]{16}/g, (m) =>
m.endsWith(AWS_DOC_EXAMPLE_SUFFIX) ? "" : m
);
}

export const SECRET_001 = {
id: "SECRET-001",
severity: "critical",
description: "High-entropy credential pattern committed to the repository.",
scan({ rel, content }) {
if (/(\.example|\.sample|\.template|\.bak|\.orig|\.fixture)$/i.test(rel)) return null;
// Dokumentations-Beispiele neutralisieren, BEVOR gematcht wird. Das
// Entfernen berührt keine Zeilenumbrüche, Zeilennummern bleiben korrekt.
const hay = stripAwsDocExamples(content);
const patterns = {
"sk-…": /sk-[A-Za-z0-9]{20,}/g,
"ghp_…": /ghp_[A-Za-z0-9]{25,}/g,
Expand All @@ -241,11 +260,11 @@ export const SECRET_001 = {
let firstLine = null;
for (const [kind, re] of Object.entries(patterns)) {
re.lastIndex = 0;
if (re.test(content)) {
if (re.test(hay)) {
detected.push(kind);
if (firstLine === null) {
const idx = content.search(re);
firstLine = idx >= 0 ? lineOf(content, idx) : null;
const idx = hay.search(re);
firstLine = idx >= 0 ? lineOf(hay, idx) : null;
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/scanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ test("real slack token format is a secret", async () => {
assert.equal(res.severity, "critical");
});

test("AWS documentation example key is not a secret", async () => {
const { SECRET_001 } = await import("../src/rules.js");
// Bewusst zusammengesetzt (wie beim Slack-Token): das Literal würde ältere
// Scanner-Versionen triggern. Zur Laufzeit ist es exakt der Beispiel-Key aus
// den AWS-Docs (Form AKIA…EXAMPLE).
const AWS_DOC_EXAMPLE = "AKIA" + "IOSFODNN7EXAMPLE";
const res = SECRET_001.scan({
rel: "AGENTS.md",
content:
'aws_access_key_id="' + AWS_DOC_EXAMPLE + '", aws_secret_access_key="test", region="us-east-1"\n',
});
assert.equal(res, null, "AWS docs example credentials document setup, they are not leaks");
});

test("non-example AWS key shape is still flagged as secret", async () => {
const { SECRET_001 } = await import("../src/rules.js");
const key = "AKIA" + "3J7Q2PLZKQ9WRTUV";
const res = SECRET_001.scan({ rel: "AGENTS.md", content: "aws_access_key_id=" + key + "\n" });
assert.ok(res, "a non-example AWS key shape must stay critical");
assert.equal(res.severity, "critical");
});

test("everyday prose with curl is not instruction override", async () => {
const { INSTR_OVR_001 } = await import("../src/rules.js");
const res = INSTR_OVR_001.scan({
Expand Down
Loading