tools: refuse getCookies without active page

Return an error message instead of dumping all cookies when no page
is loaded and no filter is provided.
This commit is contained in:
Adrià Arrufat
2026-05-24 09:00:19 +02:00
parent 0c58184137
commit 09698045c6
2 changed files with 18 additions and 1 deletions

View File

@@ -1290,7 +1290,7 @@ fn execGetCookies(arena: std.mem.Allocator, session: *lp.Session, arguments: ?st
if (args.all) break :blk null;
if (args.url) |u| break :blk arena.dupeZ(u8, u) catch return ToolError.InternalError;
if (session.currentFrame()) |f| break :blk f.url;
break :blk null;
return "No current page. Pass `url` to filter by host or `all=true` to list every cookie.";
};
const host: ?[]const u8 = if (filter_url) |u| lp.URL.getHostname(u) else null;

View File

@@ -1006,6 +1006,23 @@ test "MCP - getCookies: defaults to current page, url filter, all flag" {
try testing.expect(std.mem.indexOf(u8, out.written(), "No cookies for http://nope.test/") != null);
}
test "MCP - getCookies without a loaded page refuses instead of dumping the jar" {
defer testing.reset();
var out: std.io.Writer.Allocating = .init(testing.arena_allocator);
var server = try Server.init(testing.allocator, testing.test_app, &out.writer);
defer server.deinit();
try server.session.cookie_jar.populateFromResponse("http://example.com/", "session=abc; Path=/");
const msg =
\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"getCookies"}}
;
try router.handleMessage(server, testing.arena_allocator, msg);
const written = out.written();
try testing.expect(std.mem.indexOf(u8, written, "session=abc") == null);
try testing.expect(std.mem.indexOf(u8, written, "No current page") != null);
}
test "MCP - goto with bad waitUntil surfaces rich error" {
defer testing.reset();
var out: std.io.Writer.Allocating = .init(testing.arena_allocator);