Skip to content

Commit 01ec104

Browse files
committed
Fix clojure test cases for in-depth escaping scenario
1 parent 23b7e63 commit 01ec104

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

src/targets/clojure/clj_http.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const CodeBuilder = require('../../helpers/code-builder')
1414
const helpers = require('../../helpers/headers')
15+
const { escape } = require('../../helpers/format')
1516

1617
const Keyword = function (name) {
1718
this.name = name
@@ -60,7 +61,7 @@ const padBlock = function (x, s) {
6061
const jsToEdn = function (js) {
6162
switch (jsType(js)) {
6263
case 'string':
63-
return '"' + js.replace(/"/g, '\\"') + '"'
64+
return '"' + escape(js, { delimiter: '"' }) + '"'
6465
case 'file':
6566
return js.toString()
6667
case 'keyword':
@@ -73,7 +74,14 @@ const jsToEdn = function (js) {
7374
const obj = Object.keys(js)
7475
.reduce(function (acc, key) {
7576
const val = padBlock(key.length + 2, jsToEdn(js[key]))
76-
return acc + ':' + key + ' ' + val + '\n '
77+
78+
// This check is overly strict, but good enough for us for
79+
// all typical HTTP values we care about
80+
const safeKey = key.match(/^[a-zA-Z_][\w-]*$/)
81+
? ':' + key
82+
: jsToEdn(key)
83+
84+
return acc + safeKey + ' ' + val + '\n '
7785
}, '')
7886
.trim()
7987
return '{' + padBlock(1, obj) + '}'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(require '[clj-http.client :as client])
2+
3+
(client/post "http://example.test/%27%22%60$(%(%%7B%7B%7B/0%s//" {:headers {:squote-value-test "'"
4+
:dquote-value-test "\""
5+
:backtick-value-test "`"
6+
:dollar-parenthesis-value-test "$("
7+
:hash-brace-value-test "#{"
8+
:percent-parenthesis-value-test "%("
9+
:percent-brace-value-test "%{"
10+
:double-brace-value-test "{{"
11+
:null-value-test "\\0"
12+
:string-fmt-value-test "%s"
13+
:slash-value-test "\\"}
14+
:query-params {"'" "squote-key-test"
15+
:squote-value-test "'"
16+
"\"" "dquote-key-test"
17+
:dquote-value-test "\""
18+
"`" "backtick-key-test"
19+
:backtick-value-test "`"
20+
"$(" "dollar-parenthesis-key-test"
21+
:dollar-parenthesis-value-test "$("
22+
"#{" "hash-brace-key-test"
23+
:hash-brace-value-test "#{"
24+
"%(" "percent-parenthesis-key-test"
25+
:percent-parenthesis-value-test "%("
26+
"%{" "percent-brace-key-test"
27+
:percent-brace-value-test "%{"
28+
"{{" "double-brace-key-test"
29+
:double-brace-value-test "{{"
30+
"\\0" "null-key-test"
31+
:null-value-test "\\0"
32+
"%s" "string-fmt-key-test"
33+
:string-fmt-value-test "%s"
34+
"\\" "slash-key-test"
35+
:slash-value-test "\\"}
36+
:body "' \" ` $( #{ %( %{ {{ \\0 %s \\"})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(require '[clj-http.client :as client])
22

3-
(client/get "http://mockbin.com/har" {:query-params {:foo[bar] "baz,zap"
3+
(client/get "http://mockbin.com/har" {:query-params {"foo[bar]" "baz,zap"
44
:fiz "buz"
55
:key "value"}})

test/targets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const itShouldHaveInfo = function (name, obj) {
4242
// TODO: investigate issues with these fixtures
4343
const skipMe = {
4444
clojure: {
45-
clj_http: ['jsonObj-null-value', 'jsonObj-multiline', 'malicious']
45+
clj_http: ['jsonObj-null-value', 'jsonObj-multiline']
4646
},
4747
objc: {
4848
nsurlsession: ['malicious']

0 commit comments

Comments
 (0)