Skip to content

Commit b748eaa

Browse files
committed
test(rest): red-first pins for a bracketed packageId or type key on /meta/_drafts
The decision as data, RestServer's _drafts door, the runtime dispatcher's twin and the booted showcase over HTTP. Red on main: the bracketed key reads as no filter and every draft is listed. Claude-Session: https://claude.ai/code/session_01ADHkmSq83zsshBxiLpFJdE Co-authored-by: Claude <noreply@anthropic.com>
1 parent a2fcede commit b748eaa

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎packages/qa/dogfood/test/meta-read-doors-live.dogfood.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,41 @@ describe('[#26572] the v18 /meta read doors, over HTTP on a booted showcase', ()
264264
expect(objects.length).toBeLessThan(everyPackage.length);
265265
});
266266
});
267+
268+
// The same class on `_drafts`' own filters: `?packageId[]=` / `?type[]=`
269+
// reached the door under the key as spelled, so the list was not narrowed
270+
// and an author who asked for one package's (or one type's) drafts got
271+
// every draft. Each is refused: 400 VALIDATION_ERROR naming the key, no list.
272+
describe('GET /meta/_drafts with a bracketed `packageId` or `type` key is refused', () => {
273+
const draftsOf = (body: any): any[] => body?.drafts ?? body?.data?.drafts ?? [];
274+
275+
for (const [key, value, names] of [
276+
['packageId[]', PKG, 'package'], ['packageId[0]', PKG, 'package'],
277+
['type[]', 'view', 'metadata type'], ['type[0]', 'view', 'metadata type'],
278+
] as const) {
279+
it(`/meta/_drafts?${key}=…: 400 VALIDATION_ERROR naming the key, for the admin`, async () => {
280+
const res = await stack.apiAs(admin, 'GET', `/meta/_drafts?${key}=${encodeURIComponent(value)}`);
281+
const body: any = await res.json();
282+
expect({ status: res.status, code: codeOf(body) }, JSON.stringify(body).slice(0, 400))
283+
.toEqual({ status: 400, code: 'VALIDATION_ERROR' });
284+
expect(messageOf(body)).toMatch(new RegExp(`^Query parameter '${key.replace(/[[\]]/g, '\\$&')}' names no ${names};`));
285+
expect(draftsOf(body)).toEqual([]);
286+
});
287+
}
288+
289+
it('CONTROLS — the plain spellings narrow, and a member is still refused at the capability gate first', async () => {
290+
const scoped = await stack.apiAs(admin, 'GET', `/meta/_drafts?packageId=${encodeURIComponent(PKG)}`);
291+
expect(scoped.status).toBe(200);
292+
const inPackage = draftsOf(await scoped.json());
293+
expect(inPackage.every((d: any) => d?.packageId === PKG), JSON.stringify(inPackage).slice(0, 400)).toBe(true);
294+
295+
const typed = await stack.apiAs(admin, 'GET', '/meta/_drafts?type=view');
296+
expect(typed.status).toBe(200);
297+
const views = draftsOf(await typed.json());
298+
expect(views.every((d: any) => d?.type === 'view'), JSON.stringify(views).slice(0, 400)).toBe(true);
299+
300+
const member = await stack.apiAs(liaison, 'GET', `/meta/_drafts?packageId[]=${encodeURIComponent(PKG)}`);
301+
expect(member.status).toBe(403);
302+
});
303+
});
267304
});

