Skip to content

Commit b6732de

Browse files
Handle situations in which the certificate was not parseable.
1 parent d071cd4 commit b6732de

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/requestHandler.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("requestHandler", () => {
9696
test("withhout auth", async () => {
9797
const result = await request(server).get(certPath).expect(200);
9898

99-
expect(result.body.toString()).toEqual(settings.crypto.cert);
99+
expect(result.body.toString()).toEqual(settings.crypto?.cert);
100100
});
101101

102102
test("with auth", async () => {

src/requestHandler.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ export default class RequestHandler {
185185
}
186186

187187
root(req: express.Request, res: express.Response): void {
188-
const certificate = forge.pki.certificateFromPem(this.settings.crypto.cert);
188+
let certificate: forge.pki.Certificate | undefined;
189+
try {
190+
certificate = forge.pki.certificateFromPem(this.settings.crypto.cert);
191+
} catch (e) {
192+
// This is fine, we just won't include that in the output
193+
}
189194

190195
res.status(200).json({
191196
status: "OK",
@@ -195,12 +200,14 @@ export default class RequestHandler {
195200
},
196201
service: "Obsidian Local REST API",
197202
authenticated: this.requestIsAuthenticated(req),
198-
certificateInfo: this.requestIsAuthenticated(req)
199-
? {
200-
validityDays: getCertificateValidityDays(certificate),
201-
regenerateRecommended: !getCertificateIsUptoStandards(certificate),
202-
}
203-
: undefined,
203+
certificateInfo:
204+
this.requestIsAuthenticated(req) && certificate
205+
? {
206+
validityDays: getCertificateValidityDays(certificate),
207+
regenerateRecommended:
208+
!getCertificateIsUptoStandards(certificate),
209+
}
210+
: undefined,
204211
});
205212
}
206213

0 commit comments

Comments
 (0)