Skip to content

Commit a8cc395

Browse files
aliinektro
andauthored
fix tls-keyengine-invalid-arg-type test (#19505)
Co-authored-by: Meghan Denny <[email protected]>
1 parent 71c770f commit a8cc395

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/js/node/tls.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,25 @@ var InternalSecureContext = class SecureContext {
241241
if (secureOptions && typeof secureOptions !== "number") {
242242
throw new TypeError("secureOptions argument must be an number");
243243
}
244+
244245
this.secureOptions = secureOptions;
246+
247+
if (!$isUndefinedOrNull(options.privateKeyIdentifier)) {
248+
if ($isUndefinedOrNull(options.privateKeyEngine)) {
249+
// prettier-ignore
250+
throw $ERR_INVALID_ARG_VALUE("options.privateKeyEngine", options.privateKeyEngine);
251+
} else if (typeof options.privateKeyEngine !== "string") {
252+
// prettier-ignore
253+
throw $ERR_INVALID_ARG_TYPE("options.privateKeyEngine", ["string", "null", "undefined"], options.privateKeyEngine);
254+
}
255+
256+
if (typeof options.privateKeyIdentifier !== "string") {
257+
// prettier-ignore
258+
throw $ERR_INVALID_ARG_TYPE("options.privateKeyIdentifier", ["string", "null", "undefined"], options.privateKeyIdentifier);
259+
}
260+
}
245261
}
262+
246263
this.context = context;
247264
}
248265
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const tls = require('tls');
9+
10+
assert.throws(
11+
() => {
12+
tls.createSecureContext({ privateKeyEngine: 0,
13+
privateKeyIdentifier: 'key' });
14+
},
15+
{ code: 'ERR_INVALID_ARG_TYPE',
16+
message: / Received type number \(0\)$/ });
17+
18+
assert.throws(
19+
() => {
20+
tls.createSecureContext({ privateKeyEngine: 'engine',
21+
privateKeyIdentifier: 0 });
22+
},
23+
{ code: 'ERR_INVALID_ARG_TYPE',
24+
message: / Received type number \(0\)$/ });
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, expect, it } from "bun:test";
2+
import * as tls from "node:tls";
3+
4+
describe("tls.createSecureContext extra arguments test", () => {
5+
it("should throw an error if the privateKeyEngine is not a string", () => {
6+
// @ts-expect-error
7+
expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: 0 })).toThrow(
8+
"string, null, or undefined",
9+
);
10+
// @ts-expect-error
11+
expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: true })).toThrow(
12+
"string, null, or undefined",
13+
);
14+
// @ts-expect-error
15+
expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: {} })).toThrow(
16+
"string, null, or undefined",
17+
);
18+
});
19+
20+
it("should throw an error if the privateKeyIdentifier is not a string", () => {
21+
// @ts-expect-error
22+
expect(() => tls.createSecureContext({ privateKeyIdentifier: 0, privateKeyEngine: "valid" })).toThrow(
23+
"string, null, or undefined",
24+
);
25+
// @ts-expect-error
26+
expect(() => tls.createSecureContext({ privateKeyIdentifier: true, privateKeyEngine: "valid" })).toThrow(
27+
"string, null, or undefined",
28+
);
29+
// @ts-expect-error
30+
expect(() => tls.createSecureContext({ privateKeyIdentifier: {}, privateKeyEngine: "valid" })).toThrow(
31+
"string, null, or undefined",
32+
);
33+
});
34+
35+
it("should throw with a valid privateKeyIdentifier but missing privateKeyEngine", () => {
36+
expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid" })).toThrow(
37+
"The property 'options.privateKeyEngine' is invalid. Received undefined",
38+
);
39+
});
40+
41+
it("should not throw for invalid privateKeyEngine when privateKeyIdentifier is not provided", () => {
42+
// Node.js does not throw an error in the case where only privateKeyEngine is provided, even if
43+
// the key is invalid. The checks for both keys are only done when privateKeyIdentifier is passed.
44+
// Verifiable with: `node -p 'tls.createSecureContext({ privateKeyEngine: 0 })'`
45+
46+
// @ts-expect-error
47+
expect(() => tls.createSecureContext({ privateKeyEngine: 0 })).not.toThrow();
48+
// @ts-expect-error
49+
expect(() => tls.createSecureContext({ privateKeyEngine: true })).not.toThrow();
50+
// @ts-expect-error
51+
expect(() => tls.createSecureContext({ privateKeyEngine: {} })).not.toThrow();
52+
});
53+
54+
it("should throw for invalid privateKeyIdentifier", () => {
55+
// @ts-expect-error
56+
expect(() => tls.createSecureContext({ privateKeyIdentifier: 0 })).toThrow(
57+
"The property 'options.privateKeyEngine' is invalid. Received undefined",
58+
);
59+
60+
// @ts-expect-error
61+
expect(() => tls.createSecureContext({ privateKeyIdentifier: true })).toThrow(
62+
"The property 'options.privateKeyEngine' is invalid. Received undefined",
63+
);
64+
65+
// @ts-expect-error
66+
expect(() => tls.createSecureContext({ privateKeyIdentifier: {} })).toThrow(
67+
"The property 'options.privateKeyEngine' is invalid. Received undefined",
68+
);
69+
});
70+
});

0 commit comments

Comments
 (0)