|
| 1 | +/** |
| 2 | + * Shared harness for the per-service `path_safety.test.ts` suites. |
| 3 | + * |
| 4 | + * Each suite enumerates its service's tools from the barrel rather than listing |
| 5 | + * them by hand, so a **newly added** tool that interpolates an unguarded id into |
| 6 | + * its request path fails CI the day it lands. That enumeration, the traversal |
| 7 | + * vectors, and the URL-resolution assertions are identical across services, so |
| 8 | + * they live here once. |
| 9 | + * |
| 10 | + * Every assertion resolves the built URL with `new URL(...)` — the same |
| 11 | + * normalization `fetch` performs — instead of string-matching the template |
| 12 | + * output. String matching is exactly what let dot-segment traversal through: |
| 13 | + * the template looks correct and the parser rewrites it afterwards. |
| 14 | + */ |
| 15 | +import { expect, it } from 'vitest' |
| 16 | +import type { ToolConfig } from '@/tools/types' |
| 17 | + |
| 18 | +/** |
| 19 | + * The bare `.` and `..` entries are the whole point: they are made only of |
| 20 | + * unreserved characters, so they survive `encodeURIComponent` untouched and are |
| 21 | + * then removed by the URL parser, popping a segment off a fixed host with the |
| 22 | + * caller's bearer token still attached — including on DELETE. |
| 23 | + */ |
| 24 | +export const TRAVERSAL_IDS = [ |
| 25 | + '..', |
| 26 | + '.', |
| 27 | + ' .. ', |
| 28 | + '../../about', |
| 29 | + '..%2f..%2fabout', |
| 30 | + 'abc/../../../drives', |
| 31 | + 'abc?alt=media', |
| 32 | + 'abc#fragment', |
| 33 | + 'abc/items/../../../v2/other', |
| 34 | + '\\..\\..', |
| 35 | + '../', |
| 36 | + './', |
| 37 | +] as const |
| 38 | + |
| 39 | +export interface NamedTool { |
| 40 | + name: string |
| 41 | + tool: ToolConfig<any, any> |
| 42 | +} |
| 43 | + |
| 44 | +const SENTINEL = 'SAFEIDSENTINEL' |
| 45 | + |
| 46 | +function urlBuilder(tool: ToolConfig<any, any>): (params: Record<string, unknown>) => string { |
| 47 | + const url = tool.request?.url |
| 48 | + if (typeof url !== 'function') { |
| 49 | + throw new Error(`${tool.id} does not build its URL from params`) |
| 50 | + } |
| 51 | + return url as (params: Record<string, unknown>) => string |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Fills every declared string param with `value` so whichever one reaches the |
| 56 | + * path is exercised, while giving non-string params a shape their tool accepts. |
| 57 | + */ |
| 58 | +function buildParams( |
| 59 | + tool: ToolConfig<any, any>, |
| 60 | + value: string, |
| 61 | + fixed: Record<string, unknown> |
| 62 | +): Record<string, unknown> { |
| 63 | + const params: Record<string, unknown> = {} |
| 64 | + for (const [name, def] of Object.entries(tool.params ?? {})) { |
| 65 | + const type = (def as { type?: string }).type |
| 66 | + if (type === 'json' || type === 'array') { |
| 67 | + params[name] = [] |
| 68 | + } else if (type === 'number') { |
| 69 | + params[name] = 1 |
| 70 | + } else if (type === 'boolean') { |
| 71 | + params[name] = false |
| 72 | + } else { |
| 73 | + params[name] = value |
| 74 | + } |
| 75 | + } |
| 76 | + return { ...params, ...fixed } |
| 77 | +} |
| 78 | + |
| 79 | +function buildUrl(tool: ToolConfig<any, any>, value: string, fixed: Record<string, unknown>): URL { |
| 80 | + return new URL(urlBuilder(tool)(buildParams(tool, value, fixed))) |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Selects the tools of a service whose request path actually embeds a |
| 85 | + * caller-supplied value. A tool whose URL is static, or whose value only ever |
| 86 | + * lands in the query string, is not in this risk class and is skipped. |
| 87 | + */ |
| 88 | +export function dynamicPathTools( |
| 89 | + barrel: Record<string, unknown>, |
| 90 | + idPrefix: string, |
| 91 | + fixed: Record<string, unknown> = {} |
| 92 | +): NamedTool[] { |
| 93 | + return Object.values(barrel) |
| 94 | + .filter((value): value is ToolConfig<any, any> => { |
| 95 | + const tool = value as ToolConfig<any, any> |
| 96 | + return ( |
| 97 | + typeof tool === 'object' && |
| 98 | + tool !== null && |
| 99 | + typeof tool.id === 'string' && |
| 100 | + tool.id.startsWith(idPrefix) && |
| 101 | + typeof tool.request?.url === 'function' |
| 102 | + ) |
| 103 | + }) |
| 104 | + .filter((tool) => { |
| 105 | + try { |
| 106 | + return buildUrl(tool, SENTINEL, fixed).pathname.includes(SENTINEL) |
| 107 | + } catch { |
| 108 | + return false |
| 109 | + } |
| 110 | + }) |
| 111 | + .map((tool) => ({ name: tool.id, tool })) |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Asserts the traversal invariant for one tool. |
| 116 | + * |
| 117 | + * The invariant is stated as a **prefix** rather than an exact segment count so |
| 118 | + * it holds for genuinely hierarchical parameters too (a Supabase object key, a |
| 119 | + * People API `resourceName`): those legitimately add segments, but they may |
| 120 | + * never remove the fixed API prefix that precedes them, and no resolved segment |
| 121 | + * may be a dot segment. |
| 122 | + */ |
| 123 | +export function itResistsTraversal( |
| 124 | + { tool }: NamedTool, |
| 125 | + { origin, fixed = {} }: { origin: string; fixed?: Record<string, unknown> } |
| 126 | +): void { |
| 127 | + const baseline = buildUrl(tool, SENTINEL, fixed) |
| 128 | + const prefix = baseline.pathname |
| 129 | + .split('/') |
| 130 | + .slice(0, baseline.pathname.split('/').indexOf(SENTINEL)) |
| 131 | + |
| 132 | + it.each(TRAVERSAL_IDS)('cannot reshape the path with %j', (value) => { |
| 133 | + let url: URL |
| 134 | + try { |
| 135 | + url = buildUrl(tool, value, fixed) |
| 136 | + } catch { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + expect(url.origin).toBe(origin) |
| 141 | + const segments = url.pathname.split('/') |
| 142 | + expect(segments.slice(0, prefix.length)).toEqual(prefix) |
| 143 | + expect(segments).not.toContain('..') |
| 144 | + expect(segments).not.toContain('.') |
| 145 | + }) |
| 146 | + |
| 147 | + /** |
| 148 | + * The throw is asserted unqualified on purpose. Some parameters are already |
| 149 | + * refused earlier by a stricter service-specific validator (a Supabase SQL |
| 150 | + * identifier, a Supabase Edge Function name), and that is an equally correct |
| 151 | + * outcome — what matters is that a bare dot segment can never reach the wire. |
| 152 | + */ |
| 153 | + it('rejects a bare dot-dot segment instead of silently popping the prefix', () => { |
| 154 | + expect(() => buildUrl(tool, '..', fixed)).toThrow() |
| 155 | + }) |
| 156 | + |
| 157 | + it('rejects a bare dot segment', () => { |
| 158 | + expect(() => buildUrl(tool, '.', fixed)).toThrow() |
| 159 | + }) |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Asserts that real-world values reach the wire byte-for-byte, so a guard can |
| 164 | + * never be tightened into breaking legitimate callers. |
| 165 | + */ |
| 166 | +export function itPassesLegitimateValues( |
| 167 | + { tool }: NamedTool, |
| 168 | + { values, fixed = {} }: { values: readonly string[]; fixed?: Record<string, unknown> } |
| 169 | +): void { |
| 170 | + const baseline = buildUrl(tool, SENTINEL, fixed).pathname |
| 171 | + |
| 172 | + it.each(values)('passes %j through unchanged', (value) => { |
| 173 | + const actual = buildUrl(tool, value, fixed).pathname |
| 174 | + |
| 175 | + expect(actual).toBe(baseline.split(SENTINEL).join(value)) |
| 176 | + }) |
| 177 | +} |
0 commit comments