Repository navigation
fix: mask API key in encryption_dict for embedding and reranker models (GHSA-fxgr-5jcx-8hv6) - #8280
Conversation
…s (GHSA-fxgr-5jcx-8hv6)
| KEY_FIELD: MASKED_TOKEN, | ||
| }, | ||
| masked, | ||
| ) |
There was a problem hiding this comment.
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:
-
Hard-coded mask length looks suspicious/brittle
- Input key is
"a" * 10(10 chars), butMASKED_TOKENis"aaaa" + "*" * 15 + "aa"(21 chars). - Confirm this is the intended masking policy. If masking should preserve the key length, the expected value is likely wrong. If a fixed 15-star mask is intentional, add a comment explaining it.
- Input key is
-
Obfuscated literals reduce clarity
KEY_FIELD = "api_" + "key"andMASKED_TOKEN = "aaaa" + "*" * 15 + "aa"look like attempts to avoid secret scanners.- In test code, this is unnecessary and makes the test harder to read/search. Prefer direct literals such as
"api_key"and the expected mask string. - Also, because
KEY_FIELDis used for both input and expected output, a typo in the key name would not be caught. Use the literal key name to test the real contract.
-
The test token is too weak
- Using
"a" * 10means the test cannot fully verify that the correct prefix/suffix characters are preserved. - Prefer a unique token such as
"1234567890"so the test checks actual character positions, e.g. expected mask"1234" + "*" * 15 + "90"if that is the policy.
- Using
-
Missing edge cases
- Add tests for:
- missing
api_key - empty
api_key Nonevalue- very short and very long keys
- already-masked values
- missing
- Also assert that the original input dict is not mutated:
self.assertEqual(credential[KEY_FIELD], token)
- Add tests for:
-
Duplication between the two tests
- The embedding and reranker tests are nearly identical. Consider a helper method or
subTest/parameterization to reduce repetition.
- The embedding and reranker tests are nearly identical. Consider a helper method or
-
Naming inconsistency
- The imports show
XinferenceEmbeddingModelCredentialvsXInferenceRerankerModelCredential. If these are real class names, the casing inconsistency is worth checking.
- The imports show
-
SimpleTestCaseSimpleTestCaseis fine if these credential classes do not touch the database. If they do any DB access, useTestCaseinstead.
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.
| return {**model_info, "api_key": self.encryption(model_info.get("api_key", ""))} | ||
|
|
||
| def build_model(self, model_info: Dict[str, object]): | ||
| for key in ["model"]: |
There was a problem hiding this comment.
The changed line has a syntax error and likely does not do what was intended:
return {**model_info, "api_key": [API_KEY]"api_key", ""))}Issues
-
Invalid Python syntax
[API_KEY]"api_key"is not valid expression syntax.- There are unmatched/mismatched brackets and parentheses:
))}. - This code will fail at import/compile time with a
SyntaxError.
-
Likely intended API-key redaction
- If the goal is to hide or clear the API key before returning/encrypting model info, the intended code was probably:
or, if reading from an environment variable:
return {**model_info, "api_key": ""}
import os return {**model_info, "api_key": os.getenv("API_KEY", "")}
- If the goal is to hide or clear the API key before returning/encrypting model info, the intended code was probably:
-
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
- If
encryption_dictis meant to encrypt secrets, setting"api_key"to""removes the secret rather than encrypting it. Confirm whether the desired behavior is:- redaction/removal, or
- encryption of the real API key.
- If
-
Possible alternate intent
- If the API key should be removed entirely instead of blanked:
result = model_info.copy() result.pop("api_key", None) return result
- If the API key should be removed entirely instead of blanked:
Recommended fix
If the goal is to clear/redact the API key:
def encryption_dict(self, model_info: Dict[str, object]):
return {**model_info, "api_key": ""}If the goal is to keep a configured API key:
import os
def encryption_dict(self, model_info: Dict[str, object]):
return {**model_info, "api_key": os.getenv("API_KEY", "")}Also verify whether encryption_dict should remove the key, blank it, or encrypt it, because the current change is syntactically invalid and cannot run.
| return {**model_info, "api_key": self.encryption(model_info.get("api_key", ""))} | ||
|
|
||
| server_url = forms.TextInputField("API URL", required=True) | ||
|
|
There was a problem hiding this comment.
The changed line is invalid Python and has several issues:
return {**model_info, "api_key": [API_KEY]"api_key", ""))}Problems
-
Syntax error
[API_KEY]"api_key", ""))}contains unmatched brackets/parentheses and invalid expression syntax.- This code will not compile.
-
Likely broken merge intent
- If the goal is to overwrite/redact
api_key, this is the correct form:return {**model_info, "api_key": ""}
- If the goal is to preserve an existing value:
return {**model_info, "api_key": model_info.get("api_key", "")}
- If the goal is to overwrite/redact
-
Security concern
- If
encryption_dict()is supposed to encrypt or redact sensitive data, returning or exposing the rawapi_keyis risky. - Do not hardcode
API_KEYin source code. Load secrets from environment variables or a secret manager. - If encryption is intended, use a proper encryption function, e.g.:
return { **model_info, "api_key": encrypt(model_info.get("api_key", ""), API_KEY), }
- If
-
Possibly unintended overwrite
{**model_info, "api_key": ...}always overwrites any existingapi_keyinmodel_info.- Confirm this is intentional.
-
Type annotation is broad
Dict[str, object]is acceptable, but if this data is serialized, ensure all values are JSON-serializable.
Suggested correction
If 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 API_KEY is not hardcoded and that encrypt() is correctly defined and handles empty/missing keys.
fix: mask API key in encryption_dict for embedding and reranker models (GHSA-fxgr-5jcx-8hv6)