File tree 3 files changed +11
-2
lines changed
3 files changed +11
-2
lines changed Original file line number Diff line number Diff line change 1
1
# CHANGELOG
2
2
3
+ ## unreleased
4
+ - Fix issue where urls in the form ` https://example.com

/something ` were not properly sanitized
5
+
3
6
## 6.0.1
4
7
5
8
- Fix issue where urls in the form ` javascript:alert('xss'); ` were not properly sanitized
Original file line number Diff line number Diff line change @@ -92,6 +92,12 @@ describe("sanitizeUrl", () => {
92
92
) ;
93
93
} ) ;
94
94
95
+ it ( "removes newline entities from urls" , ( ) => {
96
+ expect ( sanitizeUrl ( "https://example.com

/something" ) ) . toBe (
97
+ "https://example.com/something"
98
+ ) ;
99
+ } ) ;
100
+
95
101
it ( "decodes html entities" , ( ) => {
96
102
// all these decode to javascript:alert('xss');
97
103
const attackVectors = [
Original file line number Diff line number Diff line change 1
1
const invalidProtocolRegex = / ^ ( [ ^ \w ] * ) ( j a v a s c r i p t | d a t a | v b s c r i p t ) / im;
2
2
const htmlEntitiesRegex = / & # ( \w + ) ( ^ \w | ; ) ? / g;
3
- const htmlTabEntityRegex = / & t a b ; / gi;
3
+ const htmlCtrlEntityRegex = / & ( n e w l i n e | t a b ) ; / gi;
4
4
const ctrlCharactersRegex =
5
5
/ [ \u0000 - \u001F \u007F - \u009F \u2000 - \u200D \uFEFF ] / gim;
6
6
const urlSchemeRegex = / ^ .+ ( : | & c o l o n ; ) / gim;
@@ -12,14 +12,14 @@ function isRelativeUrlWithoutProtocol(url: string): boolean {
12
12
13
13
// adapted from https://stackoverflow.com/a/29824550/2601552
14
14
function decodeHtmlCharacters ( str : string ) {
15
- str = str . replace ( htmlTabEntityRegex , "	" ) ;
16
15
return str . replace ( htmlEntitiesRegex , ( match , dec ) => {
17
16
return String . fromCharCode ( dec ) ;
18
17
} ) ;
19
18
}
20
19
21
20
export function sanitizeUrl ( url ?: string ) : string {
22
21
const sanitizedUrl = decodeHtmlCharacters ( url || "" )
22
+ . replace ( htmlCtrlEntityRegex , "" )
23
23
. replace ( ctrlCharactersRegex , "" )
24
24
. trim ( ) ;
25
25
You can’t perform that action at this time.
0 commit comments