feat: CF_AIG_TOKEN support for AI Gateway Authenticated Gateway#298
Open
jbbjbb wants to merge 1 commit intocloudflare:mainfrom
Open
feat: CF_AIG_TOKEN support for AI Gateway Authenticated Gateway#298jbbjbb wants to merge 1 commit intocloudflare:mainfrom
jbbjbb wants to merge 1 commit intocloudflare:mainfrom
Conversation
Pass CF_AIG_TOKEN from Worker environment to container, enabling AI Gateway Authenticated Gateway and BYOK (Bring Your Own Key). OpenClaw's config schema does not support custom headers on provider configs, so a Node.js --require hook patches globalThis.fetch to inject the cf-aig-authorization header for gateway.ai.cloudflare.com requests. This was discovered during live testing on a deployed Cloudflare Sandbox instance. Security hardening (scoped to touched files): - chmod 600 on rclone.conf (shell script + Worker-side r2.ts) - chmod 600 on openclaw.json after config patch - Remove redundant gateway token from config file (already passed via --token CLI flag) - Input validation on CF_AIG_TOKEN for control characters Fixes cloudflare#74 Duplicates: cloudflare#119 Related: cloudflare#192 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
CF_AIG_TOKENsupport to enable AI Gateway Authenticated Gateway and BYOK (Bring Your Own Key). This resolves the inability to use Authenticated Gateway or BYOK with moltworker — users currently must either disable Authenticated Gateway or pass raw provider keys into the container.Fixes #74 | Duplicates: #119 | Related: #192
Changes
Feature — CF_AIG_TOKEN passthrough + header injection:
CF_AIG_TOKENtoMoltbotEnvtype definitionCF_AIG_TOKENfrom Worker env to container viabuildEnvVars()cf-aig-authorization: Bearer <token>header via Node.js--requirehook that patchesglobalThis.fetchforgateway.ai.cloudflare.comrequestsWhy a fetch hook instead of provider config? OpenClaw's config schema strictly validates provider objects and rejects unrecognized keys like
defaultHeaders. This was discovered during live testing (see proof of work below). The--requirehook approach is scoped: it only patchesfetch()calls targeting AI Gateway URLs and does not affect other HTTP traffic.Security hardening (scoped to touched files):
chmod 600onrclone.conf(both shell script and Worker-sider2.ts)chmod 600onopenclaw.jsonafter config patch (fs.chmodSync)--tokenCLI flag — writing it to the config is unnecessary secret exposure)CF_AIG_TOKENfor control characters that could cause injection or parsing issuesProof of Work — Live Testing on Deployed Instance
Validated on a production Cloudflare Sandbox deployment (a moltworker-based system with AI Gateway + OpenRouter):
Initial attempt with
defaultHeadersin provider config — OpenClaw rejected the config:Replaced with
NODE_OPTIONS --requirefetch hook — container started successfully:Gateway started and accepted connections — logs show:
Security hardening confirmed —
chmod 600 /root/.config/rclone/rclone.confexecuted successfully in container logs.Test plan
tsc --noEmit)oxlint— 3 pre-existing errors inskills/unrelated to this PR)cf-aig-authorizationheader appears in AI Gateway request logs (requires AI Gateway dashboard access)Files changed
src/types.tsCF_AIG_TOKENtoMoltbotEnvsrc/gateway/env.tsvalidateEnvValue(),CF_AIG_TOKENpassthrough with validationsrc/gateway/env.test.tssrc/gateway/r2.tschmod 600on rclone.confstart-openclaw.shcf-aig-authorization,chmod 600on configs, remove redundant tokenwrangler.jsoncCF_AIG_TOKENsecretREADME.md.dev.vars.exampleAGENTS.md🤖 Generated with Claude Code