Skip to content

Commit

Permalink
Fix encoding method in parseUrl util
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmilburn committed Nov 8, 2023
1 parent f31b4c2 commit aae115e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 3 additions & 3 deletions admin/src/utils/__tests__/parse-url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('parseUrl', () => {
expect(result).toEqual(output);
});

it('should not double-encode query string params', () => {
it('should escape pre-existing % characters when encoding query string params', () => {
const config = {
url: 'https://www.example.com/{slug}',
query: {
Expand All @@ -61,9 +61,9 @@ describe('parseUrl', () => {
};
const data = {
slug: 'test',
foobar: 'foo%2Fbar%2Ftest',
foobar: 'foo%bar%test',
};
const output = 'https://www.example.com/test?type=page&foobar=foo%2Fbar%2Ftest';
const output = 'https://www.example.com/test?type=page&foobar=foo%25bar%25test';
const result = parseUrl(config, data, true);

expect(result).toEqual(output);
Expand Down
7 changes: 3 additions & 4 deletions admin/src/utils/parse-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ const parseUrl = (config, data) => {
return acc;
}

// We are decoding and encoding at the same time here to avoid double-encoding.
return {
...acc,
[key]: encodeURIComponent(decodeURIComponent(val)),
[key]: val,
};
}, {});
const params = Object.entries(config?.query ?? {}).reduce((acc, [key, val]) => {
return {
...acc,
[key]: interpolate(val, replacements),
[key]: encodeURIComponent(interpolate(val, replacements)),
};
}, {});

const url = interpolate(trimSlashes(config.url), replacements);
const url = encodeURI(interpolate(trimSlashes(config.url), replacements));
const query = qs.stringify(params, {
addQueryPrefix: true,

Expand Down

0 comments on commit aae115e

Please sign in to comment.