5555 * until the door asked `isSandboxOwnFault` first. So §1 holds each site that
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.
58+ *
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`.
5871 */
5972
6073import { readFileSync } from 'node:fs' ;
@@ -92,6 +105,18 @@ const SAMPLES: Readonly<Record<string, string>> = {
92105 fieldName : 'owner' ,
93106} ;
94107
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+
117+ /** The helper every capability gate throws through, read below from the producer. */
118+ const CAPABILITY_HELPER = 'throwCapabilityDenial' ;
119+
95120/** §2: the error a nested body threw, so the construction it lands in is a CRASH. */
96121const CRASH_SAMPLES : Readonly < Record < string , string > > = {
97122 ...SAMPLES ,
@@ -105,7 +130,7 @@ const CRASH_SAMPLES: Readonly<Record<string, string>> = {
105130 * runner, or a new non-literal one, fails §1's census.
106131 */
107132const RELAYS : Readonly < Record < string , string > > = {
108- message : '`throwSandboxFault` builds the error for each of its call sites, which are sites here in their own right' ,
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' ,
109134 'withoutSandboxErrorPrefix(String(errStr))' :
110135 '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)' ,
111136} ;
@@ -182,6 +207,7 @@ const census = (() => {
182207 const crashes : Site [ ] = [ ] ;
183208 const relays : string [ ] = [ ] ;
184209 const unrenderable : string [ ] = [ ] ;
210+ const capabilityGates : string [ ] = [ ] ;
185211 const visit = ( node : ts . Node ) : void => {
186212 if ( ts . isNewExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === 'SandboxError' ) {
187213 const args = node . arguments ?? [ ] ;
@@ -208,19 +234,61 @@ const census = (() => {
208234 }
209235 }
210236 if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === 'throwSandboxFault' ) {
237+ const at = `quickjs-runner.ts:${ lineOf ( node ) } ` ;
238+ const text = node . arguments [ 1 ] ?. getText ( ) ?? '' ;
239+ if ( text in RELAYS ) {
240+ relays . push ( text ) ;
241+ } else {
242+ try {
243+ faults . push ( { label : `${ at } throwSandboxFault(…)` , message : render ( node . arguments [ 1 ] , SAMPLES ) } ) ;
244+ } catch ( e ) {
245+ unrenderable . push ( `${ at } : ${ ( e as Error ) . message } ` ) ;
246+ }
247+ }
248+ }
249+ // [#26903] A capability gate: a fault on the action/job arm (see the header).
250+ if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === CAPABILITY_HELPER ) {
211251 const at = `quickjs-runner.ts:${ lineOf ( node ) } ` ;
212252 try {
213- faults . push ( { label : `${ at } throwSandboxFault(…)` , message : render ( node . arguments [ 1 ] , SAMPLES ) } ) ;
253+ faults . push ( {
254+ label : `${ at } ${ CAPABILITY_HELPER } (…), action origin` ,
255+ message : render ( node . arguments [ 3 ] , CAPABILITY_FAULT_SAMPLES ) ,
256+ } ) ;
257+ capabilityGates . push ( at ) ;
214258 } catch ( e ) {
215259 unrenderable . push ( `${ at } : ${ ( e as Error ) . message } ` ) ;
216260 }
217261 }
218262 ts . forEachChild ( node , visit ) ;
219263 } ;
220264 visit ( SOURCE ) ;
221- return { faults, crashes, relays, unrenderable } ;
265+ return { faults, crashes, relays, unrenderable, capabilityGates } ;
222266} ) ( ) ;
223267
268+ /**
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).
273+ */
274+ function capabilityHelperFaultsNonHook ( ) : boolean {
275+ let ok = false ;
276+ const visit = ( node : ts . Node ) : void => {
277+ 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 } )` ;
285+ }
286+ ts . forEachChild ( node , visit ) ;
287+ } ;
288+ visit ( SOURCE ) ;
289+ return ok ;
290+ }
291+
224292const PRODUCER_NAME = producerName ( ) ;
225293
226294/** [#22868] The fault sites whose rendered message quotes the requested object. */
@@ -281,6 +349,10 @@ describe('[#22741] §1 — every fault the runner raises itself reads the door\'
281349 expect ( PRODUCER_NAME , 'the `this.name = …` assignment in SandboxError\'s constructor' ) . toBeTypeOf ( 'string' ) ;
282350 expect ( census . unrenderable , 'sites this pin cannot render; add a sample or a relay' ) . toEqual ( [ ] ) ;
283351 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.
354+ 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 ) ;
284356 for ( const [ kind , pattern , floor ] of KINDS ) {
285357 const n = census . faults . filter ( ( s ) => pattern . test ( s . message ) ) . length ;
286358 expect ( n , `${ kind } : sites found` ) . toBeGreaterThanOrEqual ( floor ) ;
0 commit comments