Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Split the credentials out of a proxy URL written without a scheme (#482)

kevin5425383d745a
diff --git a/agent-computer/src/egress.ts b/agent-computer/src/egress.ts
index 12b9261b..9826dd9d 100644
--- a/agent-computer/src/egress.ts
+++ b/agent-computer/src/egress.ts
@@ -41,29 +41,56 @@ export function egressFor(
): Egress | null {
const raw = env[egressVariableFor(botId)] ?? env.EGRESS_PROXY_DEFAULT;
if (!raw?.trim()) return null;
+ return splitProxyCredentials(raw);
+}
+
+/**
+ * Split a proxy string into a server and its credentials.
+ *
+ * Credentials commonly arrive inside the URL, which is how proxies are handed out. They are split out
+ * so that the server string can be shown to a person without leaking a password.
+ *
+ * Two shapes reach here, and only one of them is a URL as far as the parser is concerned. A bare
+ * `host:port` is what an operator writes when they are not thinking about URLs, and Playwright and
+ * curl both take it; `new URL` reads `proxy.internal:8080` as the scheme `proxy.internal:` and the
+ * path `8080` with an empty host, rather than throwing. `username` and `password` come back empty for
+ * that shape, so a proxy written `bot:[email protected]:8080` would keep its password in the
+ * string it hands back. Re-parsing behind a synthetic scheme makes the split happen for both shapes;
+ * the synthetic scheme is then removed so the server reads the way it was written.
+ */
+export function splitProxyCredentials(raw: string): Egress {
+ const trimmed = raw.trim();
- // Credentials commonly arrive inside the URL, which is how proxies are handed out. They are split
- // out so that the server string this returns can be shown to a person without leaking a password.
+ let url: URL | null = null;
+ let synthetic = "";
try {
- const url = new URL(raw.trim());
- const username = url.username
- ? decodeURIComponent(url.username)
- : undefined;
- const password = url.password
- ? decodeURIComponent(url.password)
- : undefined;
- url.username = "";
- url.password = "";
- return {
- server: url.toString().replace(/\/$/, ""),
- ...(username ? { username } : {}),
- ...(password ? { password } : {}),
- };
- } catch {
- // Not a URL. Passed through as a bare `host:port`, which Playwright also accepts, so an operator
- // who writes the obvious thing is not told they are wrong.
- return { server: raw.trim() };
+ const asWritten = new URL(trimmed);
+ if (asWritten.host !== "") url = asWritten;
+ } catch (e) {
+ if (!(e instanceof TypeError)) throw e;
+ }
+ if (!url) {
+ synthetic = "http://";
+ try {
+ url = new URL(`${synthetic}${trimmed}`);
+ } catch (e) {
+ if (!(e instanceof TypeError)) throw e;
+ // Not addressable either way. Passed through, so an operator who writes the obvious thing is
+ // not told they are wrong.
+ return { server: trimmed };
+ }
}
+
+ const username = url.username ? decodeURIComponent(url.username) : undefined;
+ const password = url.password ? decodeURIComponent(url.password) : undefined;
+ url.username = "";
+ url.password = "";
+
+ return {
+ server: url.toString().replace(/\/$/, "").slice(synthetic.length),
+ ...(username ? { username } : {}),
+ ...(password ? { password } : {}),
+ };
}
/**
diff --git a/agent-computer/src/shell.ts b/agent-computer/src/shell.ts
index e787877e..a30c2d01 100644
--- a/agent-computer/src/shell.ts
+++ b/agent-computer/src/shell.ts
@@ -1,5 +1,7 @@
import { spawn } from "node:child_process";
+import { splitProxyCredentials } from "./egress";
+
/**
* Running a command on the Bot's computer.
*
@@ -188,16 +190,9 @@ function extraShellEnvNames(raw: string | undefined): readonly string[] {
* the network without `env` printing a password. Same split `egress.ts` uses for the browser proxy.
*/
function withoutUserinfo(raw: string): string {
- try {
- const url = new URL(raw.trim());
- if (url.username === "" && url.password === "") return raw;
- url.username = "";
- url.password = "";
- return url.toString().replace(/\/$/, "");
- } catch (e) {
- if (e instanceof TypeError) return raw;
- throw e;
- }
+ const { server, username, password } = splitProxyCredentials(raw);
+ if (username === undefined && password === undefined) return raw;
+ return server;
}
function clamp(text: string): { text: string; truncated: boolean } {
diff --git a/agent-computer/tests/egress.test.ts b/agent-computer/tests/egress.test.ts
index 53447081..c5a57b28 100644
--- a/agent-computer/tests/egress.test.ts
+++ b/agent-computer/tests/egress.test.ts
@@ -67,6 +67,35 @@ describe("resolving a Bot's proxy", () => {
server: "proxy.internal:8080",
});
});
+
+ test("a bare host:port carrying credentials is split like any other", () => {
+ // The shape above, written the way a proxy is actually handed out. `new URL` reads it as the
+ // scheme `bot:` and a path, so username and password come back empty and the password rides
+ // along in `server` -- into the label, the admin page and the API.
+ const proxy = egressFor("sales", {
+ EGRESS_PROXY_SALES: "bot:[email protected]:8080",
+ });
+ expect(proxy).toEqual({
+ server: "proxy.internal:8080",
+ username: "bot",
+ password: "s3cret",
+ });
+ expect(proxy?.server).not.toContain("s3cret");
+ });
+
+ test("a bare host:port with an encoded password decodes it too", () => {
+ const proxy = egressFor("sales", {
+ EGRESS_PROXY_SALES: "bot:p%40ss%[email protected]:8080",
+ });
+ expect(proxy?.password).toBe("p@ss:word");
+ expect(proxy?.server).toBe("proxy.internal:8080");
+ });
+
+ test("something that is not addressable at all is still passed through", () => {
+ expect(egressFor("sales", { EGRESS_PROXY_SALES: "::::" })).toEqual({
+ server: "::::",
+ });
+ });
});
describe("what gets shown to people", () => {
@@ -85,4 +114,12 @@ describe("what gets shown to people", () => {
egressLabel("sales", { EGRESS_PROXY_SALES: "proxy.internal:8080" }),
).toBe("proxy.internal:8080");
});
+
+ test("a bare host:port with credentials labels as the host only", () => {
+ const label = egressLabel("sales", {
+ EGRESS_PROXY_SALES: "bot:[email protected]:8080",
+ });
+ expect(label).toBe("proxy.internal:8080");
+ expect(label).not.toContain("s3cret");
+ });
});
diff --git a/agent-computer/tests/shell.test.ts b/agent-computer/tests/shell.test.ts
index b01a404d..959c3506 100644
--- a/agent-computer/tests/shell.test.ts
+++ b/agent-computer/tests/shell.test.ts
@@ -126,6 +126,24 @@ describe("what a command inherits", () => {
expect(JSON.stringify(env)).not.toContain("p@ss");
});
+ test("a proxy URL's userinfo does not pass when it was written without a scheme", () => {
+ // `HTTPS_PROXY=bot:[email protected]:8443` is a shape curl and wget accept. `new URL` reads
+ // it as the scheme `bot:` and a path, so the redaction above found no userinfo to strip and the
+ // password reached the Bot's shell, where `env` prints it.
+ const env = environmentForCommand(
+ source({
+ HTTP_PROXY: "bot:[email protected]:8080",
+ HTTPS_PROXY: "bot:p%[email protected]:8443",
+ }),
+ workspaceHome,
+ );
+ expect(env.HTTP_PROXY).toBe("proxy.internal:8080");
+ expect(env.HTTPS_PROXY).toBe("proxy.internal:8443");
+ expect(JSON.stringify(env)).not.toContain("s3cret");
+ expect(JSON.stringify(env)).not.toContain("p%40ss");
+ expect(JSON.stringify(env)).not.toContain("p@ss");
+ });
+
test("a proxy without userinfo is left alone", () => {
const env = environmentForCommand(source(), workspaceHome);
expect(env.HTTP_PROXY).toBe(allowed.HTTP_PROXY);