Skip to content

Commit 8119d5c

Browse files
authored
Merge pull request #386 from ziflow/master
fix: strip direction override characters
2 parents b0c15d6 + b341126 commit 8119d5c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/autolinker.ts

+12
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ export default class Autolinker {
913913
textOrHtml = textOrHtml.replace(/</g, '&lt;').replace(/>/g, '&gt;');
914914
}
915915

916+
textOrHtml = this.stripUnsafeCharacters(textOrHtml);
917+
916918
let matches = this.parse(textOrHtml),
917919
newHtml: string[] = [],
918920
lastIndex = 0;
@@ -1020,6 +1022,16 @@ export default class Autolinker {
10201022

10211023
return tagBuilder;
10221024
}
1025+
1026+
/**
1027+
* Strips characters considered as unsafe
1028+
* SNYK-AUTOLINKER-2438289
1029+
* @param text
1030+
* @private
1031+
*/
1032+
private stripUnsafeCharacters(text: string) {
1033+
return text.replace(/[\u202a-\u202e, \u200e-\u200f]/g, '');
1034+
}
10231035
}
10241036

10251037
export interface AutolinkerConfig {

tests/autolinker-url.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1265,4 +1265,30 @@ describe('Autolinker Url Matching -', () => {
12651265
);
12661266
});
12671267
});
1268+
1269+
describe('unicode exploits', () => {
1270+
it('should strip out character direction override unicodes', () => {
1271+
expect(autolinker.link('foo.combar.com')).toBe(
1272+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1273+
);
1274+
expect(autolinker.link('foo.com\u202Ebar.com')).toBe(
1275+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1276+
);
1277+
expect(autolinker.link('foo.com\u202abar.com')).toBe(
1278+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1279+
);
1280+
expect(autolinker.link('foo.com\u202bbar.com')).toBe(
1281+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1282+
);
1283+
expect(autolinker.link('foo.com\u202cbar.com')).toBe(
1284+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1285+
);
1286+
expect(autolinker.link('foo.com\u202dbar.com')).toBe(
1287+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1288+
);
1289+
expect(autolinker.link('foo.com\u202ebar.com')).toBe(
1290+
'<a href="http://foo.combar.com">foo.combar.com</a>'
1291+
);
1292+
});
1293+
});
12681294
});

0 commit comments

Comments
 (0)