|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27829] A `$searchFields` entry naming a CREDENTIAL field (`secret` / |
| 5 | + * `password`) is refused `400 INVALID_FIELD` at the Data API door, naming the |
| 6 | + * field, even when the object's own `searchableFields` declares it — over a |
| 7 | + * real `SqlDriver` (SQLite), the harness of `data-empty-search-fields-door.test.ts`. |
| 8 | + * |
| 9 | + * Before: the door (`assertSearchFieldsAreSearchable`) resolved the allowed set |
| 10 | + * from the declaration, which kept the credential entry, so the request passed; |
| 11 | + * the engine then withheld the credential column from the scan (ADR-0100) and, |
| 12 | + * by its tolerant intersection, searched the REST of the declared set. |
| 13 | + * `?$search=…&$searchFields=api_key` answered 200 with rows matched in `name` — |
| 14 | + * a narrower search answered with a wider one. The engine's withhold stays as |
| 15 | + * the fail-closed floor; this file pins the door's half: the status, `code`, |
| 16 | + * named field, that the driver was never asked, and the controls unchanged. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 20 | +import { ObjectQL } from '@objectstack/objectql'; |
| 21 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 22 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 23 | +import { RestServer } from './rest-server'; |
| 24 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 25 | + |
| 26 | +/** object name · the credential type its `api_key` field carries. */ |
| 27 | +const OBJECTS = [ |
| 28 | + ['rest_search_credential_secret_27829', 'secret'], |
| 29 | + ['rest_search_credential_password_27829', 'password'], |
| 30 | +] as const; |
| 31 | + |
| 32 | +const NAMES = ['qq7-a', 'qq7-b', 'other']; |
| 33 | + |
| 34 | +/** The term every case searches: in two rows' `name`. */ |
| 35 | +const TERM = 'qq7'; |
| 36 | + |
| 37 | +function createMockServer() { |
| 38 | + const noop = () => {}; |
| 39 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 40 | +} |
| 41 | + |
| 42 | +function makeRes() { |
| 43 | + const res: any = { |
| 44 | + write: () => true, end: () => {}, |
| 45 | + header: () => res, |
| 46 | + status: (code: number) => { res._status = code; return res; }, |
| 47 | + json: (body: any) => { res._json = body; return res; }, |
| 48 | + }; |
| 49 | + return res; |
| 50 | +} |
| 51 | + |
| 52 | +describe('[#27829] $searchFields naming a declared credential field on the Data API door — sqlite', () => { |
| 53 | + let engine: ObjectQL; |
| 54 | + let driver: any; |
| 55 | + const reads = { n: 0 }; |
| 56 | + let list: (object: string, query: Record<string, string>) => Promise<{ status: number; body: any }>; |
| 57 | + let query: (object: string, body: Record<string, unknown>) => Promise<{ status: number; body: any }>; |
| 58 | + |
| 59 | + beforeAll(async () => { |
| 60 | + driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 61 | + engine = new ObjectQL(); |
| 62 | + engine.registerDriver(driver, true); |
| 63 | + await engine.init(); |
| 64 | + for (const [name, ctype] of OBJECTS) { |
| 65 | + engine.registry.registerObject({ |
| 66 | + name, |
| 67 | + label: `Credential ${ctype} 27829`, |
| 68 | + searchableFields: ['name', 'api_key'], |
| 69 | + fields: { |
| 70 | + name: { name: 'name', type: 'text' as const }, |
| 71 | + api_key: { name: 'api_key', type: ctype }, |
| 72 | + }, |
| 73 | + } as any); |
| 74 | + } |
| 75 | + await engine.syncSchemas(); |
| 76 | + // A real `single` install's Default Organization (ADR-0131 D9, D1). |
| 77 | + await installDefaultOrganization(engine); |
| 78 | + for (const [object] of OBJECTS) { |
| 79 | + for (const [i, name] of NAMES.entries()) await engine.insert(object, { id: `r${i}`, name } as any); |
| 80 | + } |
| 81 | + |
| 82 | + const watched = new Set<string>(OBJECTS.map(([o]) => o)); |
| 83 | + for (const verb of ['find', 'findOne', 'count', 'aggregate'] as const) { |
| 84 | + const real = driver[verb].bind(driver); |
| 85 | + driver[verb] = (o: string, ...rest: unknown[]) => { if (watched.has(o)) reads.n += 1; return real(o, ...rest); }; |
| 86 | + } |
| 87 | + |
| 88 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 89 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 90 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 91 | + rest.registerRoutes(); |
| 92 | + const getRoute = rest.getRoutes().find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object'); |
| 93 | + expect(getRoute, 'GET /api/v1/data/:object').toBeDefined(); |
| 94 | + list = async (object, q) => { |
| 95 | + const res = makeRes(); |
| 96 | + // What a querystring carries: strings. |
| 97 | + await getRoute!.handler({ params: { object }, query: { $orderby: 'name', $search: TERM, ...q }, headers: {} } as any, res); |
| 98 | + return { status: res._status ?? 200, body: res._json }; |
| 99 | + }; |
| 100 | + const postRoute = rest.getRoutes().find((r: any) => r.method === 'POST' && r.path === '/api/v1/data/:object/query'); |
| 101 | + expect(postRoute, 'POST /api/v1/data/:object/query').toBeDefined(); |
| 102 | + query = async (object, body) => { |
| 103 | + const res = makeRes(); |
| 104 | + // What the wire carries: JSON. |
| 105 | + await postRoute!.handler({ params: { object }, body: JSON.parse(JSON.stringify(body)), query: {}, headers: {} } as any, res); |
| 106 | + return { status: res._status ?? 200, body: res._json }; |
| 107 | + }; |
| 108 | + }); |
| 109 | + |
| 110 | + afterAll(async () => { |
| 111 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 112 | + }); |
| 113 | + |
| 114 | + for (const [object, ctype] of OBJECTS) { |
| 115 | + it(`${ctype}: $searchFields=api_key and $searchFields=name,api_key are 400 INVALID_FIELD naming api_key — no read`, async () => { |
| 116 | + const before = reads.n; |
| 117 | + for (const searchFields of ['api_key', 'name,api_key']) { |
| 118 | + const res = await list(object, { $searchFields: searchFields }); |
| 119 | + const label = `$searchFields=${searchFields}: ${JSON.stringify(res.body)}`; |
| 120 | + expect(res.status, label).toBe(400); |
| 121 | + expect(res.body.code, label).toBe('INVALID_FIELD'); |
| 122 | + expect(res.body.error, label).toContain(`Field 'api_key' on object '${object}' is type '${ctype}'`); |
| 123 | + } |
| 124 | + expect(reads.n - before, 'no read of the object — the refusal precedes the driver').toBe(0); |
| 125 | + }); |
| 126 | + |
| 127 | + it(`${ctype}: POST query — searchFields and the object-form search.fields naming api_key are the same refusal — no read`, async () => { |
| 128 | + const before = reads.n; |
| 129 | + for (const body of [{ search: TERM, searchFields: ['api_key'] }, { search: { query: TERM, fields: ['name', 'api_key'] } }]) { |
| 130 | + const res = await query(object, body); |
| 131 | + const label = `${JSON.stringify(body)}: ${JSON.stringify(res.body)}`; |
| 132 | + expect(res.status, label).toBe(400); |
| 133 | + expect(res.body.code, label).toBe('INVALID_FIELD'); |
| 134 | + expect(res.body.error, label).toContain(`Field 'api_key' on object '${object}' is type '${ctype}'`); |
| 135 | + } |
| 136 | + expect(reads.n - before, 'no read of the object — the refusal precedes the driver').toBe(0); |
| 137 | + }); |
| 138 | + |
| 139 | + it(`${ctype}: controls — $searchFields=name narrows, no $searchFields searches the declared set, as before`, async () => { |
| 140 | + const narrowed = await list(object, { $searchFields: 'name' }); |
| 141 | + expect(narrowed.status, JSON.stringify(narrowed.body)).toBe(200); |
| 142 | + expect(narrowed.body.records.map((r: any) => r.name)).toEqual(['qq7-a', 'qq7-b']); |
| 143 | + |
| 144 | + const all = await list(object, {}); |
| 145 | + expect(all.status, JSON.stringify(all.body)).toBe(200); |
| 146 | + expect(all.body.records.map((r: any) => r.name)).toEqual(['qq7-a', 'qq7-b']); |
| 147 | + |
| 148 | + const posted = await query(object, { search: TERM, searchFields: ['name'] }); |
| 149 | + expect(posted.status, JSON.stringify(posted.body)).toBe(200); |
| 150 | + expect(posted.body.records).toHaveLength(2); |
| 151 | + }); |
| 152 | + } |
| 153 | +}); |
0 commit comments