Skip to content

Commit

Permalink
Merge pull request #386 from ziflow/master
Browse files Browse the repository at this point in the history
fix: strip direction override characters
  • Loading branch information
gregjacobs authored Sep 7, 2022
2 parents b0c15d6 + b341126 commit 8119d5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/autolinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,8 @@ export default class Autolinker {
textOrHtml = textOrHtml.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

textOrHtml = this.stripUnsafeCharacters(textOrHtml);

let matches = this.parse(textOrHtml),
newHtml: string[] = [],
lastIndex = 0;
Expand Down Expand Up @@ -1020,6 +1022,16 @@ export default class Autolinker {

return tagBuilder;
}

/**
* Strips characters considered as unsafe
* SNYK-AUTOLINKER-2438289
* @param text
* @private
*/
private stripUnsafeCharacters(text: string) {
return text.replace(/[\u202a-\u202e, \u200e-\u200f]/g, '');
}
}

export interface AutolinkerConfig {
Expand Down
26 changes: 26 additions & 0 deletions tests/autolinker-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1265,4 +1265,30 @@ describe('Autolinker Url Matching -', () => {
);
});
});

describe('unicode exploits', () => {
it('should strip out character direction override unicodes', () => {
expect(autolinker.link('foo.combar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202Ebar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202abar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202bbar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202cbar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202dbar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
expect(autolinker.link('foo.com\u202ebar.com')).toBe(
'<a href="http://foo.combar.com">foo.combar.com</a>'
);
});
});
});

0 comments on commit 8119d5c

Please sign in to comment.