fix(web): ensure at least 40% of the characters are masked for api key
- keys longer than 12 chars show prefix + last 4 chars - keys 9-12 chars show prefix + last 2 chars - shorter keys are fully masked
This commit is contained in:
parent
ce1619051d
commit
f1ac1a1072
1 changed files with 10 additions and 1 deletions
|
|
@ -307,16 +307,25 @@ func (h *Handler) handleSetDefaultModel(w http.ResponseWriter, r *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
// maskAPIKey returns a masked version of an API key for safe display.
|
// maskAPIKey returns a masked version of an API key for safe display.
|
||||||
// Keys longer than 8 chars show prefix + last 4 chars: "sk-****abcd"
|
// Keys longer than 12 chars show prefix + last 4 chars: "sk-****abcd".
|
||||||
|
// Keys 9-12 chars show prefix + last 2 chars: "sk-****cd".
|
||||||
// Shorter keys are fully masked as "****".
|
// Shorter keys are fully masked as "****".
|
||||||
// Empty keys return empty string.
|
// Empty keys return empty string.
|
||||||
|
// Ensure at least 40% of the key is masked.
|
||||||
func maskAPIKey(key string) string {
|
func maskAPIKey(key string) string {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(key) <= 8 {
|
if len(key) <= 8 {
|
||||||
return "****"
|
return "****"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show first 3 chars and last 2 chars
|
||||||
|
if len(key) <= 12 {
|
||||||
|
return key[:3] + "****" + key[len(key)-2:]
|
||||||
|
}
|
||||||
|
|
||||||
// Show first 3 chars and last 4 chars
|
// Show first 3 chars and last 4 chars
|
||||||
return key[:3] + "****" + key[len(key)-4:]
|
return key[:3] + "****" + key[len(key)-4:]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue