Skip to content

fix: encoded value be decoded issue #8701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions packages/insomnia/src/utils/url/querystring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('querystring', () => {
});
});

describe('build()', () => {
describe('buildQueryParameter()', () => {
it('builds simple param', () => {
const str = buildQueryParameter({ name: 'foo', value: 'bar??' });
expect(str).toBe('foo=bar%3F%3F');
Expand All @@ -85,6 +85,21 @@ describe('querystring', () => {
expect(str).toBe('foo');
});

it('builds param with encoded value and name', () => {
const str = buildQueryParameter({ name: 'foo%11%09%2A%9D%0F', value: 'bar%11%09%2A%9D%0F' });
expect(str).toBe('foo%11%09%2A%9D%0F=bar%11%09%2A%9D%0F');
});

it('builds param with null value and disable strict', () => {
const str = buildQueryParameter({ name: 'foo', value: null }, false);
expect(str).toBe('foo=');
});

it('builds param with spaces', () => {
const str = buildQueryParameter({ name: 'foo bar', value: 'baz qux' });
expect(str).toBe('foo%20bar=baz%20qux');
});

it('builds param with null value and enable strictNullHandling', () => {
const str = buildQueryParameter({ name: 'foo', value: null }, true, { strictNullHandling: true });
expect(str).toBe('foo');
Expand All @@ -95,6 +110,11 @@ describe('querystring', () => {
expect(str).toBe('foo');
});

it('builds param with empty string value and disable strict', () => {
const str = buildQueryParameter({ name: 'foo', value: '' }, false);
expect(str).toBe('foo=');
});

it('builds param with empty string value and enable strictNullHandling', () => {
const str = buildQueryParameter({ name: 'foo', value: '' }, true, { strictNullHandling: true });
expect(str).toBe('foo=');
Expand All @@ -112,9 +132,34 @@ describe('querystring', () => {
expect(str).toBe('number=10');
expect(str2).toBe('number=0');
});

it('builds param ignore commas in the value', () => {
const str = buildQueryParameter({ name: 'foo,', value: '1,2,3' });
expect(str).toBe('foo%2C=1,2,3');
});

it('builds param without decode the commas back', () => {
const str = buildQueryParameter({ name: 'foo', value: '1%2C' });
expect(str).toBe('foo=1%2C');
});

it.fails('build query param with __ENC__ returns incorrect result', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some risks associated with the current solution.

const str = buildQueryParameter({ name: 'foo', value: '__ENC__11' });
expect(str).toBe('foo=__ENC__11');
});

it.fails('build query param with __RAW__ returns incorrect result', () => {
const str = buildQueryParameter({ name: 'foo', value: '__RAW__22' });
expect(str).toBe('foo=__RAW__22');
});

it.fails('build query param with __RAW__AA throws error', () => {
const str = buildQueryParameter({ name: 'foo', value: '__RAW__AA' });
expect(str).toBe(expect.anything());
});
});

describe('buildFromParams()', () => {
describe('buildQueryStringFromParams()', () => {
it('builds from params', () => {
const str = buildQueryStringFromParams([
{ name: 'foo', value: 'bar??' },
Expand All @@ -128,6 +173,7 @@ describe('querystring', () => {

expect(str).toBe('foo=bar%3F%3F&foo1&foo2&hello&hi%20there=bar%3F%3F');
});

it('builds from params', () => {
const str = buildQueryStringFromParams(
[
Expand All @@ -142,6 +188,7 @@ describe('querystring', () => {

expect(str).toBe('foo=bar%3F%3F&hello=&hi%20there=bar%3F%3F&=bar%3F%3F&=');
});

it('builds from params with strict mode and strictNullHandling', () => {
const str = buildQueryStringFromParams(
[
Expand All @@ -161,7 +208,7 @@ describe('querystring', () => {
});
});

describe('deconstructToParams()', () => {
describe('deconstructQueryStringToParams()', () => {
it('builds from params', () => {
const str = deconstructQueryStringToParams('foo=bar%3F%3F&hello&hi%20there=bar%3F%3F&=&=val');

Expand All @@ -171,6 +218,7 @@ describe('querystring', () => {
{ name: 'hi there', value: 'bar??' },
]);
});

it('builds from params with =', () => {
const str = deconstructQueryStringToParams('foo=bar&1=2=3=4&hi');

Expand Down Expand Up @@ -202,6 +250,18 @@ describe('querystring', () => {
{ name: 'foo2', value: '' },
]);
});

it('builds from params with skipDecode', () => {
const str = deconstructQueryStringToParams('foo=bar%3F%3F&hello&hi%20there=bar%3F%3F&=&=val', true, {
skipDecode: true,
});

expect(str).toEqual([
{ name: 'foo', value: 'bar%3F%3F' },
{ name: 'hello', value: '' },
{ name: 'hi%20there', value: 'bar%3F%3F' },
]);
});
});

describe('smartEncodeUrl()', () => {
Expand Down Expand Up @@ -290,5 +350,15 @@ describe('querystring', () => {
});
expect(urlNoEqualSign2).toBe('https://google.com/terminologies?foo=bar&foo1');
});

it('should not decode value back', () => {
expect(
smartEncodeUrl(
'https://google.com/?text=%4E%C3%A4%72%20%6D%61%6E%20%74%65%73%74%61%72%20%74%65%73%74%61%72%20%6D%61%6E',
),
).toBe(
'https://google.com/?text=%4E%C3%A4%72%20%6D%61%6E%20%74%65%73%74%61%72%20%74%65%73%74%61%72%20%6D%61%6E',
);
});
});
});
49 changes: 24 additions & 25 deletions packages/insomnia/src/utils/url/querystring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const RFC_3986_SUB_DELIMITERS = '$+,;='; // (unintentionally?) missing: !&'()*
const URL_PATH_CHARACTER_WHITELIST = `${RFC_3986_GENERAL_DELIMITERS}${RFC_3986_SUB_DELIMITERS}`;

interface IQueryStringOptions {
// Option to distingush between parameters with(&foo=) and without(&foo) equal signs. Both are converted to empty string by default.
// Option to distinguish between parameters with(&foo=) and without(&foo) equal signs. Both are converted to empty string by default.
strictNullHandling?: boolean;
// Option to encode parameters, default to true, necessary to disable for request.settingEncodeUrl = false
encodeParams?: boolean;
Expand Down Expand Up @@ -80,7 +80,7 @@ export const buildQueryParameter = (

/** allow empty names and values */
strict?: boolean,
/** extra options like strict hanlde null value */
/** extra options like strict handle null value */
options?: IQueryStringOptions,
) => {
strict = strict === undefined ? true : strict;
Expand All @@ -101,8 +101,8 @@ export const buildQueryParameter = (
if (!encodeParams) {
return `${param.name}=${param.value}`;
}
// Don't encode ',' in values
const value = flexibleEncodeComponent(param.value || '').replace(/%2C/gi, ',');
// Don't encode ',' in values, the original proposal was to keep values like 'foo,bar' intact
const value = flexibleEncodeComponent(param.value || '', ',');
const name = flexibleEncodeComponent(param.name || '');

return `${name}=${value}`;
Expand All @@ -117,7 +117,7 @@ export const buildQueryStringFromParams = (
parameters: { name: string; value?: StrictNullSearchParamsValueType }[],
/** allow empty names and values */
strict?: boolean,
/** extra options like strict hanlde null value */
/** extra options like strict handle null value */
options?: IQueryStringOptions,
) => {
strict = strict === undefined ? true : strict;
Expand All @@ -142,14 +142,16 @@ export const buildQueryStringFromParams = (
*/
export const deconstructQueryStringToParams = <T extends IQueryStringOptions>(
qs?: string,

/** allow empty names and values */
strict?: boolean,
/** extra deconstruct options like strict hanlde null value */
options?: T,
/** extra deconstruct options like strict handle null value */
options?: T & {
/** skip decode the querystring */
skipDecode?: boolean;
},
): ProcessDeconstructFuncReturnType<T> => {
strict = strict === undefined ? true : strict;
const { strictNullHandling = false } = options || {};
const { strictNullHandling = false, skipDecode = false } = options || {};
const pairs: ProcessDeconstructFuncReturnType<T> = [];
type ValueType = (typeof pairs)[number]['value'];

Expand All @@ -167,15 +169,20 @@ export const deconstructQueryStringToParams = <T extends IQueryStringOptions>(

let name = '';
try {
name = decodeURIComponent(encodedName || '');
name = skipDecode ? encodedName : decodeURIComponent(encodedName || '');
} catch (error) {
// Just leave it
name = encodedName;
}

let value: ValueType = '';
try {
value = strictNullHandling && encodedValue === null ? null : decodeURIComponent(encodedValue || '');
value =
strictNullHandling && encodedValue === null
? null
: skipDecode
? encodedValue
: decodeURIComponent(encodedValue || '');
} catch (error) {
// Just leave it
value = encodedValue;
Expand All @@ -200,7 +207,7 @@ export const deconstructQueryStringToParams = <T extends IQueryStringOptions>(
export const smartEncodeUrl = (url: string, encode?: boolean, options?: IQueryStringOptions) => {
// Default autoEncode = true if not passed
encode = encode === undefined ? true : encode;
// Default do not strcit handle null value
// Default do not strict handle null value
const { strictNullHandling = false } = options || {};
const urlWithProto = setDefaultProtocol(url);

Expand All @@ -224,16 +231,11 @@ export const smartEncodeUrl = (url: string, encode?: boolean, options?: IQuerySt
// ~~~~~~~~~~~~~~ //

if (parsedUrl.query) {
const qsParams = deconstructQueryStringToParams(parsedUrl.query, true, { strictNullHandling });
const encodedQsParams = [];
for (const { name, value } of qsParams) {
encodedQsParams.push({
name: flexibleEncodeComponent(name),
value: strictNullHandling && value === null ? null : flexibleEncodeComponent(value as string),
});
}
// Skip decode if already encoded, to avoid some decoded characters cannot be encoded again.
// E.g. %4E -> N, encodeURIComponent(decodeURIComponent('%4E')) is N which is not expected.
const qsParams = deconstructQueryStringToParams(parsedUrl.query, true, { strictNullHandling, skipDecode: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildQueryStringFromParams will encode the name and value, and already ensure the string is not double-encoded. It's unnecessary to call flexibleEncodeComponent here.
And decoding the string will cause some strings cannot be encoded back since decodeURI and encodeURI are not exact opposites


parsedUrl.query = buildQueryStringFromParams(encodedQsParams, true, { strictNullHandling });
parsedUrl.query = buildQueryStringFromParams(qsParams, true, { strictNullHandling });
parsedUrl.search = `?${parsedUrl.query}`;
}

Expand All @@ -245,10 +247,7 @@ export const smartEncodeUrl = (url: string, encode?: boolean, options?: IQuerySt
* @param str string to encode
* @param ignore characters to ignore
*/
export const flexibleEncodeComponent = (str = '', ignore = '') => {
// Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' ');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iShot_2025-05-12_10 50 14

This line was from 9 years before, I guess the original proposal is to avoid %20 to be double-encoded when decodeURI or decodeURIComponent throws errors. I think it's safe to remove from the current code since it's using __ENC__ to avoid double-encoding now.


const flexibleEncodeComponent = (str = '', ignore = '') => {
// Handle all already-encoded characters so we don't touch them
str = str.replace(/%([0-9a-fA-F]{2})/g, '__ENC__$1');

Expand Down
Loading