mirror of https://github.com/status-im/codimd.git
lint: lib/csp.ts
add typing annotate Signed-off-by: Raccoon <raccoon@hackmd.io>
This commit is contained in:
parent
7429d5eaae
commit
90ab2d0979
13
lib/csp.ts
13
lib/csp.ts
|
@ -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,8 +57,9 @@ CspStrategy.computeDirectives = function () {
|
|||
return directives
|
||||
}
|
||||
|
||||
function mergeDirectives(existingDirectives, newDirectives) {
|
||||
function mergeDirectives(existingDirectives: Record<string, string[]>, newDirectives: Record<string, string[]>) {
|
||||
for (const propertyName in newDirectives) {
|
||||
if (Object.hasOwnProperty.call(newDirectives, propertyName)) {
|
||||
const newDirective = newDirectives[propertyName]
|
||||
if (newDirective) {
|
||||
const existingDirective = existingDirectives[propertyName] || []
|
||||
|
@ -61,6 +67,7 @@ function mergeDirectives(existingDirectives, newDirectives) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function mergeDirectivesIf(condition, existingDirectives, newDirectives) {
|
||||
if (condition) {
|
||||
|
|
Loading…
Reference in New Issue