|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29008] A field marked `searchable: true` is searched, and a `$searchFields` |
| 5 | + * override naming it is accepted, when its object declares no |
| 6 | + * `searchableFields` — over the Data API door on a real `SqlDriver` (SQLite), |
| 7 | + * the harness of `data-search-fields-credential-door.test.ts`. |
| 8 | + * |
| 9 | + * Ruling record 6573569376 (letter A): with no declaration, the set `search` |
| 10 | + * scans is the type-based auto-default plus every field marked |
| 11 | + * `searchable: true`, of any type except `secret` / `password` / `vector`, and |
| 12 | + * the ingress allow-list is the same set. Before, the card's three requests |
| 13 | + * answered `[]`, `[]`, then `400 INVALID_FIELD` "is not searchable … 'snippet' |
| 14 | + * is type 'code'" while the served schema said `searchable: true`. |
| 15 | + * |
| 16 | + * Pinned here: the card's three requests (the row comes back through `snippet` |
| 17 | + * and through `body`, and `$searchFields=snippet` answers 200 with it), and the |
| 18 | + * controls the ruling keeps — an unflagged `code` field is still neither |
| 19 | + * scanned nor accepted, a flagged `secret` is still refused, and an object |
| 20 | + * that DECLARES `searchableFields` keeps its declaration as the whole set. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 24 | +import { ObjectQL } from '@objectstack/objectql'; |
| 25 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 26 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 27 | +import { RestServer } from './rest-server'; |
| 28 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 29 | + |
| 30 | +/** No declaration: the flag decides. */ |
| 31 | +const AUTO = 'rest_search_flag_auto_29008'; |
| 32 | +/** The same fields, under a declaration that names only `name`. */ |
| 33 | +const DECLARED = 'rest_search_flag_declared_29008'; |
| 34 | + |
| 35 | +const FIELDS = { |
| 36 | + name: { name: 'name', type: 'text' as const }, |
| 37 | + snippet: { name: 'snippet', type: 'code' as const, searchable: true }, |
| 38 | + body: { name: 'body', type: 'html' as const, searchable: true }, |
| 39 | + // CONTROL — a `code` field NOT marked: the auto-default still skips its type. |
| 40 | + scratch: { name: 'scratch', type: 'code' as const }, |
| 41 | + // CONTROL — a hard exclusion: the flag cannot admit a credential. |
| 42 | + api_key: { name: 'api_key', type: 'secret' as const, searchable: true }, |
| 43 | +}; |
| 44 | + |
| 45 | +const ROWS = [ |
| 46 | + { id: 'r1', name: 'fs2 row', snippet: 'select QQCODE from x', body: '<p>holding QQHTML</p>', scratch: 'QQSCRATCH' }, |
| 47 | + { id: 'r2', name: 'other row', snippet: 'select 1', body: '<p>nothing</p>', scratch: 'nothing' }, |
| 48 | +]; |
| 49 | + |
| 50 | +function createMockServer() { |
| 51 | + const noop = () => {}; |
| 52 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 53 | +} |
| 54 | + |
| 55 | +function makeRes() { |
| 56 | + const res: any = { |
| 57 | + write: () => true, end: () => {}, |
| 58 | + header: () => res, |
| 59 | + status: (code: number) => { res._status = code; return res; }, |
| 60 | + json: (body: any) => { res._json = body; return res; }, |
| 61 | + }; |
| 62 | + return res; |
| 63 | +} |
| 64 | + |
| 65 | +describe('[#29008] field.searchable: true on the Data API door — sqlite', () => { |
| 66 | + let engine: ObjectQL; |
| 67 | + let driver: any; |
| 68 | + const reads = { n: 0 }; |
| 69 | + let list: (object: string, query: Record<string, string>) => Promise<{ status: number; body: any }>; |
| 70 | + |
| 71 | + beforeAll(async () => { |
| 72 | + driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 73 | + engine = new ObjectQL(); |
| 74 | + engine.registerDriver(driver, true); |
| 75 | + await engine.init(); |
| 76 | + engine.registry.registerObject({ name: AUTO, label: 'Search flag auto 29008', fields: FIELDS } as any); |
| 77 | + engine.registry.registerObject({ |
| 78 | + name: DECLARED, label: 'Search flag declared 29008', searchableFields: ['name'], fields: FIELDS, |
| 79 | + } as any); |
| 80 | + await engine.syncSchemas(); |
| 81 | + // A real `single` install's Default Organization (ADR-0131 D9, D1). |
| 82 | + await installDefaultOrganization(engine); |
| 83 | + for (const object of [AUTO, DECLARED]) { |
| 84 | + for (const row of ROWS) await engine.insert(object, row as any); |
| 85 | + } |
| 86 | + |
| 87 | + const watched = new Set<string>([AUTO, DECLARED]); |
| 88 | + for (const verb of ['find', 'findOne', 'count', 'aggregate'] as const) { |
| 89 | + const real = driver[verb].bind(driver); |
| 90 | + driver[verb] = (o: string, ...rest: unknown[]) => { if (watched.has(o)) reads.n += 1; return real(o, ...rest); }; |
| 91 | + } |
| 92 | + |
| 93 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 94 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 95 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 96 | + rest.registerRoutes(); |
| 97 | + const getRoute = rest.getRoutes().find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object'); |
| 98 | + expect(getRoute, 'GET /api/v1/data/:object').toBeDefined(); |
| 99 | + list = async (object, q) => { |
| 100 | + const res = makeRes(); |
| 101 | + // What a querystring carries: strings. |
| 102 | + await getRoute!.handler({ params: { object }, query: { $orderby: 'name', ...q }, headers: {} } as any, res); |
| 103 | + return { status: res._status ?? 200, body: res._json }; |
| 104 | + }; |
| 105 | + }); |
| 106 | + |
| 107 | + afterAll(async () => { |
| 108 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 109 | + }); |
| 110 | + |
| 111 | + const names = (body: any) => (body?.records ?? []).map((r: any) => r.name); |
| 112 | + |
| 113 | + it('the card\'s three requests: found through `snippet`, through `body`, and `$searchFields=snippet` is 200', async () => { |
| 114 | + const byCode = await list(AUTO, { $search: 'QQCODE' }); |
| 115 | + expect(byCode.status, JSON.stringify(byCode.body)).toBe(200); |
| 116 | + expect(names(byCode.body)).toEqual(['fs2 row']); |
| 117 | + |
| 118 | + const byHtml = await list(AUTO, { $search: 'QQHTML' }); |
| 119 | + expect(byHtml.status, JSON.stringify(byHtml.body)).toBe(200); |
| 120 | + expect(names(byHtml.body)).toEqual(['fs2 row']); |
| 121 | + |
| 122 | + const narrowed = await list(AUTO, { $search: 'QQCODE', $searchFields: 'snippet' }); |
| 123 | + expect(narrowed.status, JSON.stringify(narrowed.body)).toBe(200); |
| 124 | + expect(names(narrowed.body)).toEqual(['fs2 row']); |
| 125 | + // …and the narrowing is real: the html term is not in `snippet`. |
| 126 | + const narrowedMiss = await list(AUTO, { $search: 'QQHTML', $searchFields: 'snippet' }); |
| 127 | + expect(narrowedMiss.status, JSON.stringify(narrowedMiss.body)).toBe(200); |
| 128 | + expect(names(narrowedMiss.body)).toEqual([]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('CONTROL — an unflagged `code` field is not scanned, and naming it is still 400 INVALID_FIELD before any read', async () => { |
| 132 | + const unscanned = await list(AUTO, { $search: 'QQSCRATCH' }); |
| 133 | + expect(unscanned.status, JSON.stringify(unscanned.body)).toBe(200); |
| 134 | + expect(names(unscanned.body)).toEqual([]); |
| 135 | + |
| 136 | + const before = reads.n; |
| 137 | + const refused = await list(AUTO, { $search: 'QQSCRATCH', $searchFields: 'scratch' }); |
| 138 | + expect(refused.status, JSON.stringify(refused.body)).toBe(400); |
| 139 | + expect(refused.body.code).toBe('INVALID_FIELD'); |
| 140 | + expect(refused.body.error).toContain("Field 'scratch' on object '" + AUTO + "' is not searchable"); |
| 141 | + expect(reads.n - before, 'the refusal precedes the driver').toBe(0); |
| 142 | + }); |
| 143 | + |
| 144 | + it('CONTROL — a flagged `secret` stays excluded: naming it is 400 INVALID_FIELD before any read', async () => { |
| 145 | + const before = reads.n; |
| 146 | + const refused = await list(AUTO, { $search: 'QQKEY', $searchFields: 'api_key' }); |
| 147 | + expect(refused.status, JSON.stringify(refused.body)).toBe(400); |
| 148 | + expect(refused.body.code).toBe('INVALID_FIELD'); |
| 149 | + expect(reads.n - before, 'the refusal precedes the driver').toBe(0); |
| 150 | + }); |
| 151 | + |
| 152 | + it('CONTROL — a declared `searchableFields` stays the whole set: the flag is not read there', async () => { |
| 153 | + const byCode = await list(DECLARED, { $search: 'QQCODE' }); |
| 154 | + expect(byCode.status, JSON.stringify(byCode.body)).toBe(200); |
| 155 | + expect(names(byCode.body)).toEqual([]); |
| 156 | + |
| 157 | + const byName = await list(DECLARED, { $search: 'fs2' }); |
| 158 | + expect(byName.status, JSON.stringify(byName.body)).toBe(200); |
| 159 | + expect(names(byName.body)).toEqual(['fs2 row']); |
| 160 | + |
| 161 | + const before = reads.n; |
| 162 | + const refused = await list(DECLARED, { $search: 'QQCODE', $searchFields: 'snippet' }); |
| 163 | + expect(refused.status, JSON.stringify(refused.body)).toBe(400); |
| 164 | + expect(refused.body.code).toBe('INVALID_FIELD'); |
| 165 | + expect(refused.body.error).toContain("The object declares 'searchableFields' (name)"); |
| 166 | + expect(reads.n - before, 'the refusal precedes the driver').toBe(0); |
| 167 | + }); |
| 168 | +}); |
0 commit comments