Skip to content

Commit 317dd87

Browse files
MarcholioMarkus
authored andcommitted
fix(unescape): fixed bug where intermediate string contains escaped characters (validatorjs#1835)
* Fixed bug where intermediate string contains escaped characters * Added reference to issue Co-authored-by: Markus Tyrkkö <[email protected]> Co-authored-by: Markus <[email protected]>
1 parent 0392208 commit 317dd87

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/lib/unescape.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import assertString from './util/assertString';
22

33
export default function unescape(str) {
44
assertString(str);
5-
return (str.replace(/&amp;/g, '&')
6-
.replace(/&quot;/g, '"')
5+
return (str.replace(/&quot;/g, '"')
76
.replace(/&#x27;/g, "'")
87
.replace(/&lt;/g, '<')
98
.replace(/&gt;/g, '>')
109
.replace(/&#x2F;/g, '/')
1110
.replace(/&#x5C;/g, '\\')
12-
.replace(/&#96;/g, '`'));
11+
.replace(/&#96;/g, '`')
12+
.replace(/&amp;/g, '&'));
13+
// &amp; replacement has to be the last one to prevent
14+
// bugs with intermediate strings containing escape sequences
15+
// See: https://github.com/validatorjs/validator.js/issues/1827
1316
}

test/sanitizers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ describe('Sanitizers', () => {
184184

185185
'Backtick: &#96;':
186186
'Backtick: `',
187+
188+
'Escaped string: &amp;lt;':
189+
'Escaped string: &lt;',
187190
},
188191
});
189192
});

0 commit comments

Comments
 (0)