Skip to content

Commit aea8ee8

Browse files
rajmp999obviyus
andauthored
fix(memory-wiki): reject self-imported source pages (#125160)
Prevent unsafe-local scans from importing the active vault or copied generated source pages. Reuse canonical containment and the existing generated-page classifier while keeping legitimate files eligible. Closes #125139. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 0bd5c6a commit aea8ee8

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎extensions/memory-wiki/src/unsafe-local.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,74 @@ describe("syncMemoryWikiUnsafeLocalSources", () => {
206206
).resolves.toContain("remember this");
207207
});
208208

209+
it("skips generated source pages copied into a configured path", async () => {
210+
const privateDir = await createPrivateDir("copied-vault");
211+
const sourcePaths = [
212+
path.join(privateDir, "legitimate.md"),
213+
path.join(privateDir, "bridge-not-generated.md"),
214+
path.join(privateDir, "unsafe-local-not-generated.md"),
215+
];
216+
await Promise.all(
217+
sourcePaths.map((sourcePath) => fs.writeFile(sourcePath, `# ${path.basename(sourcePath)}\n`)),
218+
);
219+
const { rootDir: vaultDir, config } = await createVault({
220+
rootDir: nextCaseRoot("copied-vault-target"),
221+
config: {
222+
vaultMode: "unsafe-local",
223+
unsafeLocal: {
224+
allowPrivateMemoryCoreAccess: true,
225+
paths: [privateDir],
226+
},
227+
},
228+
});
229+
230+
const first = await syncMemoryWikiUnsafeLocalSources(config);
231+
const generatedPage = first.pagePaths.find((pagePath) => pagePath.includes("legitimate-md"));
232+
expect(generatedPage).toBeDefined();
233+
const copiedPage = path.join(privateDir, "mirror", generatedPage!);
234+
await fs.mkdir(path.dirname(copiedPage), { recursive: true });
235+
await fs.copyFile(path.join(vaultDir, generatedPage!), copiedPage);
236+
237+
const second = await syncMemoryWikiUnsafeLocalSources(config);
238+
239+
expect(second.artifactCount).toBe(sourcePaths.length);
240+
expect(second.importedCount).toBe(0);
241+
expect(second.skippedCount).toBe(sourcePaths.length);
242+
expect(second.pagePaths).toHaveLength(sourcePaths.length);
243+
});
244+
245+
it("skips a configured source whose canonical path is inside the active vault", async () => {
246+
const privateDir = await createPrivateDir("canonical-vault");
247+
const sourcePath = path.join(privateDir, "legitimate.md");
248+
await fs.writeFile(sourcePath, "# legitimate\n", "utf8");
249+
const { rootDir: vaultDir, config } = await createVault({
250+
rootDir: nextCaseRoot("canonical-vault-target"),
251+
config: {
252+
vaultMode: "unsafe-local",
253+
unsafeLocal: {
254+
allowPrivateMemoryCoreAccess: true,
255+
paths: [sourcePath],
256+
},
257+
},
258+
});
259+
const first = await syncMemoryWikiUnsafeLocalSources(config);
260+
const generatedPage = path.join(vaultDir, first.pagePaths[0] ?? "");
261+
const configWithVaultPage = {
262+
...config,
263+
unsafeLocal: {
264+
...config.unsafeLocal,
265+
paths: [sourcePath, generatedPage],
266+
},
267+
};
268+
269+
const second = await syncMemoryWikiUnsafeLocalSources(configWithVaultPage);
270+
271+
expect(second.artifactCount).toBe(1);
272+
expect(second.importedCount).toBe(0);
273+
expect(second.skippedCount).toBe(1);
274+
expect(second.pagePaths).toHaveLength(1);
275+
});
276+
209277
it("caps composed unsafe-local filenames to the filesystem component limit", async () => {
210278
const privateDir = await createPrivateDir(`${"漢".repeat(50)}-private`);
211279
const nestedDir = path.join(privateDir, `${"語".repeat(50)}-nested`);

‎extensions/memory-wiki/src/unsafe-local.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
renderMarkdownFence,
1515
renderWikiMarkdown,
1616
slugifyWikiSegment,
17+
toWikiPageSummary,
1718
} from "./markdown.js";
1819
import { writeImportedSourcePage } from "./source-page-shared.js";
1920
import { resolveArtifactKey } from "./source-path-shared.js";
@@ -38,6 +39,7 @@ type UnsafeLocalArtifactCollection = {
3839
};
3940

4041
const DIRECTORY_TEXT_EXTENSIONS = new Set([".json", ".jsonl", ".md", ".txt", ".yaml", ".yml"]);
42+
const GENERATED_IMPORTED_SOURCE_PREFIXES = ["bridge-", "unsafe-local-"];
4143
const UNSAFE_LOCAL_SYNC_CONCURRENCY = 16;
4244

4345
function detectFenceLanguage(filePath: string): string {
@@ -73,6 +75,7 @@ async function listAllowedFilesRecursive(rootDir: string): Promise<string[]> {
7375

7476
async function collectUnsafeLocalArtifacts(
7577
configuredPaths: string[],
78+
vaultRootKey: string,
7679
): Promise<UnsafeLocalArtifactCollection> {
7780
const artifacts: UnsafeLocalArtifact[] = [];
7881
const unavailableConfiguredPaths: string[] = [];
@@ -108,6 +111,20 @@ async function collectUnsafeLocalArtifacts(
108111

109112
const deduped = new Map<string, UnsafeLocalArtifact>();
110113
for (const artifact of artifacts) {
114+
if (isPathInside(vaultRootKey, artifact.syncKey)) {
115+
continue;
116+
}
117+
const sourceName = normalizeLowercaseStringOrEmpty(path.basename(artifact.absolutePath));
118+
if (GENERATED_IMPORTED_SOURCE_PREFIXES.some((prefix) => sourceName.startsWith(prefix))) {
119+
const sourcePage = toWikiPageSummary({
120+
absolutePath: artifact.absolutePath,
121+
relativePath: `sources/${sourceName}`,
122+
raw: await fs.readFile(artifact.absolutePath, "utf8"),
123+
});
124+
if (sourcePage?.importedSourceBody) {
125+
continue;
126+
}
127+
}
111128
deduped.set(artifact.syncKey, artifact);
112129
}
113130
return { artifacts: [...deduped.values()], unavailableConfiguredPaths };
@@ -226,8 +243,10 @@ export async function syncMemoryWikiUnsafeLocalSources(
226243
};
227244
}
228245

246+
const vaultRootKey = await resolveArtifactKey(config.vault.path);
229247
const { artifacts, unavailableConfiguredPaths } = await collectUnsafeLocalArtifacts(
230248
config.unsafeLocal.paths,
249+
vaultRootKey,
231250
);
232251
const state = await readMemoryWikiSourceSyncState(config.vault.path);
233252
let initializePromise: ReturnType<typeof initializeMemoryWikiVault> | undefined;

0 commit comments

Comments
 (0)