Skip to content

Commit

Permalink
fix(unescape): fixed bug where intermediate string contains escaped c…
Browse files Browse the repository at this point in the history
…haracters (#1835)

* Fixed bug where intermediate string contains escaped characters

* Added reference to issue

Co-authored-by: Markus Tyrkkö <markus.tyrkko@nitor.com>
Co-authored-by: Markus <markus.tyrkko@gmail.com>
  • Loading branch information
3 people authored and profnandaa committed Oct 31, 2021
1 parent c28c9aa commit f821084
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib/unescape.js
Expand Up @@ -2,12 +2,15 @@ import assertString from './util/assertString';

export default function unescape(str) {
assertString(str);
return (str.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
return (str.replace(/&quot;/g, '"')
.replace(/&#x27;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&#x2F;/g, '/')
.replace(/&#x5C;/g, '\\')
.replace(/&#96;/g, '`'));
.replace(/&#96;/g, '`')
.replace(/&amp;/g, '&'));
// &amp; replacement has to be the last one to prevent
// bugs with intermediate strings containing escape sequences
// See: https://github.com/validatorjs/validator.js/issues/1827
}
3 changes: 3 additions & 0 deletions test/sanitizers.js
Expand Up @@ -184,6 +184,9 @@ describe('Sanitizers', () => {

'Backtick: &#96;':
'Backtick: `',

'Escaped string: &amp;lt;':
'Escaped string: &lt;',
},
});
});
Expand Down

0 comments on commit f821084

Please sign in to comment.