YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
Merge commit from fork
Louis Beaumont52297f8edee9
diff --git a/packages/screenpipe-mcp/src/http-origin.test.ts b/packages/screenpipe-mcp/src/http-origin.test.tsnew file mode 100644index 000000000..bccd91930--- /dev/null+++ b/packages/screenpipe-mcp/src/http-origin.test.ts@@ -0,0 +1,50 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com+import { describe, it, expect } from "vitest";+import { request } from "node:http";+import { buildHttpServer } from "./http-server";++describe("HTTP MCP browser boundary", () => {+ it("rejects rebinding and cross-origin requests before creating a session", async () => {+ const server = buildHttpServer({ host: "127.0.0.1", mcpPort: 0, screenpipePort: 1 });+ await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));+ const address = server.address();+ if (!address || typeof address === "string") throw Error("no listener");+ const port = address.port;+ const call = (headers: Record<string, string | undefined>, method = "POST", path = "/mcp") => new Promise<{status: number; session?: string; body: string}>((resolve, reject) => {+ const req = request({ hostname: "127.0.0.1", port, method, path, headers: {+ "content-type": "application/json", accept: "application/json, text/event-stream", ...headers,+ } }, res => {+ let body = "";+ res.on("data", chunk => body += chunk);+ res.on("end", () => resolve({status: res.statusCode!, session: res.headers["mcp-session-id"] as string | undefined, body}));+ });+ req.on("error", reject);+ req.end(method === "POST" ? JSON.stringify({jsonrpc:"2.0",id:1,method:"initialize",params:{protocolVersion:"2024-11-05",capabilities:{},clientInfo:{name:"boundary-test",version:"1"}}}) : undefined);+ });+ try {+ for (const headers of [+ {host: `rebind.example:${port}`, origin: `http://rebind.example:${port}`},+ {host: `rebind.example:${port}`},+ {host: `127.0.0.1:${port}`, origin: "https://evil.example"},+ {host: `localhost:${port}`, origin: "null"},+ {host: `localhost.evil.example:${port}`},+ {host: `127.0.0.1:${port}`, origin: "http://[email protected]"},+ ]) {+ const result = await call(headers);+ expect(result.status, JSON.stringify(headers)).toBe(401);+ expect(result.session).toBeUndefined();+ }+ const health = await call({host:`127.0.0.1:${port}`}, "GET", "/health");+ expect(JSON.parse(health.body).sessions).toBe(0);+ const native = await call({host:`127.0.0.1:${port}`});+ expect(native.status).toBe(200);+ expect(native.session).toBeTruthy();+ const localBrowser = await call({host:`localhost:${port}`, origin:`http://localhost:${port}`});+ expect(localBrowser.status).toBe(200);+ } finally {+ server.closeAllConnections();+ await new Promise<void>(resolve => server.close(() => resolve()));+ }+ });+});diff --git a/packages/screenpipe-mcp/src/http-server.test.ts b/packages/screenpipe-mcp/src/http-server.test.tsindex d100b963b..5d14d70fd 100644--- a/packages/screenpipe-mcp/src/http-server.test.ts+++ b/packages/screenpipe-mcp/src/http-server.test.ts@@ -145,7 +145,7 @@ describe("isAuthorized", () => {});const local = (auth?: string) => ({socket: { remoteAddress: "127.0.0.1" },- headers: auth ? { authorization: auth } : {},+ headers: { host: "127.0.0.1:3031", ...(auth ? { authorization: auth } : {}) },});it("allows loopback with no api key", () => {diff --git a/packages/screenpipe-mcp/src/http-server.ts b/packages/screenpipe-mcp/src/http-server.tsindex cb72151a6..b04948882 100644--- a/packages/screenpipe-mcp/src/http-server.ts+++ b/packages/screenpipe-mcp/src/http-server.ts@@ -126,22 +126,37 @@ export function isLoopbackRequest(req: { socket: { remoteAddress?: string } }):}/**- * Authorization decision. Loopback is always allowed; non-loopback requires+ * Authorization decision. Literal loopback hosts with no foreign Origin are allowed; other requests require* a matching bearer token when one is configured. If no api key is set* (loopback-only deployment), non-loopback shouldn't even be reachable —* but we still 401 it as belt-and-suspenders.*/export function isAuthorized(- req: { socket: { remoteAddress?: string }; headers: { authorization?: string } },+ req: { socket: { remoteAddress?: string }; headers: { authorization?: string; host?: string; origin?: string } },apiKey: string | undefined): boolean {- if (isLoopbackRequest(req)) return true;+ // The socket alone is insufficient: a browser can resolve an attacker-owned+ // hostname to loopback, or call localhost with a foreign Origin.+ if (isLoopbackRequest(req) && isTrustedLocalHost(req.headers.host)+ && (req.headers.origin === undefined || isTrustedLocalOrigin(req.headers.origin))) return true;if (!apiKey) return false;const expected = `Bearer ${apiKey}`;const got = req.headers.authorization ?? "";return constantTimeEq(got, expected);}+function isTrustedLocalHost(host: string | undefined): boolean {+ if (!host) return false;+ // Match the raw authority, not URL-normalized input (userinfo, paths and+ // numeric aliases must not acquire localhost privileges).+ return /^(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?$/i.test(host);+}++function isTrustedLocalOrigin(origin: string): boolean {+ const match = /^https?:\/\/([^/?#]+)$/.exec(origin);+ return match !== null && isTrustedLocalHost(match[1]);+}+/** Constant-time string compare to keep timing attacks off the table. */function constantTimeEq(a: string, b: string): boolean {if (a.length !== b.length) return false;