File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 11import createBoundary from "./util/createBoundary"
2+ import escape from "./util/escapeName"
23import isFormData from "./util/isFormData"
34import isFile from "./util/isFile"
45
@@ -96,10 +97,10 @@ export class Encoder {
9697 let header = ""
9798
9899 header += `${ this . #DASHES} ${ this . boundary } ${ this . #CRLF} `
99- header += `Content-Disposition: form-data; name="${ name } "`
100+ header += `Content-Disposition: form-data; name="${ escape ( name ) } "`
100101
101102 if ( isFile ( value ) ) {
102- header += `; filename="${ value . name } "${ this . #CRLF} `
103+ header += `; filename="${ escape ( value . name ) } "${ this . #CRLF} `
103104 header += `Content-Type: ${ value . type || "application/octet-stream" } `
104105 }
105106
Original file line number Diff line number Diff line change 1+ import test from "ava"
2+
3+ import escapeName from "./escapeName"
4+
5+ const CR = "%0D"
6+ const LF = "%0A"
7+ const Q = "%22"
8+
9+ test ( "Escapes all the CRs in the name" , t => {
10+ t . is < string > ( escapeName ( "\rna\rme\r" ) , `${ CR } na${ CR } me${ CR } ` )
11+ } )
12+
13+ test ( "Keeps escaped CR as is" , t => {
14+ const expected = `name${ CR } `
15+
16+ t . is < string > ( escapeName ( expected ) , expected )
17+ } )
18+
19+ test ( "Escapes all the LFs in the name" , t => {
20+ t . is < string > ( escapeName ( "nam\ne\n" ) , `nam${ LF } e${ LF } ` )
21+ } )
22+
23+ test ( "Keeps escaped LF as is" , t => {
24+ const expected = `name${ LF } `
25+
26+ t . is < string > ( escapeName ( expected ) , expected )
27+ } )
28+
29+ test ( "Escapes all double quotes in the name" , t => {
30+ t . is < string > ( escapeName ( "\"name\"" ) , `${ Q } name${ Q } ` )
31+ } )
Original file line number Diff line number Diff line change 1+ const escapeName = ( name : unknown ) => String ( name )
2+ . replace ( / \r / g, "%0D" ) // CR
3+ . replace ( / \n / g, "%0A" ) // LF
4+ . replace ( / " / g, "%22" )
5+
6+ export default escapeName
You can’t perform that action at this time.
0 commit comments