fix(core): resolve client certificate paths like other config paths and allow a combined PEM

This commit is contained in:
Aiden Cline
2026-11-07 22:56:07 -06:00
Unverified
parent 567a9b3472
commit 88813d675e
5 changed files with 24 additions and 14 deletions
+1 -1
View File
@@ -2495,7 +2495,7 @@ export type ConfigEntry =
plugins?: Array<string | { package: string; options?: { [x: string]: JsonValue } }>
worktree?: ConfigWorktree
warming?: boolean | { prompt?: string; interval?: "auto" | (string & {}); duration?: string }
network?: { client_certificate?: { cert: string; key: string; passphrase?: string } }
network?: { client_certificate?: { cert: string; key?: string; passphrase?: string } }
providers?: {
[x: string]: {
canonical?: ProviderID
+14 -6
View File
@@ -3,12 +3,13 @@ import { Document } from "@opencode/schema/config"
import { Effect, Layer, Option } from "effect"
import { FetchHttpClient, HttpClient, HttpClientError } from "effect/http"
import { Socket } from "effect/socket"
import os from "os"
import path from "path"
import { makeGlobalNode } from "@opencode/util/effect/app-node"
import { httpClient } from "@opencode/util/effect/app-node-platform"
import { FSUtil } from "@opencode/util/fs-util"
import { Global } from "@opencode/util/global"
import { Config } from "../config.js"
import { Location } from "../location.js"
import { WebSocketConstructor } from "./websocket-constructor.js"
// Bun's fetch ends a request after five quiet minutes, before provider routes' own header and chunk timeouts.
@@ -29,6 +30,7 @@ const withClientCertificate = Layer.effect(
HttpClient.HttpClient,
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const global = yield* Global.Service
const client = yield* HttpClient.HttpClient
return HttpClient.transform(client, (effect, request) =>
Effect.gen(function* () {
@@ -41,13 +43,19 @@ const withClientCertificate = Layer.effect(
)
const certificate = source?.info.network?.client_certificate
if (!source || !certificate) return yield* effect
// Config without a file (inline content, wellknown) resolves relative paths against the Location, like `{file:}`.
const directory = source.path
? path.dirname(source.path)
: Option.match(yield* Effect.serviceOption(Location.Service), {
onNone: () => global.home,
onSome: (location) => location.directory,
})
const resolve = (file: string) =>
file.startsWith("~/")
? path.join(os.homedir(), file.slice(2))
: path.resolve(source.path ? path.dirname(source.path) : "", file)
file.startsWith("~/") ? path.join(global.home, file.slice(2)) : path.resolve(directory, file)
const tls = yield* Effect.all({
cert: fs.readFileString(resolve(certificate.cert)),
key: fs.readFileString(resolve(certificate.key)),
// A combined PEM file holds both the certificate and its key.
key: fs.readFileString(resolve(certificate.key ?? certificate.cert)),
}).pipe(
Effect.mapError(
(error) =>
@@ -80,7 +88,7 @@ const withClientCertificate = Layer.effect(
export const requestExecutor = makeGlobalNode({
service: RequestExecutor.Service,
layer: RequestExecutor.layer.pipe(Layer.provide(withoutBunTimeout), Layer.provide(withClientCertificate)),
deps: [httpClient, FSUtil.node],
deps: [httpClient, FSUtil.node, Global.node],
})
export const llmClient = makeGlobalNode({ service: LLMClient.Service, layer: LLMClient.layer, deps: [requestExecutor] })
+2 -3
View File
@@ -16664,14 +16664,13 @@
"description": "Path to the PEM client certificate: absolute, `~/`, or relative to this configuration file"
},
"key": {
"type": "string",
"description": "Path to the PEM private key: absolute, `~/`, or relative to this configuration file"
"type": "string"
},
"passphrase": {
"type": "string"
}
},
"required": ["cert", "key"],
"required": ["cert"],
"additionalProperties": false
}
},
+3 -2
View File
@@ -7,8 +7,9 @@ export class ClientCertificate extends Schema.Class<ClientCertificate>("Config.N
cert: Schema.String.annotate({
description: "Path to the PEM client certificate: absolute, `~/`, or relative to this configuration file",
}),
key: Schema.String.annotate({
description: "Path to the PEM private key: absolute, `~/`, or relative to this configuration file",
key: Schema.String.pipe(optional).annotate({
description:
"Path to the PEM private key: absolute, `~/`, or relative to this configuration file. Defaults to `cert`, for a file holding both",
}),
passphrase: Schema.String.pipe(optional).annotate({
description: "Passphrase for an encrypted private key. Use `{env:...}` or `{file:...}` to keep it out of the file",
+4 -2
View File
@@ -109,8 +109,10 @@ model requests, and the TLS handshake only sends it to servers that ask for one.
}
```
- `cert` and `key` are paths to PEM files: absolute, `~/`, or relative to the configuration file. They are read on each
request, so a rotated certificate is used without a restart.
- `cert` and `key` are paths to PEM files: absolute, `~/`, or relative to the configuration file (or to the project for
config without a file, such as `OPENCODE_CONFIG_CONTENT`). They are read on each request, so a rotated certificate is
used without a restart.
- Leave out `key` when `cert` points to one PEM file holding both the certificate and the key.
- `passphrase` is only needed for an encrypted key. Use `{env:...}` or `{file:...}` to keep it out of the file.
- Model requests with a client certificate don't follow redirects, so the certificate isn't presented to another host.