diff --git a/CHANGELOG.md b/CHANGELOG.md index 20b982c3..ef0bb840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Safely decoding URI components to prevent errors when the search query contains breaking characters such as "%" ## [1.5.0] - 2021-10-08 diff --git a/react/components/Autocomplete/index.tsx b/react/components/Autocomplete/index.tsx index f7beb548..d86918ad 100644 --- a/react/components/Autocomplete/index.tsx +++ b/react/components/Autocomplete/index.tsx @@ -143,7 +143,12 @@ class AutoComplete extends React.Component< const path = window.location.href.split("_q="); if (path[1]) { const term = path[1].split("&")[0]; - this.client.prependSearchHistory(decodeURI(term)); + + try { + this.client.prependSearchHistory(decodeURI(term)); + } catch { + this.client.prependSearchHistory(term); + } } } diff --git a/react/utils/dom-utils.ts b/react/utils/dom-utils.ts index 3af512a3..2e5e8555 100644 --- a/react/utils/dom-utils.ts +++ b/react/utils/dom-utils.ts @@ -49,7 +49,15 @@ export function getCookie(name: string): string | null { const regex = new RegExp(`(^|;)[ ]*${name}=([^;]*)`); const match = regex.exec(document.cookie); - return match ? decodeURIComponent(match[2]) : null; + if (!match) { + return null; + } + + try { + return decodeURIComponent(match[2]); + } catch { + return match[2]; + } } export function setCookie(