GeezerVison is showing github.com — fix: recover from expired enterprise auth on remote config load (#31661) · ildella/opencode@02608a4
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/core/src/v1/config/error.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,3 +32,8 @@ export const DirectoryTypoError = NamedError.create("ConfigDirectoryTypoError", | 32 | 32 | dir: Schema.String, | 33 | 33 | suggestion: Schema.String, | 34 | 34 | }) | | 35 | + | | 36 | +export const RemoteAuthError = NamedError.create("ConfigRemoteAuthError", { | | 37 | + url: Schema.String, | | 38 | + remote: Schema.String, | | 39 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/providers.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { Argv } from "yargs" | 1 | 2 | import { Auth } from "../../auth" | 2 | 3 | import { cmd } from "./cmd" | 3 | 4 | import { CliError, effectCmd, fail } from "../effect-cmd" | @@ -298,7 +299,9 @@ export const ProvidersListCommand = effectCmd({ | 298 | 299 | export const ProvidersLoginCommand = effectCmd({ | 299 | 300 | command: "login [url]", | 300 | 301 | describe: "log in to a provider", | 301 | | - builder: (yargs) => | | 302 | + // URL login skips instance bootstrap, which would load remote config with the stale token and crash before re-auth. | | 303 | + instance: (args) => !args.url, | | 304 | + builder: (yargs: Argv) => | 302 | 305 | yargs | 303 | 306 | .positional("url", { | 304 | 307 | describe: "opencode auth provider", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/error.ts +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,6 +94,18 @@ export function FormatError(input: unknown): string | undefined { | 94 | 94 | return stringField(configFrontmatter, "message") ?? "" | 95 | 95 | } | 96 | 96 |
| | 97 | + // ConfigRemoteAuthError: { url: string, remote: string } | | 98 | + const remoteAuth = configData(input, "ConfigRemoteAuthError") | | 99 | + if (remoteAuth) { | | 100 | + const url = stringField(remoteAuth, "url") | | 101 | + const remote = stringField(remoteAuth, "remote") | | 102 | + return [ | | 103 | + `Failed to load remote config${remote ? ` from ${remote}` : ""}: the server returned a login page instead of JSON.`, | | 104 | + `Authentication is missing or has expired (the endpoint is likely behind an SSO or identity-aware proxy).`, | | 105 | + ...(url ? [`Run \`opencode auth login ${url}\` to re-authenticate.`] : []), | | 106 | + ].join("\n") | | 107 | + } | | 108 | + | 97 | 109 | // ConfigInvalidError: { path?: string, message?: string, issues?: Array<{ message: string, path: string[] }> } | 98 | 110 | const configInvalid = configData(input, "ConfigInvalidError") | 99 | 111 | if (configInvalid) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,10 +19,11 @@ import type { ConsoleState } from "@opencode-ai/core/v1/config/console-state" | 19 | 19 | import { FSUtil } from "@opencode-ai/core/fs-util" | 20 | 20 | import { InstanceState } from "@/effect/instance-state" | 21 | 21 | import { Context, Duration, Effect, Exit, Fiber, Layer, Option, Schema } from "effect" | 22 | | -import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http" | | 22 | +import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http" | 23 | 23 | import { EffectFlock } from "@opencode-ai/core/util/effect-flock" | 24 | 24 | import { containsPath, type InstanceContext } from "../project/instance-context" | 25 | 25 | import { ConfigV1 } from "@opencode-ai/core/v1/config/config" | | 26 | +import { RemoteAuthError } from "@opencode-ai/core/v1/config/error" | 26 | 27 | import { ConfigPermissionV1 } from "@opencode-ai/core/v1/config/permission" | 27 | 28 | import { ConfigPluginV1 } from "@opencode-ai/core/v1/config/plugin" | 28 | 29 | import { ConfigAgent } from "./agent" | @@ -187,6 +188,7 @@ export const layer = Layer.effect( | 187 | 188 | url: string, | 188 | 189 | headers: Record<string, string> | undefined, | 189 | 190 | schema: S, | | 191 | + loginOrigin: string, | 190 | 192 | ) { | 191 | 193 | const response = yield* HttpClient.filterStatusOk(withTransientReadRetry(http)) | 192 | 194 | .execute( | @@ -195,7 +197,15 @@ export const layer = Layer.effect( | 195 | 197 | .pipe( | 196 | 198 | Effect.catch((error) => Effect.die(new Error(`failed to fetch remote config from ${url}: ${String(error)}`))), | 197 | 199 | ) | 198 | | - return yield* HttpClientResponse.schemaBodyJson(schema)(response).pipe( | | 200 | + const body = yield* response.text.pipe( | | 201 | + Effect.catch((error) => Effect.die(new Error(`failed to read remote config from ${url}: ${String(error)}`))), | | 202 | + ) | | 203 | + // An auth proxy can answer with an HTML login page at HTTP 200 (passes filterStatusOk); treat it as a re-auth error, not a decode failure. | | 204 | + const contentType = (response.headers["content-type"] ?? "").toLowerCase() | | 205 | + if (contentType.includes("html") || /^\s*<!doctype|^\s*<html/i.test(body)) { | | 206 | + return yield* Effect.die(new RemoteAuthError({ url: loginOrigin, remote: url })) | | 207 | + } | | 208 | + return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(body).pipe( | 199 | 209 | Effect.catch((error) => Effect.die(new Error(`failed to decode remote config from ${url}: ${String(error)}`))), | 200 | 210 | ) | 201 | 211 | }) | @@ -348,7 +358,7 @@ export const layer = Layer.effect( | 348 | 358 | authEnv[value.key] = value.token | 349 | 359 | const wellknownURL = `${url}/.well-known/opencode` | 350 | 360 | yield* Effect.logDebug("fetching remote config", { url: wellknownURL }) | 351 | | - const wellknown = yield* fetchRemoteJson(wellknownURL, undefined, ConfigV1.WellKnown) | | 361 | + const wellknown = yield* fetchRemoteJson(wellknownURL, undefined, ConfigV1.WellKnown, url) | 352 | 362 | const remote = yield* Effect.promise(() => | 353 | 363 | substituteWellKnownRemoteConfig({ | 354 | 364 | value: wellknown.remote_config, | @@ -360,7 +370,7 @@ export const layer = Layer.effect( | 360 | 370 | const fetchedConfig = remote | 361 | 371 | ? yield* Effect.gen(function* () { | 362 | 372 | yield* Effect.logDebug("fetching remote config", { url: remote.url }) | 363 | | - const data = yield* fetchRemoteJson(remote.url, remote.headers, Schema.Json) | | 373 | + const data = yield* fetchRemoteJson(remote.url, remote.headers, Schema.Json, url) | 364 | 374 | if (isRecord(data) && isRecord(data.config)) return data.config | 365 | 375 | if (isRecord(data)) return data | 366 | 376 | return yield* Effect.die( | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/config/config.test.ts +32-2Lines changed: 32 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { test, expect, describe, afterEach, beforeEach, spyOn } from "bun:test" | 2 | 2 | import { ConfigV1 } from "@opencode-ai/core/v1/config/config" | 3 | | -import { Effect, Exit, Layer, Option } from "effect" | | 3 | +import { Cause, Effect, Exit, Layer, Option } from "effect" | | 4 | +import { NamedError } from "@opencode-ai/core/util/error" | 4 | 5 | import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http" | 5 | 6 | import { NodeFileSystem, NodePath } from "@effect/platform-node" | 6 | 7 | import { Config } from "@/config/config" | @@ -71,16 +72,25 @@ const wellKnownAuth = (url: string) => | 71 | 72 | function remoteConfigClient(input: { | 72 | 73 | wellKnown: unknown | 73 | 74 | remote?: unknown | | 75 | + remoteHtml?: string | 74 | 76 | seen: { wellKnown?: string; remote?: string; authorization?: string } | 75 | 77 | }) { | 76 | 78 | return HttpClient.make((request) => { | 77 | 79 | if (request.url.includes(".well-known/opencode")) { | 78 | 80 | input.seen.wellKnown = request.url | 79 | 81 | return Effect.succeed(json(request, input.wellKnown)) | 80 | 82 | } | 81 | | - if (input.remote !== undefined && request.url.includes("config.example.com")) { | | 83 | + if (request.url.includes("config.example.com") && (input.remote !== undefined || input.remoteHtml !== undefined)) { | 82 | 84 | input.seen.remote = request.url | 83 | 85 | input.seen.authorization = request.headers.authorization | | 86 | + if (input.remoteHtml !== undefined) { | | 87 | + return Effect.succeed( | | 88 | + HttpClientResponse.fromWeb( | | 89 | + request, | | 90 | + new Response(input.remoteHtml, { status: 200, headers: { "content-type": "text/html; charset=utf-8" } }), | | 91 | + ), | | 92 | + ) | | 93 | + } | 84 | 94 | return Effect.succeed(json(request, input.remote)) | 85 | 95 | } | 86 | 96 | return Effect.succeed(json(request, {}, 404)) | @@ -214,6 +224,7 @@ const wellKnown = (input: { | 214 | 224 | config?: unknown | 215 | 225 | remoteConfig?: { url: string; headers?: Record<string, string> } | 216 | 226 | remote?: unknown | | 227 | + remoteHtml?: string | 217 | 228 | wellKnown?: unknown | 218 | 229 | }) => { | 219 | 230 | const seen: { wellKnown?: string; remote?: string; authorization?: string } = {} | @@ -224,6 +235,7 @@ const wellKnown = (input: { | 224 | 235 | ...(input.remoteConfig !== undefined ? { remote_config: input.remoteConfig } : {}), | 225 | 236 | }, | 226 | 237 | remote: input.remote, | | 238 | + remoteHtml: input.remoteHtml, | 227 | 239 | }) | 228 | 240 | return { | 229 | 241 | seen, | @@ -1635,6 +1647,24 @@ invalidRemoteWellKnown.it.instance("wellknown remote_config rejects non-object c | 1635 | 1647 | }), | 1636 | 1648 | ) | 1637 | 1649 |
| | 1650 | +const loginPageWellKnown = wellKnown({ | | 1651 | + remoteConfig: { url: "https://config.example.com/opencode.json" }, | | 1652 | + remoteHtml: "<!DOCTYPE html><html><head><title>Sign in</title></head><body>Login required</body></html>", | | 1653 | +}) | | 1654 | + | | 1655 | +loginPageWellKnown.it.instance( | | 1656 | + "wellknown remote_config surfaces an actionable auth error when the gateway returns an HTML login page", | | 1657 | + () => | | 1658 | + Effect.gen(function* () { | | 1659 | + const exit = yield* Config.use.get().pipe(Effect.exit) | | 1660 | + expect(loginPageWellKnown.seen.remote).toBe("https://config.example.com/opencode.json") | | 1661 | + expect(Exit.isFailure(exit)).toBe(true) | | 1662 | + const error = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined | | 1663 | + expect(NamedError.hasName(error, "ConfigRemoteAuthError")).toBe(true) | | 1664 | + expect((error as { data?: { url?: string } }).data?.url).toBe("https://example.com") | | 1665 | + }), | | 1666 | +) | | 1667 | + | 1638 | 1668 | describe("resolvePluginSpec", () => { | 1639 | 1669 | test("keeps package specs unchanged", async () => { | 1640 | 1670 | await using tmp = await tmpdir() | |
|