From 310c41fd162fb70a262f7ce0b83375af1dc0f97b Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 12 Jan 2024 23:38:58 +0100 Subject: [PATCH] Merge develop --- test/CodeQuality.spec.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/test/CodeQuality.spec.ts b/test/CodeQuality.spec.ts index 522d99b5b..719b9cca6 100644 --- a/test/CodeQuality.spec.ts +++ b/test/CodeQuality.spec.ts @@ -16,7 +16,7 @@ function detectInCode(forbidden: string, reason: string) { * @private */ function detectInCodeUnwrapped(forbidden: string, reason: string): Promise { - return new Promise((done) => { + return new Promise(() => { const excludedDirs = [ ".git", "node_modules", @@ -29,9 +29,9 @@ function detectInCodeUnwrapped(forbidden: string, reason: string): Promise ] const command = - 'grep -n "' + + "grep -n \"" + forbidden + - '" -r . ' + + "\" -r . " + excludedDirs.map((d) => "--exclude-dir=" + d).join(" ") console.log(command) exec(command, (error, stdout, stderr) => { @@ -83,6 +83,7 @@ async function validateScriptIntegrityOf(path: string): Promise { const scripts = Array.from(doc.getElementsByTagName("script")) // Maps source URL onto hash const cachedHashes: Record = {} + const failed = new Set() for (const script of scripts) { let src = script.getAttribute("src") if (src === undefined) { @@ -110,12 +111,16 @@ async function validateScriptIntegrityOf(path: string): Promise { const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data)) cachedHashes[src] = _arrayBufferToBase64(hashed) } - console.log(src, cachedHashes[src], integrity) - expect(integrity).to.equal( - "sha384-" + cachedHashes[src], - "Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum" - ) + const hashedStr = cachedHashes[src] + + const expected = "sha384-" + hashedStr + if (expected !== integrity) { + const msg = "Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum: expected " + expected + " but the HTML-file contains " + integrity + failed.add(msg) + console.warn(msg) + } } + expect(Array.from(failed).join("\n")).to.equal("") } describe("Code quality", () => { @@ -123,21 +128,21 @@ describe("Code quality", () => { "should not contain reverse", detectInCode( "reverse()", - "Reverse is stateful and changes the source list. This often causes subtle bugs" - ) + "Reverse is stateful and changes the source list. This often causes subtle bugs", + ), ) it( "should not contain 'constructor.name'", - detectInCode("constructor\\.name", "This is not allowed, as minification does erase names.") + detectInCode("constructor\\.name", "This is not allowed, as minification does erase names."), ) it( "should not contain 'innerText'", detectInCode( "innerText", - "innerText is not allowed as it is not testable with fakeDom. Use 'textContent' instead." - ) + "innerText is not allowed as it is not testable with fakeDom. Use 'textContent' instead.", + ), ) test("scripts with external sources should have an integrity hash", async () => {