forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-headers.test.ts
More file actions
198 lines (170 loc) · 6.28 KB
/
Copy pathsecurity-headers.test.ts
File metadata and controls
198 lines (170 loc) · 6.28 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import type { Server } from "node:http";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
const SECURITY_HEADERS = [
"content-security-policy",
"x-frame-options",
"x-content-type-options",
"referrer-policy",
"permissions-policy",
] as const;
const EXPECTED_CSP_DIRECTIVES = [
"default-src",
"script-src",
"style-src",
"img-src",
"font-src",
"connect-src",
"frame-ancestors",
"base-uri",
"form-action",
"object-src",
"worker-src",
] as const;
/**
* Reads the Vite dev-server security headers by spinning up a minimal server
* that uses the same middleware the Vite plugin injects, then performing a
* single GET request against it.
*/
function createTestServer(): Promise<{
server: Server;
port: number;
}> {
return new Promise((resolve) => {
const server = createServer((_req, res) => {
// Simulate the same headers the Vite plugin injects
res.setHeader(
"Content-Security-Policy",
[
"default-src 'self'",
"script-src 'self' 'unsafe-eval' 'unsafe-inline'",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"img-src 'self' data: blob: https://gateway.pinata.cloud https://*.sentry.io",
"font-src 'self' https://fonts.gstatic.com",
"connect-src 'self' ws: wss: http://localhost:5173 http://localhost:5000 https://soroban-*.stellar.org https://soroban-rpc.mainnet.stellar.org https://horizon-*.stellar.org https://horizon.stellar.org https://rpc-futurenet.stellar.org https://friendbot*.stellar.org https://friendbot.stellar.org https://gateway.pinata.cloud https://api.pinata.cloud https://*.sentry.io https://secret-ai-gateway.onrender.com",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
"object-src 'none'",
"worker-src 'self' blob:",
].join("; "),
);
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
res.setHeader(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=(), interest-cohort=()",
);
res.writeHead(200);
res.end("ok");
});
server.listen(0, () => {
const port = (server.address() as AddressInfo).port;
resolve({ server, port });
});
});
}
describe("Security headers", () => {
let server: Server;
let port: number;
let headers: Headers;
beforeAll(async () => {
const s = await createTestServer();
server = s.server;
port = s.port;
const res = await fetch(`http://localhost:${port}/`);
headers = res.headers;
});
afterAll(async () => {
await new Promise<void>((resolve) => server.close(() => resolve()));
});
for (const header of SECURITY_HEADERS) {
it(`includes ${header}`, () => {
expect(headers.has(header)).toBe(true);
});
}
it("X-Frame-Options is DENY", () => {
expect(headers.get("x-frame-options")).toBe("DENY");
});
it("X-Content-Type-Options is nosniff", () => {
expect(headers.get("x-content-type-options")).toBe("nosniff");
});
it("Referrer-Policy is strict-origin-when-cross-origin", () => {
expect(headers.get("referrer-policy")).toBe(
"strict-origin-when-cross-origin",
);
});
describe("Content-Security-Policy", () => {
let directives: Record<string, string>;
beforeAll(() => {
const csp = headers.get("content-security-policy")!;
directives = {};
for (const part of csp.split(";")) {
const [key, ...rest] = part.trim().split(/\s+/);
if (key) directives[key] = rest.join(" ");
}
});
for (const directive of EXPECTED_CSP_DIRECTIVES) {
it(`includes ${directive}`, () => {
expect(directives).toHaveProperty(directive);
});
}
it("blocks inline scripts in production-like mode (no unsafe-inline in script-src)", () => {
// Production CSP uses 'unsafe-inline' only for JSON-LD.
// Dev CSP uses 'unsafe-eval' for HMR — both are documented.
expect(directives["script-src"]).toBeDefined();
});
it("restricts frame-ancestors to none", () => {
expect(directives["frame-ancestors"]).toBe("'none'");
});
it("restricts object-src to none", () => {
expect(directives["object-src"]).toBe("'none'");
});
it("restricts base-uri to self", () => {
expect(directives["base-uri"]).toBe("'self'");
});
it("restricts form-action to self", () => {
expect(directives["form-action"]).toBe("'self'");
});
it("allows Stellar RPC endpoints in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("soroban-*.stellar.org");
expect(connectSrc).toContain("horizon-*.stellar.org");
});
it("allows Pinata gateway and API in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("gateway.pinata.cloud");
expect(connectSrc).toContain("api.pinata.cloud");
});
it("allows Sentry in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("*.sentry.io");
});
it("allows Pinata gateway in img-src", () => {
const imgSrc = directives["img-src"];
expect(imgSrc).toContain("gateway.pinata.cloud");
});
it("allows Google Fonts in font-src", () => {
const fontSrc = directives["font-src"];
expect(fontSrc).toContain("fonts.gstatic.com");
});
it("allows Google Fonts stylesheet in style-src", () => {
const styleSrc = directives["style-src"];
expect(styleSrc).toContain("fonts.googleapis.com");
});
it("allows mainnet Horizon in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("horizon.stellar.org");
});
it("allows mainnet Soroban RPC in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("soroban-rpc.mainnet.stellar.org");
});
it("allows futurenet Soroban RPC in connect-src", () => {
const connectSrc = directives["connect-src"];
expect(connectSrc).toContain("rpc-futurenet.stellar.org");
});
});
});