Skip to content

Commit

Permalink
Merge pull request #153 from vtex-apps/fix/safe-decode-uri
Browse files Browse the repository at this point in the history
Safely decoding the URI in case there is any breaking character such as "%"
  • Loading branch information
Ícaro Azevedo authored Dec 8, 2021
2 parents 104b631 + 3b2335f commit 9a047f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion react/components/Autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion react/utils/dom-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9a047f8

Please sign in to comment.