-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathuriTemplate.test.ts
More file actions
401 lines (347 loc) · 17.2 KB
/
uriTemplate.test.ts
File metadata and controls
401 lines (347 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { UriTemplate } from '../../src/shared/uriTemplate.js';
describe('UriTemplate', () => {
describe('isTemplate', () => {
it('should return true for strings containing template expressions', () => {
expect(UriTemplate.isTemplate('{foo}')).toBe(true);
expect(UriTemplate.isTemplate('/users/{id}')).toBe(true);
expect(UriTemplate.isTemplate('http://example.com/{path}/{file}')).toBe(true);
expect(UriTemplate.isTemplate('/search{?q,limit}')).toBe(true);
});
it('should return false for strings without template expressions', () => {
expect(UriTemplate.isTemplate('')).toBe(false);
expect(UriTemplate.isTemplate('plain string')).toBe(false);
expect(UriTemplate.isTemplate('http://example.com/foo/bar')).toBe(false);
expect(UriTemplate.isTemplate('{}')).toBe(false); // Empty braces don't count
expect(UriTemplate.isTemplate('{ }')).toBe(false); // Just whitespace doesn't count
});
});
describe('simple string expansion', () => {
it('should expand simple string variables', () => {
const template = new UriTemplate('http://example.com/users/{username}');
expect(template.expand({ username: 'fred' })).toBe('http://example.com/users/fred');
expect(template.variableNames).toEqual(['username']);
});
it('should handle multiple variables', () => {
const template = new UriTemplate('{x,y}');
expect(template.expand({ x: '1024', y: '768' })).toBe('1024,768');
expect(template.variableNames).toEqual(['x', 'y']);
});
it('should encode reserved characters', () => {
const template = new UriTemplate('{var}');
expect(template.expand({ var: 'value with spaces' })).toBe('value%20with%20spaces');
});
});
describe('reserved expansion', () => {
it('should not encode reserved characters with + operator', () => {
const template = new UriTemplate('{+path}/here');
expect(template.expand({ path: '/foo/bar' })).toBe('/foo/bar/here');
expect(template.variableNames).toEqual(['path']);
});
});
describe('fragment expansion', () => {
it('should add # prefix and not encode reserved chars', () => {
const template = new UriTemplate('X{#var}');
expect(template.expand({ var: '/test' })).toBe('X#/test');
expect(template.variableNames).toEqual(['var']);
});
});
describe('label expansion', () => {
it('should add . prefix', () => {
const template = new UriTemplate('X{.var}');
expect(template.expand({ var: 'test' })).toBe('X.test');
expect(template.variableNames).toEqual(['var']);
});
});
describe('path expansion', () => {
it('should add / prefix', () => {
const template = new UriTemplate('X{/var}');
expect(template.expand({ var: 'test' })).toBe('X/test');
expect(template.variableNames).toEqual(['var']);
});
});
describe('query expansion', () => {
it('should add ? prefix and name=value format', () => {
const template = new UriTemplate('X{?var}');
expect(template.expand({ var: 'test' })).toBe('X?var=test');
expect(template.variableNames).toEqual(['var']);
});
});
describe('form continuation expansion', () => {
it('should add & prefix and name=value format', () => {
const template = new UriTemplate('X{&var}');
expect(template.expand({ var: 'test' })).toBe('X&var=test');
expect(template.variableNames).toEqual(['var']);
});
});
describe('matching', () => {
it('should match simple strings and extract variables', () => {
const template = new UriTemplate('http://example.com/users/{username}');
const match = template.match('http://example.com/users/fred');
expect(match).toEqual({ username: 'fred' });
});
it('should match multiple variables', () => {
const template = new UriTemplate('/users/{username}/posts/{postId}');
const match = template.match('/users/fred/posts/123');
expect(match).toEqual({ username: 'fred', postId: '123' });
});
it('should return null for non-matching URIs', () => {
const template = new UriTemplate('/users/{username}');
const match = template.match('/posts/123');
expect(match).toBeNull();
});
it('should handle exploded arrays', () => {
const template = new UriTemplate('{/list*}');
const match = template.match('/red,green,blue');
expect(match).toEqual({ list: ['red', 'green', 'blue'] });
});
});
describe('edge cases', () => {
it('should handle empty variables', () => {
const template = new UriTemplate('{empty}');
expect(template.expand({})).toBe('');
expect(template.expand({ empty: '' })).toBe('');
});
it('should handle undefined variables', () => {
const template = new UriTemplate('{a}{b}{c}');
expect(template.expand({ b: '2' })).toBe('2');
});
it('should handle special characters in variable names', () => {
const template = new UriTemplate('{$var_name}');
expect(template.expand({ $var_name: 'value' })).toBe('value');
});
});
describe('complex patterns', () => {
it('should handle nested path segments', () => {
const template = new UriTemplate('/api/{version}/{resource}/{id}');
expect(
template.expand({
version: 'v1',
resource: 'users',
id: '123'
})
).toBe('/api/v1/users/123');
expect(template.variableNames).toEqual(['version', 'resource', 'id']);
});
it('should handle query parameters with arrays', () => {
const template = new UriTemplate('/search{?tags*}');
expect(
template.expand({
tags: ['nodejs', 'typescript', 'testing']
})
).toBe('/search?tags=nodejs,typescript,testing');
expect(template.variableNames).toEqual(['tags']);
});
it('should handle multiple query parameters', () => {
const template = new UriTemplate('/search{?q,page,limit}');
expect(
template.expand({
q: 'test',
page: '1',
limit: '10'
})
).toBe('/search?q=test&page=1&limit=10');
expect(template.variableNames).toEqual(['q', 'page', 'limit']);
});
});
describe('matching complex patterns', () => {
it('should match nested path segments', () => {
const template = new UriTemplate('/api/{version}/{resource}/{id}');
const match = template.match('/api/v1/users/123');
expect(match).toEqual({
version: 'v1',
resource: 'users',
id: '123'
});
expect(template.variableNames).toEqual(['version', 'resource', 'id']);
});
it('should match query parameters', () => {
const template = new UriTemplate('/search{?q}');
const match = template.match('/search?q=test');
expect(match).toEqual({ q: 'test' });
expect(template.variableNames).toEqual(['q']);
});
it('should match multiple query parameters', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search?q=test&page=1');
expect(match).toEqual({ q: 'test', page: '1' });
expect(template.variableNames).toEqual(['q', 'page']);
});
it('should handle partial query parameter matches correctly', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search?q=test');
expect(match).toEqual({ q: 'test' });
expect(template.variableNames).toEqual(['q', 'page']);
});
it('should match multiple query parameters if provided in a different order', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search?page=1&q=test');
expect(match).toEqual({ q: 'test', page: '1' });
expect(template.variableNames).toEqual(['q', 'page']);
});
it('should still match if additional query parameters are provided', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search?q=test&page=1&sort=desc');
expect(match).toEqual({ q: 'test', page: '1' });
expect(template.variableNames).toEqual(['q', 'page']);
});
it('should match omitted query parameters', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search');
expect(match).toEqual({});
expect(template.variableNames).toEqual(['q', 'page']);
});
it('should distinguish absent from empty query parameters', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search?q=');
expect(match).toEqual({ q: '' });
});
it('should match nested path segments with query parameters', () => {
const template = new UriTemplate('/api/{version}/{resource}{?apiKey,q,p,sort}');
const match = template.match('/api/v1/users?apiKey=testkey&q=user');
expect(match).toEqual({
version: 'v1',
resource: 'users',
apiKey: 'testkey',
q: 'user'
});
expect(template.variableNames).toEqual(['version', 'resource', 'apiKey', 'q', 'p', 'sort']);
});
it('should handle partial matches correctly', () => {
const template = new UriTemplate('/users/{id}');
expect(template.match('/users/123/extra')).toBeNull();
expect(template.match('/users')).toBeNull();
});
it('should handle encoded query parameters', () => {
const template = new UriTemplate('/search{?q}');
const match = template.match('/search?q=hello%20world');
expect(match).toEqual({ q: 'hello world' });
expect(template.variableNames).toEqual(['q']);
});
it('should not throw on malformed percent-encoding in query parameters', () => {
const template = new UriTemplate('/search{?q}');
expect(template.match('/search?q=100%')).toEqual({ q: '100%' });
expect(template.match('/search?q=%ZZ')).toEqual({ q: '%ZZ' });
});
it('should reject templates with literal ? in a string segment', () => {
expect(() => new UriTemplate('/path?fixed=1')).toThrow(/literal '\?'/);
expect(() => new UriTemplate('/path?static=1{?dynamic}')).toThrow(/literal '\?'/);
expect(() => new UriTemplate('/path?static=1{&dynamic}')).toThrow(/literal '\?'/);
expect(() => new UriTemplate('/api?v=2{&key,page}')).toThrow(/literal '\?'/);
expect(() => new UriTemplate('/api/{version}?format=json{&key}')).toThrow(/literal '\?'/);
});
it('should accept templates using {?param} for query parameters', () => {
// The supported way to express query parameters
const template = new UriTemplate('/path{?fixed}');
expect(template.match('/path?fixed=1')).toEqual({ fixed: '1' });
expect(template.match('/path')).toEqual({});
});
it('should strip fragments before matching query parameters', () => {
const template = new UriTemplate('/path{?a}');
expect(template.match('/path?a=1#frag')).toEqual({ a: '1' });
expect(template.match('/path?a=1&b=2#frag')).toEqual({ a: '1' });
});
it('should strip fragments when the URI has no query string', () => {
const template = new UriTemplate('/path{?a}');
expect(template.match('/path#frag')).toEqual({});
expect(template.match('/path')).toEqual({});
});
});
describe('security and edge cases', () => {
it('should handle extremely long input strings', () => {
const longString = 'x'.repeat(100000);
const template = new UriTemplate(`/api/{param}`);
expect(template.expand({ param: longString })).toBe(`/api/${longString}`);
expect(template.match(`/api/${longString}`)).toEqual({ param: longString });
});
it('should handle deeply nested template expressions', () => {
const template = new UriTemplate('{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}'.repeat(1000));
expect(() =>
template.expand({
a: '1',
b: '2',
c: '3',
d: '4',
e: '5',
f: '6',
g: '7',
h: '8',
i: '9',
j: '0'
})
).not.toThrow();
});
it('should handle malformed template expressions', () => {
expect(() => new UriTemplate('{unclosed')).toThrow();
expect(() => new UriTemplate('{}')).not.toThrow();
expect(() => new UriTemplate('{,}')).not.toThrow();
expect(() => new UriTemplate('{a}{')).toThrow();
});
it('should handle pathological regex patterns', () => {
const template = new UriTemplate('/api/{param}');
// Create a string that could cause catastrophic backtracking
const input = '/api/' + 'a'.repeat(100000);
expect(() => template.match(input)).not.toThrow();
});
it('should handle invalid UTF-8 sequences', () => {
const template = new UriTemplate('/api/{param}');
const invalidUtf8 = '���';
expect(() => template.expand({ param: invalidUtf8 })).not.toThrow();
expect(() => template.match(`/api/${invalidUtf8}`)).not.toThrow();
});
it('should handle template/URI length mismatches', () => {
const template = new UriTemplate('/api/{param}');
expect(template.match('/api/')).toBeNull();
expect(template.match('/api')).toBeNull();
expect(template.match('/api/value/extra')).toBeNull();
});
it('should handle repeated operators', () => {
const template = new UriTemplate('{?a}{?b}{?c}');
expect(template.expand({ a: '1', b: '2', c: '3' })).toBe('?a=1&b=2&c=3');
expect(template.variableNames).toEqual(['a', 'b', 'c']);
});
it('should handle overlapping variable names', () => {
const template = new UriTemplate('{var}{vara}');
expect(template.expand({ var: '1', vara: '2' })).toBe('12');
expect(template.variableNames).toEqual(['var', 'vara']);
});
it('should handle empty segments', () => {
const template = new UriTemplate('///{a}////{b}////');
expect(template.expand({ a: '1', b: '2' })).toBe('///1////2////');
expect(template.match('///1////2////')).toEqual({ a: '1', b: '2' });
expect(template.variableNames).toEqual(['a', 'b']);
});
it('should handle maximum template expression limit', () => {
// Create a template with many expressions
const expressions = Array(10000).fill('{param}').join('');
expect(() => new UriTemplate(expressions)).not.toThrow();
});
it('should handle maximum variable name length', () => {
const longName = 'a'.repeat(10000);
const template = new UriTemplate(`{${longName}}`);
const vars: Record<string, string> = {};
vars[longName] = 'value';
expect(() => template.expand(vars)).not.toThrow();
});
it('should not be vulnerable to ReDoS with exploded path patterns', () => {
// Test for ReDoS vulnerability (CVE-2026-0621)
// See: https://github.com/modelcontextprotocol/typescript-sdk/issues/965
const template = new UriTemplate('{/id*}');
const maliciousPayload = '/' + ','.repeat(50);
const startTime = Date.now();
template.match(maliciousPayload);
const elapsed = Date.now() - startTime;
// Should complete in under 100ms, not hang for seconds
expect(elapsed).toBeLessThan(100);
});
it('should not be vulnerable to ReDoS with exploded simple patterns', () => {
// Test for ReDoS vulnerability with simple exploded operator
const template = new UriTemplate('{id*}');
const maliciousPayload = ','.repeat(50);
const startTime = Date.now();
template.match(maliciousPayload);
const elapsed = Date.now() - startTime;
// Should complete in under 100ms, not hang for seconds
expect(elapsed).toBeLessThan(100);
});
});
});