‎packages/rest/src/meta-query-arity-doors.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,64 @@ describe('a BRACKETED `package` key on the two arity doors that read `package`',
198198
}
199199
});
200200
});
201+
202+
/**
203+
* The same class on `_drafts`' own two filters. `?packageId[]=ID`,
204+
* `?packageId[0]=ID`, `?type[]=TYPE` and `?type[0]=TYPE` reached the door under
205+
* the key as spelled, so `listDrafts` was handed no package and no type, and a
206+
* caller who named one package's (or one type's) drafts got every draft it may
207+
* read. Refused at the door's arity step, before `listDrafts`, in the same
208+
* sentence frame as the list's bracketed `package`.
209+
*/
210+
const bracketedDraftsMessage = (key: string, param: 'packageId' | 'type'): string => (param === 'packageId'
211+
? `Query parameter '${key}' names no package; the 'packageId' filter takes exactly one package id, `
212+
+ `spelled ?packageId=ID. The list was not served: a bracketed 'packageId' key is not a filter. `
213+
+ `Name one package, or leave the parameter out to list the drafts of every package.`
214+
: `Query parameter '${key}' names no metadata type; the 'type' filter takes exactly one metadata type, `
215+
+ `spelled ?type=TYPE. The list was not served: a bracketed 'type' key is not a filter. `
216+
+ `Name one metadata type, or leave the parameter out to list the drafts of every type.`);
217+
const DRAFTS_KEYS = [
218+
['packageId[]', 'packageId'], ['packageId[0]', 'packageId'], ['packageId[id]', 'packageId'],
219+
['type[]', 'type'], ['type[0]', 'type'], ['type[name]', 'type'],
220+
] as const;
221+
222+
describe('a BRACKETED `packageId` or `type` key on `_drafts`', () => {
223+
it.each(DRAFTS_KEYS)('`%s` is refused as data, naming the key', (key, param) => {
224+
expect(metaQueryArityRefusal('drafts', { [key]: 'x' })).toEqual({
225+
status: 400,
226+
code: 'VALIDATION_ERROR',
227+
message: bracketedDraftsMessage(key, param),
228+
rule: 'bracketed-parameter',
229+
});
230+
// Beside the plain key it is two spellings of one filter.
231+
expect(metaQueryArityRefusal('drafts', { [param]: 'x', [key]: 'y' })?.rule).toBe('bracketed-parameter');
232+
});
233+
234+
it('a repeated plain `packageId` still answers the multiplicity sentence first', () => {
235+
expect(metaQueryArityRefusal('drafts', { packageId: ['a', 'b'], 'type[]': 'view' })?.message)
236+
.toBe(repeatedQueryParamMessage('packageId', 2));
237+
});
238+
239+
it('is judged only on `_drafts`, and not for a key that merely starts alike', () => {
240+
expect(metaQueryArityRefusal('list', { 'packageId[]': 'x', 'type[]': 'view' })).toBeUndefined();
241+
expect(metaQueryArityRefusal('book-tree', { 'type[]': 'view' })).toBeUndefined();
242+
expect(metaQueryArityRefusal('drafts', { 'types[]': 'view', 'packageIds[]': 'x', 'package[]': 'x' })).toBeUndefined();
243+
});
244+
245+
it.each(DRAFTS_KEYS.filter(([key]) => key.endsWith('[]') || key.endsWith('[0]')))(
246+
'RestServer: `%s` answers the nested 400 before `listDrafts`',
247+
async (key, param) => {
248+
const { protocol, status, body } = await drive('drafts', { [key]: 'x' });
249+
expect(status).toBe(400);
250+
expect(body).toEqual({ error: { code: 'VALIDATION_ERROR', message: bracketedDraftsMessage(key, param) } });
251+
expect(protocol.listDrafts).not.toHaveBeenCalled();
252+
},
253+
);
254+
255+
it('RestServer: the plain spellings still narrow, and absent ones read every draft (controls)', async () => {
256+
expect((await drive('drafts', { packageId: 'com.acme', type: 'view' })).protocol.listDrafts)
257+
.toHaveBeenCalledWith({ packageId: 'com.acme', type: 'view' });
258+
expect((await drive('drafts', {})).protocol.listDrafts)
259+
.toHaveBeenCalledWith({ packageId: undefined, type: undefined });
260+
});
261+
});

‎packages/runtime/src/domains/dispatcher-query-arity.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,41 @@ describe('[#23214] /meta — the four arity doors, over the list both transports
243243
expect(answer.body?.error?.message).toMatch(/^Query parameter 'object\[\]' names no object/);
244244
});
245245
});
246+
247+
// The same class on `_drafts`' own filters: `?packageId[]=` / `?type[]=`
248+
// reached `listDrafts` as no package and no type, so the answer was every
249+
// draft. Refused at the door's arity step, with `RestServer`'s code and
250+
// sentence, before `listDrafts`.
251+
describe('a BRACKETED `packageId` or `type` key on `_drafts`', () => {
252+
const sentence = (key: string, param: string): string => (param === 'packageId'
253+
? `Query parameter '${key}' names no package; the 'packageId' filter takes exactly one package id, `
254+
+ `spelled ?packageId=ID. The list was not served: a bracketed 'packageId' key is not a filter. `
255+
+ `Name one package, or leave the parameter out to list the drafts of every package.`
256+
: `Query parameter '${key}' names no metadata type; the 'type' filter takes exactly one metadata type, `
257+
+ `spelled ?type=TYPE. The list was not served: a bracketed 'type' key is not a filter. `
258+
+ `Name one metadata type, or leave the parameter out to list the drafts of every type.`);
259+
260+
it.each([
261+
['packageId[]', 'packageId'], ['packageId[0]', 'packageId'],
262+
['type[]', 'type'], ['type[0]', 'type'],
263+
])('GET /meta/_drafts?%s=… is refused before `listDrafts`', async (key, param) => {
264+
const { dispatcher, protocol } = boot();
265+
const answer = await get(dispatcher, '/meta/_drafts', { [key]: 'x' });
266+
expect(answer.status, JSON.stringify(answer)).toBe(400);
267+
expect(answer.body?.error?.code).toBe('VALIDATION_ERROR');
268+
expect(answer.body?.error?.message).toBe(sentence(key, param));
269+
expect(protocol.listDrafts).not.toHaveBeenCalled();
270+
});
271+
272+
it('beside the plain key it is refused too, and the plain spellings still narrow (control)', async () => {
273+
const { dispatcher, protocol } = boot();
274+
expect((await get(dispatcher, '/meta/_drafts', { packageId: 'com.acme', 'packageId[]': 'com.other' })).status)
275+
.toBe(400);
276+
expect(protocol.listDrafts).not.toHaveBeenCalled();
277+
expect((await get(dispatcher, '/meta/_drafts', { packageId: 'com.acme', type: 'view' })).status).toBe(200);
278+
expect(protocol.listDrafts).toHaveBeenCalledWith({ packageId: 'com.acme', type: 'view' });
279+
});
280+
});
246281
});
247282

248283
describe('[#23214] GET /ui/view/:object — `?type=` is one view type, read only when the path names none', () => {

0 commit comments

Comments
 (0)