Skip to content

Commit 6a22fff

Browse files
fix(tools): keep an absolute caller spelling out of the link rejection too (U7, Zoo-Code-Org#1375)
The reviewer verified the ancestor fix at 69776b1 and qualified it: a caller that supplies an absolute displayPath still had that host spelling cut into linkName, so the rejection message could carry an absolute path even though it no longer carried the guard's resolved ancestor. An absolute spelling is a host path the same way the ancestor spelling is, so when the caller spelled it absolutely, keep only the component the walk stopped at - the caller's own name for the link - and drop the prefix the way the branch above drops the ancestor it resolved. The spec now drives the same dangling-link scenario through both spellings and asserts both directions for each: the link is named in the caller's words, and no absolute workspace path reaches the message. Relative spellings keep their prefix. Co-Authored-By: Claude <[email protected]>
1 parent 69776b1 commit 6a22fff

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

‎src/core/tools/__tests__/guardedWrite.spec.ts‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -352,31 +352,40 @@ describe("guardedWrite (S4a, epic #1375)", () => {
352352
// reports; rejoining the lexical names would authorize a write whose publish
353353
// lands outside the container that was checked.
354354
const planted = path.join(path.resolve(WORKSPACE), "linkdir")
355-
mockedFsRealpath.mockImplementation(async (p) => {
356-
const s = String(p)
357-
if (s === path.resolve(WORKSPACE)) return path.resolve(WORKSPACE)
358-
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" })
359-
})
360-
mockedFsLstat.mockImplementation(async (p) => {
361-
if (String(p) === planted) {
362-
return { isSymbolicLink: () => true } as never
355+
// Both spellings of the same target must land the same way: the rejection
356+
// names the link in the caller's own words and carries no host path, so an
357+
// absolute spelling must not smuggle the prefix back into the message.
358+
const rejectFor = async (spelling: string): Promise<string> => {
359+
mockedFsRealpath.mockImplementation(async (p) => {
360+
const s = String(p)
361+
if (s === path.resolve(WORKSPACE)) return path.resolve(WORKSPACE)
362+
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" })
363+
})
364+
mockedFsLstat.mockImplementation(async (p) => {
365+
if (String(p) === planted) {
366+
return { isSymbolicLink: () => true } as never
367+
}
368+
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" })
369+
})
370+
const task = createMockTask()
371+
let rejection: Error | undefined
372+
try {
373+
await guardedWrite(task, spelling, "data", "create")
374+
} catch (error: unknown) {
375+
rejection = error instanceof Error ? error : new Error(String(error))
363376
}
364-
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" })
365-
})
366-
const task = createMockTask()
367-
368-
let rejection: Error | undefined
369-
try {
370-
await guardedWrite(task, "linkdir/nested/new.txt", "data", "create")
371-
} catch (error: unknown) {
372-
rejection = error instanceof Error ? error : new Error(String(error))
377+
expect(mockedSafeWriteText).not.toHaveBeenCalled()
378+
return rejection?.message ?? ""
373379
}
374-
// The rejection names the link in the caller's own spelling and carries none
375-
// of the guard's absolute ancestor, which is a host path on the user's machine.
376-
expect(rejection?.message).toContain("runs through a link (linkdir)")
377-
expect(rejection?.message).not.toContain(path.resolve(WORKSPACE))
378-
379-
expect(mockedSafeWriteText).not.toHaveBeenCalled()
380+
const relative = await rejectFor("linkdir/nested/new.txt")
381+
// Named the way the caller named it, and none of the guard's ancestor.
382+
expect(relative).toContain("runs through a link (linkdir)")
383+
expect(relative).not.toContain(path.resolve(WORKSPACE))
384+
const absolute = await rejectFor(path.join(path.resolve(WORKSPACE), "linkdir", "nested", "new.txt"))
385+
// The same two directions for the host spelling: the link is still named,
386+
// the host prefix that spelling carried is still absent.
387+
expect(absolute).toContain("runs through a link (linkdir)")
388+
expect(absolute).not.toContain(path.resolve(WORKSPACE))
380389
})
381390

382391
it("refuses a target whose leaf is a dangling symlink", async () => {

‎src/core/tools/guardedWrite.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,14 @@ async function realpathNearest(target: string, displayPath: string): Promise<str
469469
const belowLink = missing.slice().reverse()
470470
const spelled = displayPath.split(/[\\/]/)
471471
const keep = Math.max(1, spelled.length - belowLink.length)
472-
const linkName = spelled.slice(0, keep).join("/") || path.basename(ancestor)
472+
const spelledLink = spelled.slice(0, keep).join("/")
473+
// An absolute spelling is a host path as much as the guard's resolved
474+
// ancestor is, and this rejection is model-facing text either way. When
475+
// the caller spelled the path absolutely, keep only the component the
476+
// walk stopped at - the caller's own name for the link - and drop the
477+
// prefix the way the branch above drops the ancestor it resolved.
478+
const callerName = path.isAbsolute(spelledLink) ? spelled[keep - 1] : spelledLink
479+
const linkName = callerName || path.basename(ancestor)
473480
throw new GuardRejectedError(
474481
`Path runs through a link (${linkName}) that does not resolve -- retry with a path inside the workspace.`,
475482
displayPath,

0 commit comments

Comments
 (0)