forked from MapComplete/MapComplete
Merge develop
This commit is contained in:
parent
639253d971
commit
310c41fd16
1 changed files with 18 additions and 13 deletions
|
@ -16,7 +16,7 @@ function detectInCode(forbidden: string, reason: string) {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function detectInCodeUnwrapped(forbidden: string, reason: string): Promise<void> {
|
function detectInCodeUnwrapped(forbidden: string, reason: string): Promise<void> {
|
||||||
return new Promise<void>((done) => {
|
return new Promise<void>(() => {
|
||||||
const excludedDirs = [
|
const excludedDirs = [
|
||||||
".git",
|
".git",
|
||||||
"node_modules",
|
"node_modules",
|
||||||
|
@ -29,9 +29,9 @@ function detectInCodeUnwrapped(forbidden: string, reason: string): Promise<void>
|
||||||
]
|
]
|
||||||
|
|
||||||
const command =
|
const command =
|
||||||
'grep -n "' +
|
"grep -n \"" +
|
||||||
forbidden +
|
forbidden +
|
||||||
'" -r . ' +
|
"\" -r . " +
|
||||||
excludedDirs.map((d) => "--exclude-dir=" + d).join(" ")
|
excludedDirs.map((d) => "--exclude-dir=" + d).join(" ")
|
||||||
console.log(command)
|
console.log(command)
|
||||||
exec(command, (error, stdout, stderr) => {
|
exec(command, (error, stdout, stderr) => {
|
||||||
|
@ -83,6 +83,7 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
|
||||||
const scripts = Array.from(doc.getElementsByTagName("script"))
|
const scripts = Array.from(doc.getElementsByTagName("script"))
|
||||||
// Maps source URL onto hash
|
// Maps source URL onto hash
|
||||||
const cachedHashes: Record<string, string> = {}
|
const cachedHashes: Record<string, string> = {}
|
||||||
|
const failed = new Set<string>()
|
||||||
for (const script of scripts) {
|
for (const script of scripts) {
|
||||||
let src = script.getAttribute("src")
|
let src = script.getAttribute("src")
|
||||||
if (src === undefined) {
|
if (src === undefined) {
|
||||||
|
@ -110,12 +111,16 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
|
||||||
const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
|
const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
|
||||||
cachedHashes[src] = _arrayBufferToBase64(hashed)
|
cachedHashes[src] = _arrayBufferToBase64(hashed)
|
||||||
}
|
}
|
||||||
console.log(src, cachedHashes[src], integrity)
|
const hashedStr = cachedHashes[src]
|
||||||
expect(integrity).to.equal(
|
|
||||||
"sha384-" + cachedHashes[src],
|
const expected = "sha384-" + hashedStr
|
||||||
"Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum"
|
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", () => {
|
describe("Code quality", () => {
|
||||||
|
@ -123,21 +128,21 @@ describe("Code quality", () => {
|
||||||
"should not contain reverse",
|
"should not contain reverse",
|
||||||
detectInCode(
|
detectInCode(
|
||||||
"reverse()",
|
"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(
|
it(
|
||||||
"should not contain 'constructor.name'",
|
"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(
|
it(
|
||||||
"should not contain 'innerText'",
|
"should not contain 'innerText'",
|
||||||
detectInCode(
|
detectInCode(
|
||||||
"innerText",
|
"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 () => {
|
test("scripts with external sources should have an integrity hash", async () => {
|
||||||
|
|
Loading…
Reference in a new issue