-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule for requiring HTML entities to be escaped #681
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,70 @@ | ||
# Prevent invalid characters from appearing in markup (no-unescaped-entities) | ||
|
||
This rule prevents characters that you may have meant as JSX escape characters | ||
from being accidentally injected as a text node in JSX statements. | ||
|
||
For example, if one were to misplace their closing `>` in a tag: | ||
|
||
```jsx | ||
<MyComponent | ||
name="name" | ||
type="string" | ||
foo="bar"> {/* oops! */} | ||
x="y"> | ||
Body Text | ||
</MyComponent> | ||
``` | ||
|
||
The body text of this would render as `x="y"> Body Text`, which is probably not | ||
what was intended. This rule requires that these special characters are | ||
escaped if they appear in the body of a tag. | ||
|
||
Another example is when one accidentally includes an extra closing brace. | ||
|
||
```jsx | ||
<MyComponent>{'Text'}}</MyComponent> | ||
``` | ||
|
||
The extra brace will be rendered, and the body text will be `Text}`. | ||
|
||
This rule will also check for `"` and `'`, which might be accidentally included | ||
when the closing `>` is in the wrong place. | ||
|
||
```jsx | ||
<MyComponent | ||
a="b"> {/* oops! */} | ||
c="d" | ||
Intended body text | ||
</MyComponent> | ||
``` | ||
|
||
The preferred way to include one of these characters is to use the HTML escape code. | ||
|
||
- `>` can be replaced with `>` | ||
- `"` can be replaced with `"`, `“` or `”` | ||
- `'` can be replaced with `'`, `‘` or `’` | ||
- `}` can be replaced with `}` | ||
|
||
Alternatively, you can include the literal character inside a subexpression | ||
(such as `<div>{'>'}</div>`. | ||
|
||
The characters `<` and `{` should also be escaped, but they are not checked by this | ||
rule because it is a syntax error to include those tokens inside of a tag. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
<div> > </div> | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```jsx | ||
<div> > </div> | ||
``` | ||
|
||
```jsx | ||
<div> {'>'} </div> | ||
``` |
This file contains hidden or 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
This file contains hidden or 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,80 @@ | ||
/** | ||
* @fileoverview HTML special characters should be escaped. | ||
* @author Patrick Hayes | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
// NOTE: '<' and '{' are also problematic characters, but they do not need | ||
// to be included here because it is a syntax error when these characters are | ||
// included accidentally. | ||
var DEFAULTS = ['>', '"', '\'', '}']; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Detect unescaped HTML entities, which might represent malformed tags', | ||
category: 'Possible Errors', | ||
recommended: false | ||
}, | ||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
forbid: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create: function(context) { | ||
function isInvalidEntity(node) { | ||
var configuration = context.options[0] || {}; | ||
var entities = configuration.forbid || DEFAULTS; | ||
|
||
// HTML entites are already escaped in node.value (as well as node.raw), | ||
// so pull the raw text from context.getSourceCode() | ||
for (var i = node.loc.start.line; i <= node.loc.end.line; i++) { | ||
var rawLine = context.getSourceCode().lines[i - 1]; | ||
var start = 0; | ||
var end = rawLine.length; | ||
if (i === node.loc.start.line) { | ||
start = node.loc.start.column; | ||
} | ||
if (i === node.loc.end.line) { | ||
end = node.loc.end.column; | ||
} | ||
rawLine = rawLine.substring(start, end); | ||
for (var j = 0; j < entities.length; j++) { | ||
for (var index = 0; index < rawLine.length; index++) { | ||
var c = rawLine[index]; | ||
if (c === entities[j]) { | ||
context.report({ | ||
loc: {line: i, column: start + index}, | ||
message: 'HTML entities must be escaped.', | ||
node: node | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
Literal: function(node) { | ||
if (node.type === 'Literal' && node.parent.type === 'JSXElement') { | ||
if (isInvalidEntity(node)) { | ||
context.report(node, 'HTML entities must be escaped.'); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains hidden or 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,138 @@ | ||
/** | ||
* @fileoverview Tests for no-unescaped-entities | ||
* @author Patrick Hayes | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/no-unescaped-entities'); | ||
var RuleTester = require('eslint').RuleTester; | ||
var parserOptions = { | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('no-unescaped-entities', rule, { | ||
|
||
valid: [ | ||
{ | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return (', | ||
' <div/>', | ||
' );', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>Here is some text!</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>I’ve escaped some entities: > < &</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>first line is ok', | ||
' so is second', | ||
' and here are some escaped entities: > < &</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>{">" + "<" + "&" + \'"\'}</div>;', | ||
' },', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>></div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{message: 'HTML entities must be escaped.'}] | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>first line is ok', | ||
' so is second', | ||
' and here are some bad entities: ></div>', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{message: 'HTML entities must be escaped.'}] | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>\'</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{message: 'HTML entities must be escaped.'}] | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>Multiple errors: \'>></div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'HTML entities must be escaped.'}, | ||
{message: 'HTML entities must be escaped.'}, | ||
{message: 'HTML entities must be escaped.'} | ||
] | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>{"Unbalanced braces"}}</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{message: 'HTML entities must be escaped.'}] | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also ensure that
{'>'}
is allowed, by adding a test for it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the test on line 70 should cover that case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops, missed that. thanks, that works.