Skip to content

The agent that grows with you

fix(gateway): bind an alias key read through model.api_key_from to the model route

Teknium8df6ba411c9d
diff --git a/gateway/session_policy_credentials.py b/gateway/session_policy_credentials.py
index b2156fdc7d5..98b434ece45 100644
--- a/gateway/session_policy_credentials.py
+++ b/gateway/session_policy_credentials.py
@@ -113,6 +113,11 @@ def _route_owner(config, path):
fallback sections in dictionary (one route) and list form, and each ``auxiliary.<task>`` route with
its ``fallback_chain`` entries. A secret nested deeper in a route (headers, extra_body) rides with it."""
head, rest = path[0], tuple(path[1:])
+ model = config.get('model') if isinstance(config.get('model'), dict) else {}
+ if isinstance(model.get('api_key_from'), list) and tuple(model['api_key_from']) == tuple(path):
+ # ``/model <alias>`` with a literal key: config() projects the alias entry's key into ``model.api_key``,
+ # so it authenticates the session's ``model:`` route, not the alias entry it is stored under.
+ return ('model',)
if head == 'model':
if rest[:1] == ('aliases',) and len(rest) > 2:
return ('model', 'aliases', rest[1])
diff --git a/tests/gateway/test_config_secret_scope.py b/tests/gateway/test_config_secret_scope.py
index 48eb6535c1f..3d517efd7d3 100644
--- a/tests/gateway/test_config_secret_scope.py
+++ b/tests/gateway/test_config_secret_scope.py
@@ -336,6 +336,18 @@ _SHAPES = {
'api_key': 'K-chain'}]}}},
lambda c: c['providers']['team'].__setitem__('base_url', _B),
'auxiliary.compression.fallback_chain.0.api_key'),
+ # alt-glitch critical: after ``/model <alias>`` with a literal key, the key lives at the alias entry and
+ # ``model.api_key_from`` points at it; config() projects it into ``model.api_key``, so it authenticates
+ # the session's ``model:`` route, and moving that route must refuse it.
+ 'alias_key_via_api_key_from': ({'model': {**_BASE['model'], 'api_key_from': ['model_aliases', 'alt', 'api_key']},
+ 'model_aliases': {'alt': {'provider': 'custom', 'model': 'x', 'api_key': 'K-alias'}}},
+ lambda c: c['model'].__setitem__('base_url', _B),
+ 'model_aliases.alt.api_key'),
+ 'nested_alias_key_via_api_key_from': ({'model': {**_BASE['model'], 'api_key_from': ['model', 'aliases', 'alt', 'api_key'],
+ 'aliases': {'alt': {'provider': 'custom', 'model': 'x',
+ 'api_key': 'K-alias'}}}},
+ lambda c: c['model'].__setitem__('base_url', _B),
+ 'model.aliases.alt.api_key'),
'model_alias_own_url': ({'model': {**_BASE['model'], 'aliases': {'alt': {'provider': 'custom', 'model': 'x',
'base_url': _A, 'api_key': 'K-alias'}}}},
lambda c: c['model']['aliases']['alt'].__setitem__('base_url', _B),