mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-27 20:00:51 +00:00
- Add host-rendered native IDE primitives (PluginCodeEditor/Monaco, PluginDataGrid/@tanstack/react-virtual, PluginSplitPane, PluginErCanvas) with Shadow DOM slot projection (zero iframes, zero plugin JS in renderer) - Expand Remote DOM budgets, expose full 22 Netcatty semantic CSS tokens, add dblclick/contextmenu/scroll/selection events, cursor-anchored PluginPopover, database theme icon, and full-bleed chrome flag - Add dynamic user-approved TCP/TLS byte streams (incl. localhost opt-in blocking internal control ports), in-band STARTTLS upgrade with cert fingerprint confirmation, durable revocable Vault SSH host bindings, plugin-owned encrypted secrets, native ui.openFile/saveFile streaming handles, 4 MiB clipboard, and prefix-filtered storage.keys - Optimize plugin view image pipeline with pooled sandboxed rasterizer window (auto-destroyed on idle/error/dispose), LRU version+hash cache in PluginViewResources, transient failure eviction in HostDrawnViewHost, renderer-side session deduplication, and RemoteImageElement cache + retry - Temporarily enable plugin host by default in hostBootstrap.cjs (marked TODO(revert), opt-out via NETCATTY_PLUGIN_DEV=0)
457 lines
22 KiB
JavaScript
457 lines
22 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const { test } = require("node:test");
|
|
const net = require("node:net");
|
|
const tls = require("node:tls");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { execFileSync } = require("node:child_process");
|
|
const { once } = require("node:events");
|
|
const { LIMITS, PluginByteStreamBroker, canonicalizeStreamEndpoint, isLocalControlAddress } = require("./byteStreamBroker.cjs");
|
|
const { RPC_ERRORS } = require("./rpcRouter.cjs");
|
|
|
|
function context(runtimeId = "runtime-a", controller = new AbortController()) {
|
|
return { pluginId: "test.stream", runtimeId, signal: controller.signal,
|
|
assertActive: async () => controller.signal.throwIfAborted() };
|
|
}
|
|
|
|
async function server(t, listener, options) {
|
|
const sockets = new Set();
|
|
const instance = options ? tls.createServer(options, listener) : net.createServer(listener);
|
|
instance.on("connection", (socket) => {
|
|
sockets.add(socket);
|
|
socket.on("error", () => {});
|
|
socket.on("close", () => sockets.delete(socket));
|
|
});
|
|
instance.on("tlsClientError", () => {});
|
|
instance.listen(0, "127.0.0.1");
|
|
await once(instance, "listening");
|
|
t.after(async () => {
|
|
for (const socket of sockets) socket.destroy();
|
|
await new Promise((resolve) => instance.close(resolve));
|
|
});
|
|
return instance;
|
|
}
|
|
|
|
function broker(t, endpoint, options = {}) {
|
|
const audit = [];
|
|
const requests = [];
|
|
let revoke;
|
|
const instance = new PluginByteStreamBroker({
|
|
permissionEngine: {
|
|
authorize: async (_ctx, request) => requests.push(request),
|
|
onDidRevoke(listener) { revoke = listener; return { dispose() {} }; },
|
|
},
|
|
audit: (...args) => audit.push(args),
|
|
testLocalEndpoints: endpoint ? [endpoint] : [],
|
|
...options,
|
|
});
|
|
t.after(() => instance.shutdown());
|
|
return { instance, requests, audit, revoke: (event) => revoke(event) };
|
|
}
|
|
|
|
test("stream endpoint canonicalization requires one exact host, transport and port", () => {
|
|
assert.equal(canonicalizeStreamEndpoint("tls://EXAMPLE.test:443").endpoint, "tls://example.test:443");
|
|
assert.equal(canonicalizeStreamEndpoint("tcp://[0:0:0:0:0:0:0:1]:3306").endpoint, "tcp://[::1]:3306");
|
|
for (const value of ["*", "tcp://*:3306", "tcp://x:0", "tcp://x:01", "tcp://x:65536", "udp://x:22",
|
|
"tcp://x", "tcp://x:22/", "tcp://user@x:22", "tcp://127.1:22", "tcp://x:22?x", "tcp://x.:22",
|
|
"tcp://[::1%25lo]:22", "tcp://x:22#x", "tcp://-x:22"]) {
|
|
assert.throws(() => canonicalizeStreamEndpoint(value), TypeError, value);
|
|
}
|
|
});
|
|
|
|
test("local/control address classification handles mapped IPv6 and alternate literals", () => {
|
|
for (const address of ["127.0.0.1", "127.9.8.7", "0.0.0.0", "169.254.169.254", "224.0.0.1",
|
|
"::1", "::", "::ffff:127.0.0.1", "::ffff:7f00:1", "fe80::1", "ff02::1", "2002:7f00:1::"]) {
|
|
assert.equal(isLocalControlAddress(address), true, address);
|
|
}
|
|
for (const address of ["192.168.1.2", "10.0.0.8", "203.0.113.1", "2001:db8::1", "fd00::1"]) {
|
|
assert.equal(isLocalControlAddress(address), false, address);
|
|
}
|
|
});
|
|
|
|
test("real TCP transport exchanges binary bytes and records no payloads", async (t) => {
|
|
const peer = await server(t, (socket) => socket.on("data", (bytes) => socket.write(bytes)));
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance, requests, audit } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const handle = await instance.connect(endpoint, ctx);
|
|
const bytes = Uint8Array.from([0, 255, 1, 254, 19]);
|
|
await instance.write(handle.handleId, bytes, ctx);
|
|
assert.deepEqual(await instance.read(handle.handleId, ctx), bytes);
|
|
instance.close(handle.handleId, ctx);
|
|
assert.equal(instance.connections.size, 0);
|
|
assert.deepEqual(requests[0].resources, [endpoint]);
|
|
assert.equal(requests[0].permission, "network.stream");
|
|
assert.deepEqual(audit.map((entry) => entry[1]), ["network.stream.open", "network.stream.close"]);
|
|
assert.equal(audit[1][2].bytesRead, bytes.byteLength);
|
|
assert.equal(audit[1][2].bytesWritten, bytes.byteLength);
|
|
assert.equal(JSON.stringify(audit).includes("data"), false);
|
|
});
|
|
|
|
test("remote EOF returns the final bytes and releases the connection", async (t) => {
|
|
const peer = await server(t, (socket) => socket.end("synthetic final bytes"));
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
const chunks = [];
|
|
while (true) {
|
|
const chunk = await instance.read(handleId, ctx);
|
|
if (chunk === null) break;
|
|
chunks.push(Buffer.from(chunk));
|
|
}
|
|
assert.equal(Buffer.concat(chunks).toString(), "synthetic final bytes");
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("a delayed consumer can observe EOF after the real socket has closed", async (t) => {
|
|
const peer = await server(t, (socket) => socket.end("final"));
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance, revoke } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
const socket = instance.connections.get(handleId).socket;
|
|
const closed = once(socket, "close");
|
|
assert.equal(Buffer.from(await instance.read(handleId, ctx)).toString(), "final");
|
|
await closed;
|
|
await assert.rejects(instance.read(handleId, context("other-runtime")), { code: RPC_ERRORS.permissionDenied });
|
|
assert.equal(await instance.read(handleId, ctx), null);
|
|
assert.equal(instance.connections.size, 0);
|
|
const second = await instance.connect(endpoint, ctx);
|
|
const secondClosed = once(instance.connections.get(second.handleId).socket, "close");
|
|
await instance.read(second.handleId, ctx);
|
|
await secondClosed;
|
|
revoke({ pluginId: ctx.pluginId, permission: "network.stream", resource: endpoint });
|
|
await assert.rejects(instance.read(second.handleId, ctx), { code: RPC_ERRORS.permissionDenied });
|
|
});
|
|
|
|
test("denial happens before DNS or a socket is created", async (t) => {
|
|
let lookedUp = false;
|
|
const { instance } = broker(t, null, {
|
|
permissionEngine: { authorize: async () => { throw new Error("denied"); }, onDidRevoke: () => ({ dispose() {} }) },
|
|
lookup: () => { lookedUp = true; },
|
|
});
|
|
await assert.rejects(instance.connect("tcp://example.test:22", context()), /denied/u);
|
|
assert.equal(lookedUp, false);
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("production rejects loopback, mixed DNS answers, and app control peers", async (t) => {
|
|
const endpoint = "tcp://example.test:22";
|
|
const { instance } = broker(t, null, {
|
|
lookup: async () => [{ address: "203.0.113.1", family: 4 }, { address: "127.0.0.1", family: 4 }],
|
|
});
|
|
await assert.rejects(instance.connect(endpoint, context()), { code: RPC_ERRORS.permissionDenied });
|
|
await assert.rejects(instance.connect("tcp://127.0.0.1:22", context()), { code: RPC_ERRORS.permissionDenied });
|
|
const protectedBroker = broker(t, "tcp://127.0.0.1:22", { isControlEndpoint: () => true }).instance;
|
|
await assert.rejects(protectedBroker.connect("tcp://127.0.0.1:22", context()), { code: RPC_ERRORS.permissionDenied });
|
|
});
|
|
|
|
test("runtime handles cannot cross runtime or plugin boundaries", async (t) => {
|
|
const peer = await server(t, () => {});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
for (const other of [context("runtime-b"), { ...ctx, pluginId: "test.other" }]) {
|
|
await assert.rejects(instance.read(handleId, other), { code: RPC_ERRORS.permissionDenied });
|
|
assert.throws(() => instance.close(handleId, other), { code: RPC_ERRORS.permissionDenied });
|
|
}
|
|
instance.close(handleId, ctx);
|
|
await assert.rejects(instance.write(handleId, Uint8Array.of(1), ctx), { code: RPC_ERRORS.permissionDenied });
|
|
});
|
|
|
|
test("revocation and runtime release cancel pending real reads", async (t) => {
|
|
const peer = await server(t, () => {});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance, revoke } = broker(t, endpoint);
|
|
const ctx = context();
|
|
let handle = await instance.connect(endpoint, ctx);
|
|
const reading = instance.read(handle.handleId, ctx);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
revoke({ pluginId: ctx.pluginId, permission: "network.stream", resource: endpoint });
|
|
await assert.rejects(reading, { code: RPC_ERRORS.permissionDenied });
|
|
assert.equal(instance.connections.size, 0);
|
|
handle = await instance.connect(endpoint, ctx);
|
|
const secondRead = instance.read(handle.handleId, ctx);
|
|
instance.releaseRuntime(ctx.runtimeId);
|
|
await assert.rejects(secondRead, { code: RPC_ERRORS.unavailable });
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("cancellation during DNS rejects promptly and ignores late resolution", async (t) => {
|
|
let resolveDns;
|
|
let dialed = false;
|
|
const { instance } = broker(t, null, {
|
|
lookup: () => new Promise((resolve) => { resolveDns = resolve; }),
|
|
connectSocket: () => { dialed = true; },
|
|
});
|
|
const controller = new AbortController();
|
|
const pending = instance.connect("tcp://example.test:22", context("runtime-a", controller));
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
controller.abort();
|
|
await assert.rejects(pending, { code: RPC_ERRORS.cancelled });
|
|
resolveDns([{ address: "203.0.113.1", family: 4 }]);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(dialed, false);
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("cancellation after the last async guard cannot create a late real socket", async (t) => {
|
|
const peer = await server(t, () => {});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
let dialed = false;
|
|
const { instance } = broker(t, endpoint, { connectSocket(options) {
|
|
dialed = true;
|
|
const socket = net.createConnection(options);
|
|
t.after(() => socket.destroy());
|
|
return socket;
|
|
} });
|
|
const controller = new AbortController();
|
|
let guards = 0;
|
|
const ctx = context("runtime-a", controller);
|
|
ctx.assertActive = async () => {
|
|
controller.signal.throwIfAborted();
|
|
if (++guards === 3) queueMicrotask(() => queueMicrotask(() => controller.abort()));
|
|
};
|
|
await assert.rejects(instance.connect(endpoint, ctx), { code: RPC_ERRORS.cancelled });
|
|
assert.equal(dialed, false);
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("connect and idle deadlines close resources and reject pending operations", async (t) => {
|
|
const peer = await server(t, () => {});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance } = broker(t, endpoint);
|
|
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
const reading = instance.read(handleId, ctx);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
t.mock.timers.tick(LIMITS.idleMs);
|
|
await assert.rejects(reading, { code: RPC_ERRORS.deadlineExceeded });
|
|
assert.equal(instance.connections.size, 0);
|
|
let resolveDns;
|
|
let dialed = false;
|
|
const slow = broker(t, null, {
|
|
lookup: () => new Promise((resolve) => { resolveDns = resolve; }),
|
|
connectSocket: () => { dialed = true; },
|
|
}).instance;
|
|
const pending = slow.connect("tcp://example.test:22", ctx);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
t.mock.timers.tick(LIMITS.connectMs);
|
|
await assert.rejects(pending, { code: RPC_ERRORS.deadlineExceeded });
|
|
resolveDns([{ address: "203.0.113.1", family: 4 }]);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(dialed, false);
|
|
assert.equal(slow.connections.size, 0);
|
|
});
|
|
|
|
test("concurrent operations and oversized writes cannot grow an unbounded queue", async (t) => {
|
|
const peer = await server(t, () => {});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
const pending = instance.read(handleId, ctx);
|
|
await assert.rejects(instance.read(handleId, ctx), { code: RPC_ERRORS.failedPrecondition });
|
|
await assert.rejects(instance.write(handleId, new Uint8Array(LIMITS.chunkBytes + 1), ctx), { code: RPC_ERRORS.invalidArgument });
|
|
const writing = instance.write(handleId, Uint8Array.of(1), ctx);
|
|
await assert.rejects(instance.write(handleId, Uint8Array.of(2), ctx), { code: RPC_ERRORS.resourceExhausted });
|
|
await writing;
|
|
instance.close(handleId, ctx);
|
|
await assert.rejects(pending, { code: RPC_ERRORS.cancelled });
|
|
});
|
|
|
|
test("connection reservations bound concurrent DNS operations", async (t) => {
|
|
const { instance } = broker(t, null, { lookup: () => new Promise(() => {}) });
|
|
const ctx = context();
|
|
const pending = Array.from({ length: LIMITS.connectionsPerRuntime }, () => instance.connect("tcp://example.test:22", ctx));
|
|
const results = Promise.allSettled(pending);
|
|
await assert.rejects(instance.connect("tcp://example.test:22", ctx), { code: RPC_ERRORS.resourceExhausted });
|
|
instance.releaseRuntime(ctx.runtimeId);
|
|
assert.ok((await results).every((result) => result.status === "rejected"));
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("real unread TCP output stays bounded and byte quota closes the socket", async (t) => {
|
|
const peer = await server(t, (socket) => {
|
|
const flood = () => { while (!socket.destroyed && socket.write(Buffer.alloc(LIMITS.chunkBytes))) {} };
|
|
socket.on("drain", flood);
|
|
flood();
|
|
});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
const { instance } = broker(t, endpoint, { quotaManager: {
|
|
chargeBytes() { throw new Error("quota reached"); },
|
|
} });
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
const state = instance.connections.get(handleId);
|
|
assert.ok(state.socket.readableLength <= LIMITS.bufferBytes + LIMITS.chunkBytes);
|
|
await assert.rejects(instance.read(handleId, ctx), /quota reached/u);
|
|
assert.equal(instance.connections.size, 0);
|
|
});
|
|
|
|
test("real TLS validates trust and the original hostname against a pinned IP", async (t) => {
|
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-stream-tls-test-"));
|
|
t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
|
const keyPath = path.join(directory, "test.key");
|
|
const certPath = path.join(directory, "test.crt");
|
|
execFileSync("openssl", ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", keyPath,
|
|
"-out", certPath, "-days", "1", "-subj", "/CN=stream-fixture.test", "-addext", "subjectAltName=DNS:stream-fixture.test"],
|
|
{ stdio: "ignore" });
|
|
const cert = fs.readFileSync(certPath);
|
|
const peer = await server(t, (socket) => socket.on("data", (data) => socket.write(data)), {
|
|
key: fs.readFileSync(keyPath), cert,
|
|
});
|
|
const endpoint = `tls://stream-fixture.test:${peer.address().port}`;
|
|
const lookup = async () => [{ address: "127.0.0.1", family: 4 }];
|
|
const untrusted = broker(t, endpoint, { lookup }).instance;
|
|
await assert.rejects(untrusted.connect(endpoint, context()), { code: RPC_ERRORS.unavailable });
|
|
let dialOptions;
|
|
const trusted = broker(t, endpoint, { lookup,
|
|
connectTls: (options) => { dialOptions = options; return tls.connect({ ...options, ca: cert }); },
|
|
}).instance;
|
|
const ctx = context();
|
|
const { handleId } = await trusted.connect(endpoint, ctx);
|
|
assert.equal(dialOptions.host, "127.0.0.1");
|
|
assert.equal(dialOptions.servername, "stream-fixture.test");
|
|
assert.equal(dialOptions.rejectUnauthorized, true);
|
|
await trusted.write(handleId, Uint8Array.of(42), ctx);
|
|
assert.deepEqual(await trusted.read(handleId, ctx), Uint8Array.of(42));
|
|
trusted.close(handleId, ctx);
|
|
const wrongEndpoint = `tls://wrong-fixture.test:${peer.address().port}`;
|
|
const wrong = broker(t, wrongEndpoint, { lookup, connectTls: (options) => tls.connect({ ...options, ca: cert }) }).instance;
|
|
await assert.rejects(wrong.connect(wrongEndpoint, context()), { code: RPC_ERRORS.unavailable });
|
|
});
|
|
|
|
test("dynamic local opt-in allows loopback when approved but unconditionally blocks Netcatty internal control ports and metadata IPs", async (t) => {
|
|
const peer = await server(t, (socket) => socket.on("data", (bytes) => socket.write(bytes)));
|
|
const port = peer.address().port;
|
|
const endpoint = `tcp://127.0.0.1:${port}`;
|
|
const { instance } = broker(t, null, {
|
|
isInternalControlPort: (_address, targetPort) => targetPort === port,
|
|
});
|
|
// Even with localApproved: true, Netcatty's own internal control port is unconditionally blocked.
|
|
await assert.rejects(
|
|
instance.connect(endpoint, context(), undefined, { localApproved: true, skipPermissionAuthorize: true }),
|
|
{ code: RPC_ERRORS.permissionDenied },
|
|
);
|
|
// Link-local cloud metadata IP is unconditionally blocked even with localApproved: true.
|
|
await assert.rejects(
|
|
instance.connect("tcp://169.254.169.254:80", context(), undefined, { localApproved: true, skipPermissionAuthorize: true }),
|
|
{ code: RPC_ERRORS.permissionDenied },
|
|
);
|
|
// Approved loopback to a non-control DB port succeeds cleanly.
|
|
const allowed = broker(t, null, {
|
|
isInternalControlPort: () => false,
|
|
}).instance;
|
|
const ctx = context();
|
|
const handle = await allowed.connect(endpoint, ctx, undefined, { localApproved: true, skipPermissionAuthorize: true });
|
|
await allowed.write(handle.handleId, Uint8Array.from([1, 2, 3]), ctx);
|
|
assert.deepEqual(await allowed.read(handle.handleId, ctx), Uint8Array.from([1, 2, 3]));
|
|
allowed.close(handle.handleId, ctx);
|
|
});
|
|
|
|
test("in-band STARTTLS upgradeTls upgrades a plaintext TCP stream with CA validation, pending-read resume, and explicit self-signed cert approval", async (t) => {
|
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-starttls-test-"));
|
|
t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
|
const keyPath = path.join(directory, "test.key");
|
|
const certPath = path.join(directory, "test.crt");
|
|
execFileSync("openssl", ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", keyPath,
|
|
"-out", certPath, "-days", "1", "-subj", "/CN=db.local.test", "-addext", "subjectAltName=DNS:db.local.test"],
|
|
{ stdio: "ignore" });
|
|
const key = fs.readFileSync(keyPath);
|
|
const cert = fs.readFileSync(certPath);
|
|
|
|
// Simulate PostgreSQL SSLRequest -> 'S' -> STARTTLS upgrade -> encrypted echo.
|
|
const peer = await server(t, (rawSocket) => {
|
|
rawSocket.once("data", (chunk) => {
|
|
if (chunk.toString() === "SSLREQ") {
|
|
rawSocket.write("S");
|
|
const tlsSocket = new tls.TLSSocket(rawSocket, { isServer: true, key, cert });
|
|
tlsSocket.on("error", () => {});
|
|
tlsSocket.on("data", (data) => tlsSocket.write(Buffer.concat([Buffer.from("TLS:"), data])));
|
|
}
|
|
});
|
|
});
|
|
const endpoint = `tcp://127.0.0.1:${peer.address().port}`;
|
|
|
|
// 1) Trusted CA upgrade with a pending read() already active on the plaintext socket.
|
|
{
|
|
const { instance } = broker(t, endpoint);
|
|
const ctx = context();
|
|
const { handleId } = await instance.connect(endpoint, ctx);
|
|
await instance.write(handleId, Buffer.from("SSLREQ"), ctx);
|
|
assert.equal(Buffer.from(await instance.read(handleId, ctx)).toString(), "S");
|
|
// Start a pending read (mirrors byteStreamService.pump()) BEFORE calling upgradeTls.
|
|
const pendingRead = instance.read(handleId, ctx);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
const upgraded = await instance.upgradeTls(handleId, { servername: "db.local.test", caPem: cert.toString("utf8") }, ctx);
|
|
assert.equal(upgraded.upgraded, true);
|
|
assert.equal(upgraded.authorized, true);
|
|
assert.ok(upgraded.fingerprint256);
|
|
await instance.write(handleId, Buffer.from("SELECT 1"), ctx);
|
|
assert.equal(Buffer.from(await pendingRead).toString(), "TLS:SELECT 1");
|
|
instance.close(handleId, ctx);
|
|
}
|
|
|
|
// 2) Self-signed cert fails closed without allowSelfSigned, prompts user with allowSelfSigned, and honors remembered fingerprint.
|
|
{
|
|
const trustedFingerprints = new Set();
|
|
const prompts = [];
|
|
const { instance } = broker(t, endpoint, {
|
|
isTrustedTlsCert: (_pluginId, _ep, fp) => trustedFingerprints.has(fp),
|
|
rememberTlsCert: (_pluginId, _ep, fp) => trustedFingerprints.add(fp),
|
|
confirmTlsCert: async (info) => {
|
|
prompts.push(info);
|
|
return { approved: prompts.length > 1, remember: true };
|
|
},
|
|
});
|
|
const ctx = context();
|
|
// First attempt: user rejects self-signed cert -> fails closed.
|
|
const first = await instance.connect(endpoint, ctx);
|
|
await instance.write(first.handleId, Buffer.from("SSLREQ"), ctx);
|
|
assert.equal(Buffer.from(await instance.read(first.handleId, ctx)).toString(), "S");
|
|
await assert.rejects(
|
|
instance.upgradeTls(first.handleId, { servername: "db.local.test", allowSelfSigned: true }, ctx),
|
|
{ code: RPC_ERRORS.permissionDenied },
|
|
);
|
|
assert.equal(prompts.length, 1);
|
|
assert.ok(prompts[0].fingerprint256.length > 20);
|
|
assert.match(prompts[0].subject, /CN=db\.local\.test/);
|
|
|
|
// Second attempt: user approves with remember: true.
|
|
const second = await instance.connect(endpoint, ctx);
|
|
await instance.write(second.handleId, Buffer.from("SSLREQ"), ctx);
|
|
assert.equal(Buffer.from(await instance.read(second.handleId, ctx)).toString(), "S");
|
|
const res2 = await instance.upgradeTls(second.handleId, { servername: "db.local.test", allowSelfSigned: true }, ctx);
|
|
assert.equal(res2.upgraded, true);
|
|
assert.equal(res2.authorized, false);
|
|
assert.equal(prompts.length, 2);
|
|
await instance.write(second.handleId, Buffer.from("PING"), ctx);
|
|
assert.equal(Buffer.from(await instance.read(second.handleId, ctx)).toString(), "TLS:PING");
|
|
instance.close(second.handleId, ctx);
|
|
|
|
// Third attempt: remembered fingerprint skips prompt automatically.
|
|
const third = await instance.connect(endpoint, ctx);
|
|
await instance.write(third.handleId, Buffer.from("SSLREQ"), ctx);
|
|
assert.equal(Buffer.from(await instance.read(third.handleId, ctx)).toString(), "S");
|
|
const res3 = await instance.upgradeTls(third.handleId, { servername: "db.local.test", allowSelfSigned: true }, ctx);
|
|
assert.equal(res3.upgraded, true);
|
|
assert.equal(prompts.length, 2);
|
|
instance.close(third.handleId, ctx);
|
|
}
|
|
});
|
|
|
|
test("DB-friendly stream budgets expose expanded concurrency, idle, lifetime, and byte caps", () => {
|
|
assert.equal(LIMITS.connectionsPerRuntime, 24);
|
|
assert.equal(LIMITS.idleMs, 1_800_000);
|
|
assert.equal(LIMITS.lifetimeMs, 28_800_000);
|
|
assert.equal(LIMITS.connectionBytes, 536_870_912);
|
|
});
|