test: stabilize media attachment cache path assertions

This commit is contained in:
Shakker 2026-03-30 16:43:11 +01:00 committed by Shakker
parent 4c45fc3575
commit 555a4d896c
1 changed files with 10 additions and 4 deletions

View File

@ -125,6 +125,7 @@ describe("media understanding attachments SSRF", () => {
const attachmentPath = path.join(allowedRoot, "voice-note.m4a");
await fs.mkdir(allowedRoot, { recursive: true });
await fs.writeFile(attachmentPath, "ok");
const canonicalAttachmentPath = await fs.realpath(attachmentPath).catch(() => attachmentPath);
const cache = new MediaAttachmentCache([{ index: 0, path: attachmentPath }], {
localPathRoots: [allowedRoot],
@ -134,7 +135,9 @@ describe("media understanding attachments SSRF", () => {
openSpy.mockImplementation(async (filePath, flags) => {
const handle = await originalOpen(filePath, flags);
if (filePath !== attachmentPath) {
const candidatePath =
(await fs.realpath(String(filePath)).catch(() => String(filePath))) ?? String(filePath);
if (candidatePath !== canonicalAttachmentPath) {
return handle;
}
const mockedHandle = handle as typeof handle & {
@ -159,6 +162,7 @@ describe("media understanding attachments SSRF", () => {
const attachmentPath = path.join(allowedRoot, "voice-note.m4a");
await fs.mkdir(allowedRoot, { recursive: true });
await fs.writeFile(attachmentPath, "ok");
const canonicalAttachmentPath = await fs.realpath(attachmentPath).catch(() => attachmentPath);
const cache = new MediaAttachmentCache([{ index: 0, path: attachmentPath }], {
localPathRoots: [allowedRoot],
@ -167,10 +171,12 @@ describe("media understanding attachments SSRF", () => {
await cache.getBuffer({ attachmentIndex: 0, maxBytes: 1024, timeoutMs: 1000 });
expect(openSpy).toHaveBeenCalledWith(
attachmentPath,
fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW,
expect(openSpy).toHaveBeenCalled();
const [openedPath, openedFlags] = openSpy.mock.calls[0] ?? [];
expect(await fs.realpath(String(openedPath)).catch(() => String(openedPath))).toBe(
canonicalAttachmentPath,
);
expect(openedFlags).toBe(fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
});
});
});