5656 * names the requested object to the door's `500`, and §3 holds the controls
5757 * that keep the heuristic's other answers where they were.
5858 *
59- * [#26903] The capability gates call `throwCapabilityDenial(vm, origin,
60- * capability, message)`, not `throwSandboxFault` directly. Each of those calls
61- * is still a site here, its message rendered from argument 3, but with an
62- * ACTION origin: the helper sends an action or job body's denial to
63- * `throwSandboxFault` ( the fault this file pins) and answers a HOOK body's as
64- * a coded `403` refusal, which is not a fault and is pinned by
65- * `runtime/src/sandbox/hook-capability-denial-is-a-refusal.integration.test.ts`.
66- * The census reads that routing from the helper's body rather than trusting
67- * this comment: if its first statement stops sending every non-hook origin to
68- * `throwSandboxFault`, the census is red. The helper's own
69- * `throwSandboxFault(vm, message)` relays its call sites, like the
70- * `new SandboxError(message)` inside `throwSandboxFault` .
59+ * [#26903/#26965 ] The capability gates call `throwCapabilityDenial(vm, origin,
60+ * capability, message)`, not `throwSandboxFault`. Since #26965 that helper
61+ * answers EVERY origin's denial (hook, action, job) as a coded refusal, so a
62+ * capability gate is no longer a fault site: §4 holds each one to the refusal
63+ * the runner hands out. The census reads both halves from the producer rather
64+ * than trusting this comment:
65+ *
66+ * - the helper's body calls no `throwSandboxFault` (it has no fault arm), and
67+ * no fault site's message reads as a capability denial;
68+ * - each gate's message (argument 3) is rendered as before, then run through
69+ * the helper's own sentence template ( its `new Error(…)`) with the code and
70+ * status its `Object.assign` declares, once per origin kind .
7171 */
7272
7373import { readFileSync } from 'node:fs' ;
@@ -105,18 +105,15 @@ const SAMPLES: Readonly<Record<string, string>> = {
105105 fieldName : 'owner' ,
106106} ;
107107
108- /**
109- * [#26903] A `throwCapabilityDenial` site is a fault only on the non-hook arm,
110- * so its message is rendered with an action origin (see the header).
111- */
112- const CAPABILITY_FAULT_SAMPLES : Readonly < Record < string , string > > = {
113- ...SAMPLES ,
114- 'origin.kind' : 'action' ,
115- } ;
116-
117108/** The helper every capability gate throws through, read below from the producer. */
118109const CAPABILITY_HELPER = 'throwCapabilityDenial' ;
119110
111+ /** [#26965] Every origin a body runs under answers a denial the same way; §4 renders each. */
112+ const ORIGIN_KINDS = [ 'hook' , 'action' , 'job' ] as const ;
113+
114+ /** A placeholder for `origin.kind` in a gate's rendered message, filled per kind in §4. */
115+ const ORIGIN_KIND_HOLE = '@@origin.kind@@' ;
116+
120117/** §2: the error a nested body threw, so the construction it lands in is a CRASH. */
121118const CRASH_SAMPLES : Readonly < Record < string , string > > = {
122119 ...SAMPLES ,
@@ -130,7 +127,7 @@ const CRASH_SAMPLES: Readonly<Record<string, string>> = {
130127 * runner, or a new non-literal one, fails §1's census.
131128 */
132129const RELAYS : Readonly < Record < string , string > > = {
133- message : '`throwSandboxFault` builds the error for each of its call sites, and `throwCapabilityDenial` hands it each of its own (action and job origins); both helpers\' call sites are sites here in their own right' ,
130+ message : '`throwSandboxFault` builds the error for each of its call sites, which are sites here in their own right' ,
134131 'withoutSandboxErrorPrefix(String(errStr))' :
135132 'the pump loop rebuilds a fault marked across the VM hop: the `throwSandboxFault` messages, the host-side faults thrown inside a host call (sites here), and a nested body\'s crash (§2)' ,
136133} ;
@@ -139,7 +136,6 @@ const RELAYS: Readonly<Record<string, string>> = {
139136const KINDS : ReadonlyArray < readonly [ kind : string , pattern : RegExp , floor : number ] > = [
140137 [ 'CPU budget' , / e x c e e d e d C P U b u d g e t / , 1 ] ,
141138 [ 'wall-clock ceiling' , / e x c e e d e d w a l l - c l o c k c e i l i n g / , 1 ] ,
142- [ 'capability denial' , / ^ c a p a b i l i t y ' / , 5 ] ,
143139 [ 'ctx.api unavailable' , / ^ c t x \. a p i u n a v a i l a b l e / , 1 ] ,
144140 [ 'install failure' , / ^ f a i l e d t o i n s t a l l / , 4 ] ,
145141 [ 'marshal failure' , / ^ f a i l e d t o m a r s h a l / , 1 ] ,
@@ -207,7 +203,7 @@ const census = (() => {
207203 const crashes : Site [ ] = [ ] ;
208204 const relays : string [ ] = [ ] ;
209205 const unrenderable : string [ ] = [ ] ;
210- const capabilityGates : string [ ] = [ ] ;
206+ const capabilityGates : CapabilityGate [ ] = [ ] ;
211207 const visit = ( node : ts . Node ) : void => {
212208 if ( ts . isNewExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === 'SandboxError' ) {
213209 const args = node . arguments ?? [ ] ;
@@ -246,15 +242,21 @@ const census = (() => {
246242 }
247243 }
248244 }
249- // [#26903] A capability gate: a fault on the action/job arm (see the header).
245+ // [#26965] A capability gate: a refusal for every origin, held by §4.
246+ // Its message is rendered per origin kind there; `origin.kind` stays a
247+ // placeholder here.
250248 if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === CAPABILITY_HELPER ) {
251249 const at = `quickjs-runner.ts:${ lineOf ( node ) } ` ;
252250 try {
253- faults . push ( {
254- label : `${ at } ${ CAPABILITY_HELPER } (…), action origin` ,
255- message : render ( node . arguments [ 3 ] , CAPABILITY_FAULT_SAMPLES ) ,
251+ capabilityGates . push ( {
252+ label : `${ at } ${ CAPABILITY_HELPER } (…)` ,
253+ message : render ( node . arguments [ 3 ] , { ...SAMPLES , 'origin.kind' : ORIGIN_KIND_HOLE } ) ,
254+ // The capability is a literal token, or the `required` parameter a
255+ // gate passes on (its sample is the token it is called with).
256+ capability : ts . isIdentifier ( node . arguments [ 2 ] ) && node . arguments [ 2 ] . text in SAMPLES
257+ ? SAMPLES [ node . arguments [ 2 ] . text ]
258+ : render ( node . arguments [ 2 ] , SAMPLES ) ,
256259 } ) ;
257- capabilityGates . push ( at ) ;
258260 } catch ( e ) {
259261 unrenderable . push ( `${ at } : ${ ( e as Error ) . message } ` ) ;
260262 }
@@ -265,28 +267,85 @@ const census = (() => {
265267 return { faults, crashes, relays, unrenderable, capabilityGates } ;
266268} ) ( ) ;
267269
270+ /** The value a `const NAME = <literal>` declaration in the producer holds. */
271+ function constValue ( name : string ) : string | number | undefined {
272+ let found : string | number | undefined ;
273+ const visit = ( node : ts . Node ) : void => {
274+ if ( ts . isVariableDeclaration ( node ) && ts . isIdentifier ( node . name ) && node . name . text === name && node . initializer ) {
275+ const init = node . initializer ;
276+ if ( ts . isStringLiteral ( init ) ) found = init . text ;
277+ else if ( ts . isNumericLiteral ( init ) ) found = Number ( init . text ) ;
278+ }
279+ ts . forEachChild ( node , visit ) ;
280+ } ;
281+ visit ( SOURCE ) ;
282+ return found ;
283+ }
284+
268285/**
269- * [#26903] Does the capability helper send every non-hook origin to
270- * `throwSandboxFault` before anything else? Read from its body: the first
271- * statement must be `if (origin.kind !== 'hook') throwSandboxFault(vm, message)`
272- * (the parameter names read from its own signature).
286+ * [#26965] The capability helper, read from its body: whether it can still
287+ * reach `throwSandboxFault`, the sentence template its `new Error(…)` builds,
288+ * and the `code` / `status` its `Object.assign` declares.
273289 */
274- function capabilityHelperFaultsNonHook ( ) : boolean {
275- let ok = false ;
290+ const capabilityHelper = ( ( ) => {
291+ let callsFault = false ;
292+ let template : ts . Expression | undefined ;
293+ let code : unknown ;
294+ let status : unknown ;
295+ const valueOf = ( e : ts . Expression ) : unknown => {
296+ if ( ts . isStringLiteral ( e ) ) return e . text ;
297+ if ( ts . isNumericLiteral ( e ) ) return Number ( e . text ) ;
298+ if ( ts . isIdentifier ( e ) ) return constValue ( e . text ) ;
299+ return undefined ;
300+ } ;
301+ const visitBody = ( node : ts . Node ) : void => {
302+ if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === 'throwSandboxFault' ) callsFault = true ;
303+ if ( ts . isNewExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === 'Error' ) template = node . arguments ?. [ 0 ] ;
304+ if ( ts . isPropertyAssignment ( node ) && ts . isIdentifier ( node . name ) ) {
305+ if ( node . name . text === 'code' ) code = valueOf ( node . initializer ) ;
306+ if ( node . name . text === 'status' ) status = valueOf ( node . initializer ) ;
307+ }
308+ ts . forEachChild ( node , visitBody ) ;
309+ } ;
310+ let params : string [ ] = [ ] ;
276311 const visit = ( node : ts . Node ) : void => {
277312 if ( ts . isFunctionDeclaration ( node ) && node . name ?. text === CAPABILITY_HELPER && node . body ) {
278- const [ vm , origin , , message ] = node . parameters . map ( ( p ) => p . name . getText ( ) ) ;
279- const first = node . body . statements [ 0 ] ;
280- ok = first !== undefined
281- && ts . isIfStatement ( first )
282- && first . elseStatement === undefined
283- && first . expression . getText ( ) === `${ origin } .kind !== 'hook'`
284- && first . thenStatement . getText ( ) . replace ( / ; $ / , '' ) === `throwSandboxFault(${ vm } , ${ message } )` ;
313+ params = node . parameters . map ( ( p ) => p . name . getText ( ) ) ;
314+ visitBody ( node . body ) ;
285315 }
286316 ts . forEachChild ( node , visit ) ;
287317 } ;
288318 visit ( SOURCE ) ;
289- return ok ;
319+ const [ , origin , capability , message ] = params ;
320+ /** The sentence the helper builds for one gate under one origin kind. */
321+ const sentence = ( gate : CapabilityGate , kind : string ) : string => {
322+ if ( template === undefined ) throw new Error ( `${ CAPABILITY_HELPER } builds no \`new Error(…)\`` ) ;
323+ const gateText = gate . message . split ( ORIGIN_KIND_HOLE ) . join ( kind ) ;
324+ return render ( template , {
325+ [ `${ message } .replace(/\\.$/, '')` ] : gateText . replace ( / \. $ / , '' ) ,
326+ [ message ] : gateText ,
327+ [ capability ] : gate . capability ,
328+ [ `${ origin } .kind` ] : kind ,
329+ } ) ;
330+ } ;
331+ return { callsFault, code, status, sentence, found : params . length === 4 } ;
332+ } ) ( ) ;
333+
334+ interface CapabilityGate extends Site {
335+ capability : string ;
336+ }
337+
338+ /**
339+ * [#26965] The error the runner hands out for a denied capability: the pump
340+ * loop's relay of a coded refusal — the `<kind> '<name>' threw:` wrapper in
341+ * `.message` for the log, the sentence in `innerMessage`, and the code and
342+ * status the refusal carried across the VM.
343+ */
344+ function capabilityRefusal ( gate : CapabilityGate , kind : string ) : Error {
345+ const text = capabilityHelper . sentence ( gate , kind ) ;
346+ const err = new Error ( `${ kind } '${ SAMPLES [ 'origin.name' ] } ' threw: ${ PRODUCER_NAME } : ${ text } ` ) ;
347+ err . name = PRODUCER_NAME ?? 'unread' ;
348+ return Object . assign ( err , { innerMessage : text , code : capabilityHelper . code , status : capabilityHelper . status } ) ;
290349}
291350
292351const PRODUCER_NAME = producerName ( ) ;
@@ -296,6 +355,11 @@ function namingTheObject(): Site[] {
296355 return census . faults . filter ( ( s ) => s . message . includes ( `'${ OBJECT } '` ) ) ;
297356}
298357
358+ /** [#26965] The capability gates whose rendered message quotes the requested object. */
359+ function gatesNamingTheObject ( ) : CapabilityGate [ ] {
360+ return census . capabilityGates . filter ( ( s ) => s . message . includes ( `'${ OBJECT } '` ) ) ;
361+ }
362+
299363/** The producer's own fault: its declared `name`, and no `innerMessage`. */
300364function ownFault ( message : string ) : Error {
301365 const err = new Error ( message ) ;
@@ -349,21 +413,24 @@ describe('[#22741] §1 — every fault the runner raises itself reads the door\'
349413 expect ( PRODUCER_NAME , 'the `this.name = …` assignment in SandboxError\'s constructor' ) . toBeTypeOf ( 'string' ) ;
350414 expect ( census . unrenderable , 'sites this pin cannot render; add a sample or a relay' ) . toEqual ( [ ] ) ;
351415 expect ( [ ...new Set ( census . relays ) ] . sort ( ) ) . toEqual ( Object . keys ( RELAYS ) . sort ( ) ) ;
352- // [#26903 ] The capability gates throw through the helper, and the helper
353- // keeps their action/job arm on `throwSandboxFault`, the fault rows below .
416+ // [#26965 ] The capability gates throw through the helper, and the helper
417+ // has no fault arm: no gate is a fault row here (§4 holds them) .
354418 expect ( census . capabilityGates . length , `${ CAPABILITY_HELPER } call sites` ) . toBeGreaterThanOrEqual ( 5 ) ;
355- expect ( capabilityHelperFaultsNonHook ( ) , `${ CAPABILITY_HELPER } sends every non-hook origin to throwSandboxFault first` ) . toBe ( true ) ;
419+ expect ( capabilityHelper . found , `${ CAPABILITY_HELPER } is declared with (vm, origin, capability, message)` ) . toBe ( true ) ;
420+ expect ( capabilityHelper . callsFault , `${ CAPABILITY_HELPER } reaches throwSandboxFault` ) . toBe ( false ) ;
421+ expect ( census . faults . filter ( ( s ) => / ^ c a p a b i l i t y ' / . test ( s . message ) ) , 'a capability denial raised as a fault' ) . toEqual ( [ ] ) ;
356422 for ( const [ kind , pattern , floor ] of KINDS ) {
357423 const n = census . faults . filter ( ( s ) => pattern . test ( s . message ) ) . length ;
358424 expect ( n , `${ kind } : sites found` ) . toBeGreaterThanOrEqual ( floor ) ;
359425 }
360426 // §2's population: the constructions that set `innerMessage` (crash and refusal sites).
361427 expect ( census . crashes . length ) . toBeGreaterThanOrEqual ( 4 ) ;
362- // [#22868] The sites that name the requested object: the `ctx.api.object(…)`
363- // capability gate and the missing-method arm, each held to `500` below.
428+ // [#22868] The fault site that names the requested object: the
429+ // missing-method arm, held to `500` below. [#26965] The `ctx.api.object(…)`
430+ // capability gate names it too, and §4 holds it to its `403`.
364431 const named = namingTheObject ( ) ;
365- expect ( named . filter ( ( s ) => / ^ c a p a b i l i t y ' / . test ( s . message ) ) . length , 'capability denial' ) . toBeGreaterThanOrEqual ( 1 ) ;
366432 expect ( named . filter ( ( s ) => / n o t i m p l e m e n t e d $ / . test ( s . message ) ) . length , 'missing ctx.api method' ) . toBeGreaterThanOrEqual ( 1 ) ;
433+ expect ( gatesNamingTheObject ( ) . length , 'the ctx.api.object(…) capability gate' ) . toBeGreaterThanOrEqual ( 1 ) ;
367434 } ) ;
368435
369436 it . each ( census . faults . map ( ( s ) => [ s . label , s . message ] as const ) ) ( '%s' , async ( _label , message ) => {
@@ -426,7 +493,10 @@ describe('[#22741] §3 — controls: what the predicate must NOT change', () =>
426493
427494 it ( 'CONTROL [#22868] — the same object-naming texts, thrown by anything but the sandbox, keep the heuristic\'s 404' , ( ) => {
428495 // The door asks the producer's identity, never the words: no second rule.
429- const named = namingTheObject ( ) ;
496+ const named : Site [ ] = [
497+ ...namingTheObject ( ) ,
498+ ...gatesNamingTheObject ( ) . map ( ( g ) => ( { label : g . label , message : g . message . split ( ORIGIN_KIND_HOLE ) . join ( 'hook' ) } ) ) ,
499+ ] ;
430500 expect ( named . length ) . toBeGreaterThanOrEqual ( 2 ) ;
431501 for ( const { label, message } of named ) {
432502 const err = new Error ( message ) ;
@@ -461,3 +531,31 @@ describe('[#22741] §3 — controls: what the predicate must NOT change', () =>
461531 }
462532 } ) ;
463533} ) ;
534+
535+ describe ( '[#26965] §4 — a capability denial is a coded refusal for every origin, on the door and on the row' , ( ) => {
536+ it ( 'the helper declares the standard catalog code at its status' , ( ) => {
537+ expect ( capabilityHelper . code ) . toBe ( 'INSUFFICIENT_PRIVILEGES' ) ;
538+ expect ( capabilityHelper . status ) . toBe ( 403 ) ;
539+ } ) ;
540+
541+ const cases = census . capabilityGates . flatMap ( ( gate ) => ORIGIN_KINDS . map ( ( kind ) => [ `${ gate . label } , ${ kind } origin` , gate , kind ] as const ) ) ;
542+
543+ it . each ( cases ) ( '%s' , async ( _label , gate , kind ) => {
544+ const sentence = capabilityHelper . sentence ( gate , kind ) ;
545+ // The remedy names the body's own owner, never another origin's.
546+ expect ( sentence ) . toContain ( `capability '${ gate . capability } ' not granted to ${ kind } '` ) ;
547+ expect ( sentence ) . toContain ( `Grant it by adding '${ gate . capability } ' to the ${ kind } 's body.capabilities.` ) ;
548+
549+ const err = capabilityRefusal ( gate , kind ) ;
550+ expect ( isSandboxOwnFault ( err ) ) . toBe ( false ) ;
551+ // The door answers the refusal, never the fault's 500 nor, for the gate
552+ // that names the requested object, the unknown-object heuristic's 404.
553+ const door = mapDataError ( err , OBJECT ) ;
554+ expect ( door . status ) . toBe ( 403 ) ;
555+ expect ( door . body ) . toMatchObject ( { error : sentence , code : 'INSUFFICIENT_PRIVILEGES' } ) ;
556+
557+ // The row stays a row error, reading the refusal's own sentence and code.
558+ const row = await rowFor ( ( ) => capabilityRefusal ( gate , kind ) ) ;
559+ expect ( row ) . toEqual ( { row : 1 , ok : false , action : 'failed' , error : sentence , code : 'INSUFFICIENT_PRIVILEGES' } ) ;
560+ } ) ;
561+ } ) ;
0 commit comments