A provider's fixed Headers can't say a value that changes from request
to request or from client to client. 3jRzfF52Qx on X asked for the
agent's own header values to reach the vendor. Each provider now has a
comma-separated list of request headers, saved as `passHeaders`. When the
agent's request carries one of them, its value goes to that provider as
the agent sent it.
- Where it is: the editor's "Request headers to pass" (zh 透传请求头),
under Advanced, right after Reply headers.
- The agent's value stands over a fixed Headers value of the same name.
When the agent sends none, the fixed one goes.
- Never passed on, listed or not (provider.RequestHeaderPassable):
- magpie's key: Authorization, X-Api-Key, X-Goog-Api-Key, Api-Key;
- cookies and sign-ins: Cookie, Set-Cookie(2), WWW-Authenticate,
Authentication-Info, Proxy-*;
- the connection's own fields: Host, Connection, Keep-Alive, TE,
Trailer, Transfer-Encoding, Upgrade, Expect;
- what magpie writes for the body: Content-*, Accept,
Accept-Encoding;
- Origin, Sec-* and X-Magpie-*.
Save refuses such a name with a reason (RequestHeadersOK), and so does
the editor, before the save. The editor's list is held to Go's by
TestReplyHeadersNeverAsTheProvidersAre. A name written into
providers.json by hand is dropped on read.
- Only that provider gets them. forwardOnce, which every chat,
responses, messages, count_tokens, group-member, fallback and plugin
try goes through, puts the agent's headers in the try's context under
the try's provider id (WithClientHeaders). Provider.Sign passes only
what that id lists, before an account signs, so a signed-in account's
own signing still wins. A plugin's fetch gets them in req.Header.
- Wire headers are still masked when read (5ec237568), so a passed
token header shows masked in a call's log.
Run on mote at the tree rebased on origin/main 06944e065:
- gofmt -l: clean.
- go vet: darwin (provider, gateway, gui), GOOS=linux -tags nogui
./..., and GOOS=windows ./...: ok.
- go test -timeout 45m ./internal/provider ./internal/gateway
./internal/gui: provider and gateway ok. In gui, TestCoreScriptBudget
and the i18n tests are ok and only TestMainWindowCorners/macOS_26_SDK
is red, as on main on macOS 27 (#3949).
- The core budget is not raised. This change's words, and the comments
on extra body and the never lists, were cut first. After the rebase
onto 63dc359fb it was 438 bytes over again, so three provider-editor
comments (User-Agent, balance token, plan access keys) were cut to a
line each. The core is 1,533,554 bytes, 248 under 1,533,802.
- At this head (rebased on origin/main 63dc359fb): go test -run
Budget|I18n|ReplyHeaders ./internal/gui is ok. The new provider and
gateway tests are ok. node --test pass-headers, reply-headers,
i18n-keys, header-session-hint, balance-fix and extra-body: 162 of
162 ok.
- node --test, Chromium and WebKit: pass-headers (en/zh/zh-TW/ja/de),
reply-headers, i18n-keys, provider-detect-models, header-session-hint
and extra-body: 46 of 46 ok.
- Before the budget cut, against the same base: every GUI file that
opens the provider editor and saves (45 files plus i18n-keys) had 739
of 758 ok. These were red, and red the same on a fresh origin/main
06944e065 copy: models-works-only (#4391), provider-picks-stay
(#4397) and remote-magpie (#4398). Also red: one WebKit sub-pixel
miss in provider-detect-models (#4964), which passed on main and at
this head.
New tests:
- TestRequestHeadersList: cleaning and refusals.
- TestSignPassesClientHeaders: the agent's value over the fixed one,
never magpie's key even when listed by hand, nothing for another id,
and the fixed value when nothing was sent.
- TestPassHeadersReachTheProvider: messages relayed and translated,
chat, a chat stream, responses and count_tokens.
- TestPassHeadersOnlyToTheirProvider: a fallback that lists nothing gets
nothing, the log shows the passed headers masked, and in a group the
member that lists them gets them while the one before it, which lists
nothing, doesn't.
Broken by hand, each red, then put back:
- forwardOnce without WithClientHeaders: both gateway tests red.
- RequestHeaderPassable ignoring the never list: TestRequestHeadersList
red, and TestSignPassesClientHeaders red on X-Api-Key=magpie-key
passed on.
- Sign not skipping a fixed header the agent sent: the provider test,
and the gateway test on every route, red on X-Route=route-fixed.
Not covered: embeddings, images, video and the Codex backend relay,
which don't go through forwardOnce.
Not tried:
- a real vendor or a real plugin provider with the setting;
- Windows and Linux test runs (vet only);
- a gateway test with Authorization listed by hand, which normalize
drops on read. Sign's own check is covered by
TestSignPassesClientHeaders.
Requested by 3jRzfF52Qx on X.
120 lines
6.8 KiB
JavaScript
120 lines
6.8 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// A provider's Reply headers (#4145, codex-cn: a relay's X-OneAPI-Request-ID
|
|
// lost on the way to the agent): the editor shows the saved list, a Save
|
|
// sends what is typed as a list, refuses a name magpie never passes on
|
|
// (Set-Cookie, Content-*, a name that isn't one) and says why, and an
|
|
// emptied box clears it ([]). It fits a narrow window. In English,
|
|
// Chinese, Traditional Chinese, Japanese and German, Chromium and WebKit.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const relay = {
|
|
id: "relay", name: "Relay", preset: "", icon: "generic", chat: "https://relay.example.com/v1", responses: "", anthropic: "", catalog: "", baseAPI: "chat",
|
|
models: [{ id: "gpt-5.1", name: "gpt-5.1", on: true }], agents: [], fallback: [], headers: {},
|
|
replyHeaders: ["X-Trace-Id"],
|
|
key: { set: true, masked: "sk-…one" }, keyList: [{ id: "k1", name: "", masked: "sk-…one", on: true, active: true }],
|
|
balanceToken: { takes: false, set: false }, proxy: "",
|
|
};
|
|
|
|
function serve(lang, calls) {
|
|
const providers = { providers: [relay], presets: [], excluded: [], gateway: { running: true, window: true } };
|
|
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
|
|
return async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const json = (data) => route.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json(state);
|
|
if (url.pathname === "/api/providers") return json(providers);
|
|
if (url.pathname === "/api/groups") return json({ groups: [] });
|
|
if (url.pathname.startsWith("/api/provider/")) {
|
|
calls.push([url.pathname.slice(14), route.request().postDataJSON()]);
|
|
if (url.pathname === "/api/provider/save") return json(providers);
|
|
}
|
|
if (url.pathname.startsWith("/api/")) return json({});
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
|
|
};
|
|
}
|
|
|
|
const words = {
|
|
en: { label: "Reply headers", save: "Save", never: "is never passed on", name: "isn't a header name" },
|
|
zh: { label: "透传响应头", save: "保存", never: "永不透传", name: "不是有效的头名称" },
|
|
"zh-TW": { label: "透傳回應標頭", save: "儲存", never: "一律不透傳", name: "不是有效的標頭名稱" },
|
|
ja: { label: "レスポンスヘッダー", save: "保存", never: "決して渡しません", name: "ヘッダー名ではありません" },
|
|
de: { label: "Antwort-Header", save: "Speichern", never: "wird nie weitergegeben", name: "ist kein Header-Name" },
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of Object.keys(words)) {
|
|
const w = words[lang];
|
|
test(`${engine} ${lang}: Reply headers are shown, refused, saved and cleared`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await (await browser.newContext({ viewport: { width: 900, height: 900 }, reducedMotion: "reduce" })).newPage();
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const calls = [];
|
|
await page.route("**/*", serve(lang, calls));
|
|
await page.goto("http://magpie.test/?view=providers");
|
|
const open = async () => {
|
|
await page.locator(".row.provider", { hasText: "Relay" }).click();
|
|
const ed = page.locator(".editor");
|
|
await ed.locator("input.reply-headers").waitFor();
|
|
return ed;
|
|
};
|
|
const save = async (ed) => {
|
|
await ed.locator(".bar").getByRole("button", { name: w.save, exact: true }).click();
|
|
for (let i = 0; i < 20 && !calls.some((c) => c[0] === "save"); i++) await page.waitForTimeout(50);
|
|
};
|
|
let ed = await open();
|
|
const box = ed.locator("input.reply-headers");
|
|
assert.equal(await page.locator(`label[for="${await box.getAttribute("id")}"]`).textContent(), w.label, "labelled");
|
|
assert.equal(await box.inputValue(), "X-Trace-Id", "the saved list is shown");
|
|
|
|
// refused before a Save, with why, the box focused
|
|
for (const [text, want] of [["x-trace-id, set-cookie", w.never], ["Content-Encoding", w.never], ["Access-Control-Allow-Origin", w.never], ["x trace", w.name]]) {
|
|
await box.fill(text);
|
|
await save(ed);
|
|
assert.deepEqual(calls.map((c) => c[0]), [], `${text}: nothing saved`);
|
|
const said = await ed.locator(".editor-error").textContent();
|
|
assert.ok(said.includes(want) && said.includes(w.label), `${text}: ${said}`);
|
|
assert.ok(await box.evaluate((e) => e === document.activeElement), "the box is focused");
|
|
}
|
|
|
|
// the narrow window: the box stays inside the editor
|
|
await page.setViewportSize({ width: 420, height: 900 });
|
|
const fits = await page.evaluate(() => {
|
|
const ed = document.querySelector(".editor").getBoundingClientRect();
|
|
const r = document.querySelector("input.reply-headers").getBoundingClientRect();
|
|
return r.left >= ed.left - 0.5 && r.right <= ed.right + 0.5;
|
|
});
|
|
assert.ok(fits, "fits a narrow window");
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "no sideways scroll");
|
|
await page.setViewportSize({ width: 900, height: 900 });
|
|
|
|
await box.fill(" x-oneapi-request-id ,X-Trace-Id,, ");
|
|
await save(ed);
|
|
const saved = calls.find((c) => c[0] === "save");
|
|
assert.ok(saved, "saved");
|
|
assert.deepEqual(saved[1].replyHeaders, ["x-oneapi-request-id", "X-Trace-Id"], "saved as a list");
|
|
|
|
// emptied: a Save clears it
|
|
calls.length = 0;
|
|
await page.locator(".editor").waitFor({ state: "hidden" });
|
|
ed = await open();
|
|
await ed.locator("input.reply-headers").fill("");
|
|
await save(ed);
|
|
assert.deepEqual(calls.find((c) => c[0] === "save")?.[1].replyHeaders, [], "an empty box clears it");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|