Skip to content

Commit 225a915

Browse files
authored
fix: escape $ in curl request bodies and headers (#6245)
This address a bug where a `$` character in a request body or header would not be properly escaped in a string in the generated curl command. Fixes #5390
1 parent 28b3b4c commit 225a915

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/core/curlify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function curl( request ){
2626
for( let p of request.get("headers").entries() ){
2727
let [ h,v ] = p
2828
curlified.push( "-H " )
29-
curlified.push( `"${h}: ${v}"` )
29+
curlified.push( `"${h}: ${v.replace("$", "\\$")}"` )
3030
isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(h) && /^multipart\/form-data$/i.test(v)
3131
}
3232
}
@@ -44,7 +44,7 @@ export default function curl( request ){
4444
}
4545
} else {
4646
curlified.push( "-d" )
47-
curlified.push( JSON.stringify( request.get("body") ).replace(/\\n/g, "") )
47+
curlified.push( JSON.stringify( request.get("body") ).replace(/\\n/g, "").replace("$", "\\$") )
4848
}
4949
} else if(!request.get("body") && request.get("method") === "POST") {
5050
curlified.push( "-d" )

test/mocha/core/curlify.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,17 @@ describe("curlify", function () {
319319
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -d {\"id\":\"123\",\"file\":{\"name\":\"file.txt\",\"type\":\"text/plain\"}}")
320320
})
321321
})
322+
323+
it("should escape dollar signs in headers and request body", function () {
324+
let req = {
325+
url: "http://example.com",
326+
method: "POST",
327+
headers: { "X-DOLLAR": "token/123$" },
328+
body: "CREATE ($props)"
329+
}
330+
331+
let curlified = curl(Im.fromJS(req))
332+
333+
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"X-DOLLAR: token/123\\$\" -d \"CREATE (\\$props)\"")
334+
})
322335
})

0 commit comments

Comments
 (0)