Commit 54205175 authored by tom's avatar tom

fix prod build

parent fb6694f6
import _mergeWith from 'lodash/mergeWith';
import * as descriptors from './policies';
import { makePolicyString } from './utils';
const concat = (one: Array<unknown> | undefined, two: Array<unknown> | undefined) =>
one ? one.concat(two || []) : two;
import { makePolicyString, mergeDescriptors } from './utils';
function generateCspPolicy() {
const policyDescriptor = _mergeWith(
_mergeWith(
const policyDescriptor = mergeDescriptors(
descriptors.app(),
descriptors.ad(),
descriptors.googleAnalytics(),
descriptors.googleFonts(),
concat,
),
_mergeWith(
descriptors.googleReCaptcha(),
descriptors.sentry(),
descriptors.walletConnect(),
concat,
),
concat,
);
return makePolicyString(policyDescriptor);
......
......@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import isSelfHosted from 'lib/isSelfHosted';
export default function generateAdDescriptor(): CspDev.DirectiveDescriptor {
export function ad(): CspDev.DirectiveDescriptor {
if (!isSelfHosted()) {
return {};
}
......
......@@ -19,7 +19,7 @@ function getMarketplaceAppsHosts() {
};
}
export default function generateAppDescriptor(): CspDev.DirectiveDescriptor {
export function app(): CspDev.DirectiveDescriptor {
const marketplaceAppsHosts = getMarketplaceAppsHosts();
return {
......
......@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config';
export default function generateGoogleAnalyticsDescriptor(): CspDev.DirectiveDescriptor {
export function googleAnalytics(): CspDev.DirectiveDescriptor {
if (!appConfig.googleAnalytics.propertyId) {
return {};
}
......
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
return {
......
......@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config';
export default function generateGoogleReCaptchaDescriptor(): CspDev.DirectiveDescriptor {
export function googleReCaptcha(): CspDev.DirectiveDescriptor {
if (!appConfig.reCaptcha.siteKey) {
return {};
}
......
import ad from './ad';
import app from './app';
import googleAnalytics from './googleAnalytics';
import googleFonts from './googleFonts';
import googleReCaptcha from './googleReCaptcha';
import sentry from './sentry';
import walletConnect from './walletConnect';
export {
ad,
app,
googleAnalytics,
googleFonts,
googleReCaptcha,
sentry,
walletConnect,
};
export { ad } from './ad';
export { app } from './app';
export { googleAnalytics } from './googleAnalytics';
export { googleFonts } from './googleFonts';
export { googleReCaptcha } from './googleReCaptcha';
export { sentry } from './sentry';
export { walletConnect } from './walletConnect';
import type CspDev from 'csp-dev';
export default function generateSentryDescriptor(): CspDev.DirectiveDescriptor {
export function sentry(): CspDev.DirectiveDescriptor {
return {
'connect-src': [
'sentry.io',
......
......@@ -2,7 +2,7 @@ import type CspDev from 'csp-dev';
import appConfig from 'configs/app/config';
export default function generateWalletConnectDescriptor(): CspDev.DirectiveDescriptor {
export function walletConnect(): CspDev.DirectiveDescriptor {
if (!appConfig.walletConnect.projectId || !appConfig.network.rpcUrl) {
return {};
}
......
......@@ -11,7 +11,7 @@ export const KEY_WORDS = {
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"
export function unique(array: Array<string | undefined>) {
const set: Record<string, boolean> = {};
......@@ -22,6 +22,27 @@ export function unique(array: Array<string | undefined>) {
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) {
return Object.entries(policyDescriptor)
.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