mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
f6190b54de
* feat(web,api): add fetch models and saved catalog support Split from PR #2752 (part 2 of 3). Backend: - /api/models/catalog endpoint for browsing remote model catalogs - /api/models/fetch endpoint for fetching available models from providers - Credential reuse with provider/API base matching for security - Default API base resolution for providers without explicit base Frontend: - FetchModelsDialog for importing models from remote providers - CatalogDialog for browsing and importing from model catalogs - Static import for FetchModelsDialog (replaces dynamic import from PR1) - Dynamic import retained for TestModelDialog (PR3 territory) * fix(web,api): support bare-array responses in fetchOpenAICompatibleModels * fix(web,api): tighten maskAPIKeyValue to match maskAPIKey policy For 9-12 character keys, maskAPIKeyValue exposed first 4 + last 4 chars (only 1 char masked for a 9-char key). Now uses the same policy as maskAPIKey: first 3 + last 2 for 9-12 chars, first 3 + last 4 for longer keys. Adds tests covering all key length boundaries.
88 lines
1.6 KiB
Go
88 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestMaskAPIKeyValue(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty key",
|
|
key: "",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "whitespace only",
|
|
key: " ",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "short key fully masked",
|
|
key: "abcd",
|
|
want: "****",
|
|
},
|
|
{
|
|
name: "length 8 boundary fully masked",
|
|
key: "12345678",
|
|
want: "****",
|
|
},
|
|
{
|
|
name: "length 9 boundary shows last 2",
|
|
key: "123456789",
|
|
want: "123****89",
|
|
},
|
|
{
|
|
name: "length 10 shows last 2",
|
|
key: "1234567890",
|
|
want: "123****90",
|
|
},
|
|
{
|
|
name: "length 12 boundary shows last 2",
|
|
key: "abcdefghijkl",
|
|
want: "abc****kl",
|
|
},
|
|
{
|
|
name: "length 13 boundary shows last 4",
|
|
key: "abcdefghijklm",
|
|
want: "abc****jklm",
|
|
},
|
|
{
|
|
name: "typical api key",
|
|
key: "sk-1234567890abcd",
|
|
want: "sk-****abcd",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := maskAPIKeyValue(tc.key)
|
|
if got != tc.want {
|
|
t.Fatalf("maskAPIKeyValue(%q) = %q, want %q", tc.key, got, tc.want)
|
|
}
|
|
|
|
if tc.key != "" {
|
|
displayed := strings.Replace(got, "****", "", 1)
|
|
if len(strings.TrimSpace(tc.key)) <= 8 {
|
|
if displayed != "" {
|
|
t.Fatalf("maskAPIKeyValue(%q) displayed part = %q, want empty", tc.key, displayed)
|
|
}
|
|
} else {
|
|
if len(displayed)*10 > len(strings.TrimSpace(tc.key))*6 {
|
|
t.Fatalf(
|
|
"maskAPIKeyValue(%q) displayed length = %d, want at most 60%% of %d",
|
|
tc.key,
|
|
len(displayed),
|
|
len(strings.TrimSpace(tc.key)),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|