|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27672] The runtime `/actions` door answers a sandboxed body's undeclared |
| 5 | + * abort with `HOOK_ABORTED`, the code `/api/v1/data` has answered since |
| 6 | + * #27597, instead of the `VALIDATION_ERROR` its 400 derives. |
| 7 | + * |
| 8 | + * The door's catch hands a rejection to `errorFromThrown` |
| 9 | + * (`http-dispatcher.ts`), whose code is `resolveThrownHttpError`'s |
| 10 | + * (`@objectstack/types`). The resolver now answers |
| 11 | + * `UNDECLARED_HOOK_REFUSAL_CODE` for a sandboxed refusal that declared no code, |
| 12 | + * so this door reads the one rule rather than a copy of it. §2 is the control: |
| 13 | + * a code the body declared is kept, a registered handler's plain rejection |
| 14 | + * still derives `VALIDATION_ERROR`, and a crash is still the sanitised 500. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, vi } from 'vitest'; |
| 18 | +import { UNDECLARED_HOOK_REFUSAL_CODE } from '@objectstack/types'; |
| 19 | + |
| 20 | +import { HttpDispatcher } from '../http-dispatcher.js'; |
| 21 | +import { SandboxError } from '../sandbox/quickjs-runner.js'; |
| 22 | + |
| 23 | +const scriptAction = { |
| 24 | + name: 'submit_signoff', |
| 25 | + objectName: 'crm_invoice', |
| 26 | + type: 'script', |
| 27 | + body: { language: 'js', source: 'return 1;', capabilities: ['api.write'] }, |
| 28 | +}; |
| 29 | + |
| 30 | +/** A dispatcher whose action handler rejects with `thrown`. */ |
| 31 | +function makeDispatcher(thrown: unknown) { |
| 32 | + const objectDef = { name: 'crm_invoice', actions: [scriptAction] }; |
| 33 | + const ql: any = { |
| 34 | + executeAction: vi.fn(async () => { throw thrown; }), |
| 35 | + getSchema: (n: string) => (n === objectDef.name ? objectDef : undefined), |
| 36 | + registry: { getObject: (n: string) => (n === objectDef.name ? objectDef : undefined), getItem: () => undefined }, |
| 37 | + find: vi.fn(async () => [{ id: 'inv_1', status: 'draft' }]), |
| 38 | + insert: vi.fn(), update: vi.fn(), delete: vi.fn(), |
| 39 | + }; |
| 40 | + const metadata: any = { |
| 41 | + load: vi.fn(async () => null), |
| 42 | + listObjects: vi.fn(async () => [objectDef]), |
| 43 | + getObject: vi.fn(async () => objectDef), |
| 44 | + }; |
| 45 | + const kernel: any = { |
| 46 | + context: { |
| 47 | + getService: (n: string) => |
| 48 | + n === 'objectql' || n === 'data' ? ql : n === 'metadata' ? metadata : null, |
| 49 | + }, |
| 50 | + }; |
| 51 | + return new HttpDispatcher(kernel); |
| 52 | +} |
| 53 | + |
| 54 | +async function invoke(thrown: unknown) { |
| 55 | + const res: any = await makeDispatcher(thrown).handleActions( |
| 56 | + '/crm_invoice/submit_signoff/inv_1', |
| 57 | + 'POST', |
| 58 | + {}, |
| 59 | + { request: {}, environmentId: 'platform', executionContext: { userId: 'u1', systemPermissions: [] } } as any, |
| 60 | + ); |
| 61 | + return res.response; |
| 62 | +} |
| 63 | + |
| 64 | +describe('[#27672] §1 an undeclared abort answers HOOK_ABORTED at /actions', () => { |
| 65 | + it('the action body\'s own free-text throw', async () => { |
| 66 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 67 | + const response = await invoke( |
| 68 | + new SandboxError("action 'submit_signoff' threw: Contact has no phone", 'Contact has no phone'), |
| 69 | + ); |
| 70 | + expect(response.status).toBe(400); |
| 71 | + expect(response.body.error.code).toBe('HOOK_ABORTED'); |
| 72 | + expect(response.body.error.code).toBe(UNDECLARED_HOOK_REFUSAL_CODE); |
| 73 | + expect(response.body.error.message).toBe('Contact has no phone'); |
| 74 | + expect(response.body.error).not.toHaveProperty('declaredCode'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('a hook abort relayed through the action body (the nested refusal\'s shape)', async () => { |
| 78 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 79 | + const response = await invoke(new SandboxError( |
| 80 | + "action 'submit_signoff' threw: hook 'guard' threw: Error: A finished invoice cannot be signed.", |
| 81 | + 'A finished invoice cannot be signed.', |
| 82 | + )); |
| 83 | + expect(response.status).toBe(400); |
| 84 | + expect(response.body.error.code).toBe('HOOK_ABORTED'); |
| 85 | + expect(response.body.error.message).toBe('A finished invoice cannot be signed.'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('a 4xx status the body declared without a code keeps the status', async () => { |
| 89 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 90 | + const response = await invoke(new SandboxError("action 'submit_signoff' threw: Closed.", 'Closed.', { status: 422 })); |
| 91 | + expect(response.status).toBe(422); |
| 92 | + expect(response.body.error.code).toBe('HOOK_ABORTED'); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('[#27672] §2 CONTROLS — a declared code is kept; nothing is invented elsewhere', () => { |
| 97 | + it('a registered code the body declared is its own code', async () => { |
| 98 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 99 | + const response = await invoke(new SandboxError( |
| 100 | + "action 'submit_signoff' threw: Locked.", 'Locked.', { code: 'RECORD_LOCKED', status: 409 }, |
| 101 | + )); |
| 102 | + expect(response.status).toBe(409); |
| 103 | + expect(response.body.error.code).toBe('RECORD_LOCKED'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('an unregistered code the body declared rides declaredCode beside the derived code', async () => { |
| 107 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 108 | + const response = await invoke(new SandboxError( |
| 109 | + "action 'submit_signoff' threw: Over the limit.", 'Over the limit.', { code: 'CREDIT_LIMIT', status: 400 }, |
| 110 | + )); |
| 111 | + expect(response.status).toBe(400); |
| 112 | + expect(response.body.error.code).toBe('VALIDATION_ERROR'); |
| 113 | + expect(response.body.error.declaredCode).toBe('CREDIT_LIMIT'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('a registered handler\'s plain rejection (no sandbox) still derives VALIDATION_ERROR', async () => { |
| 117 | + const response = await invoke(new Error('Lead is already converted')); |
| 118 | + expect(response.status).toBe(400); |
| 119 | + expect(response.body.error.code).toBe('VALIDATION_ERROR'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('a body that CRASHED is still the sanitised 500', async () => { |
| 123 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 124 | + const response = await invoke(new SandboxError( |
| 125 | + "action 'submit_signoff' threw: TypeError: x is not a function", 'TypeError: x is not a function', |
| 126 | + )); |
| 127 | + expect(response.status).toBe(500); |
| 128 | + expect(response.body.error.code).toBe('INTERNAL_ERROR'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments