-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Introduce a separate parsing_method rule #244
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
rpl
wants to merge
5
commits into
mozilla:main
Choose a base branch
from
rpl:feat/parsing-method-rule-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0948a5a
feat: Introduce a separate parsing_method rule.
rpl 56dd78d
docs: Fix eslint flat config examples
rpl 6e88b27
test: Add a base integration test trying to load the plugin from esli…
rpl fbf3b58
chore: Allow parsing_method to include customized messages and update…
rpl 98508bd
chore: Fixed typos in new integration test's inline comment
rpl 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,3 +33,6 @@ jobs: | |
|
|
||
| - name: Test | ||
| run: yarn run test | ||
|
|
||
| - name: Test (integration tests) | ||
| run: yarn run test-integration | ||
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,13 @@ | ||
| # parsing_method | ||
|
|
||
| The _parsing_method_ rule in _eslint-plugin-no-unsanitized_ performs basic security | ||
| checks for function calls that parse strings into new Document or DocumentFragment | ||
| instances. The idea of these checks is to allow developers to opt-in/opt-out of detecting | ||
| use of these methods, separately from the `method` rule which is reporting violation | ||
| as errors by default. | ||
|
|
||
| ### Further Reading | ||
|
|
||
| - Advanced guidance on [Fixing rule violations](fixing-violations.md) | ||
| - This rule has some [customization](customization.md) options that allow you | ||
| to add or remove functions that should not be called | ||
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,194 @@ | ||
| /** | ||
| * @file ESLint rule to disallow unsanitized method calls | ||
| * @author Frederik Braun et al. | ||
| * @copyright 2015-2017 Mozilla Corporation. All rights reserved. | ||
| */ | ||
| "use strict"; | ||
|
|
||
| const RuleHelper = require("./ruleHelper"); | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| /** | ||
| * On newer parsers, `import(foo)` gets parsed as a keyword. | ||
| * | ||
| * @param {object} ruleHelper a RuleHelper instance | ||
| * @param {object} importExpr The ImportExpression we triggered on | ||
| * @returns {undefined} Does not return | ||
| */ | ||
| function checkImport(ruleHelper, importExpr) { | ||
| const fakeCall = { | ||
| callee: { type: "Import" }, | ||
| arguments: [importExpr.source], | ||
| }; | ||
| Object.assign(fakeCall, importExpr); | ||
| ruleHelper.checkMethod(fakeCall); | ||
| } | ||
|
|
||
| /** | ||
| * Run ruleHelper.checkMethod for all but irrelevant callees (FunctionExpression, etc.) | ||
| * | ||
| * @param {object} ruleHelper a RuleHelper instance | ||
| * @param {object} callExpr The CallExpression we triggered on | ||
| * @param {object} node The callee node | ||
| * @returns {undefined} Does not return | ||
| */ | ||
| function checkCallExpression(ruleHelper, callExpr, node) { | ||
| switch (node.type) { | ||
| case "Identifier": | ||
| case "MemberExpression": | ||
| if (callExpr.arguments && callExpr.arguments.length > 0) { | ||
| ruleHelper.checkMethod(callExpr); | ||
| } | ||
| break; | ||
|
|
||
| case "TSNonNullExpression": { | ||
| const newCallExpr = Object.assign({}, callExpr); | ||
| newCallExpr.callee = node.expression; | ||
| checkCallExpression(ruleHelper, newCallExpr, node.expression); | ||
| break; | ||
| } | ||
|
|
||
| case "TaggedTemplateExpression": { | ||
| const newCallExpr = Object.assign({}, callExpr); | ||
| newCallExpr.callee = node.tag; | ||
| const expressions = node.quasi.expressions; | ||
| const strings = node.quasi.quasis; | ||
| newCallExpr.arguments = [strings, ...expressions]; | ||
| checkCallExpression(ruleHelper, newCallExpr, node.tag); | ||
| break; | ||
| } | ||
|
|
||
| case "TypeCastExpression": { | ||
| const newCallExpr = Object.assign({}, callExpr); | ||
| newCallExpr.callee = node.expression; | ||
| checkCallExpression(ruleHelper, newCallExpr, node.expression); | ||
| break; | ||
| } | ||
|
|
||
| case "AssignmentExpression": | ||
| if (node.right.type === "MemberExpression") { | ||
| const newCallExpr = Object.assign({}, callExpr); | ||
| newCallExpr.callee = node.right; | ||
| checkCallExpression(ruleHelper, newCallExpr, node.right); | ||
| break; | ||
| } | ||
| checkCallExpression(ruleHelper, callExpr, node.right); | ||
| break; | ||
|
|
||
| case "Import": | ||
| ruleHelper.checkMethod(callExpr); | ||
| break; | ||
|
|
||
| case "SequenceExpression": { | ||
| // the return value of a SequenceExpression is the last expression. | ||
| // So, we create a new mock CallExpression with the actually called | ||
| // ... expression as the callee node and pass it to checkMethod() | ||
|
|
||
| const newCallExpr = Object.assign({}, callExpr); | ||
| const idx = node.expressions.length - 1; | ||
| const called = node.expressions[idx]; | ||
| newCallExpr.callee = called; | ||
| ruleHelper.checkMethod(newCallExpr); | ||
| break; | ||
| } | ||
|
|
||
| case "TSAsExpression": | ||
| break; | ||
|
|
||
| // those are fine: | ||
| case "LogicalExpression": // Should we scan these? issue #62. | ||
| case "ConditionalExpression": | ||
| case "ArrowFunctionExpression": | ||
| case "FunctionExpression": | ||
| case "Super": | ||
| case "CallExpression": | ||
| case "ThisExpression": | ||
| case "NewExpression": | ||
| case "TSTypeAssertion": | ||
| case "AwaitExpression": // see issue #122 | ||
| break; | ||
|
|
||
| // If we don't cater for this expression throw an error | ||
| default: | ||
| ruleHelper.reportUnsupported( | ||
| node, | ||
| "Unexpected Callee", | ||
| `Unsupported Callee of type ${node.type} for CallExpression` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const defaultMeta = { | ||
| type: "problem", | ||
| // Expected to be filled by the concrete rules through the exported createMethodsRule function. | ||
| docs: {}, | ||
| schema: [ | ||
| { | ||
| type: "object", | ||
| properties: { | ||
| defaultDisable: { | ||
| type: "boolean", | ||
| }, | ||
| escape: { | ||
| type: "object", | ||
| properties: { | ||
| taggedTemplates: { | ||
| type: "array", | ||
| items: [{ type: "string" }], | ||
| }, | ||
| methods: { | ||
| type: "array", | ||
| items: [{ type: "string" }], | ||
| }, | ||
| }, | ||
| }, | ||
| objectMatches: { | ||
| type: "array", | ||
| }, | ||
| properties: { | ||
| type: "array", | ||
| }, | ||
| variableTracing: { type: "boolean" }, | ||
| }, | ||
| additionalProperties: false, | ||
| }, | ||
| { | ||
| type: "object", | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| module.exports = function createMethodsRule({ meta, defaultRuleChecks }) { | ||
| return { | ||
| meta: { | ||
| ...defaultMeta, | ||
| ...meta, | ||
| }, | ||
| create(context) { | ||
| const ruleHelper = new RuleHelper(context, defaultRuleChecks); | ||
| return { | ||
| CallExpression(node) { | ||
| checkCallExpression(ruleHelper, node, node.callee); | ||
| }, | ||
| ImportExpression(node) { | ||
| checkImport(ruleHelper, node); | ||
| }, | ||
|
|
||
| // Tagged template expressions pass arguments in a special format we need to | ||
| // map to our existing function call logic | ||
| // foo`bar${var1}${var2}` will run as foo(['bar', ''], var1, var2) | ||
| TaggedTemplateExpression(node) { | ||
| const newCallExpr = Object.assign({}, node); | ||
| newCallExpr.callee = node.tag; | ||
| const expressions = node.quasi.expressions; | ||
| const strings = node.quasi.quasis; | ||
| newCallExpr.arguments = [strings, ...expressions]; | ||
| checkCallExpression(ruleHelper, newCallExpr, node.tag); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| }; |
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
Oops, something went wrong.
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.