2022-03-15 01:42:38 +01:00
|
|
|
import { exec } from "child_process"
|
2024-01-07 17:32:14 +01:00
|
|
|
import { describe, expect, it, test } from "vitest"
|
|
|
|
import { webcrypto } from "node:crypto"
|
2023-08-23 12:50:20 +02:00
|
|
|
import { parse as parse_html } from "node-html-parser"
|
|
|
|
import { readFileSync } from "fs"
|
|
|
|
import ScriptUtils from "../scripts/ScriptUtils"
|
2024-01-07 17:59:10 +01:00
|
|
|
|
2023-11-19 04:38:34 +01:00
|
|
|
function detectInCode(forbidden: string, reason: string) {
|
|
|
|
return wrap(detectInCodeUnwrapped(forbidden, reason))
|
|
|
|
}
|
2024-01-07 17:59:10 +01:00
|
|
|
|
2022-03-15 01:42:38 +01:00
|
|
|
/**
|
|
|
|
*
|
2024-01-07 17:59:10 +01:00
|
|
|
* @param forbidden a GREP-regex. This means that '.' is a wildcard and should be escaped to match a literal dot
|
2022-03-15 01:42:38 +01:00
|
|
|
* @param reason
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-19 04:38:34 +01:00
|
|
|
function detectInCodeUnwrapped(forbidden: string, reason: string): Promise<void> {
|
2024-01-12 23:38:58 +01:00
|
|
|
return new Promise<void>(() => {
|
2023-02-08 01:14:21 +01:00
|
|
|
const excludedDirs = [
|
|
|
|
".git",
|
|
|
|
"node_modules",
|
|
|
|
"dist",
|
|
|
|
".cache",
|
|
|
|
".parcel-cache",
|
|
|
|
"assets",
|
|
|
|
"vendor",
|
|
|
|
".idea/",
|
|
|
|
]
|
2022-03-15 01:42:38 +01:00
|
|
|
|
2023-11-19 04:38:34 +01:00
|
|
|
const command =
|
2024-02-20 13:33:38 +01:00
|
|
|
'grep -n "' +
|
2023-11-19 04:38:34 +01:00
|
|
|
forbidden +
|
2024-02-20 13:33:38 +01:00
|
|
|
'" -r . ' +
|
2023-11-19 04:38:34 +01:00
|
|
|
excludedDirs.map((d) => "--exclude-dir=" + d).join(" ")
|
|
|
|
console.log(command)
|
|
|
|
exec(command, (error, stdout, stderr) => {
|
|
|
|
if (error?.message?.startsWith("Command failed: grep")) {
|
|
|
|
console.warn("Command failed!", error)
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
if (error !== null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
if (stderr !== "") {
|
|
|
|
throw stderr
|
|
|
|
}
|
2022-03-15 01:42:38 +01:00
|
|
|
|
2023-11-19 04:38:34 +01:00
|
|
|
const found = stdout
|
|
|
|
.split("\n")
|
|
|
|
.filter((s) => s !== "")
|
|
|
|
.filter((s) => !s.startsWith("./test/"))
|
|
|
|
if (found.length > 0) {
|
|
|
|
const msg = `Found a '${forbidden}' at \n ${found.join("\n ")}.\n ${reason}`
|
|
|
|
console.error(msg)
|
|
|
|
console.error(found.length, "issues found")
|
|
|
|
throw msg
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2023-11-19 04:38:34 +01:00
|
|
|
})
|
2023-05-16 01:34:57 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-01 14:32:45 +02:00
|
|
|
function wrap(promise: Promise<void>): (done: () => void) => void {
|
|
|
|
return (done) => {
|
2023-05-16 01:34:57 +02:00
|
|
|
promise.then(done)
|
2023-06-01 14:32:45 +02:00
|
|
|
}
|
2023-05-16 01:34:57 +02:00
|
|
|
}
|
2024-01-07 17:59:10 +01:00
|
|
|
|
2024-01-07 17:32:14 +01:00
|
|
|
function _arrayBufferToBase64(buffer) {
|
|
|
|
var binary = ""
|
|
|
|
var bytes = new Uint8Array(buffer)
|
|
|
|
var len = bytes.byteLength
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
binary += String.fromCharCode(bytes[i])
|
|
|
|
}
|
|
|
|
return btoa(binary)
|
|
|
|
}
|
2024-01-07 17:59:10 +01:00
|
|
|
|
2024-01-13 02:09:42 +01:00
|
|
|
const cachedHashes: Record<string, string> = {}
|
|
|
|
|
2024-01-07 17:32:14 +01:00
|
|
|
async function validateScriptIntegrityOf(path: string): Promise<void> {
|
2023-08-23 12:50:20 +02:00
|
|
|
const htmlContents = readFileSync(path, "utf8")
|
|
|
|
const doc = parse_html(htmlContents)
|
|
|
|
// @ts-ignore
|
|
|
|
const scripts = Array.from(doc.getElementsByTagName("script"))
|
2024-01-11 05:37:25 +01:00
|
|
|
// Maps source URL onto hash
|
2024-01-12 23:38:58 +01:00
|
|
|
const failed = new Set<string>()
|
2023-08-23 12:50:20 +02:00
|
|
|
for (const script of scripts) {
|
2024-01-07 17:32:14 +01:00
|
|
|
let src = script.getAttribute("src")
|
2023-08-23 12:50:20 +02:00
|
|
|
if (src === undefined) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (src.startsWith("./")) {
|
|
|
|
// Local script - no check needed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const integrity = script.getAttribute("integrity")
|
|
|
|
const ctx = "Script with source " + src + " in file " + path
|
|
|
|
if (integrity === undefined) {
|
|
|
|
throw new Error(ctx + " has no integrity value")
|
|
|
|
}
|
|
|
|
const crossorigin = script.getAttribute("crossorigin")
|
|
|
|
if (crossorigin !== "anonymous") {
|
|
|
|
throw new Error(ctx + " has crossorigin missing or not set to 'anonymous'")
|
|
|
|
}
|
2024-01-07 17:32:14 +01:00
|
|
|
if (src.startsWith("//")) {
|
|
|
|
src = "https:" + src
|
|
|
|
}
|
2024-01-11 05:37:25 +01:00
|
|
|
if (cachedHashes[src] === undefined) {
|
|
|
|
// Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks
|
|
|
|
const data: string = (await ScriptUtils.Download(src))["content"]
|
|
|
|
const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
|
|
|
|
cachedHashes[src] = _arrayBufferToBase64(hashed)
|
|
|
|
}
|
2024-01-12 23:38:58 +01:00
|
|
|
const hashedStr = cachedHashes[src]
|
|
|
|
|
|
|
|
const expected = "sha384-" + hashedStr
|
|
|
|
if (expected !== integrity) {
|
2024-02-20 13:33:38 +01:00
|
|
|
const msg =
|
|
|
|
"Loading a script from '" +
|
|
|
|
src +
|
|
|
|
"' in the file " +
|
|
|
|
path +
|
|
|
|
" has a mismatched checksum: expected " +
|
|
|
|
expected +
|
|
|
|
" but the HTML-file contains " +
|
|
|
|
integrity
|
2024-01-12 23:38:58 +01:00
|
|
|
failed.add(msg)
|
|
|
|
console.warn(msg)
|
|
|
|
}
|
2023-08-23 12:50:20 +02:00
|
|
|
}
|
2024-01-12 23:38:58 +01:00
|
|
|
expect(Array.from(failed).join("\n")).to.equal("")
|
2023-08-23 12:50:20 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 01:42:38 +01:00
|
|
|
describe("Code quality", () => {
|
2023-11-19 04:38:34 +01:00
|
|
|
it(
|
2023-02-08 01:14:21 +01:00
|
|
|
"should not contain reverse",
|
2022-03-15 01:42:38 +01:00
|
|
|
detectInCode(
|
|
|
|
"reverse()",
|
2024-02-20 13:33:38 +01:00
|
|
|
"Reverse is stateful and changes the source list. This often causes subtle bugs"
|
|
|
|
)
|
2023-02-08 01:14:21 +01:00
|
|
|
)
|
2022-03-15 01:42:38 +01:00
|
|
|
|
2023-11-19 04:38:34 +01:00
|
|
|
it(
|
2023-02-08 01:14:21 +01:00
|
|
|
"should not contain 'constructor.name'",
|
2024-02-20 13:33:38 +01:00
|
|
|
detectInCode("constructor\\.name", "This is not allowed, as minification does erase names.")
|
2023-02-08 01:14:21 +01:00
|
|
|
)
|
2022-06-28 03:21:18 +02:00
|
|
|
|
2023-11-19 04:38:34 +01:00
|
|
|
it(
|
2023-02-08 01:14:21 +01:00
|
|
|
"should not contain 'innerText'",
|
2022-06-28 03:21:18 +02:00
|
|
|
detectInCode(
|
|
|
|
"innerText",
|
2024-02-20 13:33:38 +01:00
|
|
|
"innerText is not allowed as it is not testable with fakeDom. Use 'textContent' instead."
|
|
|
|
)
|
2023-02-08 01:14:21 +01:00
|
|
|
)
|
2023-08-23 12:50:20 +02:00
|
|
|
|
2024-01-07 17:32:14 +01:00
|
|
|
test("scripts with external sources should have an integrity hash", async () => {
|
2023-08-23 12:50:20 +02:00
|
|
|
const htmlFiles = ScriptUtils.readDirRecSync(".", 1).filter((f) => f.endsWith(".html"))
|
|
|
|
for (const htmlFile of htmlFiles) {
|
2024-01-07 17:32:14 +01:00
|
|
|
await validateScriptIntegrityOf(htmlFile)
|
2023-08-23 12:50:20 +02:00
|
|
|
}
|
|
|
|
})
|
2023-06-14 20:39:36 +02:00
|
|
|
/*
|
2023-08-23 12:50:20 +02:00
|
|
|
itAsync(
|
|
|
|
"should not contain 'import * as name from \"xyz.json\"'",
|
|
|
|
detectInCode(
|
|
|
|
'import \\* as [a-zA-Z0-9_]\\+ from \\"[.-_/a-zA-Z0-9]\\+\\.json\\"',
|
|
|
|
"With vite, json files have a default export. Use import name from file.json instead"
|
|
|
|
)
|
|
|
|
)
|
2023-05-24 02:21:14 +02:00
|
|
|
/*
|
2023-08-23 12:50:20 +02:00
|
|
|
itAsync(
|
|
|
|
"should not contain '[\"default\"]'",
|
|
|
|
detectInCode('\\[\\"default\\"\\]', "Possible leftover of faulty default import")
|
|
|
|
)*/
|
2022-03-15 01:42:38 +01:00
|
|
|
})
|