@pbuilder/sdk 0.1.0 → 0.2.0
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 +7 -0
- package/dist/scaffold/filename-pipeline.js +28 -3
- 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
|
@@ -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
|
-
}
|