Skip to content

Commit

Permalink
Add support for setHTMLUnsafe - fix #232 (#235)
Browse files Browse the repository at this point in the history
Co-authored-by: Frederik Braun <fb@frederik-braun.com>
  • Loading branch information
mozfreddyb and freddyb committed Jan 29, 2024
1 parent aaff1e1 commit 64cbbae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/ruleHelper.js
Expand Up @@ -51,7 +51,7 @@ RuleHelper.prototype = {


switch(expression.type) {
case"Literal":
case "Literal":
/* surely, someone could have an evil literal in there, but that"s malice
we can just check for unsafe coding practice, not outright malice
example literal "<script>eval(location.hash.slice(1)</script>"
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/method.js
Expand Up @@ -44,6 +44,11 @@ const defaultRuleChecks = {
"document"
],
properties: [0]
},

// check first parameter to `setHTMLUnsafe()`
setHTMLUnsafe: {
properties: [0]
}
};

Expand Down
17 changes: 16 additions & 1 deletion tests/rules/method.js
Expand Up @@ -378,7 +378,12 @@ eslintTester.run("method", rule, {
// #214: We also allow *harmful* parameters.
code: "foo.insertAdjacentHTML(wrongParamCount);",
parserOptions: {ecmaVersion: 2020 },
}
},
{

// # 232: disallow setHTMLUnsafe, but OK with static string.
code: "foo.setHTMLUnsafe('static string')",
},
],

// Examples of code that should trigger the rule
Expand Down Expand Up @@ -973,5 +978,15 @@ eslintTester.run("method", rule, {
}
],
},
{
code: "foo.setHTMLUnsafe(badness)",
errors: [
{
message: /Unsafe call to foo.setHTMLUnsafe for argument 0/,
type: "CallExpression",
},
],
},

]
});

0 comments on commit 64cbbae

Please sign in to comment.