Skip to content

Commit 427bb46

Browse files
kapral18cursoragent
andcommitted
[Console] Preserve spaces in URL query strings
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b893c23 commit 427bb46

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/platform/plugins/shared/console/public/application/containers/editor/utils/tokens_utils.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ describe('tokens_utils', () => {
3131
const result = removeTrailingWhitespaces(url);
3232
expect(result).toBe(url);
3333
});
34+
it(`doesn't treat a question mark inside quotes as query string start`, () => {
35+
const url = '_search/"?literal" trailing_text';
36+
const result = removeTrailingWhitespaces(url);
37+
expect(result).toBe('_search/"?literal"');
38+
});
39+
it(`does not strip unquoted spaces inside query values (the reported bug case)`, () => {
40+
const url = 'myindex/_search?q=type:organisation AND elastic';
41+
const result = removeTrailingWhitespaces(url);
42+
expect(result).toBe(url);
43+
});
44+
it(`still detects and strips inline comment after unquoted query spaces`, () => {
45+
const url = 'myindex/_search?q=type:organisation AND elastic // filter orgs';
46+
const result = removeTrailingWhitespaces(url);
47+
expect(result).toBe('myindex/_search?q=type:organisation AND elastic');
48+
});
49+
it(`still detects inline comment after mixed whitespace in query values`, () => {
50+
const url = 'myindex/_search?q=type:organisation AND elastic \t// filter orgs';
51+
const result = removeTrailingWhitespaces(url);
52+
expect(result).toBe('myindex/_search?q=type:organisation AND elastic');
53+
});
54+
it(`still detects and strips hash comment after unquoted query spaces`, () => {
55+
const url = 'myindex/_search?q=type:organisation AND elastic # filter orgs';
56+
const result = removeTrailingWhitespaces(url);
57+
expect(result).toBe('myindex/_search?q=type:organisation AND elastic');
58+
});
3459
});
3560

3661
describe('parseLine', () => {
@@ -48,6 +73,15 @@ describe('tokens_utils', () => {
4873
expect(urlPathTokens).toEqual(['_search']);
4974
expect(urlParamsTokens[0]).toEqual(['query', '"test1 test2 test3"']);
5075
});
76+
it('preserves unquoted spaces inside query values', () => {
77+
const { method, url, urlPathTokens, urlParamsTokens } = parseLine(
78+
'GET myindex/_search?q=type:organisation AND elastic'
79+
);
80+
expect(method).toBe('GET');
81+
expect(url).toBe('myindex/_search?q=type:organisation AND elastic');
82+
expect(urlPathTokens).toEqual(['myindex', '_search']);
83+
expect(urlParamsTokens[0]).toEqual(['q', 'type:organisation AND elastic']);
84+
});
5185
it('works with multiple whitespaces', () => {
5286
const { method, url, urlPathTokens, urlParamsTokens } = parseLine(
5387
' GET _search?query="test1 test2 test3" // comment'

src/platform/plugins/shared/console/public/application/containers/editor/utils/tokens_utils.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,30 @@ export const parseBody = (value: string): string[] => {
416416
export const removeTrailingWhitespaces = (url: string): string => {
417417
let index = 0;
418418
let whitespaceIndex = -1;
419-
let isQueryParam = false;
419+
let isInQuotes = false;
420+
let seenQuestionMark = false;
420421
let char = url[index];
421422
while (char) {
422423
if (char === '"') {
423-
isQueryParam = !isQueryParam;
424-
} else if (char === ' ' && !isQueryParam) {
425-
whitespaceIndex = index;
426-
break;
424+
isInQuotes = !isInQuotes;
425+
} else if (char === '?' && !isInQuotes) {
426+
seenQuestionMark = true;
427+
} else if (char === ' ' && !isInQuotes) {
428+
if (!seenQuestionMark) {
429+
whitespaceIndex = index;
430+
break;
431+
}
432+
let commentIndex = index;
433+
while (whitespacesRegex.test(url[commentIndex])) {
434+
commentIndex++;
435+
}
436+
if (
437+
url[commentIndex] === '#' ||
438+
(url[commentIndex] === '/' && url[commentIndex + 1] === '/')
439+
) {
440+
whitespaceIndex = index;
441+
break;
442+
}
427443
}
428444
index++;
429445
char = url[index];

0 commit comments

Comments
 (0)