diff --git a/scripts/lib/memory-vault-format.js b/scripts/lib/memory-vault-format.js index 006f32e30..4c65e514f 100644 --- a/scripts/lib/memory-vault-format.js +++ b/scripts/lib/memory-vault-format.js @@ -279,8 +279,21 @@ function parseMemoryDocument(source, sourcePath = '') { return normalizeMemory({ ...parsed.values, body }); } +function collectStrings(value, strings = []) { + if (typeof value === 'string') { + strings.push(value); + } else if (Array.isArray(value)) { + value.forEach(item => collectStrings(item, strings)); + } else if (value && typeof value === 'object') { + Object.values(value).forEach(item => collectStrings(item, strings)); + } + return strings; +} + +// Scan decoded field values: JSON.stringify turns a newline or tab into `\n` +// or `\t`, and the escape letter would defeat the patterns' leading `\b`. function findPotentialSecrets(value) { - const text = typeof value === 'string' ? value : ''; + const text = typeof value === 'string' ? value : collectStrings(value).join('\n'); return SECRET_PATTERNS .filter(item => item.pattern.test(text)) .map(item => item.label) diff --git a/scripts/lib/memory-vault.js b/scripts/lib/memory-vault.js index e79cb1a73..452db369f 100644 --- a/scripts/lib/memory-vault.js +++ b/scripts/lib/memory-vault.js @@ -319,7 +319,7 @@ function normalizeSaveInput(input, options) { function saveMemory(input, options = {}) { const roots = options.roots || resolveVaultRoots(options); const memory = normalizeSaveInput(input || {}, options); - const secretKinds = findPotentialSecrets(JSON.stringify(memory)); + const secretKinds = findPotentialSecrets(memory); if (secretKinds.length > 0) { throw new Error(`Refusing to save memory containing a suspected secret (${secretKinds.join(', ')}).`); } @@ -501,7 +501,7 @@ function readMemoryFiles(options = {}) { scannedBytes += sourceBytes; const memory = parseMemoryDocument(source, filePath); assertMemoryMatchesLocation(memory, scope, root, filePath); - if (findPotentialSecrets(JSON.stringify(memory)).length > 0) { + if (findPotentialSecrets(memory).length > 0) { const error = new Error('Memory contains a suspected secret.'); error.code = 'ECC_MEMORY_SECRET'; throw error; diff --git a/tests/lib/memory-vault.test.js b/tests/lib/memory-vault.test.js index ec37e32fa..760747155 100644 --- a/tests/lib/memory-vault.test.js +++ b/tests/lib/memory-vault.test.js @@ -517,6 +517,45 @@ test('quarantines imported secrets and metadata that disagrees with its vault lo } }); +test('detects secrets that start right after a newline or tab in the body', () => { + const fixture = createFixture(); + try { + const savedToken = `ghp_${'c3'.repeat(12)}`; + assert.throws( + () => saveMemory({ title: 'Newline secret', body: `first line\n${savedToken}` }, fixedOptions(fixture.roots)), + /suspected secret/i + ); + assert.throws( + () => saveMemory({ title: 'Tab secret', body: `key:\t${savedToken}` }, fixedOptions(fixture.roots)), + /suspected secret/i + ); + + const notes = path.join(fixture.roots.project, 'notes'); + fs.mkdirSync(notes, { recursive: true }); + const importedToken = `npm_${'d4'.repeat(12)}`; + fs.writeFileSync( + path.join(notes, 'newline-secret.md'), + serializeMemoryDocument(baseMemory({ + id: 'mem_20260726_newline_secret', + kind: 'note', + links: [], + body: `Imported token:\n${importedToken}`, + })) + ); + const report = doctorMemoryVault({ + roots: fixture.roots, + scopes: ['project'], + }); + assert.deepStrictEqual( + report.invalidFiles.map(item => item.code), + ['suspected-secret'] + ); + assert.strictEqual(JSON.stringify(report).includes(importedToken), false); + } finally { + fs.rmSync(fixture.root, { recursive: true, force: true }); + } +}); + // Windows reports dev = 0 from path-based stat()/lstat() while fstat() on an open // handle reports the real volume serial number, so a strict dev comparison can never // match and every vault read/write is rejected. The stat pairs below are the values