Don't encode twice (#13138)

This commit is contained in:
melohagan 2024-02-27 11:26:45 +00:00 committed by GitHub
parent b1dd8999cb
commit 3d48a8f7f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -17,6 +17,10 @@ export function breakQueryString(qs) {
return paramObj
}
function isEncoded(str) {
return typeof str == "string" && decodeURIComponent(str) !== str
}
export function buildQueryString(obj) {
let str = ""
if (obj) {
@ -35,7 +39,7 @@ export function buildQueryString(obj) {
value = value.replace(binding, marker)
bindingMarkers[marker] = binding
})
let encoded = encodeURIComponent(value || "")
let encoded = isEncoded(value) ? value : encodeURIComponent(value || "")
Object.entries(bindingMarkers).forEach(([marker, binding]) => {
encoded = encoded.replace(marker, binding)
})

View File

@ -39,4 +39,11 @@ describe("check query string utils", () => {
expect(broken.key1).toBe(obj2.key1)
expect(broken.key2).toBe(obj2.key2)
})
it("should not encode a URL more than once when building the query string", () => {
const queryString = buildQueryString({
values: "a%2Cb%2Cc",
})
expect(queryString).toBe("values=a%2Cb%2Cc")
})
})