Skip to content
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

fix: Respect src attribute for script tags and include module type for checks #70

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 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.