Skip to content

fix(auth): gate invalid-key check on isRequireApiKeyEnabled for embeddings and web-fetch (#7785) - #7810

Merged
diegosouzapw merged 5 commits into
cheaper-inference:release/v3.8.49from
AndrianBalanescu:fix/auth-policy-embeddings-webfetch-7785
Jul 20, 2026
Merged

diegosouzapw merged 5 commits into
cheaper-inference:release/v3.8.49from
AndrianBalanescu:fix/auth-policy-embeddings-webfetch-7785

Conversation

@AndrianBalanescu

Copy link
Copy Markdown
Contributor

Problem

When REQUIRE_API_KEY=false, /api/v1/embeddings and /api/v1/web/fetch still return 401 for invalid presented keys while all other client APIs allow anonymous access. This is an inconsistency in the optional API-key policy.

Closes #7785

Root cause

Both routes checked isRequireApiKeyEnabled() for the missing-key case but NOT for the invalid-key case:

if (isRequireApiKeyEnabled() && !apiKeyRaw) {
  return errorResponse(401, "Authentication required");
}
if (apiKeyRaw && !(await isValidApiKey(apiKeyRaw))) {  // ← not gated
  return errorResponse(401, "Invalid API key");
}

The /v1/combos route already handles this correctly — when REQUIRE_API_KEY=false it falls through to anonymous access regardless of whether a (possibly invalid) key was presented.

Fix

Gate the invalid-key check on isRequireApiKeyEnabled() in both route files:

if (isRequireApiKeyEnabled() && apiKeyRaw && !(await isValidApiKey(apiKeyRaw))) {
  return errorResponse(401, "Invalid API key");
}

This matches the /v1/combos pattern and makes anonymous access consistent across all client APIs.

Changes

  • src/app/api/v1/embeddings/route.ts — gate invalid-key check on isRequireApiKeyEnabled()
  • src/app/api/v1/web/fetch/route.ts — same gate
  • tests/unit/auth-policy-embeddings-webfetch-7785.test.ts — 4 tests covering both routes × both flag states

Testing

npx tsx --test tests/unit/auth-policy-embeddings-webfetch-7785.test.ts
# tests 4
# pass 4
# fail 0

Existing embeddings test (embeddings-route-apikeymeta-6929.test.ts) still passes — no regression.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@AndrianBalanescu
AndrianBalanescu force-pushed the fix/auth-policy-embeddings-webfetch-7785 branch from a4c8745 to 0b92f2f Compare July 20, 2026 02:20
@diegosouzapw

Copy link
Copy Markdown
Collaborator

Thanks for tracking down #7785 and matching it to the /v1/combos pattern — the 2-line gate in both route.ts files is exactly the right fix, and the new test file is solid coverage for the intended behavior.

One thing to fix before merge: CI's Unit Tests (8/8) job is currently red because of this change. tests/unit/embeddings-auth.test.ts (the pre-existing regression test) has a subtest — "should return 401 when an invalid API key is provided" — that doesn't set REQUIRE_API_KEY and relies on its default value. Since REQUIRE_API_KEY defaults to "false" (src/shared/constants/featureFlagDefinitions.ts:22), that subtest now gets 400 instead of the asserted 401 (verified by reproducing it directly against your branch: POST /v1/embeddings with an invalid Bearer key now falls through to anonymous access and fails at model resolution with 400 "No credentials for embedding provider: mistral"). Issue #7785's own acceptance criteria call this out explicitly: "The existing embeddings regression test that currently codifies unconditional 401 behavior is updated to assert both enforcement states" — could you update that subtest (e.g. set REQUIRE_API_KEY="true" explicitly there) instead of leaving it as a second, now-contradictory test alongside your new file?

Also: this PR targets main, but the repo's active branch is release/v3.8.49 — could you retarget (gh pr edit 7810 --base release/v3.8.49)? We can also take care of the retarget + the test fix directly on your branch if that's easier — just flagging so you're not surprised by an extra commit.

(The other two red checks — Quality Gates Extended / Quality Ratchet — are unrelated pre-existing baseline drift on main, not caused by your diff; you can ignore those.)

@diegosouzapw
diegosouzapw changed the base branch from main to release/v3.8.49 July 20, 2026 03:37
…dings and web-fetch (cheaper-inference#7785)

When REQUIRE_API_KEY=false, /v1/embeddings and /v1/web/fetch still returned
401 for invalid presented keys while all other client APIs allowed anonymous
access. The route-local invalid-key check was not gated on
isRequireApiKeyEnabled(), unlike the /v1/combos pattern.

Gate the invalid-key check on isRequireApiKeyEnabled() in both route files so
anonymous access works consistently across all client APIs.

Refs: cheaper-inference#7785
diegosouzapw and others added 4 commits July 20, 2026 01:46
…ubtest

The "should return 401 when an invalid API key is provided" test now
correctly sets REQUIRE_API_KEY="true" so the route-level gated check
is exercised. Before, the test asserted 401 when REQUIRE_API_KEY was
not set, which after cheaper-inference#7785 fix now returns 400 (model validation fails)
instead of 401.
…ite (cheaper-inference#7785)

Per cheaper-inference#7785's acceptance criteria, the pre-existing embeddings regression
test must assert BOTH enforcement states, not just the enforced-401
case. Add the missing REQUIRE_API_KEY=false + invalid-key subtest
alongside the already-fixed REQUIRE_API_KEY=true + invalid-key subtest,
matching the coverage already present in the dedicated
auth-policy-embeddings-webfetch-7785.test.ts suite.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
@diegosouzapw

Copy link
Copy Markdown
Collaborator

Validated in local merge-train on tomni-proxmox-113 @ 19bcf534352e24cdd2448cdfdfd64d5bd9695c74 (FAST gates green: static + changed tests + vitest)

@diegosouzapw
diegosouzapw merged commit adbcd2c into cheaper-inference:release/v3.8.49 Jul 20, 2026
5 checks passed
@diegosouzapw diegosouzapw mentioned this pull request Jul 23, 2026
HouMinXi pushed a commit to HouMinXi/OmniRoute that referenced this pull request Aug 2, 2026
…dings and web-fetch (cheaper-inference#7785) (cheaper-inference#7810)

* fix(auth): gate invalid-key check on isRequireApiKeyEnabled for embeddings and web-fetch (cheaper-inference#7785)

When REQUIRE_API_KEY=false, /v1/embeddings and /v1/web/fetch still returned
401 for invalid presented keys while all other client APIs allowed anonymous
access. The route-local invalid-key check was not gated on
isRequireApiKeyEnabled(), unlike the /v1/combos pattern.

Gate the invalid-key check on isRequireApiKeyEnabled() in both route files so
anonymous access works consistently across all client APIs.

Refs: cheaper-inference#7785

* fix(tests): set REQUIRE_API_KEY=true in embeddings-auth invalid-key subtest

The "should return 401 when an invalid API key is provided" test now
correctly sets REQUIRE_API_KEY="true" so the route-level gated check
is exercised. Before, the test asserted 401 when REQUIRE_API_KEY was
not set, which after cheaper-inference#7785 fix now returns 400 (model validation fails)
instead of 401.

* test(auth): assert anonymous-passthrough in embeddings-auth legacy suite (cheaper-inference#7785)

Per cheaper-inference#7785's acceptance criteria, the pre-existing embeddings regression
test must assert BOTH enforcement states, not just the enforced-401
case. Add the missing REQUIRE_API_KEY=false + invalid-key subtest
alongside the already-fixed REQUIRE_API_KEY=true + invalid-key subtest,
matching the coverage already present in the dedicated
auth-policy-embeddings-webfetch-7785.test.ts suite.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…dings and web-fetch (cheaper-inference#7785) (cheaper-inference#7810)

* fix(auth): gate invalid-key check on isRequireApiKeyEnabled for embeddings and web-fetch (cheaper-inference#7785)

When REQUIRE_API_KEY=false, /v1/embeddings and /v1/web/fetch still returned
401 for invalid presented keys while all other client APIs allowed anonymous
access. The route-local invalid-key check was not gated on
isRequireApiKeyEnabled(), unlike the /v1/combos pattern.

Gate the invalid-key check on isRequireApiKeyEnabled() in both route files so
anonymous access works consistently across all client APIs.

Refs: cheaper-inference#7785

* fix(tests): set REQUIRE_API_KEY=true in embeddings-auth invalid-key subtest

The "should return 401 when an invalid API key is provided" test now
correctly sets REQUIRE_API_KEY="true" so the route-level gated check
is exercised. Before, the test asserted 401 when REQUIRE_API_KEY was
not set, which after cheaper-inference#7785 fix now returns 400 (model validation fails)
instead of 401.

* test(auth): assert anonymous-passthrough in embeddings-auth legacy suite (cheaper-inference#7785)

Per cheaper-inference#7785's acceptance criteria, the pre-existing embeddings regression
test must assert BOTH enforcement states, not just the enforced-401
case. Add the missing REQUIRE_API_KEY=false + invalid-key subtest
alongside the already-fixed REQUIRE_API_KEY=true + invalid-key subtest,
matching the coverage already present in the dedicated
auth-policy-embeddings-webfetch-7785.test.ts suite.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
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.

fix(auth): apply optional API-key policy consistently to embeddings and web-fetch routes

2 participants