Skip to content

Commit 4f467f9

Browse files
committed
fix(server): prevent crash when SSL certificate is invalid
1 parent 427c994 commit 4f467f9

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ SERVER_PORT=8080
33
# Server URL - Set your application url
44
SERVER_URL=http://localhost:8080
55

6+
# Paths to your SSL certificate files (used for HTTPS)
7+
SSL_CONF_PRIVKEY=/path/to/cert.key
8+
SSL_CONF_FULLCHAIN=/path/to/cert.crt
9+
610
SENTRY_DSN=
711

812
# Cors - * for all or set separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'

src/main.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,15 @@ async function bootstrap() {
128128
const httpServer = configService.get<HttpServer>('SERVER');
129129

130130
ServerUP.app = app;
131-
const server = ServerUP[httpServer.TYPE];
131+
let server = ServerUP[httpServer.TYPE];
132+
133+
if (server === null) {
134+
logger.warn("SSL cert load failed — falling back to HTTP.")
135+
logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files.");
136+
137+
httpServer.TYPE = "http"
138+
server = ServerUP[httpServer.TYPE]
139+
}
132140

133141
eventManager.init(server);
134142

src/utils/server-up.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ export class ServerUP {
1212
}
1313

1414
static get https() {
15-
const { FULLCHAIN, PRIVKEY } = configService.get<SslConf>('SSL_CONF');
16-
return https.createServer(
17-
{
18-
cert: readFileSync(FULLCHAIN),
19-
key: readFileSync(PRIVKEY),
20-
},
21-
ServerUP.#app,
22-
);
15+
try {
16+
const { FULLCHAIN, PRIVKEY } = configService.get<SslConf>('SSL_CONF');
17+
return https.createServer(
18+
{
19+
cert: readFileSync(FULLCHAIN),
20+
key: readFileSync(PRIVKEY),
21+
},
22+
ServerUP.#app,
23+
);
24+
} catch {
25+
return null
26+
}
2327
}
2428

2529
static get http() {

0 commit comments

Comments
 (0)