11// Copilot BYOK transport proxy keeps OpenClaw in charge of outbound network policy.
2- import { randomBytes } from "node:crypto" ;
2+ import { randomBytes , timingSafeEqual } from "node:crypto" ;
33import { createServer , type IncomingMessage , type ServerResponse } from "node:http" ;
44import { Readable } from "node:stream" ;
55import { pipeline } from "node:stream/promises" ;
@@ -8,6 +8,7 @@ import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
88import type { ResolvedCopilotProvider } from "./provider-bridge.js" ;
99
1010const LOOPBACK_HOST = "127.0.0.1" ;
11+ const PROXY_CREDENTIAL_HEADER_PREFIX = "x-openclaw-copilot-byok-" ;
1112
1213type CopilotByokProxyHandle = {
1314 close : ( ) => Promise < void > ;
@@ -32,14 +33,21 @@ export async function createCopilotByokProxy(
3233 const nonce = randomBytes ( 12 ) . toString ( "hex" ) ;
3334 const targetPathPrefix = trimTrailingSlash ( targetBaseUrl . pathname ) ;
3435 const proxyPathPrefix = `/${ nonce } ${ targetPathPrefix } ` ;
36+ // Azure rebuilds nonce-less /openai paths, so carry the same per-proxy secret
37+ // in an SDK provider header and validate it before accepting request bytes.
3538 const acceptsAzureSdkPaths = providerConfig . type === "azure" ;
39+ const proxyCredentialHeader = acceptsAzureSdkPaths
40+ ? createProxyCredentialHeaderName ( providerConfig . headers )
41+ : undefined ;
3642 const upstreamBearerAuthorization = resolveUpstreamBearerAuthorization ( providerConfig ) ;
3743 const activeFetches = new Set < AbortController > ( ) ;
3844 const server = createServer ( ( req , res ) => {
3945 void handleProxyRequest ( req , res , {
4046 acceptsAzureSdkPaths,
4147 activeFetches,
4248 proxyPathPrefix,
49+ proxyCredential : nonce ,
50+ proxyCredentialHeader,
4351 targetBaseUrl,
4452 targetPathPrefix,
4553 upstreamBearerAuthorization,
@@ -69,6 +77,15 @@ export async function createCopilotByokProxy(
6977 provider : {
7078 ...providerConfig ,
7179 baseUrl : sdkBaseUrl ,
80+ ...( proxyCredentialHeader
81+ ? {
82+ headers : buildSdkProviderHeaders (
83+ providerConfig . headers ,
84+ proxyCredentialHeader ,
85+ nonce ,
86+ ) ,
87+ }
88+ : { } ) ,
7289 } ,
7390 } ,
7491 close : async ( ) => {
@@ -92,6 +109,8 @@ async function handleProxyRequest(
92109 acceptsAzureSdkPaths : boolean ;
93110 activeFetches : Set < AbortController > ;
94111 proxyPathPrefix : string ;
112+ proxyCredential : string ;
113+ proxyCredentialHeader : string | undefined ;
95114 targetBaseUrl : URL ;
96115 targetPathPrefix : string ;
97116 upstreamBearerAuthorization : string | undefined ;
@@ -108,9 +127,14 @@ async function handleProxyRequest(
108127 }
109128 } ) ;
110129 try {
111- const canInjectBearerAuthorization = isNonceProtectedProxyRequest ( req , params . proxyPathPrefix ) ;
130+ const isNonceProtected = isNonceProtectedProxyRequest ( req , params . proxyPathPrefix ) ;
112131 const url = resolveTargetUrl ( req , params ) ;
113- if ( ! url ) {
132+ if (
133+ ! url ||
134+ ( ! isNonceProtected &&
135+ ( ! params . proxyCredentialHeader ||
136+ ! hasValidProxyCredential ( req , params . proxyCredentialHeader , params . proxyCredential ) ) )
137+ ) {
114138 res . writeHead ( 404 ) ;
115139 res . end ( "Not found" ) ;
116140 return ;
@@ -121,9 +145,10 @@ async function handleProxyRequest(
121145 init : {
122146 method : req . method ,
123147 headers : buildProxyRequestHeaders ( req . headers , {
124- upstreamBearerAuthorization : canInjectBearerAuthorization
148+ upstreamBearerAuthorization : isNonceProtected
125149 ? params . upstreamBearerAuthorization
126150 : undefined ,
151+ proxyCredentialHeader : params . proxyCredentialHeader ,
127152 } ) ,
128153 signal : upstreamAbort . signal ,
129154 ...( body ? { body } : { } ) ,
@@ -210,6 +235,22 @@ function isNonceProtectedProxyRequest(req: IncomingMessage, proxyPathPrefix: str
210235 ) ;
211236}
212237
238+ function hasValidProxyCredential (
239+ req : IncomingMessage ,
240+ headerName : string ,
241+ expected : string ,
242+ ) : boolean {
243+ const received = req . headers [ headerName ] ;
244+ if ( typeof received !== "string" ) {
245+ return false ;
246+ }
247+ const receivedBytes = Buffer . from ( received ) ;
248+ const expectedBytes = Buffer . from ( expected ) ;
249+ return (
250+ receivedBytes . length === expectedBytes . length && timingSafeEqual ( receivedBytes , expectedBytes )
251+ ) ;
252+ }
253+
213254async function readBody ( req : IncomingMessage ) : Promise < Buffer < ArrayBuffer > | undefined > {
214255 const chunks : Buffer [ ] = [ ] ;
215256 for await ( const chunk of req ) {
@@ -218,10 +259,17 @@ async function readBody(req: IncomingMessage): Promise<Buffer<ArrayBuffer> | und
218259 return chunks . length > 0 ? Buffer . concat ( chunks ) : undefined ;
219260}
220261
221- function normalizeProxyRequestHeaders ( headers : IncomingMessage [ "headers" ] ) : Record < string , string > {
262+ function normalizeProxyRequestHeaders (
263+ headers : IncomingMessage [ "headers" ] ,
264+ proxyCredentialHeader : string | undefined ,
265+ ) : Record < string , string > {
222266 const out : Record < string , string > = { } ;
223267 for ( const [ key , value ] of Object . entries ( headers ) ) {
224- if ( isHopByHopHeader ( key ) || key . toLowerCase ( ) === "accept-encoding" ) {
268+ if (
269+ isHopByHopHeader ( key ) ||
270+ key . toLowerCase ( ) === "accept-encoding" ||
271+ key . toLowerCase ( ) === proxyCredentialHeader
272+ ) {
225273 continue ;
226274 }
227275 const normalized = normalizeHeaderValue ( value ) ;
@@ -233,11 +281,31 @@ function normalizeProxyRequestHeaders(headers: IncomingMessage["headers"]): Reco
233281 return out ;
234282}
235283
284+ function buildSdkProviderHeaders (
285+ headers : ProviderConfig [ "headers" ] ,
286+ proxyCredentialHeader : string ,
287+ proxyCredential : string ,
288+ ) : Record < string , string > {
289+ return { ...headers , [ proxyCredentialHeader ] : proxyCredential } ;
290+ }
291+
292+ function createProxyCredentialHeaderName ( headers : ProviderConfig [ "headers" ] ) : string {
293+ for ( ; ; ) {
294+ const name = `${ PROXY_CREDENTIAL_HEADER_PREFIX } ${ randomBytes ( 12 ) . toString ( "hex" ) } ` ;
295+ if ( ! headers || ! hasHeader ( headers , name ) ) {
296+ return name ;
297+ }
298+ }
299+ }
300+
236301function buildProxyRequestHeaders (
237302 headers : IncomingMessage [ "headers" ] ,
238- params : { upstreamBearerAuthorization : string | undefined } ,
303+ params : {
304+ proxyCredentialHeader : string | undefined ;
305+ upstreamBearerAuthorization : string | undefined ;
306+ } ,
239307) : Record < string , string > {
240- const out = normalizeProxyRequestHeaders ( headers ) ;
308+ const out = normalizeProxyRequestHeaders ( headers , params . proxyCredentialHeader ) ;
241309 if ( params . upstreamBearerAuthorization && ! hasHeader ( out , "authorization" ) ) {
242310 // The SDK declares bearerToken as Authorization auth, but some BYOK
243311 // adapter paths can omit it before reaching our loopback proxy. The proxy
0 commit comments