diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28996f6..f472eda 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -236,6 +236,9 @@ jobs: [[ "$code" == "503" ]] || { echo "FAIL: unsafe Cloud response on ${route}: ${code}"; exit 1; } done echo "PASS: Worker runtime rejects all tested routes with staging disabled." + - name: Verify local Cloud D1 and Durable Object WebSocket lock flow + timeout-minutes: 8 + run: node cloud/scripts/test-local-realtime.mjs consistency: name: Repository consistency diff --git a/cloud/scripts/test-local-realtime.mjs b/cloud/scripts/test-local-realtime.mjs new file mode 100644 index 0000000..59bb3fb --- /dev/null +++ b/cloud/scripts/test-local-realtime.mjs @@ -0,0 +1,271 @@ +#!/usr/bin/env node +/** + * LOCAL workerd + D1 + Durable Object + Protocol v1 integration test. + * No remote Cloudflare API calls, Apple login or physical OS locking. + * A private temporary database and two synthetic account sessions are used. + */ +import assert from "node:assert/strict"; +import { createHash, randomBytes, randomUUID } from "node:crypto"; +import { spawn, spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync, unlinkSync, existsSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { resolve, dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import http from "node:http"; + +const root = resolve(dirname(fileURLToPath(import.meta.url)), "..", ".."); +const config = "cloud/wrangler.integration.ci.jsonc"; +const wrangler = join(root, "node_modules/wrangler/bin/wrangler.js"); +const devVars = join(root, "cloud/.dev.vars"); +const state = mkdtempSync(join(tmpdir(), "mote-cloud-ci-")); +const port = 8798; +const base = `http://127.0.0.1:${port}`; +const gate = randomBytes(32).toString("base64url"); +const internal = randomBytes(32).toString("base64url"); +const tokenA = randomBytes(32).toString("base64url"); +const tokenB = randomBytes(32).toString("base64url"); +const credentialA = randomBytes(32).toString("base64url"); +const credentialB = randomBytes(32).toString("base64url"); +const A = randomUUID(), B = randomUUID(), DA = randomUUID(), DB = randomUUID(); +const now = Date.now(); +const hash = value => createHash("sha256").update(value).digest("hex"); +let worker; +let createdVars = false; +let workerOutput = ""; +const env = { + ...process.env, + CLOUDFLARE_API_TOKEN: "", + CLOUDFLARE_ACCOUNT_ID: "", + WRANGLER_SEND_METRICS: "false", + CI: "true", +}; +function cli(args) { + const result = spawnSync(process.execPath, [wrangler, ...args], { + cwd: root, env, encoding: "utf8", maxBuffer: 8_000_000, timeout: 60_000, + }); + if (result.status !== 0) { + // Never print generated credentials or fixture SQL. + throw new Error(`Local Wrangler command failed: ${args.slice(0, 3).join(" ")}: ${(result.stderr ?? "").slice(-1300)}`); + } +} +function wsFrame(value, opcode = 1) { + const payload = Buffer.from(opcode === 1 ? JSON.stringify(value) : ""); + assert.ok(payload.length < 65536); + const size = payload.length < 126 ? 2 : 4; + const header = Buffer.alloc(size); + header[0] = 0x80 | opcode; + header[1] = 0x80 | (size === 2 ? payload.length : 126); + if (size === 4) header.writeUInt16BE(payload.length, 2); + const mask = randomBytes(4); + const masked = Buffer.from(payload); + for (let i = 0; i < payload.length; i++) masked[i] ^= mask[i % 4]; + return Buffer.concat([header, mask, masked]); +} +class NativeWs { + constructor(socket, head) { + this.socket = socket; + this.pending = []; + this.waiters = []; + this.buffer = Buffer.alloc(0); + this.closed = false; + socket.on("data", data => this.receive(data)); + socket.on("close", () => { + this.closed = true; + for (const waiter of this.waiters) {clearTimeout(waiter.timeout); waiter.reject(new Error("WS closed")); } + this.waiters = []; + }); + socket.on("error", () => { this.closed = true; }); + if (head?.length) this.receive(head); + } + receive(chunk) { + this.buffer = Buffer.concat([this.buffer, chunk]); + while (this.buffer.length >= 2) { + const opcode = this.buffer[0] & 0x0f; + const hasMask = (this.buffer[1] & 0x80) !== 0; + let length = this.buffer[1] & 0x7f; + let pos = 2; + if (length === 126) { + if (this.buffer.length < 4) return; + length = this.buffer.readUInt16BE(2); pos = 4; + } else if (length === 127) { + if (this.buffer.length < 10) return; + const big = this.buffer.readBigUInt64BE(2); + assert.ok(big < 1_000_000n); + length = Number(big); pos = 10; + } + if (hasMask) pos += 4; + if (this.buffer.length < pos + length) return; + let payload = this.buffer.subarray(pos, pos + length); + if (hasMask) { + const key = this.buffer.subarray(pos - 4, pos); + payload = Buffer.from(payload); + for (let i = 0; i < length; i++) payload[i] ^= key[i % 4]; + } + this.buffer = this.buffer.subarray(pos + length); + if (opcode === 8) { this.closed = true; this.socket.destroy(); return; } + if (opcode === 9) { this.socket.write(wsFrame({}, 10)); continue; } + if (opcode !== 1) continue; + let msg; + try { msg = JSON.parse(payload.toString("utf8")); } + catch { continue; } + const idx = this.waiters.findIndex(w => w.predicate(msg)); + if (idx >= 0) { + const [w] = this.waiters.splice(idx, 1); + clearTimeout(w.timeout); w.resolve(msg); + } else this.pending.push(msg); + } + } + send(msg) { this.socket.write(wsFrame(msg)); } + waitFor(predicate, timeoutMs = 8_000) { + const found = this.pending.findIndex(predicate); + if (found >= 0) return Promise.resolve(this.pending.splice(found, 1)[0]); + return new Promise((resolve, reject) => { + const waiter = { predicate, resolve, reject, timeout: undefined }; + waiter.timeout = setTimeout(() => { + this.waiters = this.waiters.filter(w => w !== waiter); + reject(new Error("Expected WebSocket frame missing")); + }, timeoutMs); + this.waiters.push(waiter); + }); + } + close() { if (!this.closed) {this.socket.write(wsFrame({}, 8)); this.socket.end();} } +} +function connect(deviceId, forgedHeaders = false) { + return new Promise((resolve, reject) => { + const req = http.request({ + host: "127.0.0.1", port, + path: `/cloud/v1/ws/device/${deviceId}`, + headers: { + Connection: "Upgrade", + Upgrade: "websocket", + "Sec-WebSocket-Version": "13", + "Sec-WebSocket-Key": randomBytes(16).toString("base64"), + "X-Mote-Staging-Gate": gate, + ...(forgedHeaders ? { "X-Mote-Cloud-Owner": B, + "X-Mote-Cloud-Internal": "forged-by-desktop" } : {}), + }, + }); + req.once("upgrade", (response, socket, head) => { + if (response.statusCode !== 101) {socket.destroy();reject(new Error("Not upgraded"));return;} + resolve(new NativeWs(socket, head)); + }); + req.once("response", res => {res.resume();reject(new Error(`WebSocket HTTP ${res.statusCode}`));}); + req.once("error", reject); + req.end(); + }); +} +async function call(path, { method = "GET", token, body, withGate = true } = {}) { + const resp = await fetch(base + path, { + method, + headers: { + ...(withGate ? { "X-Mote-Staging-Gate": gate } : {}), + ...(token ? { Authorization: `Bearer ${token}` } : {}), + ...(body ? { "Content-Type": "application/json" } : {}), + }, + ...(body ? { body: JSON.stringify(body) } : {}), + signal: AbortSignal.timeout(12_000), + }); + const payload = await resp.text(); + return { status: resp.status, body: payload ? JSON.parse(payload) : null }; +} +async function awaitServer() { + for (let i = 0; i < 75; i++) { + if (worker.exitCode !== null) throw new Error("Local workerd exited early"); + try { + const response = await call("/cloud/v1/health"); + if (response.status === 200) return; + } catch {/* starting */} + await new Promise(resolve => setTimeout(resolve, 400)); + } + throw new Error("Local workerd failed to become ready: " + workerOutput.slice(-2000)); +} +async function main() { + if (existsSync(devVars)) { + throw new Error("Refusing to overwrite any existing .dev.vars"); + } + writeFileSync(devVars, `MOTE_CLOUD_STAGING_GATE="${gate}"\nMOTE_CLOUD_INTERNAL_SECRET="${internal}"\n`, + { flag: "wx", mode: 0o600 }); + createdVars = true; + cli(["d1", "migrations", "apply", "CLOUD_DB", "--local", "--config", config, "--persist-to", state]); + const fixture = join(state, "fixture.sql"); + writeFileSync(fixture, ` + INSERT INTO cloud_accounts (id, apple_subject, status, created_at_ms) + VALUES ('${A}','local-a','active',${now}),('${B}','local-b','active',${now}); + INSERT INTO cloud_devices (id, owner_account_id, name, platform, credential_hash, status, created_at_ms, updated_at_ms) + VALUES ('${DA}','${A}','A PC','windows','${hash(credentialA)}','active',${now},${now}), + ('${DB}','${B}','B PC','macos','${hash(credentialB)}','active',${now},${now}); + INSERT INTO cloud_sessions (id, account_id, refresh_token_hash, access_token_hash, created_at_ms, expires_at_ms, access_expires_at_ms) + VALUES ('${randomUUID()}','${A}','${hash("refresh-a")}', '${hash(tokenA)}',${now},${now+600000},${now+600000}), + ('${randomUUID()}','${B}','${hash("refresh-b")}', '${hash(tokenB)}',${now},${now+600000},${now+600000}); + `, { mode: 0o600 }); + cli(["d1", "execute", "CLOUD_DB", "--local", "--config", config, "--persist-to", state, "--file", fixture]); + worker = spawn(process.execPath, [wrangler, "dev", "--config", config, "--local", + "--ip", "127.0.0.1", "--port", String(port), "--persist-to", state], { + cwd: root, env, stdio: ["ignore","pipe","pipe"], + }); + for (const stream of [worker.stdout, worker.stderr]) { + stream.on("data", chunk => {workerOutput = (workerOutput + chunk.toString("utf8")).slice(-8000);}); + } + await awaitServer(); + assert.equal((await call("/cloud/v1/health", { withGate:false })).status, 503); + assert.equal((await call("/cloud/v1/health")).status, 200); + const listA = await call("/cloud/v1/devices", { token: tokenA }); + const listB = await call("/cloud/v1/devices", { token: tokenB }); + assert.equal(listA.status, 200); assert.equal(listB.status, 200); + assert.deepEqual(listA.body.devices.map(d => d.id), [DA]); + assert.deepEqual(listB.body.devices.map(d => d.id), [DB]); + const command = (token,deviceId) => call(`/cloud/v1/devices/${deviceId}/commands`, { + method:"POST", token, body:{ action:"lock" }, + }); + assert.equal((await command(tokenA, DA)).status, 409, "Offline must not enqueue"); + assert.equal((await command(tokenB, DA)).status, 404, "Other tenant must be denied"); + const wrong = await connect(DA); + wrong.send({type:"auth",version:1,device_id:DA,credential:credentialB,platform:"windows",actions:["lock"]}); + await new Promise(resolve => setTimeout(resolve, 300)); + assert.ok(wrong.closed, "Incorrect device proof must disconnect"); + // Verify caller-controlled internal headers cannot redirect the tenant. + const device = await connect(DA, true); + try { + device.send({type:"auth",version:1,device_id:DA,credential:credentialA,platform:"windows",actions:["lock"]}); + const auth = await device.waitFor(x => x.type === "auth_result"); + assert.equal(auth.status,"ok"); + device.send({type:"heartbeat",version:1,device_id:DA,sent_at:Date.now()}); + assert.equal((await device.waitFor(x=>x.type==="heartbeat_ack")).type,"heartbeat_ack"); + const pending = command(tokenA, DA); + const frame = await device.waitFor(x=>x.type==="command",8_000); + assert.equal(frame.action,"lock");assert.equal(frame.device_id,DA); + assert.equal(frame.expires_at-frame.created_at,10_000); + device.send({type:"command_result",version:1,command_id:frame.id,status:"completed",completed_at:Date.now()}); + const response = await pending; + assert.equal(response.status,200);assert.equal(response.body.status,"completed"); + assert.equal((await command(tokenB, DA)).status,404); + // Both tenants are simultaneously connected: B cannot receive A's + // command and an independent B command cannot reach A's socket. + const deviceB = await connect(DB); + try { + deviceB.send({type:"auth",version:1,device_id:DB, + credential:credentialB,platform:"macos",actions:["lock"]}); + assert.equal((await deviceB.waitFor(x=>x.type==="auth_result")).status,"ok"); + const pendingB = command(tokenB, DB); + const frameB = await deviceB.waitFor(x=>x.type==="command"); + assert.equal(frameB.device_id,DB); + assert.equal(device.pending.filter(x=>x.type==="command").length,0); + deviceB.send({type:"command_result",version:1,command_id:frameB.id, + status:"completed",completed_at:Date.now()}); + assert.equal((await pendingB).status,200); + assert.equal((await command(tokenA, DB)).status,404); + } finally { deviceB.close(); } + } finally { + device.close(); + } + await new Promise(resolve=>setTimeout(resolve,350)); + assert.equal((await command(tokenA,DA)).status,409,"Disconnected commands must not queue"); + console.log("CLOUD_WORKER_LOCAL_REALTIME_PASS: D1+DO+WebSocket auth/lock/owner/offline"); +} +try {await main();} +catch(error){ console.error("CLOUD_WORKER_LOCAL_REALTIME_FAIL: "+String(error?.message??error));process.exitCode=1; } +finally{ + if(worker){worker.kill("SIGTERM");} + if(createdVars){try{unlinkSync(devVars);}catch{}} + rmSync(state,{recursive:true,force:true}); +} diff --git a/cloud/wrangler.integration.ci.jsonc b/cloud/wrangler.integration.ci.jsonc new file mode 100644 index 0000000..ec0cbd6 --- /dev/null +++ b/cloud/wrangler.integration.ci.jsonc @@ -0,0 +1,38 @@ +{ + "$schema": "../node_modules/wrangler/config-schema.json", + "name": "mote-cloud-local-ci", + "main": "../relay/src/cloud/cloudWorker.ts", + "compatibility_date": "2026-09-28", + "compatibility_flags": [ + "nodejs_compat" + ], + "workers_dev": false, + "vars": { + "MOTE_CLOUD_STAGING_ENABLED": "true", + "APPLE_CLIENT_ID": "com.nardo021.mote.localci" + }, + "d1_databases": [ + { + "binding": "CLOUD_DB", + "database_name": "mote-cloud-local-only", + "database_id": "00000000-0000-4000-8000-000000000001", + "migrations_dir": "migrations" + } + ], + "durable_objects": { + "bindings": [ + { + "name": "CLOUD_TENANTS", + "class_name": "MoteCloudTenant" + } + ] + }, + "migrations": [ + { + "tag": "cloud-v1", + "new_sqlite_classes": [ + "MoteCloudTenant" + ] + } + ] +} diff --git a/docs/cloud-p0c3c-local-integration.md b/docs/cloud-p0c3c-local-integration.md new file mode 100644 index 0000000..5251017 --- /dev/null +++ b/docs/cloud-p0c3c-local-integration.md @@ -0,0 +1,92 @@ +# Mote Cloud P0C3C โ€” Local workerd / D1 / tenant Durable Object integration + +**Date:** 2026-11-17 ยท **Status:** LOCAL-INTEGRATION PENDING CI; no Cloudflare staging Worker deployed. + +## Why this stage exists + +P0C3B proved a **disabled** Cloud Worker responds 503 in actual local +workerd. P0C2's device WebSocket/lock tests were mocked with Node's SQLite +adapter and an in-process socket. Neither test exercises the real Cloudflare +Durable Object implementation, persisted D1 binding, upgraded WebSocket +handshake or Worker-to-DO forwarding. + +P0C3C adds a separate **ephemeral, local-only** integration workload. +This is not a real Cloudflare deployment, Apple login or physical OS lock. + +## Test configuration + +`cloud/wrangler.integration.ci.jsonc`: + +- Distinct Worker name `mote-cloud-local-ci`, no `workers_dev` / routes. +- A synthetic local-only D1 name and UUID, with the **same** three schema + migrations. No remote Cloudflare D1 UUID appears in this file. +- Distinct tenant Durable Object binding and SQLite class migration. +- Enabled locally **only** with a generated 256-bit test gate and a separate + generated 256-bit internal DO secret. The secrets are written to an ignored + `cloud/.dev.vars` with exclusive create and mode 0600, then deleted. + If that file already exists, the test refuses to overwrite it. +- Compatibility date remains equal to the verified Cloud staging and CI + `2026-09-28` baseline. The existing self-host Worker is unchanged. +- `wrangler d1 ... --local --persist-to ` and + `wrangler dev --local --persist-to ` use only an ephemeral + filesystem. **No remote D1 migration, DNS, API token or Worker deployment.** + +## Concrete acceptance assertions + +`cloud/scripts/test-local-realtime.mjs` starts a local workerd server and +uses two synthetic Apple-subject account rows, two device credential hashes, +and two opaque access-token hashes. It uses Node's native HTTP Upgrade +request and masked WebSocket frames (avoids a new dependency): + +1. Missing staging gate returns HTTP 503, correct test gate allows health. +2. Account A can list only A's device and account B can list only B's. +3. An owner `lock` when A's desktop is offline returns 409 and cannot + be delivered on later connection. +4. B's valid access token cannot issue `lock` to A's device (404). +5. A WebSocket using B's device credential to authenticate as A must close. +6. A matching device credential receives Protocol v1 `auth_result=ok`. +7. The live socket sends a heartbeat and receives a real `heartbeat_ack`. +8. A phone `lock` request passes through gateway โ†’ real local tenant DO โ†’ + authenticated WebSocket as a Protocol v1 command, with `expires_at - + created_at = 10000`; the mock device's `command_result` returns an + HTTP completed result to the phone. +9. Both account A and account B can hold authenticated WebSockets at once, + without another account receiving the device command. Attempts to supply + forged `X-Mote-Cloud-Owner` and internal-secret headers from the desktop + cannot redirect it to a different account Durable Object. +10. A device disconnect returns future owner commands to 409; no offline + persistence or queue. + +The mock device **does not call macOS/Windows lock APIs**. These tests also +do not force a real Durable Object hibernation/restart or revoke a real +Cloudflare-managed connection; both remain separate release gates. + +## Run + +```sh +npm ci +npm ci --prefix relay +node cloud/scripts/test-local-realtime.mjs +``` + +The GitHub Cloud staging CI job must run this in addition to the +disabled-route workerd smoke and bundle dry-run. It must not be marked +PASS until the **current head SHA** run finishes successfully. + +## Next stage / deployment blocker + +The actual Cloudflare `mote-cloud-staging` Worker/tenant DO is still +absent. It is **not safe to activate**, because the real iOS Sign in with +Apple App ID, platform-native iOS app, operator-only staging secret +provisioning, public rate limits, account deletion, revocation broadcast +and real two-account/hardware cloud tests are incomplete. + +Once the local CI passes, the next independent deployment step is a +**disabled, non-routed** staging Worker with a reviewed operator +configuration and independently verified D1 binding. No public +`workers.dev` host/route should be enabled until the protected +end-to-end acceptance evidence exists. Do not substitute an example +Apple Bundle ID or publicly distribute a private Staging gate. + +See [P0C3B deployment guards](cloud-p0c3b-staging-gates.md) and +[P0C2 realtime design](cloud-p0c2-realtime.md).