Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ SERVER_PORT=8080
# Server URL - Set your application url
SERVER_URL=http://localhost:8080

# Paths to your SSL certificate files (used for HTTPS)
SSL_CONF_PRIVKEY=/path/to/cert.key
SSL_CONF_FULLCHAIN=/path/to/cert.crt

SENTRY_DSN=

# Cors - * for all or set separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@
const httpServer = configService.get<HttpServer>('SERVER');

ServerUP.app = app;
const server = ServerUP[httpServer.TYPE];
let server = ServerUP[httpServer.TYPE];

Check failure on line 132 in src/main.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `··`
if (server === null) {
logger.warn("SSL cert load failed — falling back to HTTP.")

Check failure on line 134 in src/main.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Replace `"SSL·cert·load·failed·—·falling·back·to·HTTP.")` with `'SSL·cert·load·failed·—·falling·back·to·HTTP.');`
logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files.");

Check failure on line 136 in src/main.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `·····`
httpServer.TYPE = "http"

Check failure on line 137 in src/main.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Replace `"http"` with `'http';`
server = ServerUP[httpServer.TYPE]

Check failure on line 138 in src/main.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
}

eventManager.init(server);

Expand Down
20 changes: 12 additions & 8 deletions src/utils/server-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
}

static get https() {
const { FULLCHAIN, PRIVKEY } = configService.get<SslConf>('SSL_CONF');
return https.createServer(
{
cert: readFileSync(FULLCHAIN),
key: readFileSync(PRIVKEY),
},
ServerUP.#app,
);
try {
const { FULLCHAIN, PRIVKEY } = configService.get<SslConf>('SSL_CONF');
return https.createServer(
{
cert: readFileSync(FULLCHAIN),
key: readFileSync(PRIVKEY),
},
ServerUP.#app,
);
} catch {
return null

Check failure on line 25 in src/utils/server-up.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
}
}

static get http() {
Expand Down
Loading