Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: proxy to window.location.origin when local hostnames differ (127.0.0.1 vs localhost) (#975)

Tim O'Farrell38253e41ba00
diff --git a/__tests__/api/agent-server-config.test.ts b/__tests__/api/agent-server-config.test.ts
index 0fd06e88f2..953cbe1f06 100644
--- a/__tests__/api/agent-server-config.test.ts
+++ b/__tests__/api/agent-server-config.test.ts
@@ -40,6 +40,16 @@ describe("agent server config", () => {
expect(getAgentServerBaseUrl()).toBe("https://work-1.example.dev");
});
+ it("uses the browser origin when browser is at localhost but config uses 127.0.0.1 (Docker CORS fix)", () => {
+ mockWindowLocation("http://localhost:8000/");
+ window.localStorage.setItem(
+ AGENT_SERVER_CONFIG_STORAGE_KEY,
+ JSON.stringify({ baseUrl: "http://127.0.0.1:8000" }),
+ );
+
+ expect(getAgentServerBaseUrl()).toBe("http://localhost:8000");
+ });
+
it("preserves a non-local backend URL from stored config", () => {
mockWindowLocation("https://work-1.example.dev/settings");
window.localStorage.setItem(
diff --git a/src/api/agent-server-config.ts b/src/api/agent-server-config.ts
index c31e4e2780..2e54d1a741 100644
--- a/src/api/agent-server-config.ts
+++ b/src/api/agent-server-config.ts
@@ -96,7 +96,9 @@ function shouldUseProxyOrigin(baseUrl: string): boolean {
const browserHostname = window.location.hostname;
return (
- localHosts.has(configuredUrl.hostname) && !localHosts.has(browserHostname)
+ localHosts.has(configuredUrl.hostname) &&
+ (!localHosts.has(browserHostname) ||
+ configuredUrl.hostname !== browserHostname)
);
} catch {
return false;