Repository navigation
fix(admin): redact saved model endpoint secrets #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4513681
fix(admin): redact saved model endpoint secrets
hedhoud ace434b
fix(admin): redact model endpoint secrets
hedhoud 6af89b3
fix(admin): address model endpoint secret review
hedhoud 1637c17
fix(api): fully redact config secrets
hedhoud 07b4e11
style(api): format redaction helper
hedhoud c6587e1
fix(admin): preserve endpoint extras during redaction
hedhoud f2ef2b7
fix(admin): allow clearing endpoint API keys
hedhoud 9b7bca0
fix(admin): keep secret validation consistent
hedhoud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Helpers for shaping public data without exposing secrets.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any | ||
|
|
||
| REDACTED_SECRET = "<redacted>" | ||
|
|
||
| SECRET_FIELD_NAMES = frozenset( | ||
| { | ||
| "api_key", | ||
| "api_token", | ||
| "auth_token", | ||
| "chainlit_auth_secret", | ||
| "client_secret", | ||
| "hf_token", | ||
| "oidc_client_secret", | ||
| "oidc_token_encryption_key", | ||
| "password", | ||
| "secret", | ||
| "secret_key", | ||
| "token", | ||
| "token_encryption_key", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def is_secret_field(key: str) -> bool: | ||
| """Return true only for known secret field names, not fuzzy token matches.""" | ||
| return key.lower() in SECRET_FIELD_NAMES | ||
|
|
||
|
|
||
| def redact_secrets(value: Any) -> Any: | ||
| """Recursively redact values for known secret fields without mutating input.""" | ||
| if isinstance(value, Mapping): | ||
| return { | ||
| key: REDACTED_SECRET if is_secret_field(str(key)) else redact_secrets(item) for key, item in value.items() | ||
| } | ||
| if isinstance(value, list): | ||
| return [redact_secrets(item) for item in value] | ||
| if isinstance(value, tuple): | ||
| return tuple(redact_secrets(item) for item in value) | ||
| return value | ||
|
|
||
|
|
||
| def redact_secret_mapping(extra: Mapping[str, Any] | None) -> dict[str, Any]: | ||
| """Drop secret fields from endpoint extras while preserving non-secret metadata.""" | ||
| return {key: redact_secrets(item) for key, item in dict(extra or {}).items() if not is_secret_field(str(key))} | ||
|
|
||
|
|
||
| def preserve_existing_secrets(existing: Mapping[str, Any] | None, incoming: Mapping[str, Any]) -> dict[str, Any]: | ||
| """Keep stored secrets when an update payload omits or echoes a redacted value.""" | ||
| return _preserve_existing_secrets(existing or {}, incoming) | ||
|
|
||
|
|
||
| def _preserve_existing_secrets(existing: Mapping[str, Any], incoming: Mapping[str, Any]) -> dict[str, Any]: | ||
| merged = dict(incoming) | ||
| for key, value in existing.items(): | ||
| incoming_value = merged.get(key) | ||
| if is_secret_field(str(key)): | ||
| if key not in merged or incoming_value == REDACTED_SECRET: | ||
| merged[key] = value | ||
| continue | ||
| if isinstance(value, Mapping) and isinstance(incoming_value, Mapping): | ||
| merged[key] = _preserve_existing_secrets(value, incoming_value) | ||
| elif isinstance(value, list) and isinstance(incoming_value, list): | ||
| merged[key] = _preserve_existing_secret_lists(value, incoming_value) | ||
| return merged | ||
|
|
||
|
|
||
| def _preserve_existing_secret_lists(existing: list[Any], incoming: list[Any]) -> list[Any]: | ||
| merged = list(incoming) | ||
| for index, existing_item in enumerate(existing): | ||
| if index >= len(merged): | ||
| break | ||
| incoming_item = merged[index] | ||
| if isinstance(existing_item, Mapping) and isinstance(incoming_item, Mapping): | ||
| merged[index] = _preserve_existing_secrets(existing_item, incoming_item) | ||
| elif isinstance(existing_item, list) and isinstance(incoming_item, list): | ||
| merged[index] = _preserve_existing_secret_lists(existing_item, incoming_item) | ||
| return merged | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "REDACTED_SECRET", | ||
| "SECRET_FIELD_NAMES", | ||
| "is_secret_field", | ||
| "preserve_existing_secrets", | ||
| "redact_secret_mapping", | ||
| "redact_secrets", | ||
| ] | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def test_redact_secrets_hides_known_secret_keys_without_false_token_matches(): | ||
| from core.utils.redaction import REDACTED_SECRET, redact_secrets | ||
|
|
||
| payload = { | ||
| "llm": {"api_key": "llm-secret", "model": "mistral"}, | ||
| "rdb": {"password": "db-secret", "host": "rdb"}, | ||
| "websearch": {"api_token": "search-secret", "max_tokens": 2048}, | ||
| "oidc_client_secret": "oidc-secret", | ||
| "chainlit_auth_secret": "chainlit-secret", | ||
| "nested": [{"token_encryption_key": "fernet-secret"}], | ||
| } | ||
|
|
||
| redacted = redact_secrets(payload) | ||
|
|
||
| assert redacted["llm"]["api_key"] == REDACTED_SECRET | ||
| assert redacted["rdb"]["password"] == REDACTED_SECRET | ||
| assert redacted["websearch"]["api_token"] == REDACTED_SECRET | ||
| assert redacted["websearch"]["max_tokens"] == 2048 | ||
| assert redacted["oidc_client_secret"] == REDACTED_SECRET | ||
| assert redacted["chainlit_auth_secret"] == REDACTED_SECRET | ||
| assert redacted["nested"][0]["token_encryption_key"] == REDACTED_SECRET | ||
| assert payload["llm"]["api_key"] == "llm-secret" |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.