Fix
This commit is contained in:
1 parent
7ab7acd533
commit
176b1fe47f
18 files changed
+423
-31
No files matched your search
@@ -270,6 +270,76 @@ func TestAuthSessionCoreEndpoints(t *testing.T) {
|
||||
t.Fatalf("revoke api-key status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// A normal teammate with an explicit API Keys area grant may manage keys,
|
||||
// without being made a company administrator. Denying the area closes both
|
||||
// the list endpoint and the create/rename/revoke endpoints.
|
||||
_, err = pg.Exec(ctx, `
|
||||
UPDATE memberships SET role = 'member', permissions = '{"settings.api_keys":false}'::jsonb
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("restrict api-key member: %v", err)
|
||||
}
|
||||
rec = do(http.MethodGet, "/api/api-keys", "", false)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("member denied api-keys list status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec = do(http.MethodPost, "/api/api-keys", `{"name":"blocked"}`, true)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("member denied api-key create status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
_, err = pg.Exec(ctx, `
|
||||
UPDATE memberships SET permissions = '{}'::jsonb
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("allow api-key member: %v", err)
|
||||
}
|
||||
rec = do(http.MethodPost, "/api/api-keys", `{"name":"member-key"}`, true)
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("member create api-key status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
memberKey := decode(t, rec)
|
||||
memberKeyID := fmt.Sprint(memberKey["id"])
|
||||
if !strings.HasPrefix(fmt.Sprint(memberKey["key"]), "dk_") {
|
||||
t.Fatalf("member create did not return one-time secret: %v", memberKey)
|
||||
}
|
||||
rec = do(http.MethodGet, "/api/api-keys", "", false)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("member list api-keys status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if strings.Contains(rec.Body.String(), fmt.Sprint(memberKey["key"])) {
|
||||
t.Fatal("list endpoint must never expose full API secrets")
|
||||
}
|
||||
rec = do(http.MethodPatch, "/api/api-keys/"+memberKeyID, `{"name":"member-renamed"}`, true)
|
||||
if rec.Code != http.StatusOK || fmt.Sprint(decode(t, rec)["name"]) != "member-renamed" {
|
||||
t.Fatalf("member rename api-key status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// Revoking the member's API Keys area closes modification of keys they
|
||||
// previously created, without affecting unrelated team permissions.
|
||||
_, err = pg.Exec(ctx, `
|
||||
UPDATE memberships SET permissions = '{"settings.api_keys":false}'::jsonb
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("remove api-key access: %v", err)
|
||||
}
|
||||
rec = do(http.MethodPatch, "/api/api-keys/"+memberKeyID, `{"name":"should-not-rename"}`, true)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("member denied api-key rename status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec = do(http.MethodDelete, "/api/api-keys/"+memberKeyID, "", true)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("member denied api-key revoke status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
_, err = pg.Exec(ctx, `
|
||||
UPDATE memberships SET permissions = '{}'::jsonb
|
||||
WHERE company_id = $1 AND user_id = $2`, companyID, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("restore api-key access: %v", err)
|
||||
}
|
||||
rec = do(http.MethodDelete, "/api/api-keys/"+memberKeyID, "", true)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("member revoke api-key status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = do(http.MethodPost, "/api/auth/logout", `{}`, true)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("logout status=%d body=%s", rec.Code, rec.Body.String())
|
||||
|
||||
Reference in new issue
Block a user