Skip to content

Prevent server crash when SSL certificate is missing or invalid #1443

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Link33d
Copy link

@Link33d Link33d commented May 13, 2025

Problem Description:

I was setting up my Evolution server for the first time. Everything went smoothly, and I was able to run it successfully. However, I ran into an issue when trying to configure the server to use HTTPS.

After changing the SERVER_TYPE environment variable to https, I encountered the following error in the console:

Error: ENOENT: no such file or directory, open ''
    at Object.openSync (node:fs:574:18)
    at readFileSync (node:fs:453:35)
    at get https [as https] (/opt/evolution-api/dist/main.js:286:131642)
    at el (/opt/evolution-api/dist/main.js:286:133569) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: ''
}

After some research, I realized that the error was caused by a missing or invalid SSL certificate. I spent quite a bit of time trying to figure out which environment variables were required to define the certificate paths properly. I ended up digging into the source code to find out.

Once I set the correct variables, the HTTPS server worked as expected.


Motivation for this pull request:

I’m submitting this PR with a few improvements to make this process easier for future users:

  1. If the SSL certificate is invalid or missing, the server no longer crashes — it gracefully falls back to HTTP.
  2. A clear and friendly warning message is printed to the console, helping users quickly identify the issue.
  3. I added the required SSL-related environment variables (SSL_CONF_PRIVKEY and SSL_CONF_FULLCHAIN) to the .env.example file, so users don’t need to search through the code or documentation to find them.

Summary by Sourcery

Catch errors when loading SSL certificates to prevent server crashes, automatically fall back to HTTP with clear console warnings, and document required SSL environment variables.

Bug Fixes:

  • Prevent server crash on missing or invalid SSL certificates by catching errors during HTTPS server creation

Enhancements:

  • Fall back to an HTTP server and adjust server type when SSL loading fails
  • Log a warning and guidance message to the console when SSL configuration is invalid

Documentation:

  • Add SSL_CONF_PRIVKEY and SSL_CONF_FULLCHAIN entries to .env.example

Copy link

sourcery-ai bot commented May 13, 2025

Reviewer's Guide

Introduces a resilient HTTPS setup by catching certificate load errors to avoid crashes, implements a fallback to HTTP with user-friendly warnings, and updates the example environment file with the required SSL configuration variables.

File-Level Changes

Change Details Files
Gracefully handle missing or invalid SSL certificates
  • Wrapped certificate reads in a try–catch
  • Return null instead of crashing when load fails
src/utils/server-up.ts
Fallback to HTTP with console warnings
  • Detect null HTTPS server instance
  • Log warning and info guiding correct env vars
  • Switch SERVER type to HTTP and reassign server
src/main.ts
Document required SSL env vars in example
  • Added SSL_CONF_PRIVKEY variable
  • Added SSL_CONF_FULLCHAIN variable
.env.example

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Link33d - I've reviewed your changes - here's some feedback:

  • Rather than silently swallowing all errors in the https getter, consider logging the specific certificate-loading error so users have more context on why the fallback occurred.
  • Instead of mutating httpServer.TYPE to switch back to HTTP, use a local fallback variable to avoid side effects on the shared config object.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@DavidsonGomes DavidsonGomes changed the base branch from main to develop May 13, 2025 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant