Skip to content

Commit

Permalink
Stop double-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Feb 28, 2020
1 parent 4785718 commit 7e4404e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
23 changes: 23 additions & 0 deletions src/plugins/kibana_utils/common/url/encode_uri_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,27 @@ describe('encodeQuery', () => {
g: 'null',
});
});

test('encodeQuery without encoding', () => {
expect(
encodeQuery(
{
a: 'asdf1234asdf',
b: "-_.!~*'() -_.!~*'()",
c: ':@$, :@$,',
d: "&;=+# &;=+#'",
f: ' ',
g: 'null',
},
v => v
)
).toEqual({
a: 'asdf1234asdf',
b: "-_.!~*'() -_.!~*'()",
c: ':@$, :@$,',
d: "&;=+# &;=+#'",
f: ' ',
g: 'null',
});
});
});
5 changes: 4 additions & 1 deletion src/plugins/kibana_utils/common/url/encode_uri_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { ParsedUrlQuery } from 'querystring';
import { stringify, ParsedUrlQuery } from 'querystring';
import { transform } from 'lodash';

/**
Expand Down Expand Up @@ -55,3 +55,6 @@ export const encodeQuery = (
);
}
});

export const makeUrlFromQuery = (query: ParsedUrlQuery | {}) =>
stringify(query, undefined, undefined, { encodeURIComponent: encodeUriQuery });
3 changes: 2 additions & 1 deletion src/plugins/kibana_utils/common/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* under the License.
*/

import { encodeUriQuery, encodeQuery } from './encode_uri_query';
import { encodeUriQuery, encodeQuery, makeUrlFromQuery } from './encode_uri_query';

export const url = {
encodeQuery,
encodeUriQuery,
makeUrlFromQuery,
};
4 changes: 2 additions & 2 deletions src/plugins/kibana_utils/public/history/remove_query_param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { parse, stringify } from 'querystring';
import { parse } from 'querystring';
import { History, Location } from 'history';
import { url } from '../../common';

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

delete query[param];

const newSearch = stringify(url.encodeQuery(query));
const newSearch = url.makeUrlFromQuery(query);
const newLocation: Location<any> = {
...oldLocation,
search: newSearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { format as formatUrl } from 'url';
import { stringify, ParsedUrlQuery } from 'querystring';
import { ParsedUrlQuery } from 'querystring';
import { parseUrl, parseUrlHash } from './parse';
import { url as urlUtils } from '../../../common';

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

if ((!hash || !hash.search) && !searchQueryString) return rawUrl; // nothing to change. return original url
return formatUrl({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

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

return formatUrl({
pathname: stripBasename(parsedUrl.pathname),
search: stringify(urlUtils.encodeQuery(parsedUrl.query)),
search: urlUtils.makeUrlFromQuery(parsedUrl.query),
hash: parsedHash
? formatUrl({
pathname: parsedHash.pathname,
search: stringify(urlUtils.encodeQuery(parsedHash.query)),
search: urlUtils.makeUrlFromQuery(parsedHash.query),
})
: parsedUrl.hash,
});
Expand Down
10 changes: 4 additions & 6 deletions x-pack/plugins/infra/public/utils/use_url_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ export const replaceStateKeyInQueryString = <UrlState extends any>(
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
url.encodeQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
})
);
return url.makeUrlFromQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
});
};

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

0 comments on commit 7e4404e

Please sign in to comment.