From 7adf3a3fc9e8f908662dd5b3c29f7fcfe26d1f02 Mon Sep 17 00:00:00 2001 From: sdivyanshu90 Date: Sun, 30 Aug 2026 11:22:03 +0530 Subject: [PATCH] fix(app): encode server credentials as UTF-8 --- packages/app/src/utils/server.test.ts | 11 +++++++++++ packages/app/src/utils/server.ts | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/app/src/utils/server.test.ts b/packages/app/src/utils/server.test.ts index 4666b7d6d03c..106bd605cf14 100644 --- a/packages/app/src/utils/server.test.ts +++ b/packages/app/src/utils/server.test.ts @@ -20,4 +20,15 @@ describe("authTokenFromCredentials", () => { test("encodes credentials with the default username", () => { expect(authTokenFromCredentials({ password: "secret" })).toBe(btoa("opencode:secret")) }) + + test("encodes unicode credentials as UTF-8", () => { + expect(authTokenFromCredentials({ password: "päss" })).toBe( + Buffer.from("opencode:päss", "utf8").toString("base64"), + ) + }) + + test("round trips multibyte credentials", () => { + const token = authTokenFromCredentials({ username: "用户", password: "秘密🔐" }) + expect(authFromToken(token)).toEqual({ username: "用户", password: "秘密🔐" }) + }) }) diff --git a/packages/app/src/utils/server.ts b/packages/app/src/utils/server.ts index 1c8292ca9d95..bdcf3b8edf5c 100644 --- a/packages/app/src/utils/server.ts +++ b/packages/app/src/utils/server.ts @@ -4,7 +4,12 @@ import type { ServerConnection } from "@/context/server" import { decode64 } from "@/utils/base64" export function authTokenFromCredentials(input: { username?: string; password: string }) { - return btoa(`${input.username ?? "opencode"}:${input.password}`) + return btoa( + Array.from( + new TextEncoder().encode(`${input.username ?? "opencode"}:${input.password}`), + (byte) => String.fromCharCode(byte), + ).join(""), + ) } export function authFromToken(token: string | null) {