From e66611e77192d87b1499f9d5f85f10589d311939 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 20 Feb 2021 13:03:41 -0800 Subject: [PATCH] fix: Error with search term including %, closes #1891 --- app/scenes/Search/Search.js | 5 +++-- app/utils/routeHelpers.js | 2 +- app/utils/urls.js | 6 ++++++ app/utils/urls.test.js | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 app/utils/urls.test.js diff --git a/app/scenes/Search/Search.js b/app/scenes/Search/Search.js index 3a46a19e3..572dfe72d 100644 --- a/app/scenes/Search/Search.js +++ b/app/scenes/Search/Search.js @@ -37,6 +37,7 @@ import NewDocumentMenu from "menus/NewDocumentMenu"; import { type LocationWithState } from "types"; import { metaDisplay } from "utils/keyboard"; import { newDocumentUrl, searchUrl } from "utils/routeHelpers"; +import { decodeURIComponentSafe } from "utils/urls"; type Props = { history: RouterHistory, @@ -55,7 +56,7 @@ class Search extends React.Component { lastParams: Object; @observable - query: string = decodeURIComponent(this.props.match.params.term || ""); + query: string = decodeURIComponentSafe(this.props.match.params.term || ""); @observable params: URLSearchParams = new URLSearchParams(); @observable offset: number = 0; @observable allowLoadMore: boolean = true; @@ -116,7 +117,7 @@ class Search extends React.Component { }; handleTermChange = () => { - const query = decodeURIComponent(this.props.match.params.term || ""); + const query = decodeURIComponentSafe(this.props.match.params.term || ""); this.query = query ? query : ""; this.offset = 0; this.allowLoadMore = true; diff --git a/app/utils/routeHelpers.js b/app/utils/routeHelpers.js index 145bb2834..85214ea16 100644 --- a/app/utils/routeHelpers.js +++ b/app/utils/routeHelpers.js @@ -74,7 +74,7 @@ export function searchUrl( let route = "/search"; if (query) { - route += `/${encodeURIComponent(query)}`; + route += `/${encodeURIComponent(query.replace("%", "%25"))}`; } search = search ? `?${search}` : ""; diff --git a/app/utils/urls.js b/app/utils/urls.js index a51ee8a6e..6f38a6e72 100644 --- a/app/utils/urls.js +++ b/app/utils/urls.js @@ -28,3 +28,9 @@ export function cdnPath(path: string): string { export function imagePath(path: string): string { return cdnPath(`/images/${path}`); } + +export function decodeURIComponentSafe(text: string) { + return text + ? decodeURIComponent(text.replace(/%(?![0-9][0-9a-fA-F]+)/g, "%25")) + : text; +} diff --git a/app/utils/urls.test.js b/app/utils/urls.test.js new file mode 100644 index 000000000..8a6741aa4 --- /dev/null +++ b/app/utils/urls.test.js @@ -0,0 +1,14 @@ +// @flow +import { decodeURIComponentSafe } from "./urls"; + +describe("decodeURIComponentSafe", () => { + test("to handle % symbols", () => { + expect(decodeURIComponentSafe("%")).toBe("%"); + expect(decodeURIComponentSafe("%25")).toBe("%"); + }); + + test("to correctly account for encoded symbols", () => { + expect(decodeURIComponentSafe("%7D")).toBe("}"); + expect(decodeURIComponentSafe("%2F")).toBe("/"); + }); +});