Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to prevent XSS, throw an error when the URL contains a JS script #2464

Merged
merged 6 commits into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/helpers/isURLSameOrigin.js
Expand Up @@ -22,14 +22,16 @@ module.exports = (
function resolveURL(url) {
var href = url;

if (isValidXss(url)) {
throw new Error('URL contains XSS injection attempt');
}

if (msie) {
// IE needs attribute set twice to normalize properties
urlParsingNode.setAttribute('href', href);
href = urlParsingNode.href;
}

isValidXss(url);

urlParsingNode.setAttribute('href', href);

// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/isValidXss.js
@@ -1,6 +1,6 @@
'use strict';

module.exports = function isValidXss(requestURL) {
var regex = RegExp('<script+.*>+.*<\/script>');
return regex.test(requestURL);
var xssRegex = /(\b)(on\S+)(\s*)=|javascript|(<\s*)(\/*)script/gi;
return xssRegex.test(requestURL);
};
5 changes: 3 additions & 2 deletions test/specs/helpers/isURLSameOrigin.spec.js
Expand Up @@ -9,7 +9,8 @@ describe('helpers::isURLSameOrigin', function () {
expect(isURLSameOrigin('https://github.com/axios/axios')).toEqual(false);
});

it('should detect xss', function () {
expect(isURLSameOrigin('https://github.com/axios/axios?<script>alert("hello")</script>')).toEqual(false)
it('should detect XSS scripts on a same origin request', function () {
expect(() => { isURLSameOrigin('https://github.com/axios/axios?<script>alert("hello")</script>'); })
.toThrowError(Error, 'URL contains XSS injection attempt')
})
});
24 changes: 24 additions & 0 deletions test/specs/helpers/isValidXss.spec.js
@@ -0,0 +1,24 @@
var isValidXss = require('../../../lib/helpers/isValidXss');

describe('helpers::isValidXss', function () {
it('should detect script tags', function () {
expect(isValidXss("<script/xss>alert('blah')</script/xss>")).toBe(true);
expect(isValidXss("<SCRIPT>alert('getting your password')</SCRIPT>")).toBe(true);
expect(isValidXss("<script src='http://xssinjections.com/inject.js'>xss</script>")).toBe(true);
expect(isValidXss("<img src='/' onerror='javascript:alert('xss')'>xss</script>")).toBe(true);
expect(isValidXss("<script>console.log('XSS')</script>")).toBe(true);
expect(isValidXss("onerror=alert('XSS')")).toBe(true);
expect(isValidXss("<a onclick='alert('XSS')'>Click Me</a>")).toBe(true);
});

it('should not detect non script tags', function() {
expect(isValidXss("<safe> tags")).toBe(false);
expect(isValidXss("<safetag>")).toBe(false);
expect(isValidXss(">>> safe <<<")).toBe(false);
expect(isValidXss("<<< safe >>>")).toBe(false);
expect(isValidXss("my script rules")).toBe(false);
expect(isValidXss("<a notonlistener='nomatch'>")).toBe(false);
expect(isValidXss("<h2>MyTitle</h2>")).toBe(false);
expect(isValidXss("<img src='#'/>")).toBe(false);
})
});