Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The available configs are:
- [No Implicit Buggy Globals](./docs/rules/no-implicit-buggy-globals.md)
- [No Inner HTML](./docs/rules/no-inner-html.md)
- [No InnerText](./docs/rules/no-innerText.md)
- [No Script Tag Building](./docs/rules/no-script-tag-building.md)
Comment thread
manuelpuyol marked this conversation as resolved.
Outdated
- [No Then](./docs/rules/no-then.md)
- [No Useless Passive](./docs/rules/no-useless-passive.md)
- [Prefer Observers](./docs/rules/prefer-observers.md)
Expand Down
24 changes: 24 additions & 0 deletions docs/rules/no-script-tag-building.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# No Script Tag Building

## 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
1 change: 1 addition & 0 deletions lib/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'github/array-foreach': 'error',
'github/no-implicit-buggy-globals': 'error',
'github/no-then': 'error',
'github/no-script-tag-building': 'error',
'i18n-text/no-en': ['error'],
'import/default': 'error',
'import/export': 'error',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'no-implicit-buggy-globals': require('./rules/no-implicit-buggy-globals'),
'no-inner-html': require('./rules/no-inner-html'),
'no-innerText': require('./rules/no-innerText'),
'no-script-tag-building': require('./rules/no-script-tag-building'),
'no-then': require('./rules/no-then'),
'no-useless-passive': require('./rules/no-useless-passive'),
'prefer-observers': require('./rules/prefer-observers'),
Expand Down
30 changes: 30 additions & 0 deletions lib/rules/no-script-tag-building.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow marking a event handler as passive when it has no effect',
Comment thread
manuelpuyol marked this conversation as resolved.
Outdated
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."
})
}
}
}
}
38 changes: 38 additions & 0 deletions tests/no-script-tag-building.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const rule = require('../lib/rules/no-script-tag-building')
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester()

ruleTester.run('no-script-tag-building', 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"',
errors: [
{
message: "Don't create dynamic script tags, add them in the server template instead.",
type: 'Literal'
}
]
}
]
})