Skip to content

Commit ca475b7

Browse files
Fix remaining review issues in docs and group scopes
1 parent 3217698 commit ca475b7

7 files changed

Lines changed: 791 additions & 409 deletions

File tree

database/account_groups.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,13 @@ func pruneDeletedGroupFromAPIKeyScopes(ctx context.Context, tx *sql.Tx, sqlite b
203203
if !containsInt64(groups, groupID) {
204204
continue
205205
}
206-
updates = append(updates, update{id: id, groups: removeInt64(groups, groupID)})
206+
nextGroups := removeInt64(groups, groupID)
207+
// If the deleted group was the key's only allowed group, keep the stale
208+
// ID instead of broadening the key into an unrestricted key.
209+
if len(nextGroups) == 0 && len(groups) > 0 {
210+
continue
211+
}
212+
updates = append(updates, update{id: id, groups: nextGroups})
207213
}
208214
if err := rows.Err(); err != nil {
209215
return err

database/sqlite_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,64 @@ func TestSQLiteUsageStatsBaselineHasBillingColumns(t *testing.T) {
551551
}
552552
}
553553

554+
func TestDeleteAccountGroupDoesNotBroadenScopedAPIKey(t *testing.T) {
555+
dbPath := filepath.Join(t.TempDir(), "codex2api.db")
556+
557+
db, err := New("sqlite", dbPath)
558+
if err != nil {
559+
t.Fatalf("New(sqlite) 返回错误: %v", err)
560+
}
561+
defer db.Close()
562+
563+
ctx := context.Background()
564+
groupA, err := db.CreateAccountGroup(ctx, "Group A", "", "#2563eb", 0)
565+
if err != nil {
566+
t.Fatalf("CreateAccountGroup A 返回错误: %v", err)
567+
}
568+
groupB, err := db.CreateAccountGroup(ctx, "Group B", "", "#16a34a", 1)
569+
if err != nil {
570+
t.Fatalf("CreateAccountGroup B 返回错误: %v", err)
571+
}
572+
573+
keyOnlyA, err := db.InsertAPIKeyWithOptions(ctx, APIKeyInput{
574+
Name: "Only A",
575+
Key: "sk-only-a-1234567890",
576+
AllowedGroupIDs: []int64{groupA},
577+
})
578+
if err != nil {
579+
t.Fatalf("InsertAPIKeyWithOptions only-a 返回错误: %v", err)
580+
}
581+
keyAB, err := db.InsertAPIKeyWithOptions(ctx, APIKeyInput{
582+
Name: "A and B",
583+
Key: "sk-a-b-1234567890",
584+
AllowedGroupIDs: []int64{groupA, groupB},
585+
})
586+
if err != nil {
587+
t.Fatalf("InsertAPIKeyWithOptions a-b 返回错误: %v", err)
588+
}
589+
590+
if err := db.DeleteAccountGroup(ctx, groupA, true); err != nil {
591+
t.Fatalf("DeleteAccountGroup 返回错误: %v", err)
592+
}
593+
594+
rows, err := db.ListAPIKeys(ctx)
595+
if err != nil {
596+
t.Fatalf("ListAPIKeys 返回错误: %v", err)
597+
}
598+
599+
got := make(map[int64][]int64)
600+
for _, row := range rows {
601+
got[row.ID] = row.AllowedGroupIDs
602+
}
603+
604+
if actual := got[keyOnlyA]; len(actual) != 1 || actual[0] != groupA {
605+
t.Fatalf("keyOnlyA allowed groups = %v, want stale [%d] to preserve deny-all semantics", actual, groupA)
606+
}
607+
if actual := got[keyAB]; len(actual) != 1 || actual[0] != groupB {
608+
t.Fatalf("keyAB allowed groups = %v, want [%d]", actual, groupB)
609+
}
610+
}
611+
554612
func TestUsageLogsPersistEffectiveModel(t *testing.T) {
555613
dbPath := filepath.Join(t.TempDir(), "codex2api.db")
556614

0 commit comments

Comments
 (0)