-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathserverCertificateError.test.ts
More file actions
297 lines (273 loc) · 8.52 KB
/
serverCertificateError.test.ts
File metadata and controls
297 lines (273 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import "@abraham/reflection";
import {
KeyUsagesExtension,
X509Certificate as X509CertificatePeculiar,
} from "@peculiar/x509";
import axios from "axios";
import { X509Certificate as X509CertificateNode } from "node:crypto";
import * as fs from "node:fs/promises";
import https from "node:https";
import { afterAll, describe, expect, it, vi } from "vitest";
import { CertificateError } from "@/error/certificateError";
import {
ServerCertificateError,
X509_ERR,
X509_ERR_CODE,
} from "@/error/serverCertificateError";
import { type Logger } from "@/logging/logger";
import { getFixturePath } from "../../utils/fixtures";
vi.mock("vscode");
describe("Certificate errors", () => {
// Before each test we make a request to sanity check that we really get the
// error we are expecting, then we run it through CertificateError.
// These tests run in Electron (BoringSSL) for accurate certificate validation testing.
it("should run in Electron environment", () => {
expect(process.versions.electron).toBeTruthy();
});
const throwingLog = (message: string) => {
throw new Error(message);
};
const logger: Logger = {
trace: throwingLog,
debug: throwingLog,
info: throwingLog,
warn: throwingLog,
error: throwingLog,
show: () => {},
};
const disposers: Array<() => void> = [];
afterAll(() => {
disposers.forEach((d) => d());
});
async function startServer(certName: string): Promise<string> {
const server = https.createServer(
{
key: await fs.readFile(getFixturePath("tls", `${certName}.key`)),
cert: await fs.readFile(getFixturePath("tls", `${certName}.crt`)),
},
(req, res) => {
if (req.url?.endsWith("/error")) {
res.writeHead(500);
res.end("error");
return;
}
res.writeHead(200);
res.end("foobar");
},
);
disposers.push(() => server.close());
return new Promise<string>((resolve, reject) => {
server.on("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address) {
throw new Error("Server has no address");
}
if (typeof address !== "string") {
const host =
address.family === "IPv6"
? `[${address.address}]`
: address.address;
return resolve(`https://${host}:${address.port}`);
}
resolve(address);
});
});
}
// Both environments give the "unable to verify" error with partial chains.
it("detects partial chains", async () => {
const address = await startServer("chain-leaf");
const request = axios.get(address, {
httpsAgent: new https.Agent({
ca: await fs.readFile(getFixturePath("tls", "chain-leaf.crt")),
}),
});
await expect(request).rejects.toHaveProperty(
"code",
X509_ERR_CODE.UNABLE_TO_VERIFY_LEAF_SIGNATURE,
);
try {
await request;
} catch (error) {
const wrapped = await ServerCertificateError.maybeWrap(
error,
address,
logger,
);
expect(wrapped instanceof CertificateError).toBeTruthy();
expect((wrapped as ServerCertificateError).x509Err).toBe(
X509_ERR.PARTIAL_CHAIN,
);
}
});
it("can bypass partial chain", async () => {
const address = await startServer("chain-leaf");
const request = axios.get(address, {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
// In Electron a self-issued certificate without the signing capability fails
// (again with the same "unable to verify" error)
it("detects self-signed certificates without signing capability", async () => {
const address = await startServer("no-signing");
const request = axios.get(address, {
httpsAgent: new https.Agent({
ca: await fs.readFile(getFixturePath("tls", "no-signing.crt")),
servername: "localhost",
}),
});
await expect(request).rejects.toHaveProperty(
"code",
X509_ERR_CODE.UNABLE_TO_VERIFY_LEAF_SIGNATURE,
);
try {
await request;
} catch (error) {
const wrapped = await ServerCertificateError.maybeWrap(
error,
address,
logger,
);
expect(wrapped instanceof CertificateError).toBeTruthy();
expect((wrapped as ServerCertificateError).x509Err).toBe(
X509_ERR.NON_SIGNING,
);
}
});
it("can bypass self-signed certificates without signing capability", async () => {
const address = await startServer("no-signing");
const request = axios.get(address, {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
// Node's X509Certificate.keyUsage is unreliable, so use a third-party parser
it("parses no-signing cert keyUsage with third-party library", async () => {
const certPem = await fs.readFile(
getFixturePath("tls", "no-signing.crt"),
"utf-8",
);
// Node's implementation seems to always return `undefined`
const nodeCert = new X509CertificateNode(certPem);
expect(nodeCert.keyUsage).toBeUndefined();
// Here we can correctly get the KeyUsages
const peculiarCert = new X509CertificatePeculiar(certPem);
const extension = peculiarCert.getExtension(KeyUsagesExtension);
expect(extension).toBeDefined();
expect(extension?.usages).toBeTruthy();
});
// Both environments give the same error code when a self-issued certificate is
// untrusted.
it("detects self-signed certificates", async () => {
const address = await startServer("self-signed");
const request = axios.get(address);
await expect(request).rejects.toHaveProperty(
"code",
X509_ERR_CODE.DEPTH_ZERO_SELF_SIGNED_CERT,
);
try {
await request;
} catch (error) {
const wrapped = await ServerCertificateError.maybeWrap(
error,
address,
logger,
);
expect(wrapped instanceof CertificateError).toBeTruthy();
expect((wrapped as ServerCertificateError).x509Err).toBe(
X509_ERR.UNTRUSTED_LEAF,
);
}
});
// Both environments have no problem if the self-issued certificate is trusted
// and has the signing capability.
it("is ok with trusted self-signed certificates", async () => {
const address = await startServer("self-signed");
const request = axios.get(address, {
httpsAgent: new https.Agent({
ca: await fs.readFile(getFixturePath("tls", "self-signed.crt")),
servername: "localhost",
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
it("can bypass self-signed certificates", async () => {
const address = await startServer("self-signed");
const request = axios.get(address, {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
// Both environments give the same error code when the chain is complete but the
// root is not trusted.
it("detects an untrusted chain", async () => {
const address = await startServer("chain");
const request = axios.get(address);
await expect(request).rejects.toHaveProperty(
"code",
X509_ERR_CODE.SELF_SIGNED_CERT_IN_CHAIN,
);
try {
await request;
} catch (error) {
const wrapped = await ServerCertificateError.maybeWrap(
error,
address,
logger,
);
expect(wrapped instanceof CertificateError).toBeTruthy();
expect((wrapped as ServerCertificateError).x509Err).toBe(
X509_ERR.UNTRUSTED_CHAIN,
);
}
});
// Both environments have no problem if the chain is complete and the root is
// trusted.
it("is ok with chains with a trusted root", async () => {
const address = await startServer("chain");
const request = axios.get(address, {
httpsAgent: new https.Agent({
ca: await fs.readFile(getFixturePath("tls", "chain-root.crt")),
servername: "localhost",
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
it("can bypass chain", async () => {
const address = await startServer("chain");
const request = axios.get(address, {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
await expect(request).resolves.toHaveProperty("data", "foobar");
});
it("falls back with different error", async () => {
const address = await startServer("chain");
const request = axios.get(address + "/error", {
httpsAgent: new https.Agent({
ca: await fs.readFile(getFixturePath("tls", "chain-root.crt")),
servername: "localhost",
}),
});
await expect(request).rejects.toThrow(/failed with status code 500/);
try {
await request;
} catch (error) {
const wrapped = await ServerCertificateError.maybeWrap(
error,
"1",
logger,
);
expect(wrapped instanceof CertificateError).toBeFalsy();
expect((wrapped as Error).message).toMatch(/failed with status code 500/);
}
});
});