From 88813d675e50a7e9165cde160b87a12fb875e599 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Sat, 7 Nov 2026 22:56:07 -0600 Subject: [PATCH] fix(core): resolve client certificate paths like other config paths and allow a combined PEM --- .../client/src/promise/generated/types.ts | 2 +- packages/core/src/effect/app-node-platform.ts | 20 +++++++++++++------ packages/protocol/openapi.json | 5 ++--- packages/schema/src/config/network.ts | 5 +++-- services/site/src/docs/content/network.mdx | 6 ++++-- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index c92107346ca..b279c01e4cc 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -2495,7 +2495,7 @@ export type ConfigEntry = plugins?: Array 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 diff --git a/packages/core/src/effect/app-node-platform.ts b/packages/core/src/effect/app-node-platform.ts index fe0b16ee249..bcccd493a0b 100644 --- a/packages/core/src/effect/app-node-platform.ts +++ b/packages/core/src/effect/app-node-platform.ts @@ -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] }) diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 895eaea5ccd..7bfda8abfbb 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -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 } }, diff --git a/packages/schema/src/config/network.ts b/packages/schema/src/config/network.ts index 1fa1e4bd408..1c044e76bd0 100644 --- a/packages/schema/src/config/network.ts +++ b/packages/schema/src/config/network.ts @@ -7,8 +7,9 @@ export class ClientCertificate extends Schema.Class("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", diff --git a/services/site/src/docs/content/network.mdx b/services/site/src/docs/content/network.mdx index a1f25558405..05ab8c41f48 100644 --- a/services/site/src/docs/content/network.mdx +++ b/services/site/src/docs/content/network.mdx @@ -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.