Compare commits

...
Author SHA1 Message Date
Michal Klos 58f7223641 feat: jp review 2026-07-08 19:48:47 +02:00
Michal Klos ee93d65a8d fix: e2e keycloak 2026-07-08 19:12:29 +02:00
Michal Klos db105a9211 fix: harden group join 2026-07-08 19:12:29 +02:00
5 changed files with 285 additions and 7 deletions

No files matched your search

@@ -0,0 +1,11 @@
Security: Make OIDC group sync opt-in
**What changed.** `PROXY_AUTOPROVISION_CLAIM_GROUPS` now defaults to `""`, which disables OIDC group membership sync (and, with it, creation of local groups from claim values). It previously defaulted to `groups`. Setting it to a non-empty claim name restores the previous behaviour unchanged: memberships are synced and groups named in the claim are created if they do not exist locally.
**Why.** With `PROXY_AUTOPROVISION_ACCOUNTS=true` and the previous `groups` default, the proxy synced group memberships from the OIDC `groups` claim on every authenticated request out of the box, creating local groups for any claim value that did not already exist. In identity providers that let ordinary users create groups with arbitrary names, this allowed an unprivileged user to inject group names into oCIS. Defaulting the claim to empty makes group sync an explicit opt-in.
**Upgrade note.** Deployments that set `PROXY_AUTOPROVISION_CLAIM_GROUPS` explicitly are unaffected. Deployments that relied on the previous `groups` default without setting it will stop syncing group memberships after upgrade; set `PROXY_AUTOPROVISION_CLAIM_GROUPS=groups` to restore the previous behaviour.
Note: matching claim values to existing local groups is still done by display name. Hardening that matching is tracked separately and is not part of this change.
https://github.com/owncloud/ocis/pull/12490
+1 -1
View File
@@ -170,7 +170,7 @@ type AutoProvisionClaims struct {
Username string `yaml:"username" env:"PROXY_AUTOPROVISION_CLAIM_USERNAME" desc:"The name of the OIDC claim that holds the username." introductionVersion:"6.0.0"`
Email string `yaml:"email" env:"PROXY_AUTOPROVISION_CLAIM_EMAIL" desc:"The name of the OIDC claim that holds the email." introductionVersion:"6.0.0"`
DisplayName string `yaml:"display_name" env:"PROXY_AUTOPROVISION_CLAIM_DISPLAYNAME" desc:"The name of the OIDC claim that holds the display name." introductionVersion:"6.0.0"`
Groups string `yaml:"groups" env:"PROXY_AUTOPROVISION_CLAIM_GROUPS" desc:"The name of the OIDC claim that holds the groups." introductionVersion:"6.1.0"`
Groups string `yaml:"groups" env:"PROXY_AUTOPROVISION_CLAIM_GROUPS" desc:"The name of the OIDC claim that holds the groups. When empty (the default), group membership sync is disabled and groups are not created from claim values. Set it (e.g. to 'groups') to enable syncing and creating groups from the claim." introductionVersion:"6.1.0"`
}
// PolicySelector is the toplevel-configuration for different selectors
@@ -90,7 +90,7 @@ func DefaultConfig() *config.Config {
Username: "preferred_username",
Email: "email",
DisplayName: "name",
Groups: "groups",
Groups: "",
},
EnableBasicAuth: false,
InsecureBackends: false,
+268 -5
View File
@@ -1,11 +1,30 @@
package backend
import "testing"
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
// TestGroupsClaimPresent guards the decision that protects users from losing all
// group memberships: only a present (and non-null) groups claim may trigger
// membership reconciliation. An absent or null claim must be treated as "do not
// sync", not as "no groups".
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
cs3 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
cs3mocks "github.com/owncloud/reva/v2/tests/cs3mocks/mocks"
"github.com/stretchr/testify/mock"
"go-micro.dev/v4/selector"
"google.golang.org/grpc"
)
// TestGroupsClaimPresent verifies only a present, non-null groups claim triggers reconciliation, so an absent claim can't strip a user's memberships.
func TestGroupsClaimPresent(t *testing.T) {
const claim = "groups"
@@ -29,3 +48,247 @@ func TestGroupsClaimPresent(t *testing.T) {
})
}
}
// With Groups unconfigured, SyncGroupMemberships must return before touching the gateway; the nil selector panics if it doesn't.
func TestSyncGroupMemberships_GroupsClaimUnconfigured(t *testing.T) {
b := &cs3backend{
Options: Options{
autoProvisionClaims: config.AutoProvisionClaims{Groups: ""},
},
}
claims := map[string]interface{}{
"groups": []interface{}{"admins"},
}
if err := b.SyncGroupMemberships(context.Background(), &cs3.User{}, claims); err != nil {
t.Fatal(err)
}
}
// graphCalls records the mutating libregraph requests so tests assert on side effects, not control flow.
type graphCalls struct {
mu sync.Mutex
addMember []string // group ids the user was added to
createGroup []string // display names of created groups
delMember []string // group ids the user was removed from
}
func (g *graphCalls) snapshot() ([]string, []string, []string) {
g.mu.Lock()
defer g.mu.Unlock()
return append([]string(nil), g.addMember...),
append([]string(nil), g.createGroup...),
append([]string(nil), g.delMember...)
}
// newGraphServer emulates the libregraph endpoints SyncGroupMemberships calls; existingGroup exists without the user as a member, currentGroups are the user's memberships.
func newGraphServer(t *testing.T, existingGroup string, currentGroups []string, calls *graphCalls) *httptest.Server {
t.Helper()
// Names GetGroup resolves (else 404); CreateGroup adds to it so a create-then-reread stays consistent.
resolvable := make(map[string]bool, len(currentGroups)+1)
for _, name := range currentGroups {
resolvable[name] = true
}
if existingGroup != "" {
resolvable[existingGroup] = true
}
mux := http.NewServeMux()
// GetUser with Expand=memberOf: return the user's current memberships.
mux.HandleFunc("/graph/v1.0/users/", func(w http.ResponseWriter, r *http.Request) {
trimmed := strings.TrimPrefix(r.URL.Path, "/graph/v1.0/users/")
u := libregraph.NewUserWithDefaults()
u.SetId(trimmed)
groups := make([]libregraph.Group, 0, len(currentGroups))
for _, name := range currentGroups {
gr := libregraph.NewGroupWithDefaults()
gr.SetId(name)
gr.SetDisplayName(name)
groups = append(groups, *gr)
}
u.SetMemberOf(groups)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(u)
})
mux.HandleFunc("/graph/v1.0/groups/", func(w http.ResponseWriter, r *http.Request) {
rest := strings.TrimPrefix(r.URL.Path, "/graph/v1.0/groups/")
switch {
case strings.HasSuffix(rest, "/members/$ref") && r.Method == http.MethodPost:
gid := strings.TrimSuffix(rest, "/members/$ref")
calls.mu.Lock()
calls.addMember = append(calls.addMember, gid)
calls.mu.Unlock()
w.WriteHeader(http.StatusNoContent)
return
case strings.Contains(rest, "/members/") && r.Method == http.MethodDelete:
gid := strings.SplitN(rest, "/members/", 2)[0]
calls.mu.Lock()
calls.delMember = append(calls.delMember, gid)
calls.mu.Unlock()
w.WriteHeader(http.StatusNoContent)
return
}
// GetGroup: rest is the group id/name.
gid, _ := url.PathUnescape(rest)
calls.mu.Lock()
known := resolvable[gid]
calls.mu.Unlock()
if known {
gr := libregraph.NewGroupWithDefaults()
gr.SetId(gid)
gr.SetDisplayName(gid)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(gr)
return
}
http.Error(w, `{"error":{"code":"itemNotFound"}}`, http.StatusNotFound)
})
// CreateGroup.
mux.HandleFunc("/graph/v1.0/groups", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "unexpected method", http.StatusMethodNotAllowed)
return
}
var gr libregraph.Group
_ = json.NewDecoder(r.Body).Decode(&gr)
gr.SetId("created-" + gr.GetDisplayName())
calls.mu.Lock()
calls.createGroup = append(calls.createGroup, gr.GetDisplayName())
resolvable[gr.GetDisplayName()] = true
resolvable[gr.GetId()] = true
calls.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(gr)
})
return httptest.NewServer(mux)
}
// newSyncBackend wires a cs3backend against srv and a token-issuing mock gateway to drive the real SyncGroupMemberships.
func newSyncBackend(t *testing.T, srv *httptest.Server, claims config.AutoProvisionClaims) *cs3backend {
t.Helper()
gatewayClient := &cs3mocks.GatewayAPIClient{}
gatewayClient.On("Authenticate", mock.Anything, mock.Anything).Return(
&gateway.AuthenticateResponse{
Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK},
Token: "service-token",
}, nil)
gatewaySelector := pool.GetSelector[gateway.GatewayAPIClient](
"GatewaySelector",
"com.owncloud.api.gateway",
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
return gatewayClient
},
)
t.Cleanup(func() { pool.RemoveSelector("GatewaySelector" + "com.owncloud.api.gateway") })
// Register the graph service so the libregraph client resolves to srv.
u, err := url.Parse(srv.URL)
if err != nil {
t.Fatal(err)
}
reg := registry.GetRegistry(registry.Inmemory())
graphService := registry.BuildHTTPService("com.owncloud.web.graph", u.Host, "")
if err := reg.Register(graphService); err != nil {
t.Fatal(err)
}
// Register a gateway node so gatewaySelector.Next() succeeds; address is unused as the selector returns the mock.
gatewayService := registry.BuildGRPCService("com.owncloud.api.gateway", "", "any", "")
if err := reg.Register(gatewayService); err != nil {
t.Fatal(err)
}
// Deregister from the process-global registry so stale nodes don't leak into later tests.
t.Cleanup(func() {
_ = reg.Deregister(graphService)
_ = reg.Deregister(gatewayService)
})
graphSelector := selector.NewSelector(selector.Registry(reg))
return &cs3backend{
graphSelector: graphSelector,
Options: Options{
logger: log.NewLogger(),
gatewaySelector: gatewaySelector,
selector: graphSelector,
autoProvisionClaims: claims,
},
}
}
func testUser() *cs3.User {
return &cs3.User{Id: &cs3.UserId{OpaqueId: "user-1"}}
}
// TestSyncGroupMemberships_ClaimSet_CreatesAndJoins verifies that with the claim configured, a non-existing claimed group is created and joined.
func TestSyncGroupMemberships_ClaimSet_CreatesAndJoins(t *testing.T) {
calls := &graphCalls{}
srv := newGraphServer(t, "", nil, calls)
defer srv.Close()
b := newSyncBackend(t, srv, config.AutoProvisionClaims{Groups: "groups"})
claims := map[string]interface{}{"groups": []interface{}{"new-group"}}
if err := b.SyncGroupMemberships(context.Background(), testUser(), claims); err != nil {
t.Fatal(err)
}
add, create, _ := calls.snapshot()
if len(create) != 1 || create[0] != "new-group" {
t.Errorf("expected group 'new-group' to be created, got %v", create)
}
if len(add) != 1 {
t.Errorf("expected user to be added to the created group exactly once, got %v", add)
}
}
// TestSyncGroupMemberships_ExistingGroupJoined verifies a claim naming an existing
// group joins the user without re-creating it.
func TestSyncGroupMemberships_ExistingGroupJoined(t *testing.T) {
calls := &graphCalls{}
// "admin" already exists as a local group; user is not yet a member.
srv := newGraphServer(t, "admin", nil, calls)
defer srv.Close()
b := newSyncBackend(t, srv, config.AutoProvisionClaims{Groups: "groups"})
claims := map[string]interface{}{"groups": []interface{}{"admin"}}
if err := b.SyncGroupMemberships(context.Background(), testUser(), claims); err != nil {
t.Fatal(err)
}
add, create, _ := calls.snapshot()
if len(add) != 1 || add[0] != "admin" {
t.Errorf("user should be joined to existing group 'admin', joined: %v", add)
}
if len(create) != 0 {
t.Errorf("an existing group must not be (re)created: %v", create)
}
}
// TestSyncGroupMemberships_Deprovision verifies a user is removed from a group absent from their claim.
func TestSyncGroupMemberships_Deprovision(t *testing.T) {
calls := &graphCalls{}
// User is currently a member of "editors"; their claim no longer carries it.
srv := newGraphServer(t, "", []string{"editors"}, calls)
defer srv.Close()
b := newSyncBackend(t, srv, config.AutoProvisionClaims{Groups: "groups"})
claims := map[string]interface{}{"groups": []interface{}{}}
if err := b.SyncGroupMemberships(context.Background(), testUser(), claims); err != nil {
t.Fatal(err)
}
_, _, del := calls.snapshot()
if len(del) != 1 || del[0] != "editors" {
t.Errorf("user should be removed from 'editors' (not in claim), removals: %v", del)
}
}
+4
View File
@@ -308,6 +308,10 @@ def main() -> int:
server_env.update({
"OCIS_EXCLUDE_RUN_SERVICES": "idp",
"PROXY_AUTOPROVISION_ACCOUNTS": "true",
# Group sync is opt-in and does not create groups by default; the group
# sync e2e test relies on both being enabled so Keycloak groups appear.
"PROXY_AUTOPROVISION_CLAIM_GROUPS": "groups",
"PROXY_AUTOPROVISION_GROUP_CREATE": "true",
"PROXY_ROLE_ASSIGNMENT_DRIVER": "oidc",
"OCIS_OIDC_ISSUER": "https://localhost:8443/realms/oCIS",
"PROXY_OIDC_REWRITE_WELLKNOWN": "true",