lint: lib/csp.ts

add typing annotate

Signed-off-by: Raccoon <raccoon@hackmd.io>
This commit is contained in:
Raccoon 2021-06-13 15:53:48 +08:00
parent 7429d5eaae
commit 90ab2d0979
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38
1 changed files with 14 additions and 7 deletions

View File

@ -1,7 +1,12 @@
import {RequestHandler} from "express";
import config from "./config";
import uuid from "uuid";
const CspStrategy: any = {}
const CspStrategy: {
computeDirectives?: () => Record<string, string[]>
addNonceToLocals?: RequestHandler
} = {}
const defaultDirectives = {
defaultSrc: ['\'self\''],
@ -37,7 +42,7 @@ const googleAnalyticsDirectives = {
}
CspStrategy.computeDirectives = function () {
const directives = {}
const directives: Record<string, string[]> = {}
mergeDirectives(directives, config.csp.directives)
mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives)
mergeDirectivesIf(config.useCDN, directives, cdnDirectives)
@ -52,12 +57,14 @@ CspStrategy.computeDirectives = function () {
return directives
}
function mergeDirectives(existingDirectives, newDirectives) {
function mergeDirectives(existingDirectives: Record<string, string[]>, newDirectives: Record<string, string[]>) {
for (const propertyName in newDirectives) {
const newDirective = newDirectives[propertyName]
if (newDirective) {
const existingDirective = existingDirectives[propertyName] || []
existingDirectives[propertyName] = existingDirective.concat(newDirective)
if (Object.hasOwnProperty.call(newDirectives, propertyName)) {
const newDirective = newDirectives[propertyName]
if (newDirective) {
const existingDirective = existingDirectives[propertyName] || []
existingDirectives[propertyName] = existingDirective.concat(newDirective)
}
}
}
}