Skip to content

fix(ai-gateway-provider): strip temp token from case-differing auth header - #679

Open
harshbhx wants to merge 1 commit into
cloudflare:mainfrom
harshbhx:fix/355-authorization-header-case
Open

harshbhx wants to merge 1 commit into
cloudflare:mainfrom
harshbhx:fix/355-authorization-header-case

Conversation

@harshbhx

Copy link
Copy Markdown

Fixes #355

Summary

Strip the injected CF_TEMP_TOKEN from the outgoing request even when the wrapped SDK writes the auth header as Authorization (or any non-lowercase spelling) on a plain-object headers bag.

Problem

In processModelRequest (packages/ai-gateway-provider/src/index.ts) the cleanup read and deleted req.request.headers[authHeader], where authHeader defaults to the lowercase "authorization". Headers instances are case-insensitive, but plain objects are not, so an Authorization key was never found and the fake Bearer CF_TEMP_TOKEN was forwarded to the upstream provider (e.g. OpenRouter) instead of being removed so BYOK / unified billing could apply.

Solution

On the plain-object path, find the existing key whose lowercase form equals authHeader.toLowerCase(), then check and delete that key. The Headers path is unchanged. I did not normalise headers where the request is captured, to keep the change small; happy to do that instead if you prefer.

Changes

File Change
packages/ai-gateway-provider/src/index.ts Case-insensitive key lookup on the plain-object path
packages/ai-gateway-provider/test/request-shaping.test.ts Regression tests with a plain-object Authorization header
.changeset/quiet-headers-match.md Patch changeset for ai-gateway-provider

Before / After

Headers { Authorization: "Bearer <CF_TEMP_TOKEN>" }:

  • Before: forwarded unchanged to the gateway entry.
  • After: header removed. Other headers and a real Authorization value are left alone.

How to test

  1. pnpm --filter ai-gateway-provider test
  2. Revert the src/index.ts change and re-run: the new "strips a plain-object Authorization header" test fails.

Testing done

  • New tests (Temp-token header cleanup in request-shaping.test.ts): temp token stripped from Authorization while other headers stay; a real Authorization key is kept.
  • CI on my fork (format, lint, test:ci, type-check, build): green (https://github.com/harshbhx/ai/actions/runs/39650916542): 50/50 tests in ai-gateway-provider, format, lint, type-check, build
  • Not run locally (2-CPU machine); only fork CI.
  • The Headers branch has no new test: the binding path spreads headers into a plain object, so a Headers test would pass vacuously.
  • Not tested against a live AI Gateway / OpenRouter.

Notes for reviewers

The tests drive processModelRequest through a small stub model, since the real provider SDKs lowercase the header themselves.

@changeset-bot

changeset-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 246f105

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
ai-gateway-provider Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@harshbhx

harshbhx commented Nov 5, 2026

Copy link
Copy Markdown
Author

Hi, friendly ping on this one. It's rebased and the checks are green on my side. Happy to adjust anything if you'd like it done differently. Thanks!

…eader

The cleanup looked up the lowercase header name on a plain-object headers bag, so a header written as `Authorization` was never found and the fake CF_TEMP_TOKEN was forwarded upstream. Resolve the existing key case-insensitively before checking and deleting it. The `Headers` path is unchanged.

Fixes cloudflare#355
@harshbhx
harshbhx force-pushed the fix/355-authorization-header-case branch from 6363844 to 246f105 Compare November 22, 2026 15:12
@harshbhx

harshbhx commented Dec 4, 2026

Copy link
Copy Markdown
Author

Hi, checking whether this is still of interest. It's rebased on main and the changeset is in. If it's not a fit, no worries, I'll close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ai-gateway-provider: BYOK fails for OpenRouter due to case-sensitive header key mismatch

1 participant