Skip to content

Commit

Permalink
fix: Respect src attribute for script tags and include module type fo…
Browse files Browse the repository at this point in the history
…r checks (#70)

This change addresses the following comments:
#48 (comment)
  • Loading branch information
d3xter666 authored Apr 12, 2024
1 parent 70b719a commit 2b28e5f
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/detectors/transpilers/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export async function extractJSScriptTags(contentStream: ReadStream) {
return attr.name.value !== "type" ||
(attr.name.value === "type" &&
["",
"module",
"text/javascript",
"application/javascript", /* legacy */
].includes(attr.value.value.toLowerCase()));
Expand Down
7 changes: 5 additions & 2 deletions src/linter/html/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export async function lintHtml(resourceName: string, contentStream: ReadStream):
const jsScriptTags = await extractJSScriptTags(contentStream);

jsScriptTags.forEach((tag) => {
const scriptContent = tag.textNodes?.map((tNode) => tNode.value).join("").trim();
// Tags with src attribute do not parse and run inline code
const hasSrc = tag.attributes.some((attr) => {
return attr.name.value.toLowerCase() === "src";
});

if (scriptContent) {
if (!hasSrc && tag.textNodes?.length > 0) {
report.addMessage({
node: tag,
severity: LintMessageSeverity.Warning,
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/linter/rules/CSPCompliance/NoInlineJS.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
});
sap.ui.xmlview({ viewContent: jQuery('#myXml').html() }).placeAt("content");
</script>

<script type="module">
import { log } from "utils";

log("Exporting dog names.");

export const names = ["Kayla", "Bentley", "Gilligan"];
</script>

<script>
// This one should be reported!
</script>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@
</mvc:View>
</script>

<script type="module">
<script src="">
sap.ui.controller("my.own.controller", {
doSomething: function () {
alert("Hello World!");
}
});
</script>

<script src>
console.log("this code won't run");
</script>

<script type="" src="./path/to/js.js">
// should not be reported as it is not a CSP violation
</script>

<script type="module" src>
import { log } from "utils";

log("Exporting dog names.");
Expand Down
Binary file modified test/lib/detectors/transpilers/xml/snapshots/transpiler.ts.snap
Binary file not shown.
20 changes: 19 additions & 1 deletion test/lib/linter/rules/snapshots/CSPCompliance.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,26 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'ui5-linter-csp-unsafe-inline-script',
severity: 1,
},
{
column: 2,
fatal: undefined,
line: 47,
message: 'Use of unsafe inline script',
messageDetails: 'Content Security Policy (https://ui5.sap.com/1.120/#/topic/fe1a6dba940e479fb7c3bc753f92b28c)',
ruleId: 'ui5-linter-csp-unsafe-inline-script',
severity: 1,
},
{
column: 2,
fatal: undefined,
line: 55,
message: 'Use of unsafe inline script',
messageDetails: 'Content Security Policy (https://ui5.sap.com/1.120/#/topic/fe1a6dba940e479fb7c3bc753f92b28c)',
ruleId: 'ui5-linter-csp-unsafe-inline-script',
severity: 1,
},
],
warningCount: 4,
warningCount: 6,
},
]

Expand Down
Binary file modified test/lib/linter/rules/snapshots/CSPCompliance.ts.snap
Binary file not shown.
Binary file modified test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap
Binary file not shown.
Binary file modified test/lib/linter/rules/snapshots/NoGlobals.ts.snap
Binary file not shown.
Binary file modified test/lib/linter/snapshots/linter.ts.snap
Binary file not shown.

0 comments on commit 2b28e5f

Please sign in to comment.