Skip to content
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

fix: pregenerate https certificate #6173

Closed
wants to merge 2 commits into from

Conversation

TrySound
Copy link
Contributor

Description

node-forge used inside selfsigned is a bit package https://packagephobia.com/result?p=node-forge
Sadly there is no js-only alternative (only openssl wrappers).

While testing this feature (server.https: true) I found chrome detects
the certificate is insecure though still allows to access the page with
https after proceeding warning wall.

So as there is not much value in using so big dependency to generate
insecure certificates @patak-dev got an idea to pregenerate single
certificate and publish it with vite.

This need to be tested on various machines though before releasing.

I made the certificate expire in 10 years. Let's cross our fingers this
will be enough.

du -ck dist/node

before: 12348
after: 9812

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

node-forge used inside selfsigned is a bit package https://packagephobia.com/result?p=node-forge
Sadly there is no js-only alternative (only `openssl` wrappers).

While testing this feature (server.https: true) I found chrome detects
the certificate is insecure though still allows to access the page with
https after proceeding warning wall.

So as there is not much value in using so big dependency to generate
insecure certificates @patak-dev got an idea to pregenerate single
certificate and publish it with vite.

This need to be tested on various machines though before releasing.

I made the certificate expire in 10 years. Let's cross our fingers this
will be enough.

```
du -ck dist/node
```
before: 12348
after: 9812
@patak-dev patak-dev added this to the 2.8 milestone Dec 18, 2021
@patak-dev patak-dev added the p2-nice-to-have Not breaking anything but nice to have (priority) label Dec 18, 2021
packages/vite/package.json Outdated Show resolved Hide resolved
@aleclarson
Copy link
Member

Looks good code-wise. I haven't tested the certificate

@patak-dev
Copy link
Member

We will need help testing this one. I think the best is to merge it during the 2.8 beta period, we can revert it if there are reported issues before releasing 2.8

@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify: is it safe to commit a private key to repo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a key that will only be used during development, and to trick the browser, it isn't a secure HTTPS certificate. But what we currently do isn't secure either. I'm still not sure if this is possible, but at least with the people we have discussed so far, it looks like inlining the cert is the same as generating it locally with fixed params.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrySound should we add a comment in the .pem so people finding this private key in our repo would not be surprised? Looks like anything before the -----BEGIN should be ignored by parsers: https://stackoverflow.com/questions/19578812/comments-in-a-pem-file
Seems there are some parsers that have issues but it may be worth trying. Or any other idea to avoid getting security complaints once we merge this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed at all? i'd rather not put a private key into a public repo for whatever reason. Someone takes it and uses it to target developers using vite in a social engineering/phishing attack.
Even without a public private key, a centralized cert is a bad idea imho. If you want to do this, generate the whole thing on the client side and preferably throw away the key afterwards so no other certs can be created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dominikg the same answer I gave @patak-dev privately in discord

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dominikg aren't we doing the same already with selfsigned with static params? I was under the impression that this cert couldn't be exploited as it is just an insecure hack to get the browser to comply even if you get a warning during development.
If having a global cert is more insecure than generating one with selfsigned, we shouldn't merge this PR. Would you explain a bit how this works?

If we don't merge this one, another option to remove selfsigned would be to deprecate this insecure feature, and add a new plugin/docs to push users to create a proper certificate, and then we can remove it in 3.0, as others are proposing in other messages

@userquin
Copy link
Contributor

userquin commented Dec 26, 2021

you can check https-localhost repo, this library generates also the ssl/tls certificate for localhost and auto install the generated certificate on the local truststore: for windows and for macos on keychain, on linux I don't know (not tested), and so avoid warning messages on chrome/edge/safari, just some hints.

EDIT: I mean, the registration part on the OS trust store.

@TrySound
Copy link
Contributor Author

@patak-dev
Copy link
Member

