Skip to content

Allow custom certificates in HTTPS mode #6544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docusaurus/docs/using-https-in-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ set HTTPS=true&&npm start
HTTPS=true npm start
```

Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.
Note that the server by default will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.

## Providing valid certificates

> Note: this feature is available with `[email protected]` and higher.

If you want to use valid certificates for local development, you can use a tool like [mkcert](https://github.com/FiloSottile/mkcert) to create a valid local certificate.

You can then set the certificate path as an environment variables when starting the dev server:

```sh
HTTPS=true HTTPS_KEY=/path/to/key.pem HTTPS_CERT=/path/to/cert.pem npm start
```
19 changes: 17 additions & 2 deletions packages/react-scripts/config/webpackDevServer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ const fs = require('fs');
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const host = process.env.HOST || '0.0.0.0';

// Enable HTTPS if the HTTPS environment variable is set to 'true'.
let https = false;
if (process.env.HTTPS === 'true') {
https = true;

// Pass a custom certificate file when the environment variables are set
const keyFile = process.env.HTTPS_KEY;
const certFile = process.env.HTTPS_CERT;
if (keyFile && certFile) {
https = {
key: fs.readFileSync(keyFile),
cert: fs.readFileSync(certFile),
};
}
}

module.exports = function(proxy, allowedHost) {
return {
// WebpackDevServer 2.4.3 introduced a security fix that prevents remote
Expand Down Expand Up @@ -79,8 +95,7 @@ module.exports = function(proxy, allowedHost) {
watchOptions: {
ignored: ignoredFiles(paths.appSrc),
},
// Enable HTTPS if the HTTPS environment variable is set to 'true'
https: protocol === 'https',
https,
host,
overlay: false,
historyApiFallback: {
Expand Down