GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: recover from expired enterprise auth on remote config load (#31661) · ildella/opencode@02608a4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • core/src/v1/config
      • error.ts
    • opencode
      • src
        • cli
          • cmd
            • providers.ts
          • error.ts
        • config
          • config.ts
      • test/config
        • config.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/v1/config/error.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/v1/config/error.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ export const DirectoryTypoError = NamedError.create("ConfigDirectoryTypoError",
3232
dir: Schema.String,
3333
suggestion: Schema.String,
3434
})
35+
36+
export const RemoteAuthError = NamedError.create("ConfigRemoteAuthError", {
37+
url: Schema.String,
38+
remote: Schema.String,
39+
})
Collapse file

‎packages/opencode/src/cli/cmd/providers.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/providers.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Argv } from "yargs"
12
import { Auth } from "../../auth"
23
import { cmd } from "./cmd"
34
import { CliError, effectCmd, fail } from "../effect-cmd"
@@ -298,7 +299,9 @@ export const ProvidersListCommand = effectCmd({
298299
export const ProvidersLoginCommand = effectCmd({
299300
command: "login [url]",
300301
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) =>
302305
yargs
303306
.positional("url", {
304307
describe: "opencode auth provider",
Collapse file

‎packages/opencode/src/cli/error.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/cli/error.ts
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ export function FormatError(input: unknown): string | undefined {
9494
return stringField(configFrontmatter, "message") ?? ""
9595
}
9696

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+
97109
// ConfigInvalidError: { path?: string, message?: string, issues?: Array<{ message: string, path: string[] }> }
98110
const configInvalid = configData(input, "ConfigInvalidError")
99111
if (configInvalid) {
Collapse file

‎packages/opencode/src/config/config.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import type { ConsoleState } from "@opencode-ai/core/v1/config/console-state"
1919
import { FSUtil } from "@opencode-ai/core/fs-util"
2020
import { InstanceState } from "@/effect/instance-state"
2121
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"
2323
import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
2424
import { containsPath, type InstanceContext } from "../project/instance-context"
2525
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
26+
import { RemoteAuthError } from "@opencode-ai/core/v1/config/error"
2627
import { ConfigPermissionV1 } from "@opencode-ai/core/v1/config/permission"
2728
import { ConfigPluginV1 } from "@opencode-ai/core/v1/config/plugin"
2829
import { ConfigAgent } from "./agent"
@@ -187,6 +188,7 @@ export const layer = Layer.effect(
187188
url: string,
188189
headers: Record<string, string> | undefined,
189190
schema: S,
191+
loginOrigin: string,
190192
) {
191193
const response = yield* HttpClient.filterStatusOk(withTransientReadRetry(http))
192194
.execute(
@@ -195,7 +197,15 @@ export const layer = Layer.effect(
195197
.pipe(
196198
Effect.catch((error) => Effect.die(new Error(`failed to fetch remote config from ${url}: ${String(error)}`))),
197199
)
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(
199209
Effect.catch((error) => Effect.die(new Error(`failed to decode remote config from ${url}: ${String(error)}`))),
200210
)
201211
})
@@ -348,7 +358,7 @@ export const layer = Layer.effect(
348358
authEnv[value.key] = value.token
349359
const wellknownURL = `${url}/.well-known/opencode`
350360
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)
352362
const remote = yield* Effect.promise(() =>
353363
substituteWellKnownRemoteConfig({
354364
value: wellknown.remote_config,
@@ -360,7 +370,7 @@ export const layer = Layer.effect(
360370
const fetchedConfig = remote
361371
? yield* Effect.gen(function* () {
362372
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)
364374
if (isRecord(data) && isRecord(data.config)) return data.config
365375
if (isRecord(data)) return data
366376
return yield* Effect.die(
Collapse file

‎packages/opencode/test/config/config.test.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/test/config/config.test.ts
+32-2Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test, expect, describe, afterEach, beforeEach, spyOn } from "bun:test"
22
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"
45
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http"
56
import { NodeFileSystem, NodePath } from "@effect/platform-node"
67
import { Config } from "@/config/config"
@@ -71,16 +72,25 @@ const wellKnownAuth = (url: string) =>
7172
function remoteConfigClient(input: {
7273
wellKnown: unknown
7374
remote?: unknown
75+
remoteHtml?: string
7476
seen: { wellKnown?: string; remote?: string; authorization?: string }
7577
}) {
7678
return HttpClient.make((request) => {
7779
if (request.url.includes(".well-known/opencode")) {
7880
input.seen.wellKnown = request.url
7981
return Effect.succeed(json(request, input.wellKnown))
8082
}
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)) {
8284
input.seen.remote = request.url
8385
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+
}
8494
return Effect.succeed(json(request, input.remote))
8595
}
8696
return Effect.succeed(json(request, {}, 404))
@@ -214,6 +224,7 @@ const wellKnown = (input: {
214224
config?: unknown
215225
remoteConfig?: { url: string; headers?: Record<string, string> }
216226
remote?: unknown
227+
remoteHtml?: string
217228
wellKnown?: unknown
218229
}) => {
219230
const seen: { wellKnown?: string; remote?: string; authorization?: string } = {}
@@ -224,6 +235,7 @@ const wellKnown = (input: {
224235
...(input.remoteConfig !== undefined ? { remote_config: input.remoteConfig } : {}),
225236
},
226237
remote: input.remote,
238+
remoteHtml: input.remoteHtml,
227239
})
228240
return {
229241
seen,
@@ -1635,6 +1647,24 @@ invalidRemoteWellKnown.it.instance("wellknown remote_config rejects non-object c
16351647
}),
16361648
)
16371649

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+
16381668
describe("resolvePluginSpec", () => {
16391669
test("keeps package specs unchanged", async () => {
16401670
await using tmp = await tmpdir()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.