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
13 changes: 11 additions & 2 deletions examples/personal-workspace-browser/automation-cadence.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { promisify } from "node:util";

import { resolveTestPython } from "../../scripts/test-python.mjs";
import { outputDir, repoRoot } from "./fixture.mjs";
import { openWorkspacePage } from "./scenario-context.mjs";
import { fetchWorkspaceBackend, openWorkspacePage } from "./scenario-context.mjs";

const execFileAsync = promisify(execFile);

Expand Down Expand Up @@ -114,6 +114,15 @@ export const automationCadenceScenario = {
}
let context;
try {
// Forwarding must not make an unrelated browser origin trusted.
const refused = await fetchWorkspaceBackend({
request: () => ({
url: () => new URL(`${path}/preview`, url).href,
headers: () => ({ origin: "https://untrusted.example", "content-type": "application/json" }),
}),
fetch: ({ url: targetUrl, headers }) => fetch(targetUrl, { method: "POST", headers, body: "{}" }),
}, authority.url);
if (refused.status !== 403) throw new Error("Forwarding admitted a foreign origin");
context = await openWorkspacePage(browser, url, {
collectCoverage,
beforeGoto: async (_api, page) => {
Expand All @@ -123,7 +132,7 @@ export const automationCadenceScenario = {
concurrentChange = false;
await interleaveGoalChange();
}
const response = await route.fetch({ url: authority.url + parsed.pathname + parsed.search });
const response = await fetchWorkspaceBackend(route, authority.url);
if (parsed.pathname.endsWith("/apply") && response.ok()) {
applyCount += 1;
changes.push(route.request().postDataJSON());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {mkdtemp, readFile, realpath, rm} from "node:fs/promises";
import {tmpdir} from "node:os";
import {join, resolve} from "node:path";
import {repoRoot, outputDir} from "./fixture.mjs";
import {openWorkspacePage} from "./scenario-context.mjs";
import {fetchWorkspaceBackend, openWorkspacePage} from "./scenario-context.mjs";
import {resolveTestPython} from "../../scripts/test-python.mjs";

// Only the backup routes use a real disposable server. Other workspace state
Expand Down Expand Up @@ -66,8 +66,7 @@ export const configurationBackupScenario = {
const {port} = backend;
context = await openWorkspacePage(browser, url, {beforeGoto: async (_api, page) => {
await page.route("**/api/chat/configuration-backup/**", async route => {
const path = new URL(route.request().url()).pathname;
const response = await route.fetch({url: `http://127.0.0.1:${port}${path}`});
const response = await fetchWorkspaceBackend(route, `http://127.0.0.1:${port}`);
await route.fulfill({response});
});
}});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolve } from "node:path";
import { createInterface } from "node:readline";
import { resolveTestPython } from "../../scripts/test-python.mjs";
import { outputDir, repoRoot } from "./fixture.mjs";
import { openWorkspacePage } from "./scenario-context.mjs";
import { fetchWorkspaceBackend, openWorkspacePage } from "./scenario-context.mjs";

const GOAL = "research-monitor";
const AGENT = "research-agent";
Expand Down Expand Up @@ -133,7 +133,7 @@ export const exploreCompositionOwnerScenario = {
await page.route("**/api/chat/goal-configuration**", async (route) => {
const target = new URL(route.request().url());
if (!target.pathname.startsWith("/api/chat/goal-configuration")) { await route.fallback(); return; }
const response = await route.fetch({ url: `${owner.url}${target.pathname}${target.search}` });
const response = await fetchWorkspaceBackend(route, owner.url);
ownerResponses.push({ method: route.request().method(), path: target.pathname, status: response.status() });
await route.fulfill({ response });
});
Expand Down
13 changes: 13 additions & 0 deletions examples/personal-workspace-browser/scenario-context.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { installApi } from "./fixture.mjs";

// A disposable backend has a different port from the browser fixture. Rebase
// same-origin requests with it; foreign origins must still reach the real guard.
export function fetchWorkspaceBackend(route, backendUrl) {
const request = route.request();
const source = new URL(request.url());
const target = new URL(backendUrl);
target.pathname = source.pathname;
target.search = source.search;
const headers = { ...request.headers(), host: target.host };
if (headers.origin === source.origin) headers.origin = target.origin;
return route.fetch({ url: target.href, headers });
}

export async function openWorkspacePage(
browser,
url,
Expand Down
Loading