Skip to content

Commit 3506a55

Browse files
authored
refactor(browser): unify browser mutation routes (#129303)
1 parent 9b6c2fb commit 3506a55

2 files changed

Lines changed: 184 additions & 208 deletions

File tree

‎extensions/browser/src/browser/routes/agent.storage.device.test.ts‎

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { createBrowserRouteApp, createBrowserRouteResponse } from "./test-helper
33
import type { BrowserRequest } from "./types.js";
44

55
const routeState = vi.hoisted(() => ({
6+
cookiesGetViaPlaywright: vi.fn(async () => ({ cookies: [] })),
67
cookiesSetManyViaPlaywright: vi.fn(async () => ({ added: 2 })),
78
setDeviceViaPlaywright: vi.fn(async () => {}),
9+
setHttpCredentialsViaPlaywright: vi.fn(async () => {}),
810
withPlaywrightRouteContext: vi.fn(),
911
}));
1012

@@ -25,8 +27,10 @@ type PlaywrightRouteParams = {
2527
tab: { targetId: string };
2628
signal: AbortSignal;
2729
pw: {
30+
cookiesGetViaPlaywright: typeof routeState.cookiesGetViaPlaywright;
2831
cookiesSetManyViaPlaywright: typeof routeState.cookiesSetManyViaPlaywright;
2932
setDeviceViaPlaywright: typeof routeState.setDeviceViaPlaywright;
33+
setHttpCredentialsViaPlaywright: typeof routeState.setHttpCredentialsViaPlaywright;
3034
};
3135
}) => Promise<unknown>;
3236
};
@@ -39,25 +43,21 @@ function getPostHandler(route: string) {
3943
return handler;
4044
}
4145

42-
describe("browser device route", () => {
43-
beforeEach(() => {
44-
routeState.cookiesSetManyViaPlaywright.mockClear();
45-
routeState.setDeviceViaPlaywright.mockClear();
46-
routeState.withPlaywrightRouteContext
47-
.mockReset()
48-
.mockImplementation(async (params: PlaywrightRouteParams) => {
49-
await params.run({
50-
cdpUrl: "http://127.0.0.1:18800",
51-
tab: { targetId: "tab-1" },
52-
signal: params.req.signal ?? new AbortController().signal,
53-
pw: {
54-
cookiesSetManyViaPlaywright: routeState.cookiesSetManyViaPlaywright,
55-
setDeviceViaPlaywright: routeState.setDeviceViaPlaywright,
56-
},
57-
});
46+
beforeEach(() => {
47+
vi.clearAllMocks();
48+
routeState.withPlaywrightRouteContext
49+
.mockReset()
50+
.mockImplementation(async (params: PlaywrightRouteParams) => {
51+
await params.run({
52+
cdpUrl: "http://127.0.0.1:18800",
53+
tab: { targetId: "tab-1" },
54+
signal: params.req.signal ?? new AbortController().signal,
55+
pw: routeState,
5856
});
59-
});
57+
});
58+
});
6059

60+
describe("browser device route", () => {
6161
it("forwards the route lease signal into the atomic device transition", async () => {
6262
const controller = new AbortController();
6363
const response = createBrowserRouteResponse();
@@ -79,27 +79,36 @@ describe("browser device route", () => {
7979
signal: controller.signal,
8080
});
8181
expect(response.body).toEqual({ ok: true, targetId: "tab-1" });
82+
expect(routeState.withPlaywrightRouteContext).toHaveBeenCalledWith(
83+
expect.objectContaining({ feature: "device emulation" }),
84+
);
85+
expect(routeState.withPlaywrightRouteContext.mock.calls[0]?.[0]).not.toHaveProperty(
86+
"enforceCurrentUrlAllowed",
87+
);
8288
});
83-
});
8489

85-
describe("browser cookie batch route", () => {
86-
beforeEach(() => {
87-
routeState.cookiesSetManyViaPlaywright.mockClear();
88-
routeState.withPlaywrightRouteContext
89-
.mockReset()
90-
.mockImplementation(async (params: PlaywrightRouteParams) => {
91-
await params.run({
92-
cdpUrl: "http://127.0.0.1:18800",
93-
tab: { targetId: "tab-1" },
94-
signal: params.req.signal ?? new AbortController().signal,
95-
pw: {
96-
cookiesSetManyViaPlaywright: routeState.cookiesSetManyViaPlaywright,
97-
setDeviceViaPlaywright: routeState.setDeviceViaPlaywright,
98-
},
99-
});
100-
});
90+
it("never publishes a successful mutation after its route lease is canceled", async () => {
91+
const controller = new AbortController();
92+
const response = createBrowserRouteResponse();
93+
routeState.setDeviceViaPlaywright.mockImplementationOnce(async () => controller.abort());
94+
95+
await expect(
96+
getPostHandler("/set/device")?.(
97+
{
98+
params: {},
99+
query: {},
100+
body: { name: "iPhone 14" },
101+
signal: controller.signal,
102+
},
103+
response.res,
104+
),
105+
).rejects.toThrow();
106+
107+
expect(response.body).toBeUndefined();
101108
});
109+
});
102110

111+
describe("browser cookie batch route", () => {
103112
it("parses and injects a non-empty cookie batch", async () => {
104113
const controller = new AbortController();
105114
const response = createBrowserRouteResponse();
@@ -150,3 +159,43 @@ describe("browser cookie batch route", () => {
150159
expect(routeState.cookiesSetManyViaPlaywright).not.toHaveBeenCalled();
151160
});
152161
});
162+
163+
describe("browser storage route boundaries", () => {
164+
it("keeps cookie reads behind the current-tab URL guard", async () => {
165+
const { app, getHandlers } = createBrowserRouteApp();
166+
registerBrowserAgentStorageRoutes(app, {} as never);
167+
const response = createBrowserRouteResponse();
168+
169+
await getHandlers.get("/cookies")?.({ params: {}, query: {} }, response.res);
170+
171+
expect(routeState.withPlaywrightRouteContext).toHaveBeenCalledWith(
172+
expect.objectContaining({ feature: "cookies", enforceCurrentUrlAllowed: true }),
173+
);
174+
expect(response.body).toEqual({ ok: true, targetId: "tab-1", cookies: [] });
175+
});
176+
177+
it("applies HTTP credentials without ever returning the password", async () => {
178+
const response = createBrowserRouteResponse();
179+
180+
await getPostHandler("/set/credentials")?.(
181+
{
182+
params: {},
183+
query: {},
184+
body: { username: "browser-user", password: "sensitive-browser-password" },
185+
},
186+
response.res,
187+
);
188+
189+
expect(routeState.setHttpCredentialsViaPlaywright).toHaveBeenCalledWith({
190+
cdpUrl: "http://127.0.0.1:18800",
191+
targetId: "tab-1",
192+
username: "browser-user",
193+
password: "sensitive-browser-password",
194+
clear: false,
195+
});
196+
expect(response.body).toEqual({ ok: true, targetId: "tab-1" });
197+
expect(routeState.withPlaywrightRouteContext).toHaveBeenCalledWith(
198+
expect.objectContaining({ feature: "http credentials" }),
199+
);
200+
});
201+
});

0 commit comments

Comments
 (0)