Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Bind the config diagnostics no-leak check to fields, not the payload (#4773)
Nick Gomez3df56fde42cc
diff --git a/packages/core/src/config/collect-config-diagnostics.test.ts b/packages/core/src/config/collect-config-diagnostics.test.tsindex 9f81cd3ca..33a4b90d5 100644--- a/packages/core/src/config/collect-config-diagnostics.test.ts+++ b/packages/core/src/config/collect-config-diagnostics.test.ts@@ -1,9 +1,10 @@import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { dirname, resolve } from 'node:path';+import { basename, dirname, resolve } from 'node:path';import { afterEach, beforeEach, describe, expect, test } from 'vitest';import { stringify } from 'yaml';import { collectConfigDiagnostics } from './collect-config-diagnostics.ts';+import { serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey } from './config-leak-serializer.test-helper.ts';import type { WriteScope } from './errors.ts';import { REMOVED_KEYS } from './removed-keys.ts';import { resolveConfigPath } from './write-config-patch.ts';@@ -26,10 +27,47 @@ let projectDir: string;let homeDir: string;const quiet = () => {};+const CONFIG_VALUES_THE_FINDING_FIELDS_MUST_NEVER_ECHO = [+ 'SENTINEL_SIBLING_DIR',+ 'SENTINEL_REMOVED_LEAF',+ 'USER_ONLY_VALUE',+ 'USER_HOST_VALUE',+ 'PROJECT_ONLY_VALUE',+ 'PROJECT_FOLDER_VALUE',+ 'LOCAL_ONLY_VALUE',+ 'midnight',+ 'PRIVATE_INVALID_VALUE',+ '900000',+] as const;++const FIXTURE_PATH_DELIBERATELY_CARRIES_EVERY_FORBIDDEN_VALUE =+ CONFIG_VALUES_THE_FINDING_FIELDS_MUST_NEVER_ECHO.join('-');++const NAME_MAX_BYTES_PER_PATH_COMPONENT = 255;++const ROOM_FOR_ONE_MORE_FORBIDDEN_VALUE =+ 1 + Math.max(...CONFIG_VALUES_THE_FINDING_FIELDS_MUST_NEVER_ECHO.map((value) => value.length));++const FIXTURE_CONFIG_SCOPES = [+ 'user',+ 'project',+ 'project-local',+] as const satisfies readonly WriteScope[];++function suppliedConfigPaths(): readonly string[] {+ return FIXTURE_CONFIG_SCOPES.map((scope) => resolveConfigPath(scope, projectDir, homeDir));+}+beforeEach(() => {const stamp = `${Date.now()}-${Math.random().toString(36).slice(2)}`;- projectDir = resolve(tmpdir(), `ok-diag-project-${stamp}`);- homeDir = resolve(tmpdir(), `ok-diag-home-${stamp}`);+ projectDir = resolve(+ tmpdir(),+ `ok-diag-project-${FIXTURE_PATH_DELIBERATELY_CARRIES_EVERY_FORBIDDEN_VALUE}-${stamp}`,+ );+ homeDir = resolve(+ tmpdir(),+ `ok-diag-home-${FIXTURE_PATH_DELIBERATELY_CARRIES_EVERY_FORBIDDEN_VALUE}-${stamp}`,+ );mkdirSync(projectDir, { recursive: true });mkdirSync(homeDir, { recursive: true });});@@ -58,6 +96,48 @@ function collect() {}describe('collectConfigDiagnostics', () => {+ test('the fixture path carries every forbidden value, so no absence check can pass on path luck', () => {+ for (const value of CONFIG_VALUES_THE_FINDING_FIELDS_MUST_NEVER_ECHO) {+ expect(projectDir, `projectDir must carry ${value}`).toContain(value);+ expect(homeDir, `homeDir must carry ${value}`).toContain(value);+ }++ for (const dir of [projectDir, homeDir]) {+ const component = basename(dir);+ expect(+ component.length + ROOM_FOR_ONE_MORE_FORBIDDEN_VALUE,+ `${component} has no room left to carry another forbidden value: beforeEach mkdirSync throws ENAMETOOLONG before any assertion in this file can report it`,+ ).toBeLessThanOrEqual(NAME_MAX_BYTES_PER_PATH_COMPONENT);+ }+ });++ test('the leak serializer exempts exactly the fixture-supplied config paths under the file key, and no other value and no other key', () => {+ const supplied = resolveConfigPath('project', projectDir, homeDir);+ const suppliedPathUnderAKeyOtherThanFile = resolveConfigPath('user', projectDir, homeDir);+ const configValueThatHappensToSitUnderTheFixtureRoot = resolve(+ projectDir,+ 'DECOY_UNDER_FIXTURE_ROOT',+ );++ const serialized = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ {+ file: supplied,+ sidelinedTo: suppliedPathUnderAKeyOtherThanFile,+ issues: [{ file: 'VALUE_KEYED_FILE_BUT_NOT_A_SUPPLIED_PATH' }],+ detail: configValueThatHappensToSitUnderTheFixtureRoot,+ },+ suppliedConfigPaths(),+ );++ expect(serialized).not.toContain(JSON.stringify(supplied));+ expect(+ serialized,+ 'the exemption is not confined to the file key: it stripped a supplied path from another key',+ ).toContain(JSON.stringify(suppliedPathUnderAKeyOtherThanFile));+ expect(serialized).toContain(JSON.stringify('VALUE_KEYED_FILE_BUT_NOT_A_SUPPLIED_PATH'));+ expect(serialized).toContain(JSON.stringify(configValueThatHappensToSitUnderTheFixtureRoot));+ });+test('no config files → empty report', () => {expect(collect()).toEqual({ diagnostics: [] });});@@ -131,7 +211,10 @@ describe('collectConfigDiagnostics', () => {expect(finding, `finding for ${entry.path.join('.')}`).toBeDefined();expect(finding).toMatchObject({ scope: 'project-local', redirect: entry.redirect });- const serialized = JSON.stringify(diagnostics);+ const serialized = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ diagnostics,+ suppliedConfigPaths(),+ );expect(serialized).not.toContain('SENTINEL_SIBLING_DIR');expect(serialized).not.toContain('SENTINEL_REMOVED_LEAF');@@ -152,7 +235,22 @@ describe('collectConfigDiagnostics', () => {setPath(localCfg, ['appearance', 'sidebar', 'showAllFiles'], false);writeScopeConfig('project-local', localCfg);- const serialized = JSON.stringify(collect());+ const report = collect();++ expect(report.diagnostics).toMatchObject([+ { code: 'REMOVED_KEY', scope: 'user', path: ['server', 'host'] },+ { code: 'REMOVED_KEY', scope: 'project', path: ['folders'] },+ {+ code: 'REMOVED_KEY',+ scope: 'project-local',+ path: ['appearance', 'sidebar', 'showAllFiles'],+ },+ ]);++ const serialized = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ report,+ suppliedConfigPaths(),+ );for (const secret of ['USER_ONLY_VALUE','USER_HOST_VALUE',@@ -170,7 +268,12 @@ describe('collectConfigDiagnostics', () => {const { diagnostics } = collect();expect(diagnostics).toEqual([{ code: 'SCHEMA_INVALID', scope: 'project-local', file }]);- expect(JSON.stringify(diagnostics)).not.toContain('midnight');+ expect(+ serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ diagnostics,+ suppliedConfigPaths(),+ ),+ ).not.toContain('midnight');expect(existsSync(file)).toBe(true);});@@ -203,8 +306,12 @@ describe('collectConfigDiagnostics', () => {],},]);- expect(JSON.stringify(diagnostics)).not.toContain('PRIVATE_INVALID_VALUE');- expect(JSON.stringify(diagnostics)).not.toContain('900000');+ const valueBearingFields = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ diagnostics,+ suppliedConfigPaths(),+ );+ expect(valueBearingFields).not.toContain('PRIVATE_INVALID_VALUE');+ expect(valueBearingFields).not.toContain('900000');expect(existsSync(file)).toBe(true);});@@ -225,4 +332,45 @@ describe('collectConfigDiagnostics', () => {expect(diagnostics).toEqual([{ code: 'UNREADABLE', scope: 'project-local', file }]);});++ test('a report mixing REMOVED_KEY and VALUE_FALLBACK pins file on both arms and proves the leak scan is not vacuous', () => {+ const removed = REMOVED_KEYS.find(+ (k) => k.path.join('.') === 'appearance.sidebar.showAllFiles',+ );+ if (!removed) throw new Error('fixture key missing from registry');++ const projectCfg: Record<string, unknown> = { content: { dir: 'LEAK_PROBE_SIBLING_DIR' } };+ setPath(projectCfg, removed.path, 'LEAK_PROBE_REMOVED_LEAF');+ const projectFile = writeScopeConfig('project', projectCfg);++ const localFile = writeScopeRaw(+ 'project-local',+ 'search:\n semantic:\n maxBatchSize: LEAK_PROBE_FALLBACK_INPUT\n',+ );++ const { diagnostics } = collect();++ expect(diagnostics).toMatchObject([+ { code: 'REMOVED_KEY', scope: 'project', file: projectFile, path: removed.path },+ { code: 'VALUE_FALLBACK', scope: 'project-local', file: localFile },+ ]);++ const valueBearingFields = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ diagnostics,+ suppliedConfigPaths(),+ );+ expect(+ valueBearingFields,+ 'the leak serializer produced no haystack, so every absence check in this file is vacuous',+ ).toContain(JSON.stringify(removed.path));+ for (const probe of [+ 'LEAK_PROBE_SIBLING_DIR',+ 'LEAK_PROBE_REMOVED_LEAF',+ 'LEAK_PROBE_FALLBACK_INPUT',+ ]) {+ expect(valueBearingFields, `raw config value ${probe} reached a finding field`).not.toContain(+ probe,+ );+ }+ });});diff --git a/packages/core/src/config/config-leak-serializer.test-helper.ts b/packages/core/src/config/config-leak-serializer.test-helper.tsnew file mode 100644index 000000000..1c94ab927--- /dev/null+++ b/packages/core/src/config/config-leak-serializer.test-helper.ts@@ -0,0 +1,9 @@+export function serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ value: unknown,+ callerSuppliedPaths: readonly string[],+): string {+ const supplied = new Set(callerSuppliedPaths);+ return JSON.stringify(value, (key, val) =>+ key === 'file' && typeof val === 'string' && supplied.has(val) ? undefined : val,+ );+}diff --git a/packages/core/src/config/read-config-safely.test.ts b/packages/core/src/config/read-config-safely.test.tsindex 4cb8b70f7..9d91cc0ec 100644--- a/packages/core/src/config/read-config-safely.test.ts+++ b/packages/core/src/config/read-config-safely.test.ts@@ -1,11 +1,12 @@import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { resolve } from 'node:path';-import { afterEach, beforeEach, describe, expect, expectTypeOf, test } from 'vitest';+import { afterEach, beforeEach, describe, expect, test } from 'vitest';import { stringify } from 'yaml';import { resolveThemePlugin } from '../theme/theme-plugins.ts';+import { serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey } from './config-leak-serializer.test-helper.ts';import { isKnownConfigError } from './errors.ts';-import { type ReadConfigSafelyResult, readConfigSafely } from './read-config-safely.ts';+import { readConfigSafely } from './read-config-safely.ts';import { REMOVED_KEYS } from './removed-keys.ts';function isPlainObject(value: unknown): value is Record<string, unknown> {@@ -33,10 +34,16 @@ function leafPresent(value: unknown, path: readonly string[]): boolean {let testDir: string;+const CONFIG_VALUES_THE_VALUE_FALLBACK_ARM_MUST_NEVER_ECHO = ['PRIVATE_INVALID_VALUE'] as const;++const FIXTURE_PATH_DELIBERATELY_CARRIES_EVERY_FORBIDDEN_VALUE =+ CONFIG_VALUES_THE_VALUE_FALLBACK_ARM_MUST_NEVER_ECHO.join('-');+beforeEach(() => {+ const stamp = `${Date.now()}-${Math.random().toString(36).slice(2)}`;testDir = resolve(tmpdir(),- `ok-readconfig-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,+ `ok-readconfig-test-${FIXTURE_PATH_DELIBERATELY_CARRIES_EVERY_FORBIDDEN_VALUE}-${stamp}`,);mkdirSync(testDir, { recursive: true });});@@ -46,10 +53,99 @@ afterEach(() => {});describe('readConfigSafely', () => {- test('successful reads expose only recovered diagnostic codes', () => {- expectTypeOf<- Extract<ReadConfigSafelyResult, { valid: true }>['diagnostics'][number]['code']- >().toEqualTypeOf<'REMOVED_KEY' | 'VALUE_FALLBACK'>();+ test('the fixture path carries every value the VALUE_FALLBACK arm must not echo, so no absence check can pass on path luck', () => {+ for (const value of CONFIG_VALUES_THE_VALUE_FALLBACK_ARM_MUST_NEVER_ECHO) {+ expect(testDir, `testDir must carry ${value}`).toContain(value);+ }+ });++ test('the REMOVED_KEY arm echoes three raw config lines centred on where the removed value begins, so the window reaches past the stale key into a still-supported setting value, which the VALUE_FALLBACK arm omits', () => {+ const path = resolve(testDir, 'removed-with-snippet.yml');+ writeFileSync(+ path,+ [+ 'alphaUnknown: OUTSIDE_WINDOW_ABOVE',+ 'folders:',+ ' - path: "FLAGGED_ENTRY_ECHOED/**"',+ 'content: {dir: NEIGHBOUR_FOREIGN_VALUE_ECHOED}',+ 'deltaUnknown: OUTSIDE_WINDOW_BELOW',+ '',+ ].join('\n'),+ 'utf-8',+ );++ const result = readConfigSafely({ absPath: path, warn: () => {} });++ expect(result.valid).toBe(true);+ if (result.valid) {+ expect(result.value.content.dir).toBe('NEIGHBOUR_FOREIGN_VALUE_ECHOED');+ const removed = result.diagnostics.filter((d) => d.code === 'REMOVED_KEY');+ expect(removed).toHaveLength(1);+ const [diag] = removed;+ if (diag?.code === 'REMOVED_KEY') {+ const snippet = diag.source?.snippet;+ expect(snippet).toContain('folders:');+ expect(snippet).toContain('FLAGGED_ENTRY_ECHOED');+ expect(snippet).toContain('NEIGHBOUR_FOREIGN_VALUE_ECHOED');+ expect(snippet).not.toContain('OUTSIDE_WINDOW_ABOVE');+ expect(snippet).not.toContain('OUTSIDE_WINDOW_BELOW');+ }+ }+ });++ test('the leak serializer exempts exactly the caller-supplied paths under the file key, and no other value and no other key', () => {+ const supplied = resolve(testDir, 'supplied.yml');+ const suppliedPathUnderAKeyOtherThanFile = resolve(testDir, 'second-supplied.yml');+ const configValueThatHappensToSitUnderTheFixtureRoot = resolve(+ testDir,+ 'DECOY_UNDER_FIXTURE_ROOT',+ );++ const serialized = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ {+ file: supplied,+ sidelinedTo: suppliedPathUnderAKeyOtherThanFile,+ issues: [{ file: 'VALUE_KEYED_FILE_BUT_NOT_THE_SUPPLIED_PATH' }],+ detail: configValueThatHappensToSitUnderTheFixtureRoot,+ },+ [supplied, suppliedPathUnderAKeyOtherThanFile],+ );++ expect(serialized).not.toContain(JSON.stringify(supplied));+ expect(+ serialized,+ 'the exemption is not confined to the file key: it stripped a supplied path from another key',+ ).toContain(JSON.stringify(suppliedPathUnderAKeyOtherThanFile));+ expect(serialized).toContain(JSON.stringify('VALUE_KEYED_FILE_BUT_NOT_THE_SUPPLIED_PATH'));+ expect(serialized).toContain(JSON.stringify(configValueThatHappensToSitUnderTheFixtureRoot));+ });++ test('a fixture with both a removed key and a non-integer embeddings batch size reports exactly REMOVED_KEY and VALUE_FALLBACK, and no third code', () => {+ const path = resolve(testDir, 'recovered-and-removed.yml');+ writeFileSync(+ path,+ [+ 'folders:',+ ' - path: "LEGACY_ENTRY/**"',+ 'search:',+ ' semantic:',+ ' maxBatchSize: NOT_AN_INTEGER',+ '',+ ].join('\n'),+ 'utf-8',+ );++ const result = readConfigSafely({ absPath: path, warn: () => {} });++ expect(result.valid).toBe(true);+ if (!result.valid) return;++ const codes = result.diagnostics.map((d) => d.code);+ expect(+ codes,+ 'the fixture produced no recovered diagnostics, so the exclusion check below is vacuous',+ ).toEqual(expect.arrayContaining(['REMOVED_KEY', 'VALUE_FALLBACK']));+ expect([...new Set(codes)].sort()).toEqual(['REMOVED_KEY', 'VALUE_FALLBACK']);});test('missing file → valid=true, value is schema defaults', () => {@@ -157,7 +253,15 @@ describe('readConfigSafely', () => {result,);expect(warnings).toEqual([]);- expect(JSON.stringify(result.diagnostics)).not.toContain('PRIVATE_INVALID_VALUE');+ const valueBearingFields = serializeEveryFieldExceptCallerSuppliedPathsUnderTheFileKey(+ result.diagnostics,+ [path],+ );+ expect(+ valueBearingFields,+ 'the leak serializer produced no haystack, so the absence check below is vacuous',+ ).toContain(JSON.stringify(['search', 'semantic', 'maxBatchSize']));+ expect(valueBearingFields).not.toContain('PRIVATE_INVALID_VALUE');expect(result.diagnostics).not.toEqual(expect.arrayContaining([expect.objectContaining({ code: 'SCHEMA_INVALID' })]),);