Skip to content

Commit

Permalink
Fix XSS vulnerabilities in link sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Aug 12, 2022
1 parent edd348d commit 1e5671d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/utils/sanitization-utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { includes } from './array-utils';

const PROTOCOL_REGEXP = /^([a-z0-9.+-]+:)/i;
const PROTOCOL_REGEXP = /.+:/i;

const badProtocols = [
'javascript:', // jshint ignore:line
'vbscript:' // jshint ignore:line
'javascript', // jshint ignore:line
'vbscript' // jshint ignore:line
];

function getProtocol(url) {
let matches = url && url.match(PROTOCOL_REGEXP);
let protocol = (matches && matches[0]) || ':';
let protocol = matches && matches[0] && matches[0].split(':')[0] || '';
return protocol;
}

export function sanitizeHref(url) {
let protocol = getProtocol(url).toLowerCase();
let protocol = getProtocol(url).toLowerCase().replace(/ /g, '');
if (includes(badProtocols, protocol)) {
return `unsafe:${url}`;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/utils/sanitization-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ test('#sanitizeHref', (assert) => {
let unsafe = [
'javascript:alert("XSS")', // jshint ignore: line
'jaVasCript:alert("XSS")', // jshint ignore: line
'javascript:javascript:alert("XSS")', // jshint ignore: line
'java script:alert("XSS")', // jshint ignore: line
'ja vas cri pt::alert("XSS")', // jshint ignore: line
'vbscript:alert("XSS")' // jshint ignore: line
];

Expand Down

0 comments on commit 1e5671d

Please sign in to comment.