UNPKG

@strapi/utils

Version:

Shared utilities for the Strapi packages

1 lines 2.68 kB
{"version":3,"file":"security.mjs","sources":["../src/security.ts"],"sourcesContent":["import { mergeWith } from 'lodash/fp';\n\nexport const CSP_DEFAULTS = {\n 'connect-src': [\"'self'\", 'https:'],\n 'img-src': [\"'self'\", 'data:', 'blob:', 'https://market-assets.strapi.io'],\n 'media-src': [\"'self'\", 'data:', 'blob:'],\n} as const;\n\n/**\n * Utility to extend Strapi middleware configuration. Mainly used to extend the CSP directives from the security middleware.\n *\n * @param middlewares - Array of middleware configurations\n * @param middleware - Middleware configuration to merge/add\n * @returns Modified middlewares array with the new configuration merged\n */\nexport const extendMiddlewareConfiguration = (\n middlewares: (string | { name?: string; config?: any })[],\n middleware: { name: string; config?: any }\n) => {\n return middlewares.map((currentMiddleware) => {\n if (typeof currentMiddleware === 'string' && currentMiddleware === middleware.name) {\n // Use the new config object if the middleware has no config property yet\n return middleware;\n }\n\n if (typeof currentMiddleware === 'object' && currentMiddleware.name === middleware.name) {\n // Deep merge (+ concat arrays) the new config with the current middleware config\n return mergeWith(\n (objValue, srcValue) => {\n if (Array.isArray(objValue)) {\n return Array.from(new Set(objValue.concat(srcValue)));\n }\n return undefined;\n },\n currentMiddleware,\n middleware\n );\n }\n\n return currentMiddleware;\n });\n};\n"],"names":["CSP_DEFAULTS","extendMiddlewareConfiguration","middlewares","middleware","map","currentMiddleware","name","mergeWith","objValue","srcValue","Array","isArray","from","Set","concat","undefined"],"mappings":";;MAEaA,YAAe,GAAA;IAC1B,aAAe,EAAA;AAAC,QAAA,QAAA;AAAU,QAAA;AAAS,KAAA;IACnC,SAAW,EAAA;AAAC,QAAA,QAAA;AAAU,QAAA,OAAA;AAAS,QAAA,OAAA;AAAS,QAAA;AAAkC,KAAA;IAC1E,WAAa,EAAA;AAAC,QAAA,QAAA;AAAU,QAAA,OAAA;AAAS,QAAA;AAAQ;AAC3C;AAEA;;;;;;AAMC,IACM,MAAMC,6BAAgC,GAAA,CAC3CC,WACAC,EAAAA,UAAAA,GAAAA;IAEA,OAAOD,WAAAA,CAAYE,GAAG,CAAC,CAACC,iBAAAA,GAAAA;AACtB,QAAA,IAAI,OAAOA,iBAAsB,KAAA,QAAA,IAAYA,iBAAsBF,KAAAA,UAAAA,CAAWG,IAAI,EAAE;;YAElF,OAAOH,UAAAA;AACT;QAEA,IAAI,OAAOE,sBAAsB,QAAYA,IAAAA,iBAAAA,CAAkBC,IAAI,KAAKH,UAAAA,CAAWG,IAAI,EAAE;;YAEvF,OAAOC,SAAAA,CACL,CAACC,QAAUC,EAAAA,QAAAA,GAAAA;gBACT,IAAIC,KAAAA,CAAMC,OAAO,CAACH,QAAW,CAAA,EAAA;AAC3B,oBAAA,OAAOE,MAAME,IAAI,CAAC,IAAIC,GAAIL,CAAAA,QAAAA,CAASM,MAAM,CAACL,QAAAA,CAAAA,CAAAA,CAAAA;AAC5C;gBACA,OAAOM,SAAAA;AACT,aAAA,EACAV,iBACAF,EAAAA,UAAAA,CAAAA;AAEJ;QAEA,OAAOE,iBAAAA;AACT,KAAA,CAAA;AACF;;;;"}