|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #3867 follow-up — the OTHER dispatcher error exit. |
| 5 | + * |
| 6 | + * #3867 sanitised `dispatcher-plugin`'s `errorResponseBase`, which handles |
| 7 | + * errors THROWN out of `dispatch()`. It does not cover the errors `dispatch()` |
| 8 | + * RETURNS: a `{handled: true, response}` result goes to `sendResult`, never |
| 9 | + * through that catch. Those bodies are built by `HttpDispatcher.error()` — the |
| 10 | + * single construction point for every returned error — and it passed the |
| 11 | + * message through verbatim. |
| 12 | + * |
| 13 | + * That path is reachable with a raw driver message today via `errorFromThrown` |
| 14 | + * (`/meta` save, `/packages` install) and the MCP transport's |
| 15 | + * `deps.error(err?.message, 500)`. |
| 16 | + * |
| 17 | + * `error()` is private, so these drive it the way real traffic does: through |
| 18 | + * `dispatch()` on routes whose service throws. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect } from 'vitest'; |
| 22 | + |
| 23 | +import { HttpDispatcher } from './http-dispatcher.js'; |
| 24 | + |
| 25 | +const SQL_DUMP = 'insert into `sys_team` (`id`) values (?) - UNIQUE constraint failed: sys_team.id'; |
| 26 | + |
| 27 | +/** |
| 28 | + * A kernel whose `protocol` service throws on `saveMetaItem` — the `/meta` PUT |
| 29 | + * route catches it and RETURNS `deps.errorFromThrown(e, 400)`, which is the |
| 30 | + * returned-error path this guard covers (it never reaches the plugin's |
| 31 | + * throw-side `errorResponseBase`). |
| 32 | + */ |
| 33 | +function makeDispatcher(saveError: unknown) { |
| 34 | + const protocol = { |
| 35 | + saveMetaItem: async () => { throw saveError; }, |
| 36 | + }; |
| 37 | + const kernel: any = { |
| 38 | + getService: (name: string) => (name === 'protocol' ? protocol : undefined), |
| 39 | + getServiceAsync: async (name: string) => (name === 'protocol' ? protocol : undefined), |
| 40 | + }; |
| 41 | + return new HttpDispatcher(kernel); |
| 42 | +} |
| 43 | + |
| 44 | +async function putMeta(saveError: unknown) { |
| 45 | + const dispatcher = makeDispatcher(saveError); |
| 46 | + return dispatcher.dispatch( |
| 47 | + 'PUT', |
| 48 | + '/meta/object/widget', |
| 49 | + { name: 'widget' }, |
| 50 | + {}, |
| 51 | + {} as any, |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +describe('#3867 follow-up — HttpDispatcher.error() does not return raw driver messages', () => { |
| 56 | + it('replaces a SQL dump on a returned 5xx', async () => { |
| 57 | + const err = Object.assign(new Error(SQL_DUMP), { status: 500 }); |
| 58 | + const result: any = await putMeta(err); |
| 59 | + |
| 60 | + expect(result.response.status).toBe(500); |
| 61 | + expect(result.response.body.error.message).toBe('Internal server error'); |
| 62 | + expect(String(result.response.body.error.message)).not.toContain('sys_team'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('leaves a deliberate 4xx message intact even when it resembles SQL', async () => { |
| 66 | + // The tier that matters: `errorFromThrown` defaults `/meta` saves to |
| 67 | + // 400, and a validation message is the caller's answer — swallowing it |
| 68 | + // would be a worse bug than the leak. |
| 69 | + const err = Object.assign(new Error('unique constraint on name — pick another'), { |
| 70 | + status: 422, |
| 71 | + }); |
| 72 | + const result: any = await putMeta(err); |
| 73 | + |
| 74 | + expect(result.response.status).toBe(422); |
| 75 | + expect(result.response.body.error.message).toBe('unique constraint on name — pick another'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('leaves an ordinary 5xx message intact — only leaks are replaced', async () => { |
| 79 | + const err = Object.assign(new Error('metadata store is unavailable'), { status: 503 }); |
| 80 | + const result: any = await putMeta(err); |
| 81 | + |
| 82 | + expect(result.response.status).toBe(503); |
| 83 | + expect(result.response.body.error.message).toBe('metadata store is unavailable'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('preserves structured `details` (code / issues) while sanitising the message', async () => { |
| 87 | + // `details` carries the semantic code and per-field `issues` the UI maps |
| 88 | + // back to inputs; it is never free-form driver prose, so the guard must |
| 89 | + // not touch it. |
| 90 | + const err = Object.assign(new Error(SQL_DUMP), { |
| 91 | + status: 500, |
| 92 | + code: 'STORAGE_FAILURE', |
| 93 | + issues: [{ path: 'name', message: 'taken', code: 'duplicate' }], |
| 94 | + }); |
| 95 | + const result: any = await putMeta(err); |
| 96 | + |
| 97 | + expect(result.response.body.error.message).toBe('Internal server error'); |
| 98 | + expect(result.response.body.error.details).toMatchObject({ |
| 99 | + code: 'STORAGE_FAILURE', |
| 100 | + issues: [{ path: 'name', message: 'taken', code: 'duplicate' }], |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments