Skip to content

Commit 7e4404e

Browse files
committedFeb 28, 2020
Stop double-encoding
1 parent 4785718 commit 7e4404e

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed
 

‎src/plugins/kibana_utils/common/url/encode_uri_query.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,27 @@ describe('encodeQuery', () => {
6767
g: 'null',
6868
});
6969
});
70+
71+
test('encodeQuery without encoding', () => {
72+
expect(
73+
encodeQuery(
74+
{
75+
a: 'asdf1234asdf',
76+
b: "-_.!~*'() -_.!~*'()",
77+
c: ':@$, :@$,',
78+
d: "&;=+# &;=+#'",
79+
f: ' ',
80+
g: 'null',
81+
},
82+
v => v
83+
)
84+
).toEqual({
85+
a: 'asdf1234asdf',
86+
b: "-_.!~*'() -_.!~*'()",
87+
c: ':@$, :@$,',
88+
d: "&;=+# &;=+#'",
89+
f: ' ',
90+
g: 'null',
91+
});
92+
});
7093
});

‎src/plugins/kibana_utils/common/url/encode_uri_query.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { ParsedUrlQuery } from 'querystring';
20+
import { stringify, ParsedUrlQuery } from 'querystring';
2121
import { transform } from 'lodash';
2222

2323
/**
@@ -55,3 +55,6 @@ export const encodeQuery = (
5555
);
5656
}
5757
});
58+
59+
export const makeUrlFromQuery = (query: ParsedUrlQuery | {}) =>
60+
stringify(query, undefined, undefined, { encodeURIComponent: encodeUriQuery });

‎src/plugins/kibana_utils/common/url/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
import { encodeUriQuery, encodeQuery } from './encode_uri_query';
20+
import { encodeUriQuery, encodeQuery, makeUrlFromQuery } from './encode_uri_query';
2121

2222
export const url = {
2323
encodeQuery,
2424
encodeUriQuery,
25+
makeUrlFromQuery,
2526
};

‎src/plugins/kibana_utils/public/history/remove_query_param.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { parse, stringify } from 'querystring';
20+
import { parse } from 'querystring';
2121
import { History, Location } from 'history';
2222
import { url } from '../../common';
2323

@@ -28,7 +28,7 @@ export function removeQueryParam(history: History, param: string, replace: boole
2828

2929
delete query[param];
3030

31-
const newSearch = stringify(url.encodeQuery(query));
31+
const newSearch = url.makeUrlFromQuery(query);
3232
const newLocation: Location<any> = {
3333
...oldLocation,
3434
search: newSearch,

‎src/plugins/kibana_utils/public/state_management/url/format.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { format as formatUrl } from 'url';
21-
import { stringify, ParsedUrlQuery } from 'querystring';
21+
import { ParsedUrlQuery } from 'querystring';
2222
import { parseUrl, parseUrlHash } from './parse';
2323
import { url as urlUtils } from '../../../common';
2424

@@ -29,7 +29,7 @@ export function replaceUrlHashQuery(
2929
const url = parseUrl(rawUrl);
3030
const hash = parseUrlHash(rawUrl);
3131
const newQuery = queryReplacer(hash?.query || {});
32-
const searchQueryString = stringify(urlUtils.encodeQuery(newQuery));
32+
const searchQueryString = urlUtils.makeUrlFromQuery(newQuery);
3333

3434
if ((!hash || !hash.search) && !searchQueryString) return rawUrl; // nothing to change. return original url
3535
return formatUrl({

‎src/plugins/kibana_utils/public/state_management/url/kbn_url_storage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { format as formatUrl } from 'url';
21-
import { stringify } from 'querystring';
21+
// import { stringify } from 'querystring';
2222
import { createBrowserHistory, History } from 'history';
2323
import { decodeState, encodeState } from '../state_encoder';
2424
import { getCurrentUrl, parseUrl, parseUrlHash } from './parse';
@@ -244,11 +244,11 @@ export function getRelativeToHistoryPath(absoluteUrl: string, history: History):
244244

245245
return formatUrl({
246246
pathname: stripBasename(parsedUrl.pathname),
247-
search: stringify(urlUtils.encodeQuery(parsedUrl.query)),
247+
search: urlUtils.makeUrlFromQuery(parsedUrl.query),
248248
hash: parsedHash
249249
? formatUrl({
250250
pathname: parsedHash.pathname,
251-
search: stringify(urlUtils.encodeQuery(parsedHash.query)),
251+
search: urlUtils.makeUrlFromQuery(parsedHash.query),
252252
})
253253
: parsedUrl.hash,
254254
});

‎x-pack/plugins/infra/public/utils/use_url_state.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ export const replaceStateKeyInQueryString = <UrlState extends any>(
115115
const encodedUrlState =
116116
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;
117117

118-
return stringify(
119-
url.encodeQuery({
120-
...previousQueryValues,
121-
[stateKey]: encodedUrlState,
122-
})
123-
);
118+
return url.makeUrlFromQuery({
119+
...previousQueryValues,
120+
[stateKey]: encodedUrlState,
121+
});
124122
};
125123

126124
const replaceQueryStringInLocation = (location: Location, queryString: string): Location => {

0 commit comments

Comments
 (0)