fix(memory): scan decoded fields for secrets, not serialized JSON

This commit is contained in:
kevin authored and GitHub committed 2026-11-05 06:27:55 +09:00
1 parent b38be6a7f6
commit 3d7034872e
3 files changed
+55 -3

No files matched your search

+14 -1
View File
@@ -279,8 +279,21 @@ function parseMemoryDocument(source, sourcePath = '<memory>') {
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)
+2 -2
View File
@@ -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;
+39
View File
@@ -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