-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rule: inline-script-disabled
- Loading branch information
Showing
6 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) 2015, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
HTMLHint.addRule({ | ||
id: 'inline-script-disabled', | ||
description: 'Inline script cannot be use.', | ||
init: function(parser, reporter){ | ||
var self = this; | ||
parser.addListener('tagstart', function(event){ | ||
var attrs = event.attrs; | ||
var attr; | ||
var col = event.col + event.tagName.length + 1; | ||
var attrName; | ||
var reEvent = /^on(unload|message|submit|select|scroll|resize|mouseover|mouseout|mousemove|mouseleave|mouseenter|mousedown|load|keyup|keypress|keydown|focus|dblclick|click|change|blur|error)$/i; | ||
|
||
for(var i=0, l=attrs.length;i<l;i++){ | ||
attr = attrs[i]; | ||
attrName = attr.name.toLowerCase(); | ||
if(reEvent.test(attrName) === true){ | ||
reporter.warn('Inline script [ '+attr.raw+' ] cannot be use.', event.line, col + attr.index, self, attr.raw); | ||
} | ||
else if(attrName === 'src' || attrName === 'href'){ | ||
if(/^\s*javascript:/i.test(attr.value)){ | ||
reporter.warn('Inline script [ '+attr.raw+' ] cannot be use.', event.line, col + attr.index, self, attr.raw); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (c) 2015, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
HTMLHint.addRule({ | ||
id: 'inline-style-disabled', | ||
description: 'Inline style cannot be use.', | ||
init: function(parser, reporter){ | ||
var self = this; | ||
parser.addListener('tagstart', function(event){ | ||
var attrs = event.attrs; | ||
var attr; | ||
var col = event.col + event.tagName.length + 1; | ||
for(var i=0, l=attrs.length;i<l;i++){ | ||
attr = attrs[i]; | ||
if(attr.name.toLowerCase() === 'style'){ | ||
reporter.warn('Inline style [ '+attr.raw+' ] cannot be use.', event.line, col + attr.index, self, attr.raw); | ||
} | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2015, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
|
||
var expect = require("expect.js"); | ||
|
||
var HTMLHint = require("../../index").HTMLHint; | ||
|
||
var ruldId = 'inline-script-disabled', | ||
ruleOptions = {}; | ||
|
||
ruleOptions[ruldId] = true; | ||
|
||
describe('Rules: '+ruldId, function(){ | ||
|
||
it('Inline on event should result in an error', function(){ | ||
var code = '<body><img src="test.gif" onclick="alert(1);"><img src="test.gif" onMouseDown="alert(1);"></body>'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(2); | ||
expect(messages[0].rule.id).to.be(ruldId); | ||
expect(messages[0].line).to.be(1); | ||
expect(messages[0].col).to.be(26); | ||
expect(messages[0].type).to.be('warning'); | ||
expect(messages[1].col).to.be(66); | ||
}); | ||
|
||
it('onttt should not result in an error', function(){ | ||
var code = '<body><img src="test.gif" onttt="alert(1);"></body>'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(0); | ||
}); | ||
|
||
it('Javascript protocol [ javascript: ] should result in an error', function(){ | ||
var code = '<body><img src="javascript:alert(1)"><img src=" JAVASCRIPT:alert(1)"></body>'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(2); | ||
expect(messages[0].rule.id).to.be(ruldId); | ||
expect(messages[0].line).to.be(1); | ||
expect(messages[0].col).to.be(11); | ||
expect(messages[0].type).to.be('warning'); | ||
expect(messages[1].col).to.be(42); | ||
|
||
code = '<body><a href="javascript:alert(1)">test1</a><a href=" JAVASCRIPT:alert(2)">test2</a></body>'; | ||
messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(2); | ||
expect(messages[0].rule.id).to.be(ruldId); | ||
expect(messages[0].line).to.be(1); | ||
expect(messages[0].col).to.be(9); | ||
expect(messages[0].type).to.be('warning'); | ||
expect(messages[1].col).to.be(48); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) 2015, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
|
||
var expect = require("expect.js"); | ||
|
||
var HTMLHint = require("../../index").HTMLHint; | ||
|
||
var ruldId = 'inline-style-disabled', | ||
ruleOptions = {}; | ||
|
||
ruleOptions[ruldId] = true; | ||
|
||
describe('Rules: '+ruldId, function(){ | ||
|
||
it('Inline style should result in an error', function(){ | ||
var code = '<body><div style="color:red;"></div></body>'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(1); | ||
expect(messages[0].rule.id).to.be(ruldId); | ||
expect(messages[0].line).to.be(1); | ||
expect(messages[0].col).to.be(11); | ||
expect(messages[0].type).to.be('warning'); | ||
|
||
code = '<body><div STYLE="color:red;"></div></body>'; | ||
messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(1); | ||
}); | ||
|
||
}); |