-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcontent-disposition.test.ts
More file actions
633 lines (553 loc) · 21.5 KB
/
content-disposition.test.ts
File metadata and controls
633 lines (553 loc) · 21.5 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import { describe, expect, it, vi } from 'vitest'
import { ContentDisposition, contentDisposition, parse } from '../../packages/content-disposition/src'
describe('contentDisposition()', () => {
it('should create an attachment header', () => {
expect(contentDisposition()).toBe('attachment')
})
})
describe('contentDisposition(filename)', () => {
it('should create a header with file name', () => {
expect(contentDisposition('plans.pdf')).toBe('attachment; filename="plans.pdf"')
})
it('should use the basename of the string', () => {
expect(contentDisposition('/path/to/plans.pdf')).toBe('attachment; filename="plans.pdf"')
})
it('should throw an error when non latin fallback is used', () => {
expect.assertions(1)
try {
contentDisposition('index.ht', { type: 'html', fallback: 'ÇŞ' })
} catch (e) {
expect((e as Error).message).toBe('fallback must be ISO-8859-1 string')
}
})
it('should use hasfallback', () => {
expect(contentDisposition('index.ht', { type: 'html', fallback: 'html' })).toEqual(
`html; filename="html"; filename*=UTF-8''index.ht`
)
})
it('should use pencode fn', () => {
expect(contentDisposition('inde(x.ht', { type: 'html', fallback: 'html' })).toEqual(
`html; filename="html"; filename*=UTF-8''inde%28x.ht`
)
})
it('should use fallback when file ext is non ascii', () => {
expect(contentDisposition('index.ĄÇḐȨĢ', { type: 'html', fallback: 'html' })).toEqual(
`html; filename="html"; filename*=UTF-8''index.%C4%84%C3%87%E1%B8%90%C8%A8%C4%A2`
)
})
it('should throw an error when non string options.type is used', () => {
expect.assertions(1)
try {
contentDisposition('index.ht', { type: { test: 'test' } as unknown as string })
} catch (e) {
expect((e as Error).message).toBe('invalid type')
}
})
it('should work when file name is empty', () => {
const encoded = contentDisposition('', { type: 'inline' })
const parsed = parse(encoded)
expect(parsed.parameters.filename).toBe('')
})
describe('when "filename" is US-ASCII', () => {
it('should only include filename parameter', () => {
expect(contentDisposition('plans.pdf')).toBe('attachment; filename="plans.pdf"')
})
it('should escape quotes', () => {
expect(contentDisposition('the "plans".pdf')).toBe('attachment; filename="the \\"plans\\".pdf"')
})
})
})
describe('parse(string)', () => {
describe('with type', () => {
it('should throw on quoted value', () => {
try {
parse('"attachment"')
} catch (e) {
expect(e.message).toBe('invalid type format')
}
})
it('should throw on trailing semi', () => {
try {
parse('attachment;')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
})
it('should parse "attachment"', () => {
expect(parse('attachment')).toStrictEqual(new ContentDisposition('attachment', {}))
})
it('should parse "inline"', () => {
expect(parse('inline')).toStrictEqual(new ContentDisposition('inline', {}))
})
it('should parse "form-data"', () => {
expect(parse('form-data')).toStrictEqual(new ContentDisposition('form-data', {}))
})
it('should parse with trailing LWS', () => {
expect(parse('attachment \t ')).toStrictEqual(new ContentDisposition('attachment', {}))
})
it('should normalize to lower-case', () => {
expect(parse('ATTACHMENT')).toStrictEqual(new ContentDisposition('attachment', {}))
})
})
describe('with parameters', () => {
it('should throw on trailing semi', () => {
try {
parse('attachment; filename="rates.pdf";')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
})
it('should throw on invalid param name', () => {
try {
parse('attachment; filename@="rates.pdf"')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
})
it('should throw on missing param value', () => {
try {
parse('attachment; filename=')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
})
it('should reject invalid parameter value', () => {
try {
parse('attachment; filename=trolly,trains')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it('should reject invalid parameters', () => {
try {
parse('attachment; filename=total/; foo=bar')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it('should reject duplicate parameters', () => {
try {
parse('attachment; filename=foo; filename=bar')
} catch (e) {
expect(e.message).toMatch(/invalid duplicate parameter/)
}
})
it('should reject missing type', () => {
try {
parse('filename="plans.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
try {
parse('; filename="plans.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
})
it('should lower-case parameter name', () => {
expect(parse('attachment; FILENAME="plans.pdf"')).toStrictEqual(
new ContentDisposition('attachment', { filename: 'plans.pdf' })
)
})
it('should parse quoted parameter value', () => {
expect(parse('attachment; filename="plans.pdf"')).toEqual({
type: 'attachment',
parameters: { filename: 'plans.pdf' }
})
})
it('should parse & unescape quoted value', () => {
expect(parse('attachment; filename="the \\"plans\\".pdf"')).toEqual({
type: 'attachment',
parameters: { filename: 'the "plans".pdf' }
})
})
it('should include all parameters', () => {
expect(parse('attachment; filename="plans.pdf"; foo=bar')).toEqual({
type: 'attachment',
parameters: { filename: 'plans.pdf', foo: 'bar' }
})
})
it('should parse parameters separated with any LWS', () => {
expect(parse('attachment;filename="plans.pdf" \t; \t\t foo=bar')).toEqual({
type: 'attachment',
parameters: { filename: 'plans.pdf', foo: 'bar' }
})
})
it('should parse token filename', () => {
expect(parse('attachment; filename=plans.pdf')).toEqual({
type: 'attachment',
parameters: { filename: 'plans.pdf' }
})
})
it('should parse ISO-8859-1 filename', () => {
expect(parse('attachment; filename="£ rates.pdf"')).toEqual({
type: 'attachment',
parameters: { filename: '£ rates.pdf' }
})
})
it('should throw error if decodedURI throws an error', () => {
const cutomdecodeURIComponent = (encodedURIComponent: string) => {
throw encodedURIComponent
}
vi.stubGlobal('decodeURIComponent', cutomdecodeURIComponent)
expect.assertions(1)
try {
parse("attachment; filename*=UTF-8'en'%E2%82%AC%20rates.pdf")
} catch (error) {
expect(error).toBeDefined()
}
vi.unstubAllGlobals()
})
})
describe('with extended parameters', () => {
it('should reject quoted extended parameter value', () => {
try {
parse('attachment; filename*="UTF-8\'\'%E2%82%AC%20rates.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid extended.*value/)
}
})
it('should parse UTF-8 extended parameter value', () => {
expect(parse("attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf")).toEqual({
type: 'attachment',
parameters: { filename: '€ rates.pdf' }
})
})
it('should parse ISO-8859-1 extended parameter value', () => {
expect(parse("attachment; filename*=ISO-8859-1''%A3%20rates.pdf")).toEqual({
type: 'attachment',
parameters: { filename: '£ rates.pdf' }
})
expect(parse("attachment; filename*=ISO-8859-1''%82%20rates.pdf")).toEqual({
type: 'attachment',
parameters: { filename: '? rates.pdf' }
})
})
it('should not be case-sensitive for charser', () => {
expect(parse("attachment; filename*=utf-8''%E2%82%AC%20rates.pdf")).toEqual({
type: 'attachment',
parameters: { filename: '€ rates.pdf' }
})
})
it('should reject unsupported charset', () => {
try {
parse("attachment; filename*=ISO-8859-2''%A4%20rates.pdf")
} catch (e) {
expect(e.message).toMatch(/unsupported charset/)
}
})
it('should reject invalid UTF-8 encoding', () => {
// %E2%82 is an incomplete UTF-8 sequence (Euro sign is %E2%82%AC)
expect(() => parse("attachment; filename*=UTF-8''%E2%82")).toThrow(/invalid encoded utf-8/)
})
it('should parse with embedded language', () => {
expect(parse("attachment; filename*=UTF-8'en'%E2%82%AC%20rates.pdf")).toEqual({
type: 'attachment',
parameters: { filename: '€ rates.pdf' }
})
})
it('should prefer extended parameter value', () => {
expect(parse('attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf')).toEqual({
type: 'attachment',
parameters: { filename: '€ rates.pdf' }
})
expect(parse('attachment; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf; filename="EURO rates.pdf"')).toEqual({
type: 'attachment',
parameters: { filename: '€ rates.pdf' }
})
})
})
describe('from TC 2231', () => {
describe('Disposition-Type Inline', () => {
it('should parse "inline"', () => {
expect(parse('inline')).toEqual({
type: 'inline',
parameters: {}
})
})
it('should reject ""inline""', () => {
try {
parse('"inline"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
})
it('should parse "inline; filename="foo.html""', () => {
expect(parse('inline; filename="foo.html"')).toEqual({
type: 'inline',
parameters: { filename: 'foo.html' }
})
})
it('should parse "inline; filename="Not an attachment!""', () => {
expect(parse('inline; filename="Not an attachment!"')).toEqual({
type: 'inline',
parameters: { filename: 'Not an attachment!' }
})
})
it('should parse "inline; filename="foo.pdf""', () => {
expect(parse('inline; filename="foo.pdf"')).toEqual({
type: 'inline',
parameters: { filename: 'foo.pdf' }
})
})
})
describe('Disposition-Type Attachment', () => {
it('should parse "attachment"', () => {
expect(parse('attachment')).toEqual({
type: 'attachment',
parameters: {}
})
})
it('should reject ""attachment""', () => {
try {
parse('"attachment')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
})
it('should parse "ATTACHMENT"', () => {
expect(parse('ATTACHMENT')).toEqual({
type: 'attachment',
parameters: {}
})
})
it('should parse "attachment; filename="foo.html""', () => {
expect(parse('attachment; filename="foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html' }
})
})
it('should parse "attachment; filename="0000000000111111111122222""', () => {
expect(parse('attachment; filename="0000000000111111111122222"')).toEqual({
type: 'attachment',
parameters: { filename: '0000000000111111111122222' }
})
})
it('should parse "attachment; filename="00000000001111111111222222222233333""', () => {
expect(parse('attachment; filename="00000000001111111111222222222233333"')).toEqual({
type: 'attachment',
parameters: { filename: '00000000001111111111222222222233333' }
})
})
it('should parse "attachment; filename="f\\oo.html""', () => {
expect(parse('attachment; filename="f\\oo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html' }
})
})
it('should parse "attachment; filename="\\"quoting\\" tested.html""', () => {
expect(parse('attachment; filename="\\"quoting\\" tested.html"')).toEqual({
type: 'attachment',
parameters: { filename: '"quoting" tested.html' }
})
})
it('should parse "attachment; filename="Here\'s a semicolon;.html""', () => {
expect(parse('attachment; filename="Here\'s a semicolon;.html"')).toEqual({
type: 'attachment',
parameters: { filename: "Here's a semicolon;.html" }
})
})
it('should parse "attachment; foo="bar"; filename="foo.html""', () => {
expect(parse('attachment; foo="bar"; filename="foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html', foo: 'bar' }
})
})
it('should parse "attachment; foo="\\"\\\\";filename="foo.html""', () => {
expect(parse('attachment; foo="\\"\\\\";filename="foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html', foo: '"\\' }
})
})
it('should parse "attachment; FILENAME="foo.html""', () => {
expect(parse('attachment; FILENAME="foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html' }
})
})
it('should parse "attachment; filename=foo.html"', () => {
expect(parse('attachment; filename=foo.html')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html' }
})
})
it('should reject "attachment; filename=foo,bar.html"', () => {
try {
parse('attachment; filename=foo,bar.html')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it('should reject "attachment; filename=foo.html ;"', () => {
try {
parse('attachment; filename=foo.html ;')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it('should reject "attachment; ;filename=foo"', () => {
try {
parse('attachment; ;filename=foo')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it('should reject "attachment; filename=foo bar.html"', () => {
try {
parse('attachment; filename=foo bar.html')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
})
it("should parse \"attachment; filename='foo.bar'", () => {
expect(parse("attachment; filename='foo.bar'")).toEqual({
type: 'attachment',
parameters: { filename: "'foo.bar'" }
})
})
it('should parse "attachment; filename="foo-ä.html""', () => {
expect(parse('attachment; filename="foo-ä.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo-ä.html' }
})
})
it('should parse "attachment; filename="foo-ä.html""', () => {
expect(parse('attachment; filename="foo-ä.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo-ä.html' }
})
})
it('should parse "attachment; filename="foo-%41.html""', () => {
expect(parse('attachment; filename="foo-%41.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo-%41.html' }
})
})
it('should parse "attachment; filename="50%.html""', () => {
expect(parse('attachment; filename="50%.html"')).toEqual({
type: 'attachment',
parameters: { filename: '50%.html' }
})
})
it('should parse "attachment; filename="foo-%\\41.html""', () => {
expect(parse('attachment; filename="foo-%\\41.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo-%41.html' }
})
})
it('should parse "attachment; name="foo-%41.html""', () => {
expect(parse('attachment; name="foo-%41.html"')).toEqual({
type: 'attachment',
parameters: { name: 'foo-%41.html' }
})
})
it('should parse "attachment; filename="ä-%41.html""', () => {
expect(parse('attachment; filename="ä-%41.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'ä-%41.html' }
})
})
it('should parse "attachment; filename="foo-%c3%a4-%e2%82%ac.html""', () => {
expect(parse('attachment; filename="foo-%c3%a4-%e2%82%ac.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo-%c3%a4-%e2%82%ac.html' }
})
})
it('should parse "attachment; filename ="foo.html""', () => {
expect(parse('attachment; filename ="foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: 'foo.html' }
})
})
it('should reject "attachment; filename="foo.html"; filename="bar.html"', () => {
expect(parse.bind(null, 'attachment; filename="foo.html"; filename="bar.html"')).toThrow(
/invalid duplicate parameter/
)
})
it('should reject "attachment; filename=foo[1](2).html"', () => {
expect(parse.bind(null, 'attachment; filename=foo[1](2).html')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename=foo-ä.html"', () => {
expect(parse.bind(null, 'attachment; filename=foo-ä.html')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename=foo-ä.html"', () => {
expect(parse.bind(null, 'attachment; filename=foo-ä.html')).toThrow(/invalid parameter format/)
})
it('should reject "filename=foo.html"', () => {
expect(parse.bind(null, 'filename=foo.html')).toThrow(/invalid type format/)
})
it('should reject "x=y; filename=foo.html"', () => {
expect(parse.bind(null, 'x=y; filename=foo.html')).toThrow(/invalid type format/)
})
it('should reject ""foo; filename=bar;baz"; filename=qux"', () => {
expect(parse.bind(null, '"foo; filename=bar;baz"; filename=qux')).toThrow(/invalid type format/)
})
it('should reject "filename=foo.html, filename=bar.html"', () => {
expect(parse.bind(null, 'filename=foo.html, filename=bar.html')).toThrow(/invalid type format/)
})
it('should reject "; filename=foo.html"', () => {
expect(parse.bind(null, '; filename=foo.html')).toThrow(/invalid type format/)
})
it('should reject ": inline; attachment; filename=foo.html', () => {
expect(parse.bind(null, ': inline; attachment; filename=foo.html')).toThrow(/invalid type format/)
})
it('should reject "inline; attachment; filename=foo.html', () => {
expect(parse.bind(null, 'inline; attachment; filename=foo.html')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; inline; filename=foo.html', () => {
expect(parse.bind(null, 'attachment; inline; filename=foo.html')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename="foo.html".txt', () => {
expect(parse.bind(null, 'attachment; filename="foo.html".txt')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename="bar', () => {
expect(parse.bind(null, 'attachment; filename="bar')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename=foo"bar;baz"qux', () => {
expect(parse.bind(null, 'attachment; filename=foo"bar;baz"qux')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename=foo.html, attachment; filename=bar.html', () => {
expect(parse.bind(null, 'attachment; filename=foo.html, attachment; filename=bar.html')).toThrow(
/invalid parameter format/
)
})
it('should reject "attachment; foo=foo filename=bar', () => {
expect(parse.bind(null, 'attachment; foo=foo filename=bar')).toThrow(/invalid parameter format/)
})
it('should reject "attachment; filename=bar foo=foo', () => {
expect(parse.bind(null, 'attachment; filename=bar foo=foo')).toThrow(/invalid parameter format/)
})
it('should reject "attachment filename=bar', () => {
expect(parse.bind(null, 'attachment filename=bar')).toThrow(/invalid type format/)
})
it('should reject "filename=foo.html; attachment', () => {
expect(parse.bind(null, 'filename=foo.html; attachment')).toThrow(/invalid type format/)
})
it('should parse "attachment; xfilename=foo.html"', () => {
expect(parse('attachment; xfilename=foo.html')).toEqual({
type: 'attachment',
parameters: { xfilename: 'foo.html' }
})
})
it('should parse "attachment; filename="/foo.html""', () => {
expect(parse('attachment; filename="/foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: '/foo.html' }
})
})
it('should parse "attachment; filename="\\\\foo.html""', () => {
expect(parse('attachment; filename="\\\\foo.html"')).toEqual({
type: 'attachment',
parameters: { filename: '\\foo.html' }
})
})
})
})
})
describe('contentDisposition edge cases', () => {
it('should handle filename with only filename* (no fallback, non-quoted)', () => {
// Filename with control char that makes it non-quotable and no latin1 fallback
const result = contentDisposition('file\x80name.txt')
expect(result).toContain('filename*=')
})
})