We discussed with @userquin, and he didn't mean that we should use https-localhost instead of this PR (I thought the same given the initial comment).
IIUC he thinks this PR is ok, but that we should push users to create their own secure certificates and provide a new option in Vite to let them specify the cert to use. This is not directly related to this PR where we are evaluating two insecure options. And can be considered a suggestion for future improvements.

@TrySound
Copy link
Contributor Author

But you do let to specify own cert already. I use it daily. And agree we should provide some recipes

https: {
  key: fs.readFileSync('....pem'),
  cert: fs.readFileSync('....pem'),
}

@TrySound
Copy link
Contributor Author

Btw this is what we do in our development (written by my teammate)
https://dev.to/istarkov/fast-and-easy-way-to-setup-web-developer-certificates-450e

@patak-dev
Copy link
Member

patak-dev commented Dec 26, 2021

Well, better like that, ha 🤦🏼
We should definitely add some best practices to the docs. HTTPS setup questions are one of the most common

import { generate } from 'selfsigned'

/**
* https://github.com/webpack/webpack-dev-server/blob/master/lib/utils/createCertificate.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this link does not work, createCertificate.js does not exist in the webpack-dev-server repo current master.

Skimming the repo it looks like they only offer configuration options for bringing your own cert, no auto- or pregenerated certs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were talking with @userquin that it may also be better for us to do the same. Maybe in Vite 3.0 we can remove the autogenerated cert to push users to create secure certificates

@dominikg
Copy link
Contributor

dominikg commented Dec 27, 2021

As mentioned on discord i don't think vite should be distributing pregenerated certificates as it could be a potential security issue and is extra maintenance overhead.

Instead add config options for ssl and a third party vite-plugin-local-ssl-setup could use these options to generate a proper cert on the developers machine.

something like

{
  name: 'vite-plugin-local-ssl-setup',
  config: () => {
    // check if local cert exists, generate one if not (use a node package or platform dependant scripts around openssl)
    const ssl = localSSL();
    return { server: { ssl } }
  }
}

as it is a plugin that can be installed independently of vite, the dependency won't affect users that don't need it.

Edit, more ideas:

  • Instead of a plugin it could also be a one-time script that modifies the users vite config
  • additional documentation on how to create and handle certs.

The documentation part is important i think. Proper handling of ssl certificates and related keys is something web developers need to be aware of. Vite users who lack the knowledge to do it themselves should be educated in it instead of given an insecure default handling that may lead them to believe it is ok to do that in other places too.

@aleclarson
Copy link
Member

Agreed with @dominikg

I suggest we maintain a @vitejs/plugin-selfsigned-https package in the Vite monorepo.

@patak-dev
Copy link
Member

Agree with both, but this is something we could do in Vite 3.0. We could deprecate now and start pushing user in the right direction though

TrySound added a commit to TrySound/vite that referenced this pull request Dec 30, 2021
> watching The Matrix while hacking these certificates

Alternative to vitejs#6173

Since sharing same certificate for all users is not the best approach
I suggest to try decompose node-forge which is used internally by
selfsigned package. We can just use part of the package which already
gives a good result.

Eventually we will deprecate this feature but would be good to have less
impact on current major too.

```
du -sk dist/node
```
before: 10760
after: 9944
patak-dev pushed a commit that referenced this pull request Dec 30, 2021
> watching The Matrix while hacking these certificates

Alternative to #6173

Since sharing same certificate for all users is not the best approach
I suggest to try decompose node-forge which is used internally by
selfsigned package. We can just use part of the package which already
gives a good result.

Eventually we will deprecate this feature but would be good to have less
impact on current major too.

```
du -sk dist/node
```
before: 10760
after: 9944
@patak-dev
Copy link
Member

patak-dev commented Dec 30, 2021

Closing in favour of #6325

@patak-dev patak-dev closed this Dec 30, 2021
@userquin
Copy link
Contributor

userquin commented Dec 31, 2021

@patak-dev should be 6325 instead 3625?

@Niputi
Copy link
Contributor

Niputi commented Dec 31, 2021

comment changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants