|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Guards the one Attio site that interpolates a parameter into the request |
| 5 | + * *query* zone: `attio_assert_record`'s `matching_attribute`. |
| 6 | + * |
| 7 | + * `matchingAttribute` is `required: true, visibility: 'user-or-llm'`, so |
| 8 | + * prompt injection controls it. Interpolated raw, a value carrying `&` appends |
| 9 | + * arbitrary extra query parameters to the request, and a value carrying `#` |
| 10 | + * truncates the URL at a fragment — both while the caller's Attio OAuth token |
| 11 | + * is still attached. |
| 12 | + * |
| 13 | + * Every assertion resolves the built URL through `new URL(...)` — the same |
| 14 | + * parse `fetch` performs — and inspects `searchParams`, never the raw string. |
| 15 | + * A `.includes()` assertion on the template is exactly the weak form that lets |
| 16 | + * a broken rewrite pass. |
| 17 | + */ |
| 18 | +import { describe, expect, it } from 'vitest' |
| 19 | +import { attioAssertRecordTool } from '@/tools/attio/assert_record' |
| 20 | + |
| 21 | +const ORIGIN = 'https://api.attio.com' |
| 22 | +const PATHNAME = '/v2/objects/people/records' |
| 23 | + |
| 24 | +function buildUrl(matchingAttribute: unknown): URL { |
| 25 | + return new URL( |
| 26 | + (attioAssertRecordTool.request!.url as (p: any) => string)({ |
| 27 | + accessToken: 'token', |
| 28 | + objectType: 'people', |
| 29 | + matchingAttribute, |
| 30 | + values: '{}', |
| 31 | + }) |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +/** Values a real caller supplies; each must survive verbatim. */ |
| 36 | +const LEGITIMATE = [ |
| 37 | + 'email_addresses', |
| 38 | + 'domains', |
| 39 | + 'custom.attr-1', |
| 40 | + 'attr+plus', |
| 41 | + 'attr with space', |
| 42 | + '97052eb9-e65e-443f-a297-f2d9a4a7f795', |
| 43 | +] as const |
| 44 | + |
| 45 | +/** Vectors that reshape the request when interpolated raw. */ |
| 46 | +const INJECTIONS = [ |
| 47 | + 'email_addresses&limit=1&x=y', |
| 48 | + 'a#b', |
| 49 | + 'email_addresses#', |
| 50 | + 'x&matching_attribute=y', |
| 51 | + 'a=b&c=d', |
| 52 | +] as const |
| 53 | + |
| 54 | +describe('attio_assert_record matching_attribute query safety', () => { |
| 55 | + it('builds the expected origin and path', () => { |
| 56 | + const url = buildUrl('email_addresses') |
| 57 | + expect(url.origin).toBe(ORIGIN) |
| 58 | + expect(url.pathname).toBe(PATHNAME) |
| 59 | + }) |
| 60 | + |
| 61 | + it.each(LEGITIMATE)('round-trips %j verbatim', (value) => { |
| 62 | + const url = buildUrl(value) |
| 63 | + expect(url.searchParams.get('matching_attribute')).toBe(value) |
| 64 | + expect([...url.searchParams.keys()]).toEqual(['matching_attribute']) |
| 65 | + }) |
| 66 | + |
| 67 | + it.each(INJECTIONS)('confines %j to the matching_attribute value', (value) => { |
| 68 | + const url = buildUrl(value) |
| 69 | + |
| 70 | + expect(url.origin).toBe(ORIGIN) |
| 71 | + expect(url.pathname).toBe(PATHNAME) |
| 72 | + expect(url.hash).toBe('') |
| 73 | + expect([...url.searchParams.keys()]).toEqual(['matching_attribute']) |
| 74 | + expect(url.searchParams.get('matching_attribute')).toBe(value) |
| 75 | + }) |
| 76 | + |
| 77 | + it('does not change the wire bytes for a legitimate slug', () => { |
| 78 | + const raw = (attioAssertRecordTool.request!.url as (p: any) => string)({ |
| 79 | + accessToken: 'token', |
| 80 | + objectType: 'people', |
| 81 | + matchingAttribute: 'email_addresses', |
| 82 | + values: '{}', |
| 83 | + }) |
| 84 | + expect(raw).toBe(`${ORIGIN}${PATHNAME}?matching_attribute=email_addresses`) |
| 85 | + }) |
| 86 | + |
| 87 | + it('still trims surrounding whitespace', () => { |
| 88 | + expect(buildUrl(' email_addresses ').searchParams.get('matching_attribute')).toBe( |
| 89 | + 'email_addresses' |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it('stringifies a numeric value instead of throwing a TypeError', () => { |
| 94 | + expect(buildUrl(123).searchParams.get('matching_attribute')).toBe('123') |
| 95 | + }) |
| 96 | + |
| 97 | + it.each([null, undefined, '', ' '])('rejects %j by name', (value) => { |
| 98 | + expect(() => buildUrl(value)).toThrow(/matchingAttribute/) |
| 99 | + }) |
| 100 | +}) |
0 commit comments