1
+ import { RequestHandler } from "express" ;
1
2
import config from "./config" ;
2
3
import uuid from "uuid" ;
3
4
4
- const CspStrategy : any = { }
5
+
6
+ const CspStrategy : {
7
+ computeDirectives ?: ( ) => Record < string , string [ ] >
8
+ addNonceToLocals ?: RequestHandler
9
+ } = { }
5
10
6
11
const defaultDirectives = {
7
12
defaultSrc : [ '\'self\'' ] ,
@@ -37,7 +42,7 @@ const googleAnalyticsDirectives = {
37
42
}
38
43
39
44
CspStrategy . computeDirectives = function ( ) {
40
- const directives = { }
45
+ const directives : Record < string , string [ ] > = { }
41
46
mergeDirectives ( directives , config . csp . directives )
42
47
mergeDirectivesIf ( config . csp . addDefaults , directives , defaultDirectives )
43
48
mergeDirectivesIf ( config . useCDN , directives , cdnDirectives )
@@ -52,12 +57,14 @@ CspStrategy.computeDirectives = function () {
52
57
return directives
53
58
}
54
59
55
- function mergeDirectives ( existingDirectives , newDirectives ) {
60
+ function mergeDirectives ( existingDirectives : Record < string , string [ ] > , newDirectives : Record < string , string [ ] > ) {
56
61
for ( const propertyName in newDirectives ) {
57
- const newDirective = newDirectives [ propertyName ]
58
- if ( newDirective ) {
59
- const existingDirective = existingDirectives [ propertyName ] || [ ]
60
- existingDirectives [ propertyName ] = existingDirective . concat ( newDirective )
62
+ if ( Object . hasOwnProperty . call ( newDirectives , propertyName ) ) {
63
+ const newDirective = newDirectives [ propertyName ]
64
+ if ( newDirective ) {
65
+ const existingDirective = existingDirectives [ propertyName ] || [ ]
66
+ existingDirectives [ propertyName ] = existingDirective . concat ( newDirective )
67
+ }
61
68
}
62
69
}
63
70
}
0 commit comments