Repository navigation
fix: mask API key in encryption_dict for embedding and reranker models (GHSA-fxgr-5jcx-8hv6) #8280
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ def is_valid( | |
| return True | ||
|
|
||
| def encryption_dict(self, model_info: Dict[str, object]): | ||
| return model_info | ||
| return {**model_info, "api_key": self.encryption(model_info.get("api_key", ""))} | ||
|
|
||
| server_url = forms.TextInputField("API URL", required=True) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changed line is invalid Python and has several issues: return {**model_info, "api_key": [API_KEY]"api_key", ""))}Problems
Suggested correctionIf the intent is to redact the API key: def encryption_dict(self, model_info: Dict[str, object]):
return {**model_info, "api_key": ""}If the intent is to encrypt it: def encryption_dict(self, model_info: Dict[str, object]):
return {
**model_info,
"api_key": encrypt(str(model_info.get("api_key", "")), API_KEY),
}Also ensure |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from django.test import SimpleTestCase | ||
|
|
||
| from models_provider.impl.xinference_model_provider.credential.embedding import ( | ||
| XinferenceEmbeddingModelCredential, | ||
| ) | ||
| from models_provider.impl.xinference_model_provider.credential.reranker import ( | ||
| XInferenceRerankerModelCredential, | ||
| ) | ||
|
|
||
|
|
||
| KEY_FIELD = "api_" + "key" | ||
| MASKED_TOKEN = "aaaa" + "*" * 15 + "aa" | ||
|
|
||
|
|
||
| class XinferenceCredentialMaskingTests(SimpleTestCase): | ||
| def test_embedding_credential_masks_token(self): | ||
| credential = {"api_base": "http://model.example.com/v1"} | ||
| credential[KEY_FIELD] = "a" * 10 | ||
|
|
||
| masked = XinferenceEmbeddingModelCredential().encryption_dict(credential) | ||
|
|
||
| self.assertEqual( | ||
| { | ||
| "api_base": "http://model.example.com/v1", | ||
| KEY_FIELD: MASKED_TOKEN, | ||
| }, | ||
| masked, | ||
| ) | ||
|
|
||
| def test_reranker_credential_masks_token(self): | ||
| credential = {"server_url": "http://model.example.com/v1"} | ||
| credential[KEY_FIELD] = "a" * 10 | ||
|
|
||
| masked = XInferenceRerankerModelCredential().encryption_dict(credential) | ||
|
|
||
| self.assertEqual( | ||
| { | ||
| "server_url": "http://model.example.com/v1", | ||
| KEY_FIELD: MASKED_TOKEN, | ||
| }, | ||
| masked, | ||
| ) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, the tests are readable and likely pass if the masking behavior is exactly as expected. No syntax-level bugs are evident, but there are several test-quality and design concerns:
Suggested improvement example: TOKEN = "1234567890"
MASKED_TOKEN = "1234" + "*" * 15 + "90"
KEY_FIELD = "api_key"
def assert_masked(self, credential_cls, base_field):
credential = {base_field: "http://model.example.com/v1", KEY_FIELD: TOKEN}
masked = credential_cls().encryption_dict(credential)
self.assertEqual(
{base_field: "http://model.example.com/v1", KEY_FIELD: MASKED_TOKEN},
masked,
)
self.assertEqual(TOKEN, credential[KEY_FIELD])Main recommendation: verify the intended masking policy and replace obfuscated literals with clear, realistic test values. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changed line has a syntax error and likely does not do what was intended:
Issues
Invalid Python syntax
[API_KEY]"api_key"is not valid expression syntax.))}.SyntaxError.Likely intended API-key redaction
Shallow copy
{**model_info, ...}creates a shallow copy. Nested mutable values are still shared with the original dictionary. This is usually fine for serialization/encryption, but if the caller mutates nested objects, the originalmodel_infomay also be affected.Security consideration
encryption_dictis meant to encrypt secrets, setting"api_key"to""removes the secret rather than encrypting it. Confirm whether the desired behavior is:Possible alternate intent
Recommended fix
If the goal is to clear/redact the API key:
If the goal is to keep a configured API key:
Also verify whether
encryption_dictshould remove the key, blank it, or encrypt it, because the current change is syntactically invalid and cannot run.