mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 23:06:17 +01:00
test(dot): settle known pre-forward retirement refusals
Return the existing 403 refusal before relay creation when the exact current lease is no longer active. Preserve all current tuple checks, post-admission unknown outcomes and strict gateway closure. Exact verified-HTTPS control first reproduced the old undefined-versus403 failure; current treatment remains unqualified. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
9f7279fd61
commit
480582d28d
2 files changed
+51
-1
No files matched your search
@@ -24,7 +24,11 @@ export async function startOwnedWssGateway(options) {
|
||||
const current = await options.readCurrentLease();
|
||||
if (current.status !== "active" || current.generation !== options.generation
|
||||
|| current.providerLeaseId !== options.resource.id || current.companyId !== options.resource.companyId
|
||||
|| current.runId !== options.resource.runId || current.containerId !== options.resource.containerId || current.environmentId !== options.resource.environmentId) throw new Error("current_ingress_lease_denied");
|
||||
|| current.runId !== options.resource.runId || current.containerId !== options.resource.containerId || current.environmentId !== options.resource.environmentId) {
|
||||
// A current-lease refusal happens before any relay or forwarded bytes.
|
||||
// It is a known denial, including a reconnect while the lease retires.
|
||||
socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); return;
|
||||
}
|
||||
await options.verifyOwnedContainer(); // exact ID + image + labels, no reuse
|
||||
const child = spawn(options.docker, ["--host", options.endpoint, "exec", "-i", options.resource.containerId,
|
||||
"/usr/bin/node", options.relayPath, options.descriptorPath], {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { request } from "node:https";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { expect, it } from "vitest";
|
||||
import { startOwnedWssGateway } from "./owned-wss-gateway.mjs";
|
||||
|
||||
it("refuses an authenticated reconnect to a retired lease before relay creation and joins its gateway", async () => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), "owned-ingress-denial-"));
|
||||
let gateway: Awaited<ReturnType<typeof startOwnedWssGateway>> | undefined;
|
||||
let closeAttempted = false;
|
||||
try {
|
||||
const key = path.join(root, "key.pem"), cert = path.join(root, "cert.pem"), config = path.join(root, "tls.cnf");
|
||||
await writeFile(config, "[req]\ndistinguished_name=dn\nx509_extensions=ext\nprompt=no\n[dn]\nCN=Owned ingress regression\n[ext]\nsubjectAltName=IP:127.0.0.1\nbasicConstraints=critical,CA:TRUE\nkeyUsage=critical,digitalSignature,keyEncipherment,keyCertSign\n", { mode: 0o600 });
|
||||
await promisify(execFile)("/usr/bin/openssl", ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", key, "-out", cert, "-days", "1", "-config", config], { timeout: 5000 });
|
||||
const pending = new Set<Promise<void>>();
|
||||
let leaseReads = 0, containerVerifications = 0;
|
||||
const resource = { id: "owned-regression", companyId: "company", environmentId: "environment", runId: "run", containerId: "container" };
|
||||
gateway = await startOwnedWssGateway({ key: await readFile(key), cert: await readFile(cert), path: "/owned", secret: "owned-regression-secret", servername: "127.0.0.1", generation: "generation", resource, pending,
|
||||
// This control deliberately stops BEFORE any Docker/relay invocation.
|
||||
docker: "owned-test-must-not-spawn", endpoint: "unix:///owned-test-must-not-connect",
|
||||
ownedHome: root, ownedDockerConfig: root, runnerPort: 43127, relayPath: "/not-invoked", descriptorPath: "/not-invoked",
|
||||
readCurrentLease: async () => { leaseReads++; return { status: "stopped", generation: "generation", providerLeaseId: resource.id, companyId: resource.companyId, environmentId: resource.environmentId, runId: resource.runId, containerId: resource.containerId }; },
|
||||
verifyOwnedContainer: async () => { containerVerifications++; throw new Error("A denied lease must never reach container verification"); },
|
||||
});
|
||||
const ca = await readFile(cert);
|
||||
const observed = await new Promise<{ status?: number; error?: string }>((resolve, reject) => {
|
||||
const req = request({ host: "127.0.0.1", port: gateway!.port, path: "/owned", ca, rejectUnauthorized: true,
|
||||
headers: { Host: `127.0.0.1:${gateway!.port}`, Connection: "Upgrade", Upgrade: "websocket", "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==", "Sec-WebSocket-Version": "13", "x-owned-linux-ingress": "owned-regression-secret" } });
|
||||
req.once("response", response => { response.resume(); response.once("end", () => resolve({ status: response.statusCode })); });
|
||||
req.once("upgrade", (_response, socket) => { socket.end(); reject(new Error("Denied lease unexpectedly upgraded")); });
|
||||
req.once("error", error => resolve({ error: error.name })); req.end();
|
||||
});
|
||||
let closeError: unknown;
|
||||
closeAttempted = true;
|
||||
try { await gateway.close(); } catch (error) { closeError = error; }
|
||||
// These guards establish the actual current-lease branch before the RED oracle.
|
||||
expect(leaseReads).toBe(1); expect(containerVerifications).toBe(0); expect(gateway.records).toHaveLength(0);
|
||||
expect(observed.status).toBe(403);
|
||||
expect(observed.error).toBeUndefined(); expect(gateway.unknown).toEqual([]); expect(pending.size).toBe(0); expect(closeError).toBeUndefined();
|
||||
} finally {
|
||||
try { if (gateway && !closeAttempted) await gateway.close(); } finally { await rm(root, { recursive: true, force: true }); }
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user