GeezerVison is showing github.com — fix(server): honor OPENCODE_SERVER_USERNAME for serve and --server (#… · anomalyco/opencode@0749024
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/env.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,10 @@ export const password = Config.Redacted("OPENCODE_PASSWORD").pipe( | 12 | 12 | Config.withDefault(undefined), | 13 | 13 | ) | 14 | 14 |
| | 15 | +// The Basic auth username paired with the password above, for the same | | 16 | +// clients and servers. | | 17 | +export const username = Config.String("OPENCODE_SERVER_USERNAME").pipe(Config.withDefault("opencode")) | | 18 | + | 15 | 19 | export function session() { | 16 | 20 | return Object.fromEntries( | 17 | 21 | Object.entries(process.env).filter( | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/server-process.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,8 @@ const processEffect = Effect.fnUntraced(function* (options: Options) { | 109 | 109 | port, | 110 | 110 | cors: options.cors ?? config.cors, | 111 | 111 | password, | | 112 | + // Service and stdio clients authenticate with the default username, so only a manually run server adopts it. | | 113 | + username: options.mode === "default" ? yield* Env.username : undefined, | 112 | 114 | pty: { handoff }, | 113 | 115 | simulation: truthy(process.env.OPENCODE_SIMULATE), | 114 | 116 | database: { | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/services/server-connection.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,9 @@ export const resolve = Effect.fn("cli.server-connection.resolve")(function* (arg | 31 | 31 | const password = yield* Env.password | 32 | 32 | const endpoint = { | 33 | 33 | url: server, | 34 | | - auth: password ? { type: "basic" as const, username: "opencode", password: Redacted.value(password) } : undefined, | | 34 | + auth: password | | 35 | + ? { type: "basic" as const, username: yield* Env.username, password: Redacted.value(password) } | | 36 | + : undefined, | 35 | 37 | } satisfies Endpoint | 36 | 38 | const client = OpenCode.make({ baseUrl: endpoint.url, headers: Service.headers(endpoint) }) | 37 | 39 | const health = yield* Effect.tryPromise({ | @@ -94,7 +96,7 @@ function connectError(endpoint: Endpoint, cause: unknown) { | 94 | 96 | return new Error( | 95 | 97 | endpoint.auth === undefined | 96 | 98 | ? `Server at ${endpoint.url} requires a password; set OPENCODE_PASSWORD` | 97 | | - : `Server at ${endpoint.url} rejected the password`, | | 99 | + : `Server at ${endpoint.url} rejected the username or password`, | 98 | 100 | { cause }, | 99 | 101 | ) | 100 | 102 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/test/server-connection.test.ts +29-1Lines changed: 29 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import { NodeFileSystem, NodeServices } from "@effect/platform-node" | 2 | 2 | import { Global } from "@opencode/util/global" | 3 | 3 | import { OPENCODE_VERSION } from "../src/version" | 4 | 4 | import { expect, test } from "bun:test" | 5 | | -import { Console, Effect, FileSystem, Option, Scope } from "effect" | | 5 | +import { ConfigProvider, Console, Effect, FileSystem, Option, Scope } from "effect" | 6 | 6 | import { Command } from "effect/cli" | 7 | 7 | import fs from "node:fs/promises" | 8 | 8 | import os from "node:os" | @@ -60,6 +60,34 @@ test("resolution groups Effect-native lifecycle operations only for the managed | 60 | 60 | } | 61 | 61 | }) | 62 | 62 |
| | 63 | +test("--server authenticates with the configured username", async () => { | | 64 | + const server = Bun.serve({ | | 65 | + port: 0, | | 66 | + fetch(request) { | | 67 | + if (request.headers.get("authorization") !== `Basic ${btoa("custom:secret")}`) | | 68 | + return new Response(null, { status: 401 }) | | 69 | + return Response.json({ version: OPENCODE_VERSION, pid: process.pid, urls: [] }) | | 70 | + }, | | 71 | + }) | | 72 | + try { | | 73 | + const resolved = await Effect.runPromise( | | 74 | + ServerConnection.resolve({ server: server.url.toString() }).pipe( | | 75 | + Effect.provide( | | 76 | + ConfigProvider.layer( | | 77 | + ConfigProvider.fromEnv({ env: { OPENCODE_PASSWORD: "secret", OPENCODE_SERVER_USERNAME: "custom" } }), | | 78 | + ), | | 79 | + ), | | 80 | + Effect.provide(Global.layerWith({})), | | 81 | + Effect.provide(NodeFileSystem.layer), | | 82 | + Effect.scoped, | | 83 | + ), | | 84 | + ) | | 85 | + expect(resolved.endpoint.auth).toEqual({ type: "basic", username: "custom", password: "secret" }) | | 86 | + } finally { | | 87 | + await server.stop(true) | | 88 | + } | | 89 | +}) | | 90 | + | 63 | 91 | test("service options only require a matching version when requested", async () => { | 64 | 92 | const root = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-service-options-")) | 65 | 93 | const layer = Global.layerWith({ config: path.join(root, "config"), state: path.join(root, "state") }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/auth.ts +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,11 +13,13 @@ export type Info = { | 13 | 13 | readonly username: string | 14 | 14 | } | 15 | 15 |
| | 16 | +export const DEFAULT_USERNAME = "opencode" | | 17 | + | 16 | 18 | export const SESSION_TTL_SECONDS = 30 * 24 * 60 * 60 | 17 | 19 |
| 18 | 20 | export class Config extends Context.Service<Config, Info>()("@opencode/ServerAuthConfig") { | 19 | | - static configLayer(input: Pick<Info, "password">) { | 20 | | - return Layer.succeed(this, this.of({ ...input, username: "opencode" })) | | 21 | + static configLayer(input: { readonly password: Option.Option<string>; readonly username?: string }) { | | 22 | + return Layer.succeed(this, this.of({ password: input.password, username: input.username ?? DEFAULT_USERNAME })) | 21 | 23 | } | 22 | 24 |
| 23 | 25 | static get layer() { | @@ -29,11 +31,13 @@ export function required(config: Info) { | 29 | 31 | return Option.isSome(config.password) && config.password.value !== "" | 30 | 32 | } | 31 | 33 |
| 32 | | -// Session tokens issued by pairing links are accepted anywhere the password is. | | 34 | +// Session tokens issued by pairing links are accepted anywhere the password is. Clients holding a token do not know the | | 35 | +// configured username, so tokens are checked without it. | 33 | 36 | export function authorized(credentials: DecodedCredentials, config: Info) { | 34 | | - if (Option.isNone(config.password) || credentials.username !== config.username) return false | | 37 | + if (Option.isNone(config.password)) return false | 35 | 38 | const password = Redacted.value(credentials.password) | 36 | | - return password === config.password.value || verifySession(password, config) | | 39 | + if (verifySession(password, config)) return true | | 40 | + return credentials.username === config.username && password === config.password.value | 37 | 41 | } | 38 | 42 |
| 39 | 43 | // Sessions are signed with a key derived from the server password, so rotating the password revokes every session. | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/options.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ export const ServerOptions = Schema.Struct({ | 14 | 14 | hostname: Schema.optional(Schema.String), | 15 | 15 | port: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(65_535))), | 16 | 16 | password: Schema.optional(Schema.String), | | 17 | + username: Schema.optional(Schema.String), | 17 | 18 | cors: Schema.optional(Schema.Array(Schema.String)), | 18 | 19 | simulation: Schema.optional(Schema.Boolean), | 19 | 20 | database: Schema.optional(Database.Options), | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/process.ts +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,7 +66,11 @@ export const start = Effect.fn("ServerProcess.start")(function* <E, R>( | 66 | 66 | } | 67 | 67 | const urls = () => [...localURLs(), ...(remoteURLs?.() ?? [])] | 68 | 68 | const application = yield* Ref.make(Option.none<App>()) | 69 | | - const app = dispatch(password, status, application, options.app?.version ?? "unknown", urls, Global.Path.tmp) | | 69 | + const auth = ServerAuth.Config.of({ | | 70 | + password: Option.some(password), | | 71 | + username: options.username ?? ServerAuth.DEFAULT_USERNAME, | | 72 | + }) | | 73 | + const app = dispatch(auth, status, application, options.app?.version ?? "unknown", urls, Global.Path.tmp) | 70 | 74 | // Request fibers may continue inbound trace context, but must not inherit the server startup parent. | 71 | 75 | yield* bound.http | 72 | 76 | .serve( | @@ -172,14 +176,13 @@ function addressInUse(error: unknown) { | 172 | 176 | } | 173 | 177 |
| 174 | 178 | function dispatch( | 175 | | - password: string, | | 179 | + auth: ServerAuth.Info, | 176 | 180 | status: Status.Interface, | 177 | 181 | application: Ref.Ref<Option.Option<App>>, | 178 | 182 | version: string, | 179 | 183 | urls: () => ReadonlyArray<string>, | 180 | 184 | tmp: string, | 181 | 185 | ): App { | 182 | | - const auth = ServerAuth.Config.of({ password: Option.some(password), username: "opencode" }) | 183 | 186 | return Effect.gen(function* () { | 184 | 187 | const request = yield* HttpServerRequest.HttpServerRequest | 185 | 188 | const url = new URL(request.url, "http://localhost") | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/routes.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,7 +89,7 @@ export function createRoutes( | 89 | 89 | ) { | 90 | 90 | return makeRoutes( | 91 | 91 | options.password | 92 | | - ? ServerAuth.Config.configLayer({ password: Option.some(options.password) }) | | 92 | + ? ServerAuth.Config.configLayer({ password: Option.some(options.password), username: options.username }) | 93 | 93 | : ServerAuth.Config.layer, | 94 | 94 | options, | 95 | 95 | serviceURLs, | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/test/auth.test.ts +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,10 +2,17 @@ import { expect, test } from "bun:test" | 2 | 2 | import { ServerAuth } from "@opencode/server/auth" | 3 | 3 | import { Option, Redacted } from "effect" | 4 | 4 |
| 5 | | -test("accepts only the fixed opencode username", () => { | 6 | | - const config = { password: Option.some("secret"), username: "opencode" } | 7 | | - expect(ServerAuth.authorized({ username: "opencode", password: Redacted.make("secret") }, config)).toBe(true) | 8 | | - expect(ServerAuth.authorized({ username: "custom", password: Redacted.make("secret") }, config)).toBe(false) | | 5 | +test("passwords require the configured username", () => { | | 6 | + const config = { password: Option.some("secret"), username: "custom" } | | 7 | + expect(ServerAuth.authorized({ username: "custom", password: Redacted.make("secret") }, config)).toBe(true) | | 8 | + expect(ServerAuth.authorized({ username: "opencode", password: Redacted.make("secret") }, config)).toBe(false) | | 9 | +}) | | 10 | + | | 11 | +test("session tokens are accepted with any username", () => { | | 12 | + const config = { password: Option.some("secret"), username: "custom" } | | 13 | + const token = ServerAuth.issueSession(config) | | 14 | + if (!token) throw new Error("Expected a session token") | | 15 | + expect(ServerAuth.authorized({ username: "opencode", password: Redacted.make(token) }, config)).toBe(true) | 9 | 16 | }) | 10 | 17 |
| 11 | 18 | test("session tokens expire, resist tampering, and are revoked by changing the password", () => { | |
|