mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(core): follow Console redirects for skills and web search (#62015)
This commit is contained in:
committed by
GitHub
Unverified
parent
1c3decd370
commit
7e42be4248
@@ -1,8 +1,16 @@
|
||||
import { Duration, Effect, Equal, FiberHandle, Option, Schema, Scope, Semaphore } from "effect"
|
||||
import { Duration, Effect, Equal, FiberHandle, Option, Schema, Scope, Semaphore, Stream } from "effect"
|
||||
import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect/integration"
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import type { SessionHttpResponse } from "@opencode/plugin/effect/session"
|
||||
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import {
|
||||
FetchHttpClient,
|
||||
HttpBody,
|
||||
HttpClient,
|
||||
HttpClientError,
|
||||
HttpClientRequest,
|
||||
HttpClientResponse,
|
||||
HttpMethod,
|
||||
} from "effect/http"
|
||||
import { App } from "../../app.js"
|
||||
import { Bus } from "../../bus.js"
|
||||
import { Config } from "../../config.js"
|
||||
@@ -389,17 +397,14 @@ export const OpencodePlugin = define<
|
||||
providerID,
|
||||
}),
|
||||
)
|
||||
const response = yield* HttpClient.withScope(HttpClient.filterStatusOk(http))
|
||||
.execute(request)
|
||||
.pipe(
|
||||
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(WebSearch.Response)),
|
||||
Effect.scoped,
|
||||
Effect.timeoutOrElse({
|
||||
duration: Duration.seconds(25),
|
||||
orElse: () => Effect.fail(new Error("OpenCode web search request timed out")),
|
||||
}),
|
||||
)
|
||||
const response = yield* follow(http, request).pipe(
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(WebSearch.Response)),
|
||||
Effect.scoped,
|
||||
Effect.timeoutOrElse({
|
||||
duration: Duration.seconds(25),
|
||||
orElse: () => Effect.fail(new Error("OpenCode web search request timed out")),
|
||||
}),
|
||||
)
|
||||
if (response.providerID !== providerID) {
|
||||
return yield* Effect.fail(
|
||||
new Error(`OpenCode web search returned provider ${response.providerID} instead of ${providerID}`),
|
||||
@@ -627,23 +632,79 @@ function organizationName(credential: Credential.Value) {
|
||||
return typeof credential.metadata?.orgName === "string" ? credential.metadata.orgName : undefined
|
||||
}
|
||||
|
||||
const MAX_REDIRECTS = 5
|
||||
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308])
|
||||
// Headers that name or authenticate the caller. They stay with the Console's origin when a redirect leaves it.
|
||||
const CREDENTIAL_HEADERS = new Set(["authorization", "cookie", "proxy-authorization", "x-org-id"])
|
||||
|
||||
// The Console answers skill downloads with a presigned-storage redirect and web search through a gateway that may
|
||||
// redirect too, so a redirect is a normal answer. Following it here, instead of letting fetch follow it, bounds the
|
||||
// hops and keeps the connection's credentials from crossing an origin boundary.
|
||||
function follow(
|
||||
http: HttpClient.HttpClient,
|
||||
request: HttpClientRequest.HttpClientRequest,
|
||||
redirects = 0,
|
||||
): Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError | Error, Scope.Scope> {
|
||||
// Scoped per hop so a caller's timeout or interruption aborts the request in flight.
|
||||
return HttpClient.withScope(http)
|
||||
.execute(request)
|
||||
.pipe(
|
||||
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }),
|
||||
Effect.flatMap((response) => {
|
||||
const location = response.headers.location
|
||||
if (!REDIRECT_STATUSES.has(response.status) || !location) return HttpClientResponse.filterStatusOk(response)
|
||||
if (redirects >= MAX_REDIRECTS) return Effect.fail(new Error(`Too many redirects (more than ${MAX_REDIRECTS})`))
|
||||
return Effect.flatMap(redirectTarget(request, location), (target) =>
|
||||
// Release this hop's connection before the next request instead of holding it until the download ends.
|
||||
HttpClientResponse.stream(Effect.succeed(response)).pipe(
|
||||
Stream.runDrain,
|
||||
// The redirect's own body is irrelevant, so failing to read it must not fail the download.
|
||||
Effect.ignore,
|
||||
Effect.andThen(follow(http, redirected(request, target, response.status), redirects + 1)),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
function redirectTarget(request: HttpClientRequest.HttpClientRequest, location: string) {
|
||||
return Effect.try({
|
||||
try: () => {
|
||||
const target = new URL(location, request.url)
|
||||
if (target.protocol !== "http:" && target.protocol !== "https:")
|
||||
throw new Error(`redirect to unsupported protocol ${target.protocol}`)
|
||||
return target
|
||||
},
|
||||
catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
|
||||
})
|
||||
}
|
||||
|
||||
function redirected(request: HttpClientRequest.HttpClientRequest, target: URL, status: number) {
|
||||
// A 303 always, and a 301 or 302 after a POST, become a bodyless GET, as fetch would send them.
|
||||
const get = status === 303 || ((status === 301 || status === 302) && request.method === "POST")
|
||||
const headers =
|
||||
target.origin === new URL(request.url).origin
|
||||
? request.headers
|
||||
: Object.fromEntries(Object.entries(request.headers).filter(([name]) => !CREDENTIAL_HEADERS.has(name)))
|
||||
// `HttpClientRequest.method` is typed `string`, while `make` only accepts a known method.
|
||||
// The `href` string keeps the location's query exactly as sent: a `URL` would decode and re-encode it,
|
||||
// which breaks a presigned archive download whose signature covers the encoding.
|
||||
return HttpClientRequest.make(get ? "GET" : (request.method as HttpMethod.HttpMethod))(target.href, {
|
||||
headers,
|
||||
body: get ? HttpBody.empty : request.body,
|
||||
})
|
||||
}
|
||||
|
||||
function downloadSkill(http: HttpClient.HttpClient, server: string, slug: string, headers: Record<string, string>) {
|
||||
return Effect.gen(function* () {
|
||||
if (!SkillDiscovery.isSafeSegment(slug)) return yield* Effect.fail(new Error(`unsafe skill slug: ${slug}`))
|
||||
const bundle = yield* HttpClient.withScope(HttpClient.filterStatusOk(http))
|
||||
.execute(
|
||||
HttpClientRequest.get(`${server}/api/v2/library/skills/${encodeURIComponent(slug)}`).pipe(
|
||||
HttpClientRequest.acceptJson,
|
||||
HttpClientRequest.setHeaders(headers),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
// The connection's credentials must not follow a redirect to another server.
|
||||
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(SkillBundle)),
|
||||
Effect.scoped,
|
||||
Effect.timeout("30 seconds"),
|
||||
)
|
||||
const bundle = yield* follow(
|
||||
http,
|
||||
HttpClientRequest.get(`${server}/api/v2/library/skills/${encodeURIComponent(slug)}`).pipe(
|
||||
HttpClientRequest.acceptJson,
|
||||
HttpClientRequest.setHeaders(headers),
|
||||
),
|
||||
).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(SkillBundle)), Effect.scoped, Effect.timeout("30 seconds"))
|
||||
if (bundle.slug !== slug) return yield* Effect.fail(new Error(`expected skill ${slug}, received ${bundle.slug}`))
|
||||
const unsafe = bundle.files.find((file) => !SkillDiscovery.isSafeRelativePath(file.path))
|
||||
if (unsafe) return yield* Effect.fail(new Error(`unsafe skill file path: ${unsafe.path}`))
|
||||
|
||||
@@ -30,6 +30,7 @@ import { OpencodePlugin } from "@opencode/core/plugin/provider/opencode"
|
||||
import { Provider } from "@opencode/core/provider"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { SessionMessage } from "@opencode/core/session/message"
|
||||
import { Skill } from "@opencode/core/skill"
|
||||
import { WebSearch } from "@opencode/core/websearch"
|
||||
import { withEnv } from "../fixture/env"
|
||||
import { drain } from "../lib/clock"
|
||||
@@ -56,6 +57,11 @@ const consoleDocument = (config: Config.Interface) =>
|
||||
),
|
||||
)
|
||||
const consoleSearch = { id: WebSearch.ID.make("opencode"), name: "OpenCode Web Search" }
|
||||
// A Console skill archive as the plugin downloads it: the version's files, with SKILL.md carrying the frontmatter.
|
||||
const skillBundle = {
|
||||
slug: "deploy",
|
||||
files: [{ path: "SKILL.md", content: "---\nname: Deploy\ndescription: Deploys the service.\n---\n\n# Deploy\n" }],
|
||||
}
|
||||
const consolePolicies = (config: Config.Interface) =>
|
||||
consoleDocument(config).pipe(Effect.map((document) => document?.info.policies ?? []))
|
||||
|
||||
@@ -1593,16 +1599,21 @@ describe("OpencodePlugin", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.live("does not forward hosted search credentials through redirects", () =>
|
||||
it.live("follows a hosted search redirect without sending credentials to another origin", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const requests: string[] = []
|
||||
const requests: Array<{ path: string; method: string; authorization: string | null; body?: unknown }> = []
|
||||
const state = { crossOrigin: false }
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
fetch: async (request) => {
|
||||
const url = new URL(request.url)
|
||||
requests.push(url.pathname)
|
||||
requests.push({
|
||||
path: url.pathname,
|
||||
method: request.method,
|
||||
authorization: request.headers.get("authorization"),
|
||||
...(request.method === "POST" ? { body: await request.json() } : {}),
|
||||
})
|
||||
if (url.pathname === "/console/api/v2/config") {
|
||||
return Response.json({
|
||||
providers: {},
|
||||
@@ -1611,6 +1622,7 @@ describe("OpencodePlugin", () => {
|
||||
}
|
||||
if (url.pathname === "/console/api/websearch") {
|
||||
if (state.crossOrigin) url.hostname = "127.0.0.1"
|
||||
// A gateway hop keeps the method and body, so the request has to survive it.
|
||||
return Response.redirect(`${url.origin}/outside-console`, 307)
|
||||
}
|
||||
return Response.json({ providerID: "opencode", results: [] })
|
||||
@@ -1632,16 +1644,218 @@ describe("OpencodePlugin", () => {
|
||||
})
|
||||
yield* addPlugin()
|
||||
|
||||
expect(
|
||||
(yield* websearch.query({ query: "private search", providerID: consoleSearch.id }).pipe(Effect.flip))._tag,
|
||||
).toBe("WebSearch.Request")
|
||||
expect(requests).toEqual(["/console/api/v2/config", "/console/api/websearch"])
|
||||
// A same-origin hop is still the Console, so it keeps the caller's credentials.
|
||||
expect(yield* websearch.query({ query: "private search", providerID: consoleSearch.id })).toEqual(
|
||||
new WebSearch.Response({ providerID: WebSearch.ID.make("opencode"), results: [] }),
|
||||
)
|
||||
expect(requests.at(-1)).toEqual({
|
||||
path: "/outside-console",
|
||||
method: "POST",
|
||||
authorization: "Bearer secret",
|
||||
body: { query: "private search", providerID: "opencode" },
|
||||
})
|
||||
|
||||
// Another origin never receives them.
|
||||
state.crossOrigin = true
|
||||
expect(yield* websearch.query({ query: "private search", providerID: consoleSearch.id })).toEqual(
|
||||
new WebSearch.Response({ providerID: WebSearch.ID.make("opencode"), results: [] }),
|
||||
)
|
||||
expect(requests.at(-1)).toEqual({
|
||||
path: "/outside-console",
|
||||
method: "POST",
|
||||
authorization: null,
|
||||
body: { query: "private search", providerID: "opencode" },
|
||||
})
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("follows a 302 hosted search redirect as a GET", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const requests: Array<{ path: string; method: string; authorization: string | null }> = []
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
requests.push({
|
||||
path: url.pathname,
|
||||
method: request.method,
|
||||
authorization: request.headers.get("authorization"),
|
||||
})
|
||||
if (url.pathname === "/api/v2/config") {
|
||||
return Response.json({ providers: {}, websearch: { provider: "opencode" } })
|
||||
}
|
||||
if (url.pathname === "/api/websearch") return Response.redirect(`${url.origin}/gateway/search`, 302)
|
||||
return Response.json({ providerID: "opencode", results: [] })
|
||||
},
|
||||
})
|
||||
return { requests, server }
|
||||
}),
|
||||
({ requests, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const websearch = yield* WebSearch.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({
|
||||
type: "key",
|
||||
key: "secret",
|
||||
metadata: { server: server.url.origin, orgID: "org_test" },
|
||||
}),
|
||||
})
|
||||
yield* addPlugin()
|
||||
|
||||
expect(yield* websearch.query({ query: "gateway search", providerID: consoleSearch.id })).toEqual(
|
||||
new WebSearch.Response({ providerID: WebSearch.ID.make("opencode"), results: [] }),
|
||||
)
|
||||
expect(requests.at(-1)).toEqual({
|
||||
path: "/gateway/search",
|
||||
method: "GET",
|
||||
authorization: "Bearer secret",
|
||||
})
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("stops following a hosted search redirect loop at the hop limit", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const requests: string[] = []
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
requests.push(url.pathname)
|
||||
if (url.pathname === "/api/v2/config") {
|
||||
return Response.json({ providers: {}, websearch: { provider: "opencode" } })
|
||||
}
|
||||
return Response.redirect(`${url.origin}/loop`, 302)
|
||||
},
|
||||
})
|
||||
return { requests, server }
|
||||
}),
|
||||
({ requests, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const websearch = yield* WebSearch.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({
|
||||
type: "key",
|
||||
key: "secret",
|
||||
metadata: { server: server.url.origin, orgID: "org_test" },
|
||||
}),
|
||||
})
|
||||
yield* addPlugin()
|
||||
|
||||
expect(
|
||||
(yield* websearch.query({ query: "private search", providerID: consoleSearch.id }).pipe(Effect.flip))._tag,
|
||||
(yield* websearch.query({ query: "looping search", providerID: consoleSearch.id }).pipe(Effect.flip))._tag,
|
||||
).toBe("WebSearch.Request")
|
||||
expect(requests).toEqual(["/console/api/v2/config", "/console/api/websearch", "/console/api/websearch"])
|
||||
expect(requests.filter((path) => path === "/loop")).toHaveLength(5)
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("downloads a skill through a redirect without sending credentials to another origin", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const downloads: Array<{ path: string; search: string; authorization: string | null; orgID: string | null }> =
|
||||
[]
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
downloads.push({
|
||||
path: url.pathname,
|
||||
search: url.search,
|
||||
authorization: request.headers.get("authorization"),
|
||||
orgID: request.headers.get("x-org-id"),
|
||||
})
|
||||
if (url.pathname === "/api/v2/config") {
|
||||
return Response.json({ providers: {}, library: { skills: [{ slug: "deploy", version: "1" }] } })
|
||||
}
|
||||
if (url.pathname === "/api/v2/library/skills/deploy") {
|
||||
// Skill archives come from presigned storage on its own host, where the signature covers the encoding.
|
||||
const target = new URL("/archive/deploy.json?expires=1&sig=a%20b~c", url)
|
||||
target.hostname = "127.0.0.1"
|
||||
return Response.redirect(target.toString(), 302)
|
||||
}
|
||||
return Response.json(skillBundle)
|
||||
},
|
||||
})
|
||||
return { downloads, server }
|
||||
}),
|
||||
({ downloads, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const skills = yield* Skill.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({
|
||||
type: "key",
|
||||
key: "secret",
|
||||
metadata: { server: server.url.origin, orgID: "org_test" },
|
||||
}),
|
||||
})
|
||||
yield* addPlugin()
|
||||
|
||||
yield* eventually(skills.list(), (list) => list.some((skill) => skill.id === Skill.ID.make("deploy")))
|
||||
expect(downloads).toContainEqual({
|
||||
path: "/api/v2/library/skills/deploy",
|
||||
search: "",
|
||||
authorization: "Bearer secret",
|
||||
orgID: "org_test",
|
||||
})
|
||||
expect(downloads.at(-1)).toEqual({
|
||||
path: "/archive/deploy.json",
|
||||
search: "?expires=1&sig=a%20b~c",
|
||||
authorization: null,
|
||||
orgID: null,
|
||||
})
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("keeps credentials on a same-origin skill redirect", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const downloads: Array<{ path: string; authorization: string | null }> = []
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
const url = new URL(request.url)
|
||||
downloads.push({ path: url.pathname, authorization: request.headers.get("authorization") })
|
||||
if (url.pathname === "/api/v2/config") {
|
||||
return Response.json({ providers: {}, library: { skills: [{ slug: "deploy", version: "1" }] } })
|
||||
}
|
||||
if (url.pathname === "/api/v2/library/skills/deploy")
|
||||
return Response.redirect(`${url.origin}/assets/deploy.json`, 302)
|
||||
return Response.json(skillBundle)
|
||||
},
|
||||
})
|
||||
return { downloads, server }
|
||||
}),
|
||||
({ downloads, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const skills = yield* Skill.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({
|
||||
type: "key",
|
||||
key: "secret",
|
||||
metadata: { server: server.url.origin, orgID: "org_test" },
|
||||
}),
|
||||
})
|
||||
yield* addPlugin()
|
||||
|
||||
yield* eventually(skills.list(), (list) => list.some((skill) => skill.id === Skill.ID.make("deploy")))
|
||||
expect(downloads.at(-1)).toEqual({ path: "/assets/deploy.json", authorization: "Bearer secret" })
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user