Settings > Proxy > Custom now shows Username and Password fields beside the
address for every proxy kind. Before, only a SOCKS5 address had them (the
password is masked). An HTTP or HTTPS proxy's login had to go inside the URL,
so a password holding @ : / % # or a space broke it. Each field is
percent-encoded (encodeURIComponent) when the address is saved.
The setting's format is unchanged: still one URL with the login as its
userinfo. A proxy saved before with user:pass@ loads into the fields. It is
split the way Go's url.Parse splits it (last @, then first :), so
"https://al%20ice:se@cr%23et@proxy.example:8443" shows "al ice" and
"se@cr#et". It is not split through WHATWG URL, which would add a "/" or drop
a default port. A part left as it was loaded keeps its own escapes, so
non-UTF-8 bytes survive. A login pasted into the address moves into the
fields. Describe, Redact and the status line already hid the userinfo
(#1886), and a new test checks that again.
Credit: 微雨南山 on Discord (#feedback), who asked for macOS-style
address / username / password fields after a special character in the
password broke the URL.
Tests:
- internal/netproxy/login_test.go: the login "me@corp:x" / "p@ss:w/rd% #1 x",
saved as the page encodes it, reads back as typed. It signs in to a fake HTTP
proxy (Basic Proxy-Authorization), and to a fake SOCKS5 proxy through both Go's
transport and the plugin bridge's socksHandshake. It also reaches a CLI's
HTTPS_PROXY. One character off is refused (407 / SOCKS fails), and Describe and
Redact show no part of it. TestBunProxyLoginWithReservedCharacters signs in with
the real bun 1.4.2's fetch({proxy}). These pass on the old Go code too: the Go
side needed no change.
- internal/gui/tests/settings-proxy-auth.test.cjs, Chromium and WebKit, en zh
zh-TW ja de, at 560 and 375px: an HTTP proxy has the fields, the login
round-trips encoded and comes back decoded, the password is type=password, and
the status hides it. A login saved in the address loads into the fields, and
Enter on it saves nothing. On the old app.js both tests fail in every language
checked ("an HTTP proxy has a username field / false !== true"; the address
still showed 'https://al%20ice:se@cr%23et@proxy.example:8443').
Run:
- gofmt -l: empty.
- go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet: clean.
- Full Go suite at c7ad43426 through the suite lock (-tags nogui -timeout 45m):
68 ok, one red, internal/gui TestCoreScriptBudget. The first app.js was
1338904 bytes over 1338084. The helpers and comments were tightened (not the
budget raised): app.js is 172 bytes under its parent. Then internal/gui,
internal/netproxy and internal/settings: ok at the final tree. After the
rebase onto c551a2299, app.js is 1,338,757 against the parent's 1,338,929.
TestCoreScriptBudget, internal/netproxy and the two proxy GUI files (30/30)
pass again.
- settings-proxy-auth + settings-proxy-remember: 30/30, and 30/30 three more
times at load 60-150.
- Whole GUI suite: stopped after ~2h, at 11,507 passes, to free the suite lock.
It ran beside two other sessions' GUI suites (load ~300). It showed 43
top-level reds, whose errors were not printed because the run was cut short.
Three were in the proxy files, and those passed alone afterwards. All are
filed in #3393, not compared against origin/main.
Not tried:
- a real authenticated proxy, and the real app;
- Windows and Linux runs;
- how Codex's reqwest or other agent CLIs decode the percent-encoded *_PROXY
login.
The plugin host's host.js proxyRefuses (auth only when there is a username,
and a decodeURIComponent that can throw), and the per-provider, account, key
and sign-in proxy fields that still take the login inside one URL, are filed
as #3382.
215 lines
12 KiB
JavaScript
215 lines
12 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// A proxy's optional username and password, in fields of their own for
|
|
// every kind of proxy (SOCKS, and HTTP for 微雨南山 on Discord), survive
|
|
// saving/reloading, never appear in the address or status, and typing
|
|
// between fields cannot save a partial login. A login saved in the address
|
|
// before loads into the fields.
|
|
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");
|
|
function server(lang, posts, proxy = "") {
|
|
let cur = {
|
|
theme: "light", lang, tray: "panel", currency: "usd", textSize: 100,
|
|
proxy, proxyNow: proxy, proxySource: proxy ? "settings" : "none",
|
|
trayUsageEvery: 3, version: "test", dir: "/test", lanURLs: [],
|
|
visionModels: [], imageGenModels: [], workbuddyCheckins: [],
|
|
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
|
|
};
|
|
return async (route) => {
|
|
const req = route.request(), url = new URL(req.url());
|
|
const json = (data) => route.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = ${JSON.stringify({ 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({ agents: [], profiles: [], settings: { lang, theme: "light" }, fx: cur.fx });
|
|
if (url.pathname === "/api/settings") {
|
|
if (req.method() === "POST") {
|
|
const body = req.postDataJSON();
|
|
posts.push(body);
|
|
cur = { ...cur, ...body, proxyNow: body.proxy, proxySource: body.proxy === "direct" ? "off" : body.proxy ? "settings" : "none" };
|
|
}
|
|
return json(cur);
|
|
}
|
|
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
|
if (url.pathname === "/api/usage/quotas") return json([]);
|
|
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
|
|
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)];
|
|
return route.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
const labels = {
|
|
en: { user: "Username (optional)", pass: "Password (optional)", custom: "Custom", save: "Save", off: "Off", auto: "Auto", saved: "Saved" },
|
|
zh: { user: "用户名(可选)", pass: "密码(可选)", custom: "自定义", save: "保存", off: "关闭", auto: "自动", saved: "已保存" },
|
|
"zh-TW": { user: "使用者名稱(選填)", pass: "密碼(選填)", custom: "自定義", save: "儲存", off: "關閉", auto: "自動", saved: "已儲存" },
|
|
ja: { user: "ユーザー名(任意)", pass: "パスワード(任意)", custom: "カスタム", save: "保存", off: "オフ", auto: "自動", saved: "保存しました" },
|
|
de: { user: "Benutzername (optional)", pass: "Passwort (optional)", custom: "Benutzerdefiniert", save: "Speichern", off: "Aus", auto: "Automatisch", saved: "Gespeichert" },
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of Object.keys(labels)) {
|
|
const l = labels[lang];
|
|
test(`${engine} ${lang}: optional proxy credentials in settings`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const context = await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(4000);
|
|
const posts = [], errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/*", server(lang, posts));
|
|
await page.goto("http://magpie.test/?view=settings&tab=network");
|
|
const box = page.locator("#proxySegs");
|
|
const address = box.locator("input.proxy");
|
|
const user = box.getByRole("textbox", { name: l.user, exact: true });
|
|
const pass = box.getByLabel(l.pass, { exact: true });
|
|
const saved = async (action, want) => {
|
|
const count = posts.length;
|
|
const response = page.waitForResponse((r) => r.url().endsWith("/api/settings") && r.request().method() === "POST");
|
|
await action();
|
|
await response;
|
|
await page.waitForFunction((word) => document.querySelector("#status")?.textContent.includes(word), l.saved);
|
|
assert.equal(posts.length, count + 1, "save once for the whole proxy");
|
|
assert.equal(posts.at(-1).proxy, want);
|
|
};
|
|
|
|
await box.getByRole("button", { name: l.custom, exact: true }).click();
|
|
await address.fill("socks5://127.0.0.1:1080");
|
|
await user.fill("a@b:/% 用户");
|
|
await user.press("Tab");
|
|
assert.equal(posts.length, 0, "tabbing from username to password must not save half a login");
|
|
await pass.fill("p@ss:/% #密码");
|
|
assert.equal(await pass.getAttribute("type"), "password");
|
|
const encoded = "socks5://a%40b%3A%2F%25%20%E7%94%A8%E6%88%B7:p%40ss%3A%2F%25%20%23%E5%AF%86%E7%A0%81@127.0.0.1:1080";
|
|
await saved(async () => {
|
|
// WebKit may skip Save when tabbing, leaving the editor and saving
|
|
// now. If Tab stays inside, clicking outside saves the same login.
|
|
await pass.press("Tab");
|
|
await page.locator("#proxySub").click();
|
|
}, encoded);
|
|
await page.reload();
|
|
assert.equal(await address.inputValue(), "socks5://127.0.0.1:1080");
|
|
assert.equal(await user.inputValue(), "a@b:/% 用户");
|
|
assert.equal(await pass.inputValue(), "p@ss:/% #密码");
|
|
assert(!((await page.locator("#proxySub").innerText()).includes("p%40ss")), "status exposes the encoded password");
|
|
assert(!((await page.locator("#proxySub").innerText()).includes("p@ss")), "status exposes the password");
|
|
|
|
for (const width of [1100, 560, 375]) {
|
|
await page.setViewportSize({ width, height: 760 });
|
|
for (const field of [address, user, pass]) {
|
|
const rect = await field.boundingBox();
|
|
assert(rect && rect.x >= 0 && rect.x + rect.width <= width, `proxy field outside ${width}px window`);
|
|
}
|
|
if (process.env.ARTIFACT_DIR) {
|
|
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
|
|
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-proxy-${width}.png`) });
|
|
}
|
|
}
|
|
|
|
await user.fill("");
|
|
await pass.fill("");
|
|
await saved(() => pass.press("Enter"), "socks5://127.0.0.1:1080");
|
|
await page.reload();
|
|
assert.equal(await user.inputValue(), "");
|
|
assert.equal(await pass.inputValue(), "");
|
|
|
|
// Pasting an existing URL separates and decodes the login, including IPv6.
|
|
await address.fill("socks5h://name:p%40ss@[::1]:1080");
|
|
assert.equal(await address.inputValue(), "socks5h://[::1]:1080");
|
|
assert.equal(await user.inputValue(), "name");
|
|
assert.equal(await pass.inputValue(), "p@ss");
|
|
await saved(() => pass.press("Enter"), "socks5h://name:p%40ss@[::1]:1080");
|
|
await pass.fill("discard-me");
|
|
const beforeEscape = posts.length;
|
|
await pass.press("Escape");
|
|
assert.equal(await pass.inputValue(), "p@ss");
|
|
assert.equal(posts.length, beforeEscape, "Escape must discard, not save, the draft");
|
|
|
|
// Arbitrary encoded bytes in an existing password remain masked and intact.
|
|
await address.fill("socks5://name:p%FFss@127.0.0.1:1080");
|
|
assert.equal(await address.inputValue(), "socks5://127.0.0.1:1080");
|
|
assert.equal(await pass.inputValue(), "p%FFss");
|
|
await saved(() => box.getByRole("button", { name: l.save, exact: true }).click(), "socks5://name:p%FFss@127.0.0.1:1080");
|
|
|
|
// An HTTP proxy has the same fields (微雨南山 on Discord): its login
|
|
// is typed as it is, @ : / % # and spaces included, and each part is
|
|
// encoded into the address, which keeps no slash or port of URL's.
|
|
await address.fill("http://127.0.0.1:7890");
|
|
assert.equal(await user.isVisible(), true, "an HTTP proxy has a username field");
|
|
assert.equal(await pass.isVisible(), true, "an HTTP proxy has a password field");
|
|
await user.fill("");
|
|
await pass.fill("");
|
|
await saved(() => address.press("Enter"), "http://127.0.0.1:7890");
|
|
await user.fill("me@corp:x");
|
|
await pass.fill("p@ss:w/rd% #1 x");
|
|
// the bytes internal/netproxy's TestProxyLoginWithReservedCharacters signs in with
|
|
const httpLogin = "http://me%40corp%3Ax:p%40ss%3Aw%2Frd%25%20%231%20x@127.0.0.1:7890";
|
|
await saved(() => pass.press("Enter"), httpLogin);
|
|
await page.reload();
|
|
assert.equal(await address.inputValue(), "http://127.0.0.1:7890");
|
|
assert.equal(await user.inputValue(), "me@corp:x");
|
|
assert.equal(await pass.inputValue(), "p@ss:w/rd% #1 x");
|
|
assert.equal(await pass.getAttribute("type"), "password");
|
|
for (const secret of ["p@ss", "p%40ss", "corp"]) assert(!(await page.locator("#proxySub").innerText()).includes(secret), `status shows ${secret}`);
|
|
for (const width of [560, 375]) {
|
|
await page.setViewportSize({ width, height: 760 });
|
|
for (const field of [address, user, pass]) {
|
|
const rect = await field.boundingBox();
|
|
assert(rect && rect.x >= 0 && rect.x + rect.width <= width, `HTTP proxy field outside ${width}px window`);
|
|
}
|
|
if (process.env.ARTIFACT_DIR) await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-http-proxy-${width}.png`) });
|
|
}
|
|
await page.setViewportSize({ width: 1100, height: 760 });
|
|
|
|
// A bare host:port stays as typed, with or without a login.
|
|
await user.fill("");
|
|
await pass.fill("");
|
|
await address.fill("127.0.0.1:7891");
|
|
await saved(() => page.locator("#proxySub").click(), "127.0.0.1:7891");
|
|
await user.fill("u");
|
|
await pass.fill("p w");
|
|
await saved(() => pass.press("Enter"), "u:p%20w@127.0.0.1:7891");
|
|
await page.reload();
|
|
assert.equal(await address.inputValue(), "127.0.0.1:7891");
|
|
assert.equal(await pass.inputValue(), "p w");
|
|
|
|
await address.fill("socks5://127.0.0.1:1080");
|
|
await user.fill("unsaved-user");
|
|
await pass.fill("unsaved-password");
|
|
await saved(() => box.getByRole("button", { name: l.off, exact: true }).click(), "direct");
|
|
await saved(() => box.getByRole("button", { name: l.auto, exact: true }).click(), "");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
|
|
test(`${engine} ${lang}: an HTTP proxy saved with its login in the address loads into the fields`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const context = await browser.newContext({ viewport: { width: 375, height: 760 }, reducedMotion: "reduce" });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(4000);
|
|
const posts = [], errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
// as one typed whole before: a raw @ in the password, as Go reads it
|
|
await page.route("**/*", server(lang, posts, "https://al%20ice:se@cr%23et@proxy.example:8443"));
|
|
await page.goto("http://magpie.test/?view=settings&tab=network");
|
|
const box = page.locator("#proxySegs");
|
|
assert.equal(await box.locator("input.proxy").inputValue(), "https://proxy.example:8443");
|
|
assert.equal(await box.getByRole("textbox", { name: l.user, exact: true }).inputValue(), "al ice");
|
|
const pass = box.getByLabel(l.pass, { exact: true });
|
|
assert.equal(await pass.inputValue(), "se@cr#et");
|
|
assert.equal(await pass.getAttribute("type"), "password");
|
|
const sub = await page.locator("#proxySub").innerText();
|
|
assert(!sub.includes("se@cr") && !sub.includes("al%20ice"), `status shows the login: ${sub}`);
|
|
// left as it was, it is saved as it was
|
|
await pass.press("Enter");
|
|
assert.equal(posts.length, 0, "an unchanged login is not saved again");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|