/
index.tsx
314 lines (275 loc) · 9.38 KB
/
index.tsx
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* eslint-disable no-console */
import fs from 'fs';
import http from 'http';
import path from 'path';
import url from 'url';
import express from 'express';
import helmet from 'helmet';
import proxy from 'http-proxy-middleware';
import cookiesMiddleware, {
RequestWithCookies,
} from 'universal-cookie-express';
import { getApiHost } from '../api';
export const DEFAULT_HOST = 'localhost';
export const DEFAULT_PORT = 3000;
export const STATIC_PATH = '/static/';
export type ServerEnvVars = {
NODE_ENV: string;
PORT: number;
PUBLIC_URL: string;
REACT_APP_API_HOST: string;
REACT_APP_AUTHENTICATION_COOKIE: string;
REACT_APP_AUTH_TOKEN_PLACEHOLDER: string;
REACT_APP_CRA_PORT: number;
REACT_APP_REVIEWERS_HOST: string;
REACT_APP_SENTRY_DSN: string;
REACT_APP_USE_INSECURE_PROXY: string;
SERVER_HOST: string;
};
// This function injects the authentication token into the HTML sent to the
// browser (`index.html`). The token is stored into a HttpOnly cookie.
export const injectAuthenticationToken = (
req: RequestWithCookies,
html: string,
env: ServerEnvVars,
): string => {
// Try to retrieve the authentication cookie, which contains the token.
const authToken = req.universalCookies.get(
env.REACT_APP_AUTHENTICATION_COOKIE,
);
// Replace the placeholder in the `index.html` file with the token.
return html.replace(
`"${env.REACT_APP_AUTH_TOKEN_PLACEHOLDER}"`,
JSON.stringify(authToken || '').replace(/</g, '\\u003c'),
);
};
type CreateServerParams = {
_injectAuthenticationToken?: typeof injectAuthenticationToken;
env: ServerEnvVars;
rootPath: string;
};
export const createServer = ({
env,
rootPath,
_injectAuthenticationToken = injectAuthenticationToken,
}: CreateServerParams) => {
const useInsecureProxy = env.REACT_APP_USE_INSECURE_PROXY === 'true';
const isProduction = env.NODE_ENV === 'production';
const reportUri = `${getApiHost({
apiHost: env.REACT_APP_API_HOST,
useInsecureProxy,
})}/__cspreport__`;
// This CSP is a tight default. This CSP should be on all requests that
// aren't serving a document. This CSP should be set with statics.
const baseCSP = {
defaultSrc: ["'none'"],
baseUri: ["'none'"],
formAction: ["'none'"],
objectSrc: ["'none'"],
reportUri,
};
let staticSrc = "'none'";
if (env.PUBLIC_URL) {
staticSrc =
// When PUBLIC_URL is set to `/` (for local dev), we serve the statics
// locally hence "'self'".
env.PUBLIC_URL === '/' ? "'self'" : `${env.PUBLIC_URL}${STATIC_PATH}`;
}
const connectSrc = [
// While using the proxy or while in development, relax connect-src.
// Otherwise, use the API host or 'none'.
useInsecureProxy || !isProduction
? "'self'"
: env.REACT_APP_API_HOST || "'none'",
];
if (env.REACT_APP_SENTRY_DSN) {
const dsn = env.REACT_APP_SENTRY_DSN;
try {
const { host, protocol } = url.parse(dsn);
if (host && protocol) {
const sentrySrc = `${protocol}//${host}`;
console.log(`Adding Sentry as "${sentrySrc}" to CSP connect-src`);
connectSrc.push(sentrySrc);
} else {
throw new Error('could not parse host or protocol');
}
} catch (e) {
throw new Error(
`Could not parse REACT_APP_SENTRY_DSN=${dsn}: ${e.message}`,
);
}
}
// This config sets the non-static CSP for deployed instances.
const prodCSP = {
defaultSrc: ["'none'"],
childSrc: ["'none'"],
connectSrc,
baseUri: ["'self'"],
fontSrc: ["'none'"],
formAction: ["'none'"],
frameAncestors: ["'none'"],
frameSrc: ["'none'"],
imgSrc: [
staticSrc,
`${env.REACT_APP_REVIEWERS_HOST}/en-US/reviewers/download-git-file/`,
],
manifestSrc: ["'none'"],
mediaSrc: ["'none'"],
objectSrc: ["'none'"],
scriptSrc: [staticSrc],
styleSrc: [staticSrc],
workerSrc: ["'none'"],
reportUri,
};
// Create an express server.
const app = express();
// Setup the default CSP that will be used if not overridden elsewhere.
app.use(
helmet.contentSecurityPolicy({
directives: baseCSP,
browserSniff: false,
}),
);
// Set other security headers.
app.use(helmet.frameguard({ action: 'deny' }));
app.use(
helmet.hsts({
includeSubDomains: false,
maxAge: 31536000, // 1 year in seconds
}),
);
app.use(helmet.noSniff());
app.use(helmet.referrerPolicy({ policy: 'no-referrer' }));
app.use(helmet.xssFilter());
// Express configuration.
app.set('host', env.SERVER_HOST || DEFAULT_HOST);
app.set('port', env.PORT || DEFAULT_PORT);
app.disable('x-powered-by');
app.use(cookiesMiddleware());
// We use a proxy to forward API requests to REACT_APP_API_HOST (i.e. the
// AMO/addons-server API). This is useful for local development.
if (useInsecureProxy) {
if (isProduction) {
console.warn(`Using an insecure proxy with NODE_ENV=production is risky`);
}
app.use(
proxy(['/api/**', '/**/validation.json'], {
target: env.REACT_APP_API_HOST,
autoRewrite: true,
changeOrigin: true,
cookieDomainRewrite: '',
protocolRewrite: 'http',
secure: false,
onProxyRes: (proxyResponse: http.IncomingMessage) => {
// We make cookies unsecure because local development uses http and
// not https. Without this change, the server code would not be able
// to read the cookie that stores the authentication token.
if (proxyResponse.headers['set-cookie']) {
const cookies = proxyResponse.headers[
'set-cookie'
].map((cookie: string) => cookie.replace(/;\s*?(Secure)/i, ''));
// eslint-disable-next-line no-param-reassign
proxyResponse.headers['set-cookie'] = cookies;
}
},
}),
);
app.use(
proxy(['/**/reviewers/download-git-file/**/'], {
target: env.REACT_APP_REVIEWERS_HOST,
autoRewrite: true,
changeOrigin: true,
cookieDomainRewrite: '',
protocolRewrite: 'http',
secure: false,
}),
);
}
// Return 200 for csp reports - this will need to be overridden when deployed.
app.post('/__cspreport__', (req, res) => res.status(200).end('ok'));
app.get('/__version__', (req, res) => {
const versionFile = path.join(rootPath, 'version.json');
return res.sendFile(versionFile);
});
app.get(['/__heartbeat__', '/__lbheartbeat__'], (req, res) => {
return res.status(200).end('ok');
});
if (isProduction) {
// In production mode, we use a simple node server that serves all the
// static files, including the `index.html` file in which we inject the
// authentication token.
const indexHtml = fs.readFileSync(
path.join(rootPath, 'build', 'index.html'),
'utf8',
);
// Serve all the static files but `index.html`.
app.use(express.static(path.join(rootPath, 'build'), { index: false }));
// This configures the main CSP for deployed instances.
app.use(
helmet.contentSecurityPolicy({
directives: prodCSP,
browserSniff: false,
}),
);
// This handles all the incoming requests.
app.get('/*', (req: express.Request, res: express.Response) => {
const html = _injectAuthenticationToken(
req as RequestWithCookies,
indexHtml,
env,
);
res.set('cache-control', 'private, no-store');
res.send(html);
});
} else {
// In development mode, we create a proxy to forward all the requests to
// the Create-React-App application. When the `index.html` is sent back to
// the client, we inject the authentication token. This setup allows us to
// use CRA as usual (in dev mode) and the same authentication mechanism
// used in production mode.
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const modifyResponse = require('http-proxy-response-rewrite');
// This sets the development CSP that works with webpack and devtools.
app.use(
helmet.contentSecurityPolicy({
directives: {
...prodCSP,
imgSrc: [...prodCSP.imgSrc, "'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
},
browserSniff: false,
}),
);
// In production, this file will be uploaded in the `/static/` directory
// and the `index.html` uses this directory, so we need this route to serve
// this file in development.
app.get(`${STATIC_PATH}favicon.ico`, (req, res) => {
return res.sendFile(path.join(rootPath, 'public', 'favicon.ico'));
});
app.use(
proxy('/', {
target: `http://localhost:${env.REACT_APP_CRA_PORT}`,
// We need WebSocket for Hot Module Reload (HMR).
ws: true,
onProxyRes: (
proxyResponse: http.IncomingMessage,
req: http.IncomingMessage & RequestWithCookies,
res: http.ServerResponse,
) => {
const contentType = proxyResponse.headers['content-type'] || '';
if (contentType.includes('text/html')) {
// eslint-disable-next-line no-param-reassign
proxyResponse.headers['cache-control'] = 'private, no-store';
modifyResponse(
res,
proxyResponse.headers['content-encoding'],
(body: string) => _injectAuthenticationToken(req, body, env),
);
}
},
}),
);
}
return app;
};