-
Notifications
You must be signed in to change notification settings - Fork 63
Add rule to prohibit building script tags in the client #223
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # No Dynamic Script Tag | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Creating dynamic script tags bypasses a lot of security measures - like SRIs - and pose a potential threat to your application. | ||
| Instead of creating a `script` tag in the client, provide all necessary `script` tags in the page's HTML. | ||
|
|
||
| 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| document.createElement('script') | ||
| document.getElementById('some-id').type = 'text/javascript' | ||
| ``` | ||
|
|
||
| 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```html | ||
| <!-- index.html --> | ||
| <script src="/index.js" type="text/javascript"> | ||
| ``` | ||
|
|
||
| ## Version | ||
|
|
||
| 4.3.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow creating dynamic script tags', | ||
| url: require('../url')(module) | ||
| }, | ||
| fixable: 'code', | ||
| schema: [] | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| 'CallExpression[callee.property.name="createElement"][arguments.length > 0]': function (node) { | ||
| if (node.arguments[0].value !== 'script') return | ||
|
|
||
| context.report({ | ||
| node: node.arguments[0], | ||
| message: "Don't create dynamic script tags, add them in the server template instead." | ||
| }) | ||
| }, | ||
| 'AssignmentExpression[left.property.name="type"][right.value="text/javascript"]': function (node) { | ||
| context.report({ | ||
| node: node.right, | ||
| message: "Don't create dynamic script tags, add them in the server template instead." | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const rule = require('../lib/rules/no-dynamic-script-tag') | ||
| const RuleTester = require('eslint').RuleTester | ||
|
|
||
| const ruleTester = new RuleTester() | ||
|
|
||
| ruleTester.run('no-dynamic-script-tag', rule, { | ||
| valid: [ | ||
| { | ||
| code: 'document.createElement("div")' | ||
| }, | ||
| { | ||
| code: 'document.createElement("span")' | ||
| }, | ||
| { | ||
| code: 'document.createElement("span").type = "foo"' | ||
| } | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'document.createElement("script")', | ||
| errors: [ | ||
| { | ||
| message: "Don't create dynamic script tags, add them in the server template instead.", | ||
| type: 'Literal' | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| code: 'document.createElement("span").type = "text/javascript"', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to test that |
||
| errors: [ | ||
| { | ||
| message: "Don't create dynamic script tags, add them in the server template instead.", | ||
| type: 'Literal' | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.