@pbuilder/sdk 0.1.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commons/index.d.ts +12 -5
- package/dist/commons/index.js +12 -5
- package/dist/core/authoring-error.d.ts +6 -4
- package/dist/core/authoring-error.js +11 -9
- package/dist/core/context.d.ts +0 -2
- package/dist/core/context.js +6 -38
- package/dist/runner-manifest.json +4 -4
- package/dist/scaffold/classify-transport.d.ts +0 -3
- package/dist/scaffold/classify-transport.js +25 -25
- package/dist/scaffold/expander.d.ts +2 -1
- package/dist/scaffold/expander.js +83 -20
- package/dist/scaffold/filename-pipeline.d.ts +12 -2
- package/dist/scaffold/filename-pipeline.js +34 -6
- package/dist/scaffold/index.d.ts +13 -12
- package/dist/scaffold/index.js +21 -17
- package/dist/scaffold/path-guards.d.ts +34 -0
- package/dist/scaffold/path-guards.js +140 -0
- package/dist/scaffold/walk.d.ts +15 -13
- package/dist/scaffold/walk.js +126 -43
- package/package.json +1 -1
- package/dist/scaffold/containment.d.ts +0 -54
- package/dist/scaffold/containment.js +0 -242
package/dist/scaffold/walk.js
CHANGED
|
@@ -1,42 +1,53 @@
|
|
|
1
1
|
// src/scaffold/walk.ts (ADR-0044): folder enumeration for `scaffold` — lstat-based
|
|
2
2
|
// symlinked-directory non-descent (REQ-FSC-09.1) and a 10 000-entry bound (REQ-FSC-09.2).
|
|
3
|
-
// Pure enumeration only: source/destination eligibility
|
|
4
|
-
//
|
|
5
|
-
// the latter landing in S-002).
|
|
3
|
+
// Pure enumeration only: source/destination eligibility and the by-value/by-reference
|
|
4
|
+
// verdict are NOT this module's concern (classify-transport.ts + path-guards.ts).
|
|
6
5
|
import { lstatSync, readdirSync, statSync } from "node:fs";
|
|
7
|
-
import { join } from "node:path";
|
|
6
|
+
import { join, posix, resolve } from "node:path";
|
|
8
7
|
import { invalidInput } from "../core/authoring-error.js";
|
|
9
8
|
import { isErrnoException } from "../core/fs-errors.js";
|
|
10
9
|
export const DEFAULT_WALK_ENTRY_BOUND = 10_000;
|
|
11
10
|
function boundExceededMessage(bound) {
|
|
12
11
|
return `invalid input: scaffold walk exceeded the ${bound}-entry bound`;
|
|
13
12
|
}
|
|
14
|
-
// judgment-day iteration 2 fix: the ROOT `readdirSync` (below) has no
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// `readdirSync`
|
|
22
|
-
//
|
|
23
|
-
// author-facing, package-relative `from` the caller
|
|
24
|
-
// from `fromAbs`); a caller that omits it (only the
|
|
13
|
+
// judgment-day iteration 2 fix: the ROOT `readdirSync` (below) has no guard the caller
|
|
14
|
+
// already provides — `runScaffold` only lexically screens the root (`validateSourceLexical`,
|
|
15
|
+
// ADR-0077), never that it exists or is a directory (that check is deliberately left for
|
|
16
|
+
// `walkFolder` to answer). A `from` that resolves to a regular FILE or does not exist at
|
|
17
|
+
// all therefore used to reach `readdirSync` unguarded and throw a raw Node `Error` — never
|
|
18
|
+
// an `AuthoringError` — whose message echoes the ABSOLUTE filesystem path (no-echo
|
|
19
|
+
// violation). Ruling 8 (REQ-FSC-10.4) extends the SAME guard shape to every RECURSIVE
|
|
20
|
+
// sub-directory `readdirSync` and per-entry `lstatSync` below — a mid-walk failure (an
|
|
21
|
+
// entry vanishes, or an EACCES mid-tree) is no more entitled to leak a raw Node error than
|
|
22
|
+
// the root is. `rootRelPath` is the author-facing, package-relative `from` the caller
|
|
23
|
+
// already has in hand (never re-derived from `fromAbs`); a caller that omits it (only the
|
|
24
|
+
// direct-unit-test callers in walk.test.ts
|
|
25
25
|
// do) falls back to a locator-free phrasing rather than ever risking an absolute-path leak.
|
|
26
|
+
/** Every message helper below (root and per-entry alike) shares this shape: a locator-free
|
|
27
|
+
* phrasing when `relPath` is undefined (only the direct unit-test callers in walk.test.ts
|
|
28
|
+
* omit it), the locator-bearing phrasing otherwise — never an absolute path either way. */
|
|
29
|
+
function withOptionalLocator(relPath, plain, withLocator) {
|
|
30
|
+
return relPath === undefined ? plain : withLocator(relPath);
|
|
31
|
+
}
|
|
26
32
|
function rootNotDirectoryMessage(rootRelPath) {
|
|
27
|
-
return rootRelPath
|
|
28
|
-
? "invalid input: scaffold \"from\" must be a folder — it resolves to a regular file"
|
|
29
|
-
: `invalid input: scaffold "from" (${rootRelPath}) must be a folder — it resolves to a regular file`;
|
|
33
|
+
return withOptionalLocator(rootRelPath, "invalid input: scaffold \"from\" must be a folder — it resolves to a regular file", (relPath) => `invalid input: scaffold "from" (${relPath}) must be a folder — it resolves to a regular file`);
|
|
30
34
|
}
|
|
31
35
|
function rootNotFoundMessage(rootRelPath) {
|
|
32
|
-
return rootRelPath
|
|
33
|
-
? "invalid input: scaffold \"from\" folder does not exist"
|
|
34
|
-
: `invalid input: scaffold "from" folder (${rootRelPath}) does not exist`;
|
|
36
|
+
return withOptionalLocator(rootRelPath, "invalid input: scaffold \"from\" folder does not exist", (relPath) => `invalid input: scaffold "from" folder (${relPath}) does not exist`);
|
|
35
37
|
}
|
|
36
38
|
function rootUnreadableMessage(rootRelPath) {
|
|
37
|
-
return rootRelPath
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
return withOptionalLocator(rootRelPath, "invalid input: scaffold \"from\" folder could not be read", (relPath) => `invalid input: scaffold "from" folder (${relPath}) could not be read`);
|
|
40
|
+
}
|
|
41
|
+
// Owner ruling 16 (2026-07-29): REQ-FSC-09 mandates the walk never descend into ANY
|
|
42
|
+
// symlinked directory, but only the NESTED case was implemented — the walk ROOT itself
|
|
43
|
+
// went straight to `readdirSync` with no `lstatSync` ahead of it, so a symlinked `from`
|
|
44
|
+
// was FOLLOWED rather than rejected (probe-proven: enumerates content outside the
|
|
45
|
+
// package). A symlinked root is now an explicit, named rejection — never a silent skip
|
|
46
|
+
// (skipping would make `scaffold` a silent no-op for a root that resolves to a real,
|
|
47
|
+
// populated directory, which is a worse surprise than a nested skip) — reusing the SAME
|
|
48
|
+
// `invalid-input` reason and locator shape every other root-failure message already uses.
|
|
49
|
+
function rootIsSymlinkMessage(rootRelPath) {
|
|
50
|
+
return withOptionalLocator(rootRelPath, "invalid input: scaffold \"from\" must not be a symlinked directory", (relPath) => `invalid input: scaffold "from" (${relPath}) must not be a symlinked directory`);
|
|
40
51
|
}
|
|
41
52
|
// Never re-throws the raw Node error (no-echo, REQ-AEC-05/FIT-11 posture already held by
|
|
42
53
|
// every other scaffold-family rejection): ENOTDIR (the root resolves to a non-directory —
|
|
@@ -53,6 +64,28 @@ function rootReadFailure(err, rootRelPath) {
|
|
|
53
64
|
}
|
|
54
65
|
return invalidInput(rootUnreadableMessage(rootRelPath));
|
|
55
66
|
}
|
|
67
|
+
// REQ-FSC-10.4 (ruling 8, design §4 Q3): the recursive `readdirSync` (a nested directory
|
|
68
|
+
// this walk itself discovered) and the per-entry `lstatSync` had NO guard ahead of them —
|
|
69
|
+
// unlike the ROOT read above — so a mid-walk failure (the entry vanishes, or an EACCES
|
|
70
|
+
// mid-tree) used to reach the caller as a raw Node `Error`, echoing an ABSOLUTE path
|
|
71
|
+
// (no-echo violation). `rootReadFailure`'s SHAPE is reused (AuthoringError, invalid-input,
|
|
72
|
+
// package-relative, no-echo) but its three texts are ROOT-specific and would misname a
|
|
73
|
+
// per-entry failure — these two templates are ENTRY-specific instead. `X` is
|
|
74
|
+
// `posix.join(rootRelPath, entryRelPath)`; when `rootRelPath` is undefined (only the
|
|
75
|
+
// direct unit-test callers in walk.test.ts), both fall back to a locator-free phrasing,
|
|
76
|
+
// exactly as the three root texts already do — never an absolute path.
|
|
77
|
+
function entryUnreadableMessage(entryRelPath) {
|
|
78
|
+
return withOptionalLocator(entryRelPath, "invalid input: scaffold entry could not be read", (relPath) => `invalid input: scaffold entry (${relPath}) could not be read`);
|
|
79
|
+
}
|
|
80
|
+
function entryDisappearedMessage(entryRelPath) {
|
|
81
|
+
return withOptionalLocator(entryRelPath, "invalid input: scaffold entry disappeared during the walk", (relPath) => `invalid input: scaffold entry (${relPath}) disappeared during the walk`);
|
|
82
|
+
}
|
|
83
|
+
function entryReadFailure(err, entryRelPath) {
|
|
84
|
+
if (isErrnoException(err) && err.code === "ENOENT") {
|
|
85
|
+
return invalidInput(entryDisappearedMessage(entryRelPath));
|
|
86
|
+
}
|
|
87
|
+
return invalidInput(entryUnreadableMessage(entryRelPath));
|
|
88
|
+
}
|
|
56
89
|
// A symlink's OWN type (lstat) is always "symbolic link" — this asks what the link's
|
|
57
90
|
// TARGET is, via `statSync` (follows the link), ONLY to decide non-descent (REQ-FSC-09.1).
|
|
58
91
|
// NOT a containment check: a broken symlink (target absent) is treated as a
|
|
@@ -67,29 +100,62 @@ function symlinkTargetIsDirectory(absPath) {
|
|
|
67
100
|
}
|
|
68
101
|
/**
|
|
69
102
|
* Enumerates every file under `fromAbs`, mirroring nested directory structure into
|
|
70
|
-
* posix-separated `relPath`s (sorted for deterministic output).
|
|
71
|
-
* NEVER descended — even when
|
|
72
|
-
* (REQ-FSC-09.1) — skipped silently, no error.
|
|
73
|
-
*
|
|
74
|
-
*
|
|
103
|
+
* posix-separated `relPath`s (sorted for deterministic output). A NESTED symlinked
|
|
104
|
+
* directory is NEVER descended — even when its target resolves inside the package
|
|
105
|
+
* (REQ-FSC-09.1) — skipped silently, no error. The ROOT is held to a stricter standard
|
|
106
|
+
* (owner ruling 16): a symlinked `from` itself is REJECTED (`AuthoringError`,
|
|
107
|
+
* `invalid-input`) rather than followed or silently skipped — a bare no-op would hide a
|
|
108
|
+
* root that resolves to a real, populated directory. Fails loud, naming the bound, once
|
|
109
|
+
* the enumerated entry count exceeds `bound` (REQ-FSC-09.2); `bound` is injectable so a
|
|
110
|
+
* test can drive the branch without materializing 10,001 real files.
|
|
75
111
|
*
|
|
76
|
-
* `rootRelPath` (judgment-day iteration 2 fix,
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* regular file, or missing — `runScaffold`'s `
|
|
80
|
-
*
|
|
81
|
-
* doc-comment)
|
|
82
|
-
*
|
|
83
|
-
* valid tree is unchanged.
|
|
112
|
+
* `rootRelPath` (judgment-day iteration 2 fix, extended by ruling 8 / REQ-FSC-10.4): the
|
|
113
|
+
* author-facing, package-relative `from` — threaded through to name BOTH the ROOT and any
|
|
114
|
+
* RECURSIVE entry in an `AuthoringError` message: the root if it itself is not a readable
|
|
115
|
+
* directory (a regular file, or missing — `runScaffold`'s `validateSourceLexical` only
|
|
116
|
+
* screens the root lexically, deliberately leaving existence/type to this function, see its
|
|
117
|
+
* own doc-comment); a nested entry if a recursive `readdirSync`/`lstatSync` fails mid-walk
|
|
118
|
+
* (the entry vanished, or an EACCES mid-tree, REQ-FSC-10.4).
|
|
84
119
|
*/
|
|
85
120
|
export function walkFolder(fromAbs, bound = DEFAULT_WALK_ENTRY_BOUND, rootRelPath) {
|
|
121
|
+
// judgment-day round 2 fix (F1): POSIX `lstat` FOLLOWS the final symlink when the path
|
|
122
|
+
// ends with a separator — an un-normalized `fromAbs` (trailing slash, doubled trailing
|
|
123
|
+
// slash, or a literal "./" segment) would resolve THROUGH a symlinked root before the
|
|
124
|
+
// `isSymbolicLink()` check below ever ran, bypassing the ruling-16 rejection entirely
|
|
125
|
+
// (probe-proven leak). `resolve` strips any trailing separator and collapses "./"/".."
|
|
126
|
+
// segments — the SAME normalization every recursive `join(root, relDir)` below now
|
|
127
|
+
// builds on, so a caller passing an un-normalized root is corrected in exactly ONE place.
|
|
128
|
+
const root = resolve(fromAbs);
|
|
86
129
|
const entries = [];
|
|
87
130
|
const dirStack = [""];
|
|
131
|
+
// judgment-day round 3 fix (F6): counts EVERY enumerated dirent (files, directories, and
|
|
132
|
+
// skipped symlinked directories alike) — the prior bound only incremented on
|
|
133
|
+
// `entries.push` (files), so a directory-only tree was entirely unbounded regardless of
|
|
134
|
+
// depth or breadth. REQ-FSC-09.2 frames the bound as a loop-safety/DoS guard over
|
|
135
|
+
// "enumerated entries," not "emitted files" — every name a `readdirSync` call returns is
|
|
136
|
+
// one enumerated entry, whatever it turns out to be.
|
|
137
|
+
let enumeratedCount = 0;
|
|
88
138
|
while (dirStack.length > 0) {
|
|
89
139
|
const relDir = dirStack.pop();
|
|
90
|
-
const absDir = relDir === "" ?
|
|
140
|
+
const absDir = relDir === "" ? root : join(root, relDir);
|
|
91
141
|
let names;
|
|
92
142
|
if (relDir === "") {
|
|
143
|
+
// REQ-FSC-09.1 (owner ruling 16): the root gets the SAME non-descent guarantee a
|
|
144
|
+
// nested symlinked directory already has — `lstatSync` first (never `readdirSync`
|
|
145
|
+
// straight off `fromAbs`, which would silently FOLLOW a symlinked root) so a
|
|
146
|
+
// missing/non-directory root still reaches `rootReadFailure` unchanged (lstat fails
|
|
147
|
+
// with the identical errno readdir would have), and a symlinked root rejects
|
|
148
|
+
// explicitly instead of either being followed or silently skipped.
|
|
149
|
+
let rootLst;
|
|
150
|
+
try {
|
|
151
|
+
rootLst = lstatSync(absDir);
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
throw rootReadFailure(err, rootRelPath);
|
|
155
|
+
}
|
|
156
|
+
if (rootLst.isSymbolicLink()) {
|
|
157
|
+
throw invalidInput(rootIsSymlinkMessage(rootRelPath));
|
|
158
|
+
}
|
|
93
159
|
try {
|
|
94
160
|
names = readdirSync(absDir).sort();
|
|
95
161
|
}
|
|
@@ -98,12 +164,32 @@ export function walkFolder(fromAbs, bound = DEFAULT_WALK_ENTRY_BOUND, rootRelPat
|
|
|
98
164
|
}
|
|
99
165
|
}
|
|
100
166
|
else {
|
|
101
|
-
|
|
167
|
+
try {
|
|
168
|
+
names = readdirSync(absDir).sort();
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
const entryX = rootRelPath === undefined ? undefined : posix.join(rootRelPath, relDir);
|
|
172
|
+
throw entryReadFailure(err, entryX);
|
|
173
|
+
}
|
|
102
174
|
}
|
|
103
175
|
for (const name of names) {
|
|
176
|
+
// F6: incremented for EVERY name `readdirSync` returned, before this entry is
|
|
177
|
+
// classified — a directory that will be pushed onto `dirStack` (never reaching
|
|
178
|
+
// `entries.push` below) still counts against the bound.
|
|
179
|
+
enumeratedCount += 1;
|
|
180
|
+
if (enumeratedCount > bound) {
|
|
181
|
+
throw invalidInput(boundExceededMessage(bound));
|
|
182
|
+
}
|
|
104
183
|
const relPath = relDir === "" ? name : `${relDir}/${name}`;
|
|
105
184
|
const absPath = join(absDir, name);
|
|
106
|
-
|
|
185
|
+
let lst;
|
|
186
|
+
try {
|
|
187
|
+
lst = lstatSync(absPath);
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
const entryX = rootRelPath === undefined ? undefined : posix.join(rootRelPath, relPath);
|
|
191
|
+
throw entryReadFailure(err, entryX);
|
|
192
|
+
}
|
|
107
193
|
if (lst.isSymbolicLink()) {
|
|
108
194
|
if (symlinkTargetIsDirectory(absPath)) {
|
|
109
195
|
continue; // REQ-FSC-09.1: never descended, no error for the skip
|
|
@@ -115,9 +201,6 @@ export function walkFolder(fromAbs, bound = DEFAULT_WALK_ENTRY_BOUND, rootRelPat
|
|
|
115
201
|
continue;
|
|
116
202
|
}
|
|
117
203
|
entries.push({ relPath, absPath });
|
|
118
|
-
if (entries.length > bound) {
|
|
119
|
-
throw invalidInput(boundExceededMessage(bound));
|
|
120
|
-
}
|
|
121
204
|
}
|
|
122
205
|
}
|
|
123
206
|
return entries;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbuilder/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "The public TypeScript/Bun authoring layer for Project Builder schematics. Authors file mutations (create, modify, remove, rename, move, copy, copyIn), lowers them to IR, and talks to the Project Builder engine over framed stdio (see docs/engine-sdk-wire-spec.md).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { type Stats } from "node:fs";
|
|
2
|
-
export declare function isWithinCeiling(candidateAbs: string, ceilingAbs: string): boolean;
|
|
3
|
-
/**
|
|
4
|
-
* The realpath'd form of the containment ceiling — a RUN invariant (`packageRoot` is
|
|
5
|
-
* seeded once per run and always exists, so this realpath cannot ENOENT). Loop callers
|
|
6
|
-
* (`runScaffold`) compute it once and thread it through `classifyTransport` →
|
|
7
|
-
* `validateSourceContainment` instead of re-realpath'ing the same directory per entry.
|
|
8
|
-
*/
|
|
9
|
-
export declare function resolveRealCeiling(packageRoot: string): string;
|
|
10
|
-
/**
|
|
11
|
-
* Validates ONE package-local SOURCE path against the dual-anchor containment model
|
|
12
|
-
* (REQ-PRC-01..08) and returns its realpath'd absolute path plus the `lstat` used to
|
|
13
|
-
* prove regular-file eligibility — callers (`classify-transport.ts`) reuse this `Stats`
|
|
14
|
-
* for the CCL-06 size gate instead of stat'ing again. Throws one of the four `source-*`
|
|
15
|
-
* `AuthoringError`s (REQ-AEC-10) on any failure; never reads file CONTENT.
|
|
16
|
-
* `realCeiling` (optional) is the precomputed `resolveRealCeiling(packageRoot)` — passed
|
|
17
|
-
* by per-entry loop callers; single-shot callers omit it and it is derived here.
|
|
18
|
-
*/
|
|
19
|
-
export declare function validateSourceContainment(params: {
|
|
20
|
-
packageDir: string;
|
|
21
|
-
packageRoot: string;
|
|
22
|
-
relPath: string;
|
|
23
|
-
realCeiling?: string;
|
|
24
|
-
}): {
|
|
25
|
-
absPath: string;
|
|
26
|
-
stat: Stats;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Validates `scaffold`'s walk ROOT (`from`, a DIRECTORY — never file-checked) against the
|
|
30
|
-
* SAME lexical + realpath ceiling machinery `validateSourceContainment` uses (steps 1-3,
|
|
31
|
-
* via `resolveContainedRealpath` — no parallel check forked for the directory case).
|
|
32
|
-
* Rejects `source-outside-package` for an escaping or out-of-ceiling root; a
|
|
33
|
-
* legitimately ABSENT in-ceiling root is left alone (no-op) so `walkFolder`'s own
|
|
34
|
-
* existing not-found behavior is preserved unchanged — this function only ever narrows
|
|
35
|
-
* what `runScaffold` was already going to do, it never invents a new not-found path.
|
|
36
|
-
* MUST run before `walkFolder` so the 10k-entry bound can never fire on an out-of-ceiling
|
|
37
|
-
* tree (owner-ratified final-verify remediation, package-root-containment domain).
|
|
38
|
-
*/
|
|
39
|
-
export declare function validateSourceRootContainment(params: {
|
|
40
|
-
packageDir: string;
|
|
41
|
-
packageRoot: string;
|
|
42
|
-
relPath: string;
|
|
43
|
-
realCeiling?: string;
|
|
44
|
-
}): void;
|
|
45
|
-
/**
|
|
46
|
-
* Validates a FINAL, SDK-computed DESTINATION path (`scaffold`'s computed `to`, or
|
|
47
|
-
* `copyIn`'s `to`) — LEXICAL ONLY, no realpath (the destination may not exist yet,
|
|
48
|
-
* REQ-PRC-09). Applied post-rename, post-token-translation, immediately pre-emit.
|
|
49
|
-
* Rejects with the EXISTING `invalid-input` reason (scaffold-family author misuse,
|
|
50
|
-
* REQ-AEC-12) — never a `source-*` reason, which is reserved for source READS. The
|
|
51
|
-
* engine's own post-render check (REQ-PRC-06/BRC-08) remains the real control for
|
|
52
|
-
* rendered forms the SDK cannot evaluate.
|
|
53
|
-
*/
|
|
54
|
-
export declare function validateDestinationLexical(relPath: string): void;
|
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
// src/scaffold/containment.ts (ADR-0044, ADR-0045, ADR-0046): the dual-anchor
|
|
2
|
-
// containment enforcement (package-root-containment REQ-PRC-01..09) — REPLACES S-001's
|
|
3
|
-
// MINIMAL placeholder lexical guard that used to live inline in classify-transport.ts.
|
|
4
|
-
// Two DISTINCT checks, never conflated (REQ-PRC-01):
|
|
5
|
-
//
|
|
6
|
-
// 1. `validateSourceContainment` — realpath-based, segment-aware, case-fold ceiling
|
|
7
|
-
// check + regular-file allow-list for a SOURCE path (PRC-01..08). Mints the four
|
|
8
|
-
// `source-*` AuthoringReasons (REQ-AEC-10): `source-not-found`,
|
|
9
|
-
// `source-outside-package`, `source-not-regular-file`, `source-unreadable`.
|
|
10
|
-
// 2. `validateDestinationLexical` — lexical-only guard for a `to` destination
|
|
11
|
-
// (PRC-09) — no realpath (the destination may not exist yet). Reuses the EXISTING
|
|
12
|
-
// `invalid-input` reason (scaffold-family author misuse, REQ-AEC-12), never a
|
|
13
|
-
// `source-*` reason — this is about the emitted DIRECTIVE SHAPE, not a source read.
|
|
14
|
-
//
|
|
15
|
-
// Classification pipeline order (design §Data Model, pinned): lexical `../`/absolute
|
|
16
|
-
// screen → resolve + segment-aware ceiling check (pre-realpath) → (out-of-ceiling ⇒
|
|
17
|
-
// source-outside-package, NO existence probe, PRC-07) → realpath. ENOENT ordering (S1,
|
|
18
|
-
// closes the ancestor-symlink existence oracle, REQ-PRC-07.2): on realpath ENOENT,
|
|
19
|
-
// `source-not-found` may be concluded ONLY AFTER the nearest EXISTING ancestor's
|
|
20
|
-
// realpath is proven in-ceiling; an out-of-ceiling ancestor still answers
|
|
21
|
-
// `source-outside-package`, never differentiating existence beyond the boundary. Then
|
|
22
|
-
// `lstat` allow-list (`isFile()` only, else `source-not-regular-file` — never a
|
|
23
|
-
// directory-blacklist, PRC-04.4's FIFO kill). Only after ALL of this may content be read
|
|
24
|
-
// (PRC-08) — `source-unreadable` covers a LATER read failure of an already-proven
|
|
25
|
-
// in-ceiling regular file (permission/I/O error), surfaced by classify-transport.ts's
|
|
26
|
-
// own `readFileSync`, not by this module (this module never reads content).
|
|
27
|
-
import { lstatSync, readlinkSync, realpathSync, existsSync } from "node:fs";
|
|
28
|
-
import { dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
29
|
-
import { AuthoringError, invalidInput } from "../core/authoring-error.js";
|
|
30
|
-
import { isErrnoException } from "../core/fs-errors.js";
|
|
31
|
-
const FOLD_CASE = process.platform === "win32" || process.platform === "darwin";
|
|
32
|
-
const fold = (p) => (FOLD_CASE ? p.toLowerCase() : p);
|
|
33
|
-
// Segment-aware ceiling comparison (REQ-PRC-04, kills the bare-`startsWith` mutant —
|
|
34
|
-
// REQ-PRC-04.5's sibling-prefix scenario: `/pkg-evil/x`.startsWith(`/pkg`) would wrongly
|
|
35
|
-
// admit it). `candidate` is within `ceiling` iff it EQUALS the ceiling or starts with the
|
|
36
|
-
// ceiling PLUS a path separator — never a bare string prefix. Case-folds on
|
|
37
|
-
// case-insensitive platforms ONLY (product ruling Q24 — win32/darwin); canonical-form
|
|
38
|
-
// hardening (UNC/device/reserved-DOS/drive-relative) stays engine-side (BRC-08).
|
|
39
|
-
export function isWithinCeiling(candidateAbs, ceilingAbs) {
|
|
40
|
-
const candidate = fold(candidateAbs);
|
|
41
|
-
const ceiling = fold(ceilingAbs);
|
|
42
|
-
const ceilingWithSep = ceiling.endsWith(sep) ? ceiling : ceiling + sep;
|
|
43
|
-
return candidate === ceiling || candidate.startsWith(ceilingWithSep);
|
|
44
|
-
}
|
|
45
|
-
// Lexical `../`/absolute screen shared by BOTH the source guard (REQ-PRC-04's first
|
|
46
|
-
// clause) and the destination guard (REQ-PRC-09, symmetric by design): a literal ".."
|
|
47
|
-
// segment or an absolute-looking path (POSIX leading `/` or a Windows drive letter)
|
|
48
|
-
// resolves outside any ceiling by construction — no filesystem touch needed to reject it.
|
|
49
|
-
function isLexicallyEscaping(relPath) {
|
|
50
|
-
const isAbsolute = relPath.startsWith("/") || /^[A-Za-z]:[\\/]/.test(relPath);
|
|
51
|
-
const segments = relPath.split(/[\\/]+/);
|
|
52
|
-
return isAbsolute || segments.includes("..");
|
|
53
|
-
}
|
|
54
|
-
function sourceRejection(reason, relPath) {
|
|
55
|
-
// No `message` override: messageFor() derives the AEC-11 neutral template from
|
|
56
|
-
// `reason` + `path` alone — `path` here is ALWAYS the package-relative `relPath`
|
|
57
|
-
// (REQ-PRC-05), never an absolute filesystem path.
|
|
58
|
-
return new AuthoringError({ verb: undefined, path: relPath, reason, appliedCount: 0 });
|
|
59
|
-
}
|
|
60
|
-
// Walks UP from `dirname(absPath)` to find the nearest EXISTING ancestor and returns its
|
|
61
|
-
// realpath — the GENUINELY-missing-entry half of the S1 ENOENT ordering fix (an entry
|
|
62
|
-
// that does not exist AT ALL, lstat included; no symlink indirection involved).
|
|
63
|
-
// Terminates at the filesystem root, which always exists.
|
|
64
|
-
function nearestExistingAncestorRealpath(absPath) {
|
|
65
|
-
let dir = dirname(absPath);
|
|
66
|
-
for (;;) {
|
|
67
|
-
if (existsSync(dir)) {
|
|
68
|
-
return realpathSync(dir);
|
|
69
|
-
}
|
|
70
|
-
const parent = dirname(dir);
|
|
71
|
-
if (parent === dir) {
|
|
72
|
-
return realpathSync(dir); // filesystem root — defensive terminator, always exists
|
|
73
|
-
}
|
|
74
|
-
dir = parent;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// The BROKEN-SYMLINK half of the S1 ENOENT ordering fix (REQ-PRC-07.2): `symlinkAbsPath`
|
|
78
|
-
// itself EXISTS (its `lstat` succeeded) and IS a symlink, but `realpath` failed because its
|
|
79
|
-
// target does not exist — so there is nothing to realpath TO directly. The returned value
|
|
80
|
-
// is compared against the REALPATH'D ceiling, so it MUST be in real space: the raw
|
|
81
|
-
// `readlink` target text is LEXICAL (verify-in-loop-4 CRITICAL — on macOS a raw absolute
|
|
82
|
-
// target under `/var/...` never string-matches a `/private/var/...` real ceiling), and even
|
|
83
|
-
// a relative target joined under a realpath'd parent stays partially lexical when an
|
|
84
|
-
// EXISTING intermediate directory of the target is itself a symlink. Both target forms are
|
|
85
|
-
// therefore normalized identically: lexical absolute form first (anchored on the symlink's
|
|
86
|
-
// realpath'd parent for a relative target — the containing directory exists, the symlink's
|
|
87
|
-
// own lstat succeeded), then `nearestExistingAncestorRealpath` brings the result into real
|
|
88
|
-
// space. The missing tail cannot alter ceiling membership (`resolve` already collapsed any
|
|
89
|
-
// `..`), and the target is never existence-probed beyond the same ancestor walk the
|
|
90
|
-
// plain-missing case already uses (REQ-PRC-07: the containment verdict precedes existence
|
|
91
|
-
// knowledge). Single-hop only (the symlink's immediate target); deeper multi-hop broken
|
|
92
|
-
// chains are not a scenario this domain pins.
|
|
93
|
-
function resolveBrokenSymlinkTargetRealAncestor(symlinkAbsPath) {
|
|
94
|
-
const target = readlinkSync(symlinkAbsPath);
|
|
95
|
-
const lexicalTargetAbs = isAbsolute(target)
|
|
96
|
-
? resolve(target)
|
|
97
|
-
: resolve(join(realpathSync(dirname(symlinkAbsPath)), target));
|
|
98
|
-
return nearestExistingAncestorRealpath(lexicalTargetAbs);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* The realpath'd form of the containment ceiling — a RUN invariant (`packageRoot` is
|
|
102
|
-
* seeded once per run and always exists, so this realpath cannot ENOENT). Loop callers
|
|
103
|
-
* (`runScaffold`) compute it once and thread it through `classifyTransport` →
|
|
104
|
-
* `validateSourceContainment` instead of re-realpath'ing the same directory per entry.
|
|
105
|
-
*/
|
|
106
|
-
export function resolveRealCeiling(packageRoot) {
|
|
107
|
-
return realpathSync(resolve(packageRoot));
|
|
108
|
-
}
|
|
109
|
-
// Steps 1-3 of the containment pipeline (design §Classification pipeline order, pinned):
|
|
110
|
-
// lexical `../`/absolute screen → pre-realpath segment-aware ceiling check → realpath +
|
|
111
|
-
// ENOENT ordering (S1, REQ-PRC-07.2). Shared by `validateSourceContainment` (file source,
|
|
112
|
-
// adds step 4's regular-file allow-list) and `validateSourceRootContainment` (scaffold's
|
|
113
|
-
// walk ROOT — a directory, no step 4). Extracted so the ENOENT/symlink-ordering logic has
|
|
114
|
-
// exactly ONE implementation — never forked in parallel for the directory case.
|
|
115
|
-
function resolveContainedRealpath(params) {
|
|
116
|
-
const { packageDir, relPath, realCeiling } = params;
|
|
117
|
-
const lexicalCeiling = resolve(params.packageRoot);
|
|
118
|
-
// Step 1 (REQ-PRC-04 first clause): lexical `../`/absolute screen, independent of any
|
|
119
|
-
// realpath check — a literal ".." segment or an absolute path resolves outside the
|
|
120
|
-
// ceiling by construction (REQ-PRC-04.1/.6).
|
|
121
|
-
if (isLexicallyEscaping(relPath)) {
|
|
122
|
-
throw sourceRejection("source-outside-package", relPath);
|
|
123
|
-
}
|
|
124
|
-
const lexicalAbs = resolve(join(packageDir, relPath));
|
|
125
|
-
// Step 2 (design §Classification pipeline): pre-realpath segment-aware ceiling check
|
|
126
|
-
// on the lexically-resolved path — out-of-ceiling ⇒ source-outside-package, NO
|
|
127
|
-
// existence probe (REQ-PRC-07: containment precedes any existence/stat probing).
|
|
128
|
-
if (!isWithinCeiling(lexicalAbs, lexicalCeiling)) {
|
|
129
|
-
throw sourceRejection("source-outside-package", relPath);
|
|
130
|
-
}
|
|
131
|
-
// Step 3: realpath — resolves symlinks. ENOENT ordering (S1, REQ-PRC-07.2): a missing
|
|
132
|
-
// target may be classified not-found ONLY AFTER the location it would resolve TO
|
|
133
|
-
// is proven in-ceiling. Two distinct ENOENT shapes, both resolved into REAL space (the
|
|
134
|
-
// ceiling side of this comparison is `realCeiling` — never compare a lexical candidate
|
|
135
|
-
// against it):
|
|
136
|
-
// (a) `lexicalAbs` itself is a BROKEN SYMLINK (its own entry exists, lstat succeeds,
|
|
137
|
-
// but the target it points to does not) — the target's real-space nearest existing
|
|
138
|
-
// ancestor is checked against the ceiling.
|
|
139
|
-
// (b) `lexicalAbs` does not exist AT ALL (no symlink indirection) — walk up to the
|
|
140
|
-
// nearest EXISTING ancestor and check ITS realpath against the ceiling.
|
|
141
|
-
// Either way: out-of-ceiling ⇒ source-outside-package (never differentiates existence
|
|
142
|
-
// beyond the boundary, REQ-PRC-07.1); in-ceiling ⇒ notFound.
|
|
143
|
-
let realAbs;
|
|
144
|
-
try {
|
|
145
|
-
realAbs = realpathSync(lexicalAbs);
|
|
146
|
-
}
|
|
147
|
-
catch (err) {
|
|
148
|
-
if (isErrnoException(err) && err.code === "ENOENT") {
|
|
149
|
-
let selfIsBrokenSymlink = false;
|
|
150
|
-
try {
|
|
151
|
-
selfIsBrokenSymlink = lstatSync(lexicalAbs).isSymbolicLink();
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
selfIsBrokenSymlink = false; // lexicalAbs itself doesn't exist — case (b)
|
|
155
|
-
}
|
|
156
|
-
const resolvedRealAncestor = selfIsBrokenSymlink
|
|
157
|
-
? resolveBrokenSymlinkTargetRealAncestor(lexicalAbs)
|
|
158
|
-
: nearestExistingAncestorRealpath(lexicalAbs);
|
|
159
|
-
if (!isWithinCeiling(resolvedRealAncestor, realCeiling)) {
|
|
160
|
-
throw sourceRejection("source-outside-package", relPath);
|
|
161
|
-
}
|
|
162
|
-
return { notFound: true };
|
|
163
|
-
}
|
|
164
|
-
throw sourceRejection("source-unreadable", relPath);
|
|
165
|
-
}
|
|
166
|
-
if (!isWithinCeiling(realAbs, realCeiling)) {
|
|
167
|
-
throw sourceRejection("source-outside-package", relPath);
|
|
168
|
-
}
|
|
169
|
-
return { realAbs };
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Validates ONE package-local SOURCE path against the dual-anchor containment model
|
|
173
|
-
* (REQ-PRC-01..08) and returns its realpath'd absolute path plus the `lstat` used to
|
|
174
|
-
* prove regular-file eligibility — callers (`classify-transport.ts`) reuse this `Stats`
|
|
175
|
-
* for the CCL-06 size gate instead of stat'ing again. Throws one of the four `source-*`
|
|
176
|
-
* `AuthoringError`s (REQ-AEC-10) on any failure; never reads file CONTENT.
|
|
177
|
-
* `realCeiling` (optional) is the precomputed `resolveRealCeiling(packageRoot)` — passed
|
|
178
|
-
* by per-entry loop callers; single-shot callers omit it and it is derived here.
|
|
179
|
-
*/
|
|
180
|
-
export function validateSourceContainment(params) {
|
|
181
|
-
const { packageDir, packageRoot, relPath } = params;
|
|
182
|
-
// `packageRoot` always exists (context.ts's ancestor walk proved it via `existsSync`),
|
|
183
|
-
// so this realpath cannot ENOENT.
|
|
184
|
-
const realCeiling = params.realCeiling ?? realpathSync(resolve(packageRoot));
|
|
185
|
-
const resolution = resolveContainedRealpath({ packageDir, packageRoot, relPath, realCeiling });
|
|
186
|
-
if ("notFound" in resolution) {
|
|
187
|
-
throw sourceRejection("source-not-found", relPath);
|
|
188
|
-
}
|
|
189
|
-
const { realAbs } = resolution;
|
|
190
|
-
// Step 4 (REQ-PRC-04): lstat ALLOW-LIST — isFile() only, never a directory-blacklist
|
|
191
|
-
// (PRC-04.4's FIFO kill: a FIFO/socket/device is equally ineligible).
|
|
192
|
-
let stat;
|
|
193
|
-
try {
|
|
194
|
-
stat = lstatSync(realAbs);
|
|
195
|
-
}
|
|
196
|
-
catch (err) {
|
|
197
|
-
if (isErrnoException(err) && err.code === "ENOENT") {
|
|
198
|
-
// TOCTOU: existed for realpath, vanished before lstat — treat as not-found rather
|
|
199
|
-
// than leaking a raw Node error.
|
|
200
|
-
throw sourceRejection("source-not-found", relPath);
|
|
201
|
-
}
|
|
202
|
-
throw sourceRejection("source-unreadable", relPath);
|
|
203
|
-
}
|
|
204
|
-
if (!stat.isFile()) {
|
|
205
|
-
throw sourceRejection("source-not-regular-file", relPath);
|
|
206
|
-
}
|
|
207
|
-
return { absPath: realAbs, stat };
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Validates `scaffold`'s walk ROOT (`from`, a DIRECTORY — never file-checked) against the
|
|
211
|
-
* SAME lexical + realpath ceiling machinery `validateSourceContainment` uses (steps 1-3,
|
|
212
|
-
* via `resolveContainedRealpath` — no parallel check forked for the directory case).
|
|
213
|
-
* Rejects `source-outside-package` for an escaping or out-of-ceiling root; a
|
|
214
|
-
* legitimately ABSENT in-ceiling root is left alone (no-op) so `walkFolder`'s own
|
|
215
|
-
* existing not-found behavior is preserved unchanged — this function only ever narrows
|
|
216
|
-
* what `runScaffold` was already going to do, it never invents a new not-found path.
|
|
217
|
-
* MUST run before `walkFolder` so the 10k-entry bound can never fire on an out-of-ceiling
|
|
218
|
-
* tree (owner-ratified final-verify remediation, package-root-containment domain).
|
|
219
|
-
*/
|
|
220
|
-
export function validateSourceRootContainment(params) {
|
|
221
|
-
const { packageDir, packageRoot, relPath } = params;
|
|
222
|
-
const realCeiling = params.realCeiling ?? realpathSync(resolve(packageRoot));
|
|
223
|
-
resolveContainedRealpath({ packageDir, packageRoot, relPath, realCeiling });
|
|
224
|
-
}
|
|
225
|
-
function destinationEscapeMessage(relPath) {
|
|
226
|
-
return (`invalid input: destination "${relPath}" escapes the workspace tree ` +
|
|
227
|
-
'(literal ".." segment or absolute path)');
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Validates a FINAL, SDK-computed DESTINATION path (`scaffold`'s computed `to`, or
|
|
231
|
-
* `copyIn`'s `to`) — LEXICAL ONLY, no realpath (the destination may not exist yet,
|
|
232
|
-
* REQ-PRC-09). Applied post-rename, post-token-translation, immediately pre-emit.
|
|
233
|
-
* Rejects with the EXISTING `invalid-input` reason (scaffold-family author misuse,
|
|
234
|
-
* REQ-AEC-12) — never a `source-*` reason, which is reserved for source READS. The
|
|
235
|
-
* engine's own post-render check (REQ-PRC-06/BRC-08) remains the real control for
|
|
236
|
-
* rendered forms the SDK cannot evaluate.
|
|
237
|
-
*/
|
|
238
|
-
export function validateDestinationLexical(relPath) {
|
|
239
|
-
if (isLexicallyEscaping(relPath)) {
|
|
240
|
-
throw invalidInput(destinationEscapeMessage(relPath));
|
|
241
|
-
}
|
|
242
|
-
}
|