Skip to content

Commit f338479

Browse files
committed
fix(next): add missing connect csp
Because: - Some customers are experiencing CSP errors related to connect-src missing some values. This commit: - Adds auth-server and profile-server env vars to connect-src csp. - Adds logic for Security Policy Reporting to Sentry Closes #PAY-3483
1 parent 79ad93d commit f338479

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

apps/payments/next/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ GLEAN_CLIENT_CONFIG__CHANNEL='development'
126126
# CSP Config
127127
CSP__ACCOUNTS_STATIC_CDN=https://cdn.accounts.firefox.com
128128
CSP__PAYPAL_API='https://www.sandbox.paypal.com'
129+
CSP__SENTRY_REPORT_URI=
129130

130131
# Sentry Config
131132
SENTRY__DSN=

apps/payments/next/config/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class CspConfig {
2727

2828
@IsUrl()
2929
paypalApi!: string;
30+
31+
@IsOptional()
32+
@IsString()
33+
sentryReportUri?: string;
3034
}
3135

3236
class PaypalConfig {

apps/payments/next/middleware.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ export function middleware(request: NextRequest) {
2121
const accountsStaticCdn = process.env.CSP__ACCOUNTS_STATIC_CDN;
2222
const PAYPAL_SCRIPT_URL = '*.paypal.com';
2323
const PAYPAL_OBJECTS = '*.paypalobjects.com';
24+
const AUTH_SERVER_URL = process.env.AUTH__ISSUER_URL;
2425
const PROFILE_CLIENT_URL = process.env.PROFILE_CLIENT_CONFIG__URL;
2526
const PROFILE_DEFAULT_IMAGES_URL = process.env.PROFILE_DEFAULT_IMAGES_URL;
2627
const PROFILE_UPLOADED_IMAGES_URL = process.env.PROFILE_UPLOADED_IMAGES_URL;
2728
const FEATURE_FLAG_SUB_MANAGE = process.env.FEATURE_FLAG_SUB_MANAGE;
2829
const CONTENT_SERVER_URL = process.env.CONTENT_SERVER_CLIENT_CONFIG__URL;
30+
const SENTRY_SERVER = 'https://*.sentry.io';
31+
const SENTRY_CSP_ENDPOINT = process.env.CSP__SENTRY_REPORT_URI ?? '';
2932

3033
if (!FEATURE_FLAG_SUB_MANAGE) {
3134
const pathSections = request.nextUrl.pathname.split('/').filter(Boolean);
@@ -43,8 +46,8 @@ export function middleware(request: NextRequest) {
4346
const cspHeader = `
4447
base-uri 'self';
4548
child-src 'self' ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS};
46-
connect-src 'self' https://api.stripe.com ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS};
47-
default-src 'self';
49+
connect-src 'self' ${AUTH_SERVER_URL} ${PROFILE_CLIENT_URL} https://api.stripe.com ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS} ${SENTRY_SERVER};
50+
default-src 'self' ${SENTRY_SERVER};
4851
font-src 'self';
4952
frame-ancestors 'none';
5053
frame-src https://*.js.stripe.com https://js.stripe.com https://hooks.stripe.com ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS};
@@ -55,6 +58,8 @@ export function middleware(request: NextRequest) {
5558
} https://*.js.stripe.com https://js.stripe.com ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS};
5659
style-src 'self' 'unsafe-hashes' 'sha256-0hAheEzaMe6uXIKV4EehS9pu1am1lj/KnnzrOYqckXk=' 'sha256-GsQC5AaXpdCaKTyWbxBzn7nitfp0Otwn7I/zu0rUKOs=' 'sha256-zlqnbDt84zf1iSefLU/ImC54isoprH/MRiVZGskwexk=' ${PAYPAL_SCRIPT_URL} ${PAYPAL_OBJECTS} 'nonce-${nonce}';
5760
upgrade-insecure-requests;
61+
${SENTRY_CSP_ENDPOINT ? `report-uri ${SENTRY_CSP_ENDPOINT};` : ''}
62+
${SENTRY_CSP_ENDPOINT ? 'report-to csp-endpoint;' : ''}
5863
`;
5964
// Replace newline characters and spaces
6065
const contentSecurityPolicyHeaderValue = cspHeader
@@ -95,6 +100,21 @@ export function middleware(request: NextRequest) {
95100
path: '/',
96101
});
97102

103+
if (SENTRY_CSP_ENDPOINT) {
104+
response.headers.set(
105+
'Report-To',
106+
JSON.stringify({
107+
group: 'csp-endpoint',
108+
max_age: 10886400,
109+
endpoints: [{ url: SENTRY_CSP_ENDPOINT }],
110+
})
111+
);
112+
response.headers.set(
113+
'Reporting-Endpoints',
114+
`csp-endpoint=${SENTRY_CSP_ENDPOINT}`
115+
);
116+
}
117+
98118
return response;
99119
}
100120

0 commit comments

Comments
 (0)