forked from veridatum-labs/earnproof-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-headers.ts
More file actions
147 lines (130 loc) · 4.38 KB
/
Copy pathsecurity-headers.ts
File metadata and controls
147 lines (130 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
loadPublicEnv,
resolveDeploymentProfile,
type EnvLike,
type PublicEnv,
} from "../lib/validation/env";
export type SecurityHeader = { key: string; value: string };
export type SecurityPolicy = {
profile: ReturnType<typeof resolveDeploymentProfile>;
env: PublicEnv;
headers: SecurityHeader[];
csp: string;
connectSrc: string[];
scriptSrc: string[];
};
export type BuildSecurityPolicyOptions = {
env?: EnvLike;
nonce?: string;
};
const CSP_SEPARATOR = "; ";
function originOf(url: string): string {
return new URL(url).origin;
}
function unique(values: Array<string | undefined>): string[] {
return [...new Set(values.filter((value): value is string => Boolean(value)))];
}
function cspValue(parts: string[]): string {
return parts.filter(Boolean).join(" ");
}
/**
* Build the browser security policy for a deployment profile.
*
* Script and style sources use a per-request nonce. `'unsafe-inline'` and
* `*` wildcards are not emitted. See SECURITY.md for the nonce justification
* (Next.js requires a nonce to attach to its own runtime scripts).
*/
export function buildSecurityPolicy(
options: BuildSecurityPolicyOptions = {},
): SecurityPolicy {
const envSource = options.env ?? process.env;
const env = loadPublicEnv(envSource);
const profile = resolveDeploymentProfile(envSource);
const appOrigin = originOf(env.NEXT_PUBLIC_APP_URL);
const apiOrigin = originOf(env.NEXT_PUBLIC_API_URL);
const horizonOrigin = originOf(env.NEXT_PUBLIC_STELLAR_HORIZON_URL);
const vitalsOrigin = env.NEXT_PUBLIC_WEB_VITALS_ENDPOINT
? originOf(env.NEXT_PUBLIC_WEB_VITALS_ENDPOINT)
: undefined;
const nonce = options.nonce;
const nonceSource = nonce ? `'nonce-${nonce}'` : undefined;
// `'strict-dynamic'` lets the nonce-bearing Next.js runtime load its own
// scripts. It is not a host wildcard and does not allow attacker-supplied
// inline event handlers.
const scriptSrc = unique(["'self'", nonceSource, nonce ? "'strict-dynamic'" : undefined]);
const styleSrc = unique(["'self'", nonceSource]);
const connectSrc = unique([
"'self'",
appOrigin,
apiOrigin,
horizonOrigin,
vitalsOrigin,
]);
const isHttpsApp = env.NEXT_PUBLIC_APP_URL.startsWith("https://");
const directives = [
`default-src ${cspValue(["'self'"])}`,
`script-src ${cspValue(scriptSrc)}`,
`style-src ${cspValue(styleSrc)}`,
`img-src ${cspValue(["'self'", "data:", "blob:"])}`,
`font-src ${cspValue(["'self'"])}`,
`connect-src ${cspValue(connectSrc)}`,
`media-src ${cspValue(["'self'", "blob:"])}`,
`worker-src ${cspValue(["'self'", "blob:"])}`,
`manifest-src ${cspValue(["'self'"])}`,
`object-src ${cspValue(["'none'"])}`,
`base-uri ${cspValue(["'self'"])}`,
`form-action ${cspValue(["'self'"])}`,
`frame-src ${cspValue(["'none'"])}`,
`frame-ancestors ${cspValue(["'none'"])}`,
`child-src ${cspValue(["'none'"])}`,
isHttpsApp ? "upgrade-insecure-requests" : "",
].filter(Boolean);
const csp = directives.join(CSP_SEPARATOR);
const headers: SecurityHeader[] = [
{ key: "Content-Security-Policy", value: csp },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
{
key: "Permissions-Policy",
value: [
"camera=(self)",
"microphone=()",
"geolocation=()",
"payment=()",
"usb=()",
"interest-cohort=()",
].join(", "),
},
{ key: "X-DNS-Prefetch-Control", value: "off" },
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
{ key: "Cross-Origin-Resource-Policy", value: "same-origin" },
];
if (isHttpsApp) {
headers.push({
key: "Strict-Transport-Security",
value: "max-age=63072000; includeSubDomains; preload",
});
}
return {
profile,
env,
headers,
csp,
connectSrc,
scriptSrc,
};
}
export function nextHeaderList(policy: SecurityPolicy): Array<{ key: string; value: string }> {
return policy.headers;
}
export function documentedConnectOrigins(env: PublicEnv): string[] {
return unique([
new URL(env.NEXT_PUBLIC_APP_URL).origin,
new URL(env.NEXT_PUBLIC_API_URL).origin,
new URL(env.NEXT_PUBLIC_STELLAR_HORIZON_URL).origin,
env.NEXT_PUBLIC_WEB_VITALS_ENDPOINT
? new URL(env.NEXT_PUBLIC_WEB_VITALS_ENDPOINT).origin
: undefined,
]);
}