Skip to content

Stricter html string check #639

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/unescaped-html-literal.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Constructing raw HTML with string literals is error prone and may lead to security issues.

Instead use [`lit-html`](https://github.com/Polymer/lit-html)'s `html` tagged template literal to safely construct HTML literal strings. Alternatively, you can use document builder APIs like `document.createElement`.
Instead use [`lit-html`](https://github.com/Polymer/lit-html)'s `html` tagged template literal to safely construct HTML literal strings. Alternatively, you can implement your own `html` tagged template literal function, or use document builder APIs like `document.createElement`.

👎 Examples of **incorrect** code for this rule:

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/unescaped-html-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
},

create(context) {
const htmlOpenTag = /^<[a-zA-Z]/
const htmlOpenTag = /^\s*<[a-zA-Z]/

return {
Literal(node) {
Expand Down
10 changes: 10 additions & 0 deletions tests/unescaped-html-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ ruleTester.run('unescaped-html-literal', rule, {
},
],
},
{
code: "const helloHTML = ` \n\t<div>Hello ${name}!</div>`",
parserOptions: {ecmaVersion: 2017},
errors: [
{
message: 'Unescaped HTML literal. Use html`` tag template literal for secure escaping.',
type: 'TemplateLiteral',
},
],
},
{
code: 'const helloHTML = foo`<div>Hello ${name}!</div>`',
parserOptions: {ecmaVersion: 2017},
Expand Down