Merge branch '21572-user-packages'
[arvados.git] / services / workbench2 / config / webpackDevServer.config.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 'use strict';
6
7 const fs = require('fs');
8 const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware');
9 const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
10 const ignoredFiles = require('react-dev-utils/ignoredFiles');
11 const redirectServedPath = require('react-dev-utils/redirectServedPathMiddleware');
12 const paths = require('./paths');
13 const getHttpsConfig = require('./getHttpsConfig');
14
15 const host = process.env.HOST || '0.0.0.0';
16 const sockHost = process.env.WDS_SOCKET_HOST;
17 const sockPath = process.env.WDS_SOCKET_PATH; // default: '/ws'
18 const sockPort = process.env.WDS_SOCKET_PORT;
19
20 module.exports = function (proxy, allowedHost) {
21   const disableFirewall =
22     !proxy || process.env.DANGEROUSLY_DISABLE_HOST_CHECK === 'true';
23   return {
24     // WebpackDevServer 2.4.3 introduced a security fix that prevents remote
25     // websites from potentially accessing local content through DNS rebinding:
26     // https://github.com/webpack/webpack-dev-server/issues/887
27     // https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
28     // However, it made several existing use cases such as development in cloud
29     // environment or subdomains in development significantly more complicated:
30     // https://github.com/facebook/create-react-app/issues/2271
31     // https://github.com/facebook/create-react-app/issues/2233
32     // While we're investigating better solutions, for now we will take a
33     // compromise. Since our WDS configuration only serves files in the `public`
34     // folder we won't consider accessing them a vulnerability. However, if you
35     // use the `proxy` feature, it gets more dangerous because it can expose
36     // remote code execution vulnerabilities in backends like Django and Rails.
37     // So we will disable the host check normally, but enable it if you have
38     // specified the `proxy` setting. Finally, we let you override it if you
39     // really know what you're doing with a special environment variable.
40     // Note: ["localhost", ".localhost"] will support subdomains - but we might
41     // want to allow setting the allowedHosts manually for more complex setups
42     allowedHosts: disableFirewall ? 'all' : [allowedHost],
43     headers: {
44       'Access-Control-Allow-Origin': '*',
45       'Access-Control-Allow-Methods': '*',
46       'Access-Control-Allow-Headers': '*',
47     },
48     // Enable gzip compression of generated files.
49     compress: true,
50     static: {
51       // By default WebpackDevServer serves physical files from current directory
52       // in addition to all the virtual build products that it serves from memory.
53       // This is confusing because those files won’t automatically be available in
54       // production build folder unless we copy them. However, copying the whole
55       // project directory is dangerous because we may expose sensitive files.
56       // Instead, we establish a convention that only files in `public` directory
57       // get served. Our build script will copy `public` into the `build` folder.
58       // In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
59       // <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
60       // In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
61       // Note that we only recommend to use `public` folder as an escape hatch
62       // for files like `favicon.ico`, `manifest.json`, and libraries that are
63       // for some reason broken when imported through webpack. If you just want to
64       // use an image, put it in `src` and `import` it from JavaScript instead.
65       directory: paths.appPublic,
66       publicPath: [paths.publicUrlOrPath],
67       // By default files from `contentBase` will not trigger a page reload.
68       watch: {
69         // Reportedly, this avoids CPU overload on some systems.
70         // https://github.com/facebook/create-react-app/issues/293
71         // src/node_modules is not ignored to support absolute imports
72         // https://github.com/facebook/create-react-app/issues/1065
73         ignored: ignoredFiles(paths.appSrc),
74       },
75     },
76     client: {
77       webSocketURL: {
78         // Enable custom sockjs pathname for websocket connection to hot reloading server.
79         // Enable custom sockjs hostname, pathname and port for websocket connection
80         // to hot reloading server.
81         hostname: sockHost,
82         pathname: sockPath,
83         port: sockPort,
84       },
85       overlay: {
86         errors: true,
87         warnings: false,
88       },
89     },
90     devMiddleware: {
91       // It is important to tell WebpackDevServer to use the same "publicPath" path as
92       // we specified in the webpack config. When homepage is '.', default to serving
93       // from the root.
94       // remove last slash so user can land on `/test` instead of `/test/`
95       publicPath: paths.publicUrlOrPath.slice(0, -1),
96     },
97
98     https: getHttpsConfig(),
99     host,
100     historyApiFallback: {
101       // Paths with dots should still use the history fallback.
102       // See https://github.com/facebook/create-react-app/issues/387.
103       disableDotRule: true,
104       index: paths.publicUrlOrPath,
105     },
106     // `proxy` is run between `before` and `after` `webpack-dev-server` hooks
107     proxy,
108     onBeforeSetupMiddleware(devServer) {
109       // Keep `evalSourceMapMiddleware`
110       // middlewares before `redirectServedPath` otherwise will not have any effect
111       // This lets us fetch source contents from webpack for the error overlay
112       devServer.app.use(evalSourceMapMiddleware(devServer));
113
114       if (fs.existsSync(paths.proxySetup)) {
115         // This registers user provided middleware for proxy reasons
116         require(paths.proxySetup)(devServer.app);
117       }
118     },
119     onAfterSetupMiddleware(devServer) {
120       // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
121       devServer.app.use(redirectServedPath(paths.publicUrlOrPath));
122
123       // This service worker file is effectively a 'no-op' that will reset any
124       // previous service worker registered for the same host:port combination.
125       // We do this in development to avoid hitting the production cache if
126       // it used the same host and port.
127       // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
128       devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
129     },
130   };
131 };