Skip to content

Commit cb969ed

Browse files
u5a unit 5b: rank ancestor pins by the spelling that matched, and cover the accept path of a path-only pin
Two open review rows on this head were edits, and each lands here with a row that fails without it. src/services/file-safety/safeWriteText.ts : _deepestPinnedDirectory accepted a candidate that matched through either spelling but ranked them by the length of their dir field, so a pin that only reached the created name through the location its alias resolved to could outrank a deeper pin whose own name contains it, and the containment check then ran against the shallower location. A directory created under the deep pin that became a link into the shallow one passed it. Candidates are now ranked by the length of the spelling that actually matched the created name. src/services/file-safety/__tests__/safeWriteText.spec.ts : two rows. - "refuses to publish when a deeper pin covers the created name better than a longer alias spelling does" (line 2241) is red on the pre-fix production file: promise resolved undefined instead of rejecting, measured as 1 failed | 102 passed (103) with the head production file beside it, against 101 passed on the untouched head footing and 103 passed with this commit. - "publishes when only the path form of the name pin is given and its parent resolves in place" (line 2493) is the accept path the reject-only row left untested: under a mutant that refuses path-form pins in the effective-parent selection it is red (2 failed | 101 passed, 103), and no row in the block it was asked for was red under that mutant on the head footing (1 failed | 100 passed, 101), which is the hole the row closes. Legs the repository hook would have run, carried rather than skipped: - vitest.services.config.ts over services/file-safety/__tests__/safeWriteText.spec.ts + safeWriteText.integration.spec.ts : 2 files passed, 106 tests passed, exit 0. - prettier 3.9.10 (pinned isolated install) --check over the whole tree : All matched files use Prettier code style, exit 0. - tsc --noEmit in src : 429 error lines on this tree and 429 on the untouched head beside it, and after stripping line and column numbers the two lists differ by zero lines; both exit 2 on the pre-existing baseline of this checkout. - knip --treat-config-hints-as-errors : byte-for-byte the same findings on this tree and on the head footing (Unused files 1, Unused dependencies 2, Unlisted binaries 8, Unused exports 1, unused exported enum members 4, configuration hints 2), exit 1 on both sides. - eslint per file : not run. The pinned config cannot start on this host because @eslint/js is absent from the install (Error [ERR_MODULE_NOT_FOUND] from packages/config-eslint/base.js), exit 2. - src/eslint-suppressions.json : untouched, read back byte-identical to the head blob at 30,910 B. No changeset, no CHANGELOG edit, no suppression added.
1 parent 4a20286 commit cb969ed

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,46 @@ describe("asks landed at the refreshed 2357 footing", () => {
22382238
await expect(promise).resolves.toBeUndefined()
22392239
expect(fs.rename).toHaveBeenCalledWith(expect.stringContaining("safeWriteText_"), target)
22402240
})
2241+
it("refuses to publish when a deeper pin covers the created name better than a longer alias spelling does", async () => {
2242+
// Two pins cover the name mkdir reported creating. The identity pin reached it
2243+
// through an alias, so its dir is the alias spelling /ws/long-root-alias - longer
2244+
// than anything on the deeper side - while only the location that alias resolved to,
2245+
// /ws, covers the created name at all. The path-form pin names the created directory
2246+
// itself. Ranking candidates by the length of their dir field put the longer alias
2247+
// first, so the containment check ran against /ws and a created /ws/a/new that had
2248+
// become a link into /ws/other passed it, publishing through ground the caller never
2249+
// authorized under that name.
2250+
const shallow = path.resolve("/ws")
2251+
const aliasDir = path.resolve("/ws/long-root-alias")
2252+
const created = path.resolve("/ws/a/new")
2253+
const redirected = path.join(shallow, "other", "new")
2254+
const target = path.join(created, "target.txt")
2255+
let linkPlanted = false
2256+
const enoent = () => Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" })
2257+
vi.mocked(fs.realpath).mockImplementation(async (p) => {
2258+
const s = String(p)
2259+
if (s === target || (s === created && !linkPlanted)) throw enoent()
2260+
// The alias spelling stands for the location it was recorded resolving to, and the
2261+
// created name became a link into shallower grounds between the canonicalization
2262+
// and the mkdir answer.
2263+
if (path.resolve(s) === aliasDir) return shallow
2264+
return s === created ? redirected : s
2265+
})
2266+
vi.mocked(fs.mkdir).mockImplementation(async () => {
2267+
linkPlanted = true
2268+
return created
2269+
})
2270+
vi.mocked(fsSync.openSync).mockReturnValue(1)
2271+
const promise = safeWriteText(target, "data", {
2272+
platform: "linux",
2273+
expectedAncestorIdentities: [{ dir: aliasDir, dev: 1n, ino: 100n, resolvedPath: shallow }],
2274+
expectedResolvedPath: target,
2275+
})
2276+
await expect(promise).rejects.toBeInstanceOf(AncestorReplacedError)
2277+
await expect(promise).rejects.toThrow(/outside the authorized location/)
2278+
expect(fs.rename).not.toHaveBeenCalled()
2279+
expect(fs.link).not.toHaveBeenCalled()
2280+
})
22412281
})
22422282
describe("target group carried onto the staging descriptor before publication", () => {
22432283
// The commit publishes the staging inode, so the group the published file ends up in is whatever
@@ -2450,4 +2490,16 @@ describe("effective parent validated when mkdir created nothing", () => {
24502490
expect(fs.rename).not.toHaveBeenCalled()
24512491
expect(fs.link).not.toHaveBeenCalled()
24522492
})
2493+
it("publishes when only the path form of the name pin is given and its parent resolves in place", async () => {
2494+
// The accept path of the guard this block established. The narrowing that sends every
2495+
// path-only pin through the effective-parent check left the reject case tested and the
2496+
// accept case untested, so a regression that refused every path-only pin - publishing
2497+
// nothing for a parent that resolved exactly where the caller pinned it - would have
2498+
// passed this suite.
2499+
_scenario(false, false)
2500+
await expect(
2501+
safeWriteText(target, "data", { platform: "linux", expectedResolvedPath: target }),
2502+
).resolves.toBeUndefined()
2503+
expect(fs.rename).toHaveBeenCalledWith(expect.stringContaining("safeWriteText_"), target)
2504+
})
24532505
})

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ function _deepestAuthorizedLocation(locations: AuthorizedLocation[], target: str
423423
*/
424424
function _deepestPinnedDirectory(locations: AuthorizedLocation[], target: string): AuthorizedLocation | undefined {
425425
let deepest: AuthorizedLocation | undefined
426+
let deepestMatchLength = -1
426427
for (const candidate of locations) {
427428
// Either spelling is the caller's own decision: the name it walked, or the location
428429
// it recorded that name resolving to when it walked through a symlink alias. A
@@ -432,8 +433,16 @@ function _deepestPinnedDirectory(locations: AuthorizedLocation[], target: string
432433
if (!underTheNamedSpelling && !underTheAuthorizedLocation) {
433434
continue
434435
}
435-
if (!deepest || candidate.dir.length > deepest.dir.length) {
436+
// Depth is measured on the spelling that matched. A pin that reaches the name only
437+
// through the location its alias resolved to authorized shallower ground than a pin
438+
// whose own name contains it, and ranking the candidates by the length of their dir
439+
// field let a long alias spelling outrank a deeper name: the containment check that
440+
// follows then ran against the shallower location, and a directory created under the
441+
// deeper pin that had become a link into the shallower one passed it.
442+
const matchedSpelling = underTheNamedSpelling ? candidate.dir : candidate.location
443+
if (matchedSpelling.length > deepestMatchLength) {
436444
deepest = candidate
445+
deepestMatchLength = matchedSpelling.length
437446
}
438447
}
439448
return deepest

0 commit comments

Comments
 (0)