From c2c021c8ca5bc9e9196e155eb63a2efc2bef8dae Mon Sep 17 00:00:00 2001 From: Vansh Singh Date: Tue, 17 Nov 2026 11:33:15 +0530 Subject: [PATCH] fix(copilot): accept empty or whitespace stored key with key ref (#52014) (#52032) * fix(copilot): accept empty or whitespace stored key with key ref (#52014) * fix(copilot): treat blank GitHub App fields as unset and keep other values fail-closed Booleans and other non-string App field values stay configured so a non-App request carrying them still fails closed, and a non-string key ref no longer passes as a blank one * fix(copilot): never store blank GitHub App fields on a credential POST /credentials drops a blank or whitespace-only App field instead of storing it, and PATCH treats one as a deletion. A stored blank key came back masked as ***** and the dashboard then edited it as an uploaded key * fix(copilot): keep non-admin credential creates with blank App fields forbidden * test(copilot): pin that a null App key on update keeps the stored key * test(copilot): pin that a false or 0 key ref next to an uploaded key fails closed * fix(copilot): keep signing a whitespace-only key that has no key ref A named credential with a whitespace-only App key and no key ref answered 400 at the head, which the router never retries. The merge base reached JWT signing and answered 401, so a model group with a working sibling kept serving. Whitespace still counts as unset next to a key ref. --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com> --- litellm/llms/github_copilot/app_auth.py | 22 +++-- .../proxy/credential_endpoints/endpoints.py | 17 +++- .../unit/llms/github_copilot/test_app_auth.py | 92 +++++++++++++++++++ .../credential_endpoints/test_endpoints.py | 87 ++++++++++++++++++ 4 files changed, 208 insertions(+), 10 deletions(-) diff --git a/litellm/llms/github_copilot/app_auth.py b/litellm/llms/github_copilot/app_auth.py index 3b8e4fc7f2b..1c1419ae4fb 100644 --- a/litellm/llms/github_copilot/app_auth.py +++ b/litellm/llms/github_copilot/app_auth.py @@ -62,23 +62,33 @@ def _value(source: object, key: str) -> object: return getattr(source, key, None) +def _is_blank_text(value: object) -> bool: + return isinstance(value, str) and not value.strip() + + +def _is_blank(value: object) -> bool: + return value is None or _is_blank_text(value) + + +def blank_app_fields(values: Mapping[str, object]) -> tuple[str, ...]: + return tuple(key for key in APP_FIELDS if _is_blank_text(values.get(key))) + + def app_config(source: object, *, validate_stored_key: bool = False) -> AppConfig | None: name: Final = _value(source, "litellm_credential_name") credential: Final = CredentialAccessor.find_credential(name) if isinstance(name, str) else None values: Final = credential.credential_values if credential is not None else source mode: Final = _value(values, "github_copilot_auth_type") - configured: Final = any(_value(values, key) is not None for key in APP_FIELDS) + configured: Final = not all(_is_blank(_value(values, key)) for key in APP_FIELDS) if mode != APP_AUTH_TYPE and not configured: return None app_id: Final = _value(values, APP_FIELDS[0]) installation: Final = _value(values, APP_FIELDS[1]) key_ref: Final = _value(values, APP_FIELDS[2]) stored_key: Final = _value(values, APP_PRIVATE_KEY) - stored_key_selected: Final = isinstance(stored_key, str) and bool(stored_key) - valid_key_source: Final = ( - stored_key_selected and not key_ref and (credential is not None or validate_stored_key) - ) or ( - stored_key is None + stored_key_selected: Final = isinstance(stored_key, str) and bool(stored_key) and _is_blank(key_ref) + valid_key_source: Final = (stored_key_selected and (credential is not None or validate_stored_key)) or ( + _is_blank(stored_key) and isinstance(key_ref, str) and bool(key_ref.strip()) and "-----BEGIN" not in key_ref diff --git a/litellm/proxy/credential_endpoints/endpoints.py b/litellm/proxy/credential_endpoints/endpoints.py index 9175616bb57..7706d71879f 100644 --- a/litellm/proxy/credential_endpoints/endpoints.py +++ b/litellm/proxy/credential_endpoints/endpoints.py @@ -175,6 +175,10 @@ def _reject_overlapping_credential_values(credential: UpdateCredentialItem) -> N ) +def _without_keys(credential_values: Mapping[str, object], keys: tuple[str, ...]) -> Mapping[str, object]: + return MappingProxyType({key: value for key, value in credential_values.items() if key not in keys}) + + def _without_null_values(credential_values: Mapping[str, object]) -> dict[str, object]: """A null carries no credential, and the federation resolver refuses a foreign variant's field by KEY, so a stored ``{"anthropic_issuer_url": null}`` wedges every deployment that names this @@ -343,6 +347,7 @@ async def create_credential( Stores credential in DB. Reloads credentials in memory. """ + from litellm.llms.github_copilot.app_auth import blank_app_fields from litellm.proxy.proxy_server import DB_RECONCILE_LOCK, prisma_client try: @@ -364,11 +369,12 @@ async def create_credential( credential_info: Final = _CREDENTIAL_DICT_ADAPTER.validate_python( credential.credential_info # pyright: ignore[reportUnknownMemberType] # untyped credential field ) - normalized_values: Final = _normalized_github_copilot_values( + copilot_values: Final = _normalized_github_copilot_values( _CREDENTIAL_DICT_ADAPTER.validate_python(credential_values), credential_info, ) - _reject_non_admin_wif_fields(server_owned_wif_fields_written(normalized_values), user_api_key_dict) + _reject_non_admin_wif_fields(server_owned_wif_fields_written(copilot_values), user_api_key_dict) + normalized_values: Final = _without_keys(copilot_values, blank_app_fields(copilot_values)) _validate_github_app_values(normalized_values) _reject_non_admin_wif_fields( await named_credential_wif_fields(credential.credential_name, prisma_client), user_api_key_dict @@ -1007,6 +1013,7 @@ async def update_credential( """ [BETA] endpoint. This might change unexpectedly. """ + from litellm.llms.github_copilot.app_auth import blank_app_fields from litellm.proxy.proxy_server import DB_RECONCILE_LOCK, prisma_client try: @@ -1040,7 +1047,9 @@ async def update_credential( decrypted_values: Final = _CREDENTIAL_DICT_ADAPTER.validate_python( decrypted_stored_credential.credential_values ) - incoming_values: Final = _without_masked_read_backs(requested_values, _stored_objects(decrypted_values)) + unmasked_values: Final = _without_masked_read_backs(requested_values, _stored_objects(decrypted_values)) + cleared_app_fields: Final = blank_app_fields(unmasked_values) + incoming_values: Final = _without_keys(unmasked_values, cleared_app_fields) patch: Final = CredentialItem( credential_name=credential_name, display_name=( @@ -1050,7 +1059,7 @@ async def update_credential( ), credential_info=_CREDENTIAL_DICT_ADAPTER.validate_python(credential.credential_info), credential_values=_CREDENTIAL_DICT_ADAPTER.validate_python(incoming_values), - credential_values_to_delete=credential.credential_values_to_delete, + credential_values_to_delete=(*(credential.credential_values_to_delete or ()), *cleared_app_fields), ) stored_info: Final = _CREDENTIAL_DICT_ADAPTER.validate_python( db_credential.credential_info # pyright: ignore[reportUnknownMemberType] # untyped credential field diff --git a/tests/unit/llms/github_copilot/test_app_auth.py b/tests/unit/llms/github_copilot/test_app_auth.py index bc2d0149fda..59d71b80551 100644 --- a/tests/unit/llms/github_copilot/test_app_auth.py +++ b/tests/unit/llms/github_copilot/test_app_auth.py @@ -846,6 +846,98 @@ def test_invalid_app_configuration_fails_closed(override: Mapping[str, object]) ) +@pytest.mark.parametrize("named", (False, True)) +@pytest.mark.parametrize("blank_stored_key", ("", " ")) +def test_key_ref_is_accepted_with_blank_stored_key( + monkeypatch: pytest.MonkeyPatch, blank_stored_key: str, named: bool +) -> None: + values: Final = { + "github_copilot_auth_type": "github_app", + "github_copilot_app_id": _CONFIG.app_id, + "github_copilot_app_installation_id": _CONFIG.installation_id, + "github_copilot_app_private_key_ref": _CONFIG.private_key_ref, + "github_copilot_app_private_key": blank_stored_key, + } + monkeypatch.setattr( + litellm, + "credential_list", + [CredentialItem(credential_name="copilot-app", credential_values=values, credential_info={})], + ) + assert app_config({"litellm_credential_name": "copilot-app"} if named else values) == _CONFIG + + +def test_named_credential_with_whitespace_only_key_and_no_key_ref_fails_at_signing( + monkeypatch: pytest.MonkeyPatch, +) -> None: + values: Final = { + "github_copilot_auth_type": "github_app", + "github_copilot_app_id": _CONFIG.app_id, + "github_copilot_app_installation_id": _CONFIG.installation_id, + "github_copilot_app_private_key": " ", + } + monkeypatch.setattr( + litellm, + "credential_list", + [CredentialItem(credential_name="copilot-app", credential_values=values, credential_info={})], + ) + config: Final = app_config({"litellm_credential_name": "copilot-app"}) + assert config is not None + with pytest.raises(AuthenticationError, match="Unable to sign a GitHub App JWT"): + manager("", lambda request: httpx.Response(500), Clock()).resolve(config) + + +@pytest.mark.parametrize("blank", ("", " ")) +def test_blank_app_fields_without_app_auth_type_are_not_app_auth(blank: str) -> None: + assert app_config({key: blank for key in APP_FIELDS}) is None + + +@pytest.mark.parametrize("value", (False, 0, "123")) +def test_non_blank_app_field_without_app_auth_type_fails_closed(value: object) -> None: + with pytest.raises(BadRequestError, match="GitHub App authentication requires"): + app_config({"github_copilot_app_installation_id": value}) + + +@pytest.mark.parametrize("blank_key_ref", ("", " ")) +def test_uploaded_key_is_accepted_with_blank_key_ref(private_key: str, blank_key_ref: str) -> None: + config: Final = app_config( + { + "github_copilot_auth_type": "github_app", + "github_copilot_app_id": _CONFIG.app_id, + "github_copilot_app_installation_id": _CONFIG.installation_id, + "github_copilot_app_private_key_ref": blank_key_ref, + "github_copilot_app_private_key": private_key, + }, + validate_stored_key=True, + ) + assert config is not None + assert (config.app_id, config.installation_id) == (_CONFIG.app_id, _CONFIG.installation_id) + + +@pytest.mark.parametrize( + "override", + [ + {"github_copilot_app_private_key": 12345}, + {"github_copilot_app_private_key": False}, + {"github_copilot_app_private_key_ref": 12345}, + {"github_copilot_app_private_key_ref": False}, + {"github_copilot_app_private_key_ref": 0}, + ], +) +def test_non_string_key_source_fails_closed(private_key: str, override: Mapping[str, object]) -> None: + with pytest.raises(BadRequestError, match="GitHub App authentication requires"): + app_config( + { + "github_copilot_auth_type": "github_app", + "github_copilot_app_id": _CONFIG.app_id, + "github_copilot_app_installation_id": _CONFIG.installation_id, + "github_copilot_app_private_key_ref": _CONFIG.private_key_ref, + "github_copilot_app_private_key": private_key, + **override, + }, + validate_stored_key=True, + ) + + def test_network_failure_does_not_expose_jwt(private_key: str) -> None: def fail(request: httpx.Request) -> httpx.Response: raise httpx.ConnectError(request.headers["authorization"], request=request) diff --git a/tests/unit/proxy/credential_endpoints/test_endpoints.py b/tests/unit/proxy/credential_endpoints/test_endpoints.py index 6e8474c8e02..b40f4496286 100644 --- a/tests/unit/proxy/credential_endpoints/test_endpoints.py +++ b/tests/unit/proxy/credential_endpoints/test_endpoints.py @@ -3528,6 +3528,40 @@ def test_update_github_app_validates_merged_values_before_storage(credential_sto update.assert_awaited_once() +@pytest.mark.parametrize("blank", ["", " "]) +def test_create_github_app_never_stores_a_blank_app_field(credential_store: _InstallCredentialStore, blank: str) -> None: + credential_store(create=AsyncMock()) + response: Final = _create_credential( + { + "credential_name": "copilot-app", + "credential_values": {**_github_app_values(), "github_copilot_app_private_key": blank}, + "credential_info": {"custom_llm_provider": "github_copilot"}, + } + ) + assert response.status_code == 200, response.text + listed: Final = _list_credentials() + assert listed.status_code == 200 + assert "github_copilot_app_private_key" not in listed.json()["credentials"][0]["credential_values"] + + +@pytest.mark.parametrize("blank", ["", " "]) +def test_non_admin_cannot_create_a_credential_with_a_blank_app_field( + credential_store: _InstallCredentialStore, blank: str +) -> None: + create: Final = AsyncMock() + credential_store(create=create) + response: Final = _post_credential( + { + "credential_name": "copilot-app", + "credential_values": {"github_copilot_app_private_key": blank}, + "credential_info": {"custom_llm_provider": "github_copilot"}, + }, + auth=_as_non_admin, + ) + assert response.status_code == 403, response.text + create.assert_not_awaited() + + @pytest.fixture def app_pem() -> str: from cryptography.hazmat.primitives import serialization @@ -3598,6 +3632,59 @@ def test_uploaded_app_key_replaces_reference_with_explicit_deletion( assert app_pem not in update.await_args.kwargs["data"]["credential_values"] +@pytest.mark.parametrize("blank", ["", " "]) +def test_update_github_app_treats_a_blank_app_field_as_a_deletion( + credential_store: _InstallCredentialStore, app_pem: str, blank: str +) -> None: + uploaded: Final = {key: value for key, value in _github_app_values().items() if not key.endswith("_ref")} + stored: Final = _encrypted_row( + CredentialItem( + credential_name="copilot-app", + credential_values={**uploaded, "github_copilot_app_private_key": app_pem}, + credential_info={"custom_llm_provider": "github_copilot"}, + ) + ) + update: Final = AsyncMock() + credential_store(find_by_name=AsyncMock(return_value=stored), update_by_name=update) + response: Final = _patch_credential( + "copilot-app", + { + "credential_info": {"custom_llm_provider": "github_copilot"}, + "credential_values": { + "github_copilot_app_private_key_ref": _github_app_values()["github_copilot_app_private_key_ref"], + "github_copilot_app_private_key": blank, + }, + }, + ) + assert response.status_code == 200, response.text + assert _written_values(update) == _github_app_values() + + +def test_update_github_app_keeps_a_stored_key_sent_as_null( + credential_store: _InstallCredentialStore, app_pem: str +) -> None: + uploaded: Final = {key: value for key, value in _github_app_values().items() if not key.endswith("_ref")} + stored_values: Final = {**uploaded, "github_copilot_app_private_key": app_pem} + stored: Final = _encrypted_row( + CredentialItem( + credential_name="copilot-app", + credential_values=stored_values, + credential_info={"custom_llm_provider": "github_copilot"}, + ) + ) + update: Final = AsyncMock() + credential_store(find_by_name=AsyncMock(return_value=stored), update_by_name=update) + response: Final = _patch_credential( + "copilot-app", + { + "credential_info": {"custom_llm_provider": "github_copilot"}, + "credential_values": {"github_copilot_app_private_key": None}, + }, + ) + assert response.status_code == 200, response.text + assert _written_values(update) == stored_values + + @pytest.mark.parametrize("pem", ["not-a-private-key", "x" * 16385, "-----BEGIN PRIVATE KEY----- secret-invalid"]) def test_invalid_uploaded_app_key_is_rejected_without_echoing_it( credential_store: _InstallCredentialStore, pem: str