Commit 54205175 authored by tom's avatar tom

fix prod build

parent fb6694f6
import _mergeWith from 'lodash/mergeWith';
import * as descriptors from './policies'; import * as descriptors from './policies';
import { makePolicyString } from './utils'; import { makePolicyString, mergeDescriptors } from './utils';
const concat = (one: Array<unknown> | undefined, two: Array<unknown> | undefined) =>
one ? one.concat(two || []) : two;
function generateCspPolicy() { function generateCspPolicy() {
const policyDescriptor = _mergeWith( const policyDescriptor = mergeDescriptors(
_mergeWith( descriptors.app(),
descriptors.app(), descriptors.ad(),
descriptors.ad(), descriptors.googleAnalytics(),
descriptors.googleAnalytics(), descriptors.googleFonts(),
descriptors.googleFonts(), descriptors.googleReCaptcha(),
concat, descriptors.sentry(),
), descriptors.walletConnect(),
_mergeWith(
descriptors.googleReCaptcha(),
descriptors.sentry(),
descriptors.walletConnect(),
concat,
),
concat,
); );
return makePolicyString(policyDescriptor); return makePolicyString(policyDescriptor);
......
...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev'; ...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import isSelfHosted from 'lib/isSelfHosted'; import isSelfHosted from 'lib/isSelfHosted';
export default function generateAdDescriptor(): CspDev.DirectiveDescriptor { export function ad(): CspDev.DirectiveDescriptor {
if (!isSelfHosted()) { if (!isSelfHosted()) {
return {}; return {};
} }
......
...@@ -19,7 +19,7 @@ function getMarketplaceAppsHosts() { ...@@ -19,7 +19,7 @@ function getMarketplaceAppsHosts() {
}; };
} }
export default function generateAppDescriptor(): CspDev.DirectiveDescriptor { export function app(): CspDev.DirectiveDescriptor {
const marketplaceAppsHosts = getMarketplaceAppsHosts(); const marketplaceAppsHosts = getMarketplaceAppsHosts();
return { return {
......
...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev'; ...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config'; import appConfig from 'configs/app/config';
export default function generateGoogleAnalyticsDescriptor(): CspDev.DirectiveDescriptor { export function googleAnalytics(): CspDev.DirectiveDescriptor {
if (!appConfig.googleAnalytics.propertyId) { if (!appConfig.googleAnalytics.propertyId) {
return {}; return {};
} }
......
import type CspDev from 'csp-dev'; import type CspDev from 'csp-dev';
export default function generateGoogleFontsDescriptor(): CspDev.DirectiveDescriptor { export function googleFonts(): CspDev.DirectiveDescriptor {
// we use Inter and Poppins in the app // we use Inter and Poppins in the app
return { return {
......
...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev'; ...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config'; import appConfig from 'configs/app/config';
export default function generateGoogleReCaptchaDescriptor(): CspDev.DirectiveDescriptor { export function googleReCaptcha(): CspDev.DirectiveDescriptor {
if (!appConfig.reCaptcha.siteKey) { if (!appConfig.reCaptcha.siteKey) {
return {}; return {};
} }
......
import ad from './ad'; export { ad } from './ad';
import app from './app'; export { app } from './app';
import googleAnalytics from './googleAnalytics'; export { googleAnalytics } from './googleAnalytics';
import googleFonts from './googleFonts'; export { googleFonts } from './googleFonts';
import googleReCaptcha from './googleReCaptcha'; export { googleReCaptcha } from './googleReCaptcha';
import sentry from './sentry'; export { sentry } from './sentry';
import walletConnect from './walletConnect'; export { walletConnect } from './walletConnect';
export {
ad,
app,
googleAnalytics,
googleFonts,
googleReCaptcha,
sentry,
walletConnect,
};
import type CspDev from 'csp-dev'; import type CspDev from 'csp-dev';
export default function generateSentryDescriptor(): CspDev.DirectiveDescriptor { export function sentry(): CspDev.DirectiveDescriptor {
return { return {
'connect-src': [ 'connect-src': [
'sentry.io', 'sentry.io',
......
...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev'; ...@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config'; import appConfig from 'configs/app/config';
export default function generateWalletConnectDescriptor(): CspDev.DirectiveDescriptor { export function walletConnect(): CspDev.DirectiveDescriptor {
if (!appConfig.walletConnect.projectId || !appConfig.network.rpcUrl) { if (!appConfig.walletConnect.projectId || !appConfig.network.rpcUrl) {
return {}; return {};
} }
......
...@@ -11,7 +11,7 @@ export const KEY_WORDS = { ...@@ -11,7 +11,7 @@ export const KEY_WORDS = {
UNSAFE_EVAL: '\'unsafe-eval\'', UNSAFE_EVAL: '\'unsafe-eval\'',
}; };
// we cannot use lodash/uniq in middleware code since it calls new Set() and it'is causing an error in Nextjs // we cannot use lodash/uniq and lodash/mergeWith in middleware code since it calls new Set() and it'is causing an error in Next.js
// "Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime" // "Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime"
export function unique(array: Array<string | undefined>) { export function unique(array: Array<string | undefined>) {
const set: Record<string, boolean> = {}; const set: Record<string, boolean> = {};
...@@ -22,6 +22,27 @@ export function unique(array: Array<string | undefined>) { ...@@ -22,6 +22,27 @@ export function unique(array: Array<string | undefined>) {
return Object.keys(set); return Object.keys(set);
} }
export function mergeDescriptors(...descriptors: Array<CspDev.DirectiveDescriptor>) {
return descriptors.reduce((result, item) => {
for (const _key in item) {
const key = _key as CspDev.Directive;
const value = item[key];
if (!value) {
continue;
}
if (result[key]) {
result[key]?.push(...value);
} else {
result[key] = [ ...value ];
}
}
return result;
}, {} as CspDev.DirectiveDescriptor);
}
export function makePolicyString(policyDescriptor: CspDev.DirectiveDescriptor) { export function makePolicyString(policyDescriptor: CspDev.DirectiveDescriptor) {
return Object.entries(policyDescriptor) return Object.entries(policyDescriptor)
.map(([ key, value ]) => { .map(([ key, value ]) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment