Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ refused in every mode, including `allow_all`, and the browser's WebRTC traffic n
filter instead of around it. A computer run without an API server can set
`EGRESS_POLICY_REQUIRED=0` to keep the old behaviour.

### A malformed `%` in a stream URL no longer returns a 500

A request to `/api/computers/<id>/stream` whose id held a broken percent-escape, such as `%zz`, made the server throw and answer 500. It is now treated as not matching the stream route and goes through normal routing. Valid ids behave as before.

### `start.sh` names the port to change on macOS

When the API server's or the app's port was held by another process, `start.sh` was meant to say
Expand Down
18 changes: 18 additions & 0 deletions server/src/computer/stream-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Which Bot's screen. The Bot is named in the path and its computer is located the same way every
* other call locates it, so the live stream cannot point at a different Bot's browser.
*
* Its own module so it can be tested without starting a server: `index.ts` calls `serve()` at
* module scope, so importing it to reach one pure function binds a port.
*/
export function streamPathBotId(pathname: string): string | null {
const match = pathname.match(/^\/api\/computers\/([^/]+)\/stream$/);
if (!match?.[1]) return null;
try {
return decodeURIComponent(match[1]);
} catch {
// A malformed escape is not a Bot id. Decoding it used to throw URIError out of the fetch
// handler, turning one bad URL into a 500; let normal routing answer it instead.
return null;
}
}
10 changes: 1 addition & 9 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
parseApprovalContinuation,
parseApprovalResult,
} from "./approvals/types";
import { streamPathBotId } from "./computer/stream-path";
import {
type AuditInitiator,
createAuditReader,
Expand Down Expand Up @@ -3057,15 +3058,6 @@ const toStreamUrl = (baseUrl: string, botId: string) =>
// be reachable without it.
`${baseUrl.replace(/^http/, "ws").replace(/\/$/, "")}/stream?bot=${encodeURIComponent(botId)}&token=${encodeURIComponent(config.computer?.token ?? "")}`;

/**
* Which Bot's screen. The Bot is named in the path and its computer is located the same way every
* other call locates it, so the live stream cannot point at a different Bot's browser.
*/
const streamPathBotId = (pathname: string): string | null => {
const match = pathname.match(/^\/api\/computers\/([^/]+)\/stream$/);
return match?.[1] ? decodeURIComponent(match[1]) : null;
};

/** What each proxied socket carries: where to connect inward, and the socket once opened. */
type StreamData = {
upstream: string;
Expand Down
23 changes: 23 additions & 0 deletions server/tests/computer-stream-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "bun:test";
import { streamPathBotId } from "../src/computer/stream-path";

describe("which Bot a stream path names", () => {
test("a plain id reads as itself", () => {
expect(streamPathBotId("/api/computers/my-bot/stream")).toBe("my-bot");
});

test("a percent-encoded id decodes", () => {
expect(streamPathBotId("/api/computers/my%20bot/stream")).toBe("my bot");
});

test("a malformed escape is not a Bot id, not a 500", () => {
expect(streamPathBotId("/api/computers/%zz/stream")).toBeNull();
expect(streamPathBotId("/api/computers/%E2%28/stream")).toBeNull();
});

test("other paths name no Bot", () => {
expect(streamPathBotId("/api/computers/my-bot/files")).toBeNull();
expect(streamPathBotId("/api/computers/stream")).toBeNull();
expect(streamPathBotId("/")).toBeNull();
});
});
Loading