1- import { extend } from 'extend2 ' ;
1+ import extend from 'extend ' ;
22import type { Context , Next } from '@eggjs/core' ;
3- import * as utils from '../utils.js' ;
3+ import { checkIfIgnore } from '../utils.js' ;
44import type { SecurityConfig } from '../../types.js' ;
55
66const HEADER = [
@@ -23,10 +23,9 @@ export default (options: SecurityConfig['csp']) => {
2323 ...options ,
2424 ...ctx . securityOptions . csp ,
2525 } ;
26- if ( utils . checkIfIgnore ( opts , ctx ) ) return ;
26+ if ( checkIfIgnore ( opts , ctx ) ) return ;
2727
2828 let finalHeader ;
29- let value ;
3029 const matchedOption = extend ( true , { } , opts . policy ) ;
3130 const bufArray = [ ] ;
3231
@@ -38,30 +37,30 @@ export default (options: SecurityConfig['csp']) => {
3837 }
3938
4039 for ( const key in matchedOption ) {
41- value = matchedOption [ key ] ;
42- value = Array . isArray ( value ) ? value : [ value ] ;
43-
40+ const value = matchedOption [ key ] ;
4441 // Other arrays are splitted into strings EXCEPT `sandbox`
45- if ( key === 'sandbox' && value [ 0 ] === true ) {
42+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox
43+ if ( key === 'sandbox' && value === true ) {
4644 bufArray . push ( key ) ;
4745 } else {
46+ let values = ( Array . isArray ( value ) ? value : [ value ] ) as string [ ] ;
4847 if ( key === 'script-src' ) {
49- const hasNonce = value . some ( function ( val ) {
48+ const hasNonce = values . some ( function ( val ) {
5049 return val . indexOf ( 'nonce-' ) !== - 1 ;
5150 } ) ;
5251
5352 if ( ! hasNonce ) {
54- value . push ( '\'nonce-' + ctx . nonce + '\'' ) ;
53+ values . push ( '\'nonce-' + ctx . nonce + '\'' ) ;
5554 }
5655 }
5756
58- value = value . map ( function ( d ) {
57+ values = values . map ( function ( d ) {
5958 if ( d . startsWith ( '.' ) ) {
6059 d = '*' + d ;
6160 }
6261 return d ;
6362 } ) ;
64- bufArray . push ( key + ' ' + value . join ( ' ' ) ) ;
63+ bufArray . push ( key + ' ' + values . join ( ' ' ) ) ;
6564 }
6665 }
6766 const headerString = bufArray . join ( ';' ) ;
0 commit comments