fix(electron): avoid double encoding urls params. Fixes #5380. (#5507)

* fix(common): avoid double encoding urls params
This commit is contained in:
Siddharth Gelera (reaper)
2025-09-12 19:08:53 +05:30
committed by GitHub
parent c08827b0c0
commit 2b6da56c3c
2 changed files with 109 additions and 2 deletions

View File

@@ -13,6 +13,11 @@ const getContentType = (headers = {}) => {
return contentType;
};
const getRawQueryString = (url) => {
const queryIndex = url.indexOf('?');
return queryIndex !== -1 ? url.slice(queryIndex) : '';
};
const interpolateVars = (request, envVariables = {}, runtimeVariables = {}, processEnvVars = {}) => {
const globalEnvironmentVariables = request?.globalEnvironmentVariables || {};
const oauth2CredentialVariables = request?.oauth2CredentialVariables || {};
@@ -126,7 +131,7 @@ const interpolateVars = (request, envVariables = {}, runtimeVariables = {}, proc
if (request?.pathParams?.length) {
let url = request.url;
const urlSearchRaw = getRawQueryString(request.url)
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}
@@ -152,7 +157,7 @@ const interpolateVars = (request, envVariables = {}, runtimeVariables = {}, proc
.join('');
const trailingSlash = url.pathname.endsWith('/') ? '/' : '';
request.url = url.origin + urlPathnameInterpolatedWithPathParams + trailingSlash + url.search;
request.url = url.origin + urlPathnameInterpolatedWithPathParams + trailingSlash + urlSearchRaw;
}
if (request.proxy) {

View File

@@ -54,6 +54,108 @@ describe('interpolate-vars: interpolateVars', () => {
});
});
describe('With path params', () => {
it('keeps the original url search params as is', async () => {
const request = {
method: 'GET',
url: 'http://example.com/:param/?search=hello world',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const result = interpolateVars(request, null, null, null);
expect(result.url).toBe('http://example.com/foobar/?search=hello world');
});
it('keeps the original url search params as is even when url might not have protocl ', async () => {
const request = {
method: 'GET',
url: 'example.com/:param/?search=hello world',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const result = interpolateVars(request, null, null, null);
expect(result.url).toBe('http://example.com/foobar/?search=hello world');
});
it('keeps the original url search params as is even when encoded', async () => {
const request = {
method: 'GET',
url: 'http://example.com/:param?search=hello%20world',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const result = interpolateVars(request, null, null, null);
expect(result.url).toBe('http://example.com/foobar?search=hello%20world');
});
it('keeps the original url search params as is with edge cases', async () => {
const requestOne = {
method: 'GET',
url: 'https://example.com/:param?x=1#section',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const requestTwo = {
method: 'GET',
url: 'https://example.com/:param?x?y=2',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const resultOne = interpolateVars(requestOne, null, null, null);
expect(resultOne.url).toBe('https://example.com/foobar?x=1#section');
const resultTwo = interpolateVars(requestTwo, null, null, null);
expect(resultTwo.url).toBe('https://example.com/foobar?x?y=2');
});
it('keeps the original url even without search', async () => {
const request = {
method: 'GET',
url: 'http://example.com/:param',
pathParams: [
{
type: 'path',
name: 'param',
value: 'foobar'
}
]
};
const result = interpolateVars(request, null, null, null);
expect(result.url).toBe('http://example.com/foobar');
});
});
describe('With process environment variables', () => {
/*
* It should NOT turn process env vars into literal segments.