fix: [OCISDEV-1523] fix the LDAP master-ID filter for an empty base filter (#13096)

* fix: [OCISDEV-1523] fix the LDAP master-ID filter for an empty base filter

EnhanceFilterWithMasterID replaced an empty LDAP filter with a master-ID-only
filter. This action restricted an unrestricted search to master-ID users
only. The bug broke the cross-instance user search in multi-instance
deployments. The function now keeps an empty filter unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: [OCISDEV-1523] require an explicit LDAP user filter with a master ID

EnhanceFilterWithMasterID in ocis-pkg/ldap/filter.go now keeps an empty
LDAP filter unchanged instead of restricting it to master-ID users only.
The auth-basic, users, groups, and graph services reuse this function to
compute their default LDAP user filter at startup.

An unset filter now means these services resolve or authenticate every
user in the shared LDAP directory, not only master-ID users. This action
is a behavior change for a deployment that sets
OCIS_MULTI_INSTANCE_MASTER_ID without an explicit OCIS_LDAP_USER_FILTER.

Each service now rejects startup when OCIS_MULTI_INSTANCE_MASTER_ID is
set and its own user filter is empty. This action keeps a multi-instance
deployment scoped to its own users by default.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* refactor: [OCISDEV-1523] simplify EnhanceFilterWithMasterID guard clauses

The function collapses three early-return guards into one condition.
This action removes a duplicated rationale comment between filter.go
and filter_test.go.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Roman PerekhodandClaude Sonnet 5 authored and GitHub committed 2026-10-08 17:50:06 +02:00
1 parent 8d8a015d1a
commit 8948de6633
16 files changed
+312 -17

No files matched your search

@@ -0,0 +1,25 @@
Bugfix: Fix the LDAP master-ID filter when the base filter is empty
The function `EnhanceFilterWithMasterID` in `ocis-pkg/ldap/filter.go` adds a
master-ID clause to an LDAP filter with OR. An empty filter means "no
restriction". Before this fix, the function replaced an empty filter with a
master-ID-only filter. This action restricted an unrestricted search to
master-ID users only.
The bug broke the search for a user on another instance in multi-instance
deployments. This search uses the Graph API endpoint `/graph/v1.0/users`.
Multi-instance deployments use this search to find a user to share a resource
with.
Now the function keeps an empty filter unchanged. The function still adds a
master-ID clause to a filter that is not empty.
The `graph`, `auth-basic`, `users`, and `groups` services also use this
function to compute their default LDAP user filter when
`OCIS_MULTI_INSTANCE_MASTER_ID` is set. An empty user filter now means these
services resolve or authenticate every user in the shared LDAP directory,
not only master-ID users. Each of these services now rejects startup when
`OCIS_MULTI_INSTANCE_MASTER_ID` is set and its own user filter is empty.
Set an explicit `OCIS_LDAP_USER_FILTER` for every multi-instance deployment.
https://github.com/owncloud/ocis/pull/13096
+6 -11
View File
@@ -7,14 +7,13 @@ import (
"github.com/go-ldap/ldap/v3"
)
// EnhanceFilterWithMasterID OR's the given LDAP filter with master-id match clauses.
// Returns filter unchanged when masterID is empty or both attributes are empty.
// EnhanceFilterWithMasterID adds master-ID match clauses to the given LDAP filter with OR.
// An empty filter means "no restriction". Do not OR an empty filter with a master-ID clause.
// This action would restrict an unrestricted search to the master ID only.
// The function returns the filter without a change when masterID is empty, when both
// attributes are empty, or when filter is empty.
func EnhanceFilterWithMasterID(filter, masterID, memberAttr, guestAttr string) string {
if masterID == "" {
return filter
}
if memberAttr == "" && guestAttr == "" {
if masterID == "" || (memberAttr == "" && guestAttr == "") || filter == "" {
return filter
}
@@ -35,9 +34,5 @@ func EnhanceFilterWithMasterID(filter, masterID, memberAttr, guestAttr string) s
masterIDFilter = fmt.Sprintf("(|%s)", strings.Join(masterIDParts, ""))
}
if filter == "" {
return masterIDFilter
}
return fmt.Sprintf("(|%s%s)", filter, masterIDFilter)
}
+2 -2
View File
@@ -54,12 +54,12 @@ func TestEnhanceFilterWithMasterID(t *testing.T) {
expectedFilter: "(|(ownCloudGuestOf=instance-1)(ownCloudGuestOf=0000-0000-000-0000))",
},
{
name: "empty existing filter",
name: "empty existing filter stays unrestricted",
filter: "",
masterID: "0000-0000-000-0000",
memberAttr: "ownCloudMemberOf",
guestAttr: "ownCloudGuestOf",
expectedFilter: "(|(ownCloudMemberOf=0000-0000-000-0000)(ownCloudGuestOf=0000-0000-000-0000))",
expectedFilter: "",
},
{
name: "LDAP injection protection",
+7
View File
@@ -46,6 +46,13 @@ func MissingLDAPBindPassword(service string) error {
service, defaults.BaseConfigPath())
}
func MissingLDAPUserFilterForMasterID(service string) error {
return fmt.Errorf("The ldap user_filter has not been set for %s, but OCIS_MULTI_INSTANCE_MASTER_ID is set. "+
"Set an explicit user_filter to scope this instance to its own users. "+
"Without a user_filter, every user in the shared LDAP directory can access this instance.",
service)
}
func MissingServiceUserPassword(service, serviceUser string) error {
return fmt.Errorf("The password of service user %s has not been set properly in your config for %s. "+
"Make sure your %s config contains the proper values "+
+1 -1
View File
@@ -82,7 +82,7 @@ type LDAPProvider struct {
IDP string `yaml:"idp" env:"OCIS_URL;OCIS_OIDC_ISSUER;AUTH_BASIC_IDP_URL" desc:"The identity provider value to set in the userids of the CS3 user objects for users returned by this user provider." introductionVersion:"pre5.0"`
DisableUserMechanism string `yaml:"disable_user_mechanism" env:"OCIS_LDAP_DISABLE_USER_MECHANISM;AUTH_BASIC_DISABLE_USER_MECHANISM" desc:"An option to control the behavior for disabling users. Valid options are 'none', 'attribute' and 'group'. If set to 'group', disabling a user via API will add the user to the configured group for disabled users, if set to 'attribute' this will be done in the ldap user entry, if set to 'none' the disable request is not processed." introductionVersion:"pre5.0"`
LdapDisabledUsersGroupDN string `yaml:"ldap_disabled_users_group_dn" env:"OCIS_LDAP_DISABLED_USERS_GROUP_DN;AUTH_BASIC_DISABLED_USERS_GROUP_DN" desc:"The distinguished name of the group to which added users will be classified as disabled when 'disable_user_mechanism' is set to 'group'." introductionVersion:"pre5.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance allows login for every user in the shared LDAP directory." introductionVersion:"7.0"`
UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"`
UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"`
UserSchema LDAPUserSchema `yaml:"user_schema"`
@@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error {
return shared.MissingLDAPBindPassword(cfg.Service.Name)
}
if cfg.AuthProvider == "ldap" && cfg.AuthProviders.LDAP.MasterID != "" && cfg.AuthProviders.LDAP.UserFilter == "" {
return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name)
}
return nil
}
@@ -0,0 +1,63 @@
package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/owncloud/ocis/v2/services/auth-basic/pkg/config"
)
func TestValidateMasterIDRequiresUserFilter(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "auth-basic"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
AuthProvider: "ldap",
AuthProviders: config.AuthProviders{
LDAP: config.LDAPProvider{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.Error(t, err)
}
func TestValidateMasterIDWithUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "auth-basic"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
AuthProvider: "ldap",
AuthProviders: config.AuthProviders{
LDAP: config.LDAPProvider{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "(ownCloudMemberOf=instance-1)",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}
func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "auth-basic"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
AuthProvider: "ldap",
AuthProviders: config.AuthProviders{
LDAP: config.LDAPProvider{
BindPassword: "secret",
MasterID: "",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}
+1 -1
View File
@@ -198,6 +198,6 @@ type Validation struct {
type MultiInstanceConfig struct {
Enabled bool `yaml:"enabled" env:"OCIS_MULTI_INSTANCE_ENABLED" desc:"Enable multiple instances of Infinite Scale." introductionVersion:"8.0.0"`
InstanceID string `yaml:"instanceid" env:"OCIS_MULTI_INSTANCE_INSTANCEID" desc:"The unique ID of this instance." introductionVersion:"8.0.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master ID that grants access to all instances. Users with this ID in their memberOf or guestOf claims can access any instance. Leave empty to disable." introductionVersion:"8.1.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master ID that grants access to all instances. Users with this ID in their memberOf or guestOf claims can access any instance. Leave empty to disable. Also set 'OCIS_LDAP_USER_FILTER'. Without an explicit user filter, this instance resolves every user in the shared LDAP directory." introductionVersion:"8.1.0"`
QueryRegexp string `yaml:"query_regexp" env:"OCIS_MULTI_INSTANCE_QUERY_TEMPLATE" desc:"The regular expression extracting username and instancename from a user provided search." introductionVersion:"8.0.0"`
}
@@ -98,6 +98,10 @@ func validateLDAPSettings(cfg *config.Config) error {
return shared.MissingLDAPBindPassword(cfg.Service.Name)
}
if cfg.MultiInstance.MasterID != "" && cfg.Identity.LDAP.UserFilter == "" {
return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name)
}
// ensure that "GroupBaseDN" is below "GroupBaseDN"
if cfg.Identity.LDAP.WriteEnabled && cfg.Identity.LDAP.GroupCreateBaseDN != cfg.Identity.LDAP.GroupBaseDN {
baseDN, err := ldap.ParseDN(cfg.Identity.LDAP.GroupBaseDN)
@@ -0,0 +1,63 @@
package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/owncloud/ocis/v2/services/graph/pkg/config"
)
func TestValidateLDAPSettingsMasterIDRequiresUserFilter(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "graph"},
Identity: config.Identity{
LDAP: config.LDAP{
BindPassword: "secret",
UserFilter: "",
},
},
MultiInstance: config.MultiInstanceConfig{
MasterID: "master-1",
},
}
err := validateLDAPSettings(cfg)
assert.Error(t, err)
}
func TestValidateLDAPSettingsMasterIDWithUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "graph"},
Identity: config.Identity{
LDAP: config.LDAP{
BindPassword: "secret",
UserFilter: "(ownCloudMemberOf=instance-1)",
},
},
MultiInstance: config.MultiInstanceConfig{
MasterID: "master-1",
},
}
err := validateLDAPSettings(cfg)
assert.NoError(t, err)
}
func TestValidateLDAPSettingsNoMasterIDWithEmptyUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "graph"},
Identity: config.Identity{
LDAP: config.LDAP{
BindPassword: "secret",
UserFilter: "",
},
},
MultiInstance: config.MultiInstanceConfig{
MasterID: "",
},
}
err := validateLDAPSettings(cfg)
assert.NoError(t, err)
}
+1 -1
View File
@@ -79,7 +79,7 @@ type LDAPDriver struct {
UserObjectClass string `yaml:"user_object_class" env:"OCIS_LDAP_USER_OBJECTCLASS;GROUPS_LDAP_USER_OBJECTCLASS" desc:"The object class to use for users in the default user search filter ('inetOrgPerson')." introductionVersion:"pre5.0"`
GroupObjectClass string `yaml:"group_object_class" env:"OCIS_LDAP_GROUP_OBJECTCLASS;GROUPS_LDAP_GROUP_OBJECTCLASS" desc:"The object class to use for groups in the default group search filter ('groupOfNames')." introductionVersion:"pre5.0"`
IDP string `yaml:"idp" env:"OCIS_URL;OCIS_OIDC_ISSUER;GROUPS_IDP_URL" desc:"The identity provider value to set in the group IDs of the CS3 group objects for groups returned by this group provider." introductionVersion:"pre5.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance resolves every user in the shared LDAP directory." introductionVersion:"7.0"`
UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"`
UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"`
UserSchema LDAPUserSchema `yaml:"user_schema"`
@@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error {
return shared.MissingLDAPBindPassword(cfg.Service.Name)
}
if cfg.Driver == "ldap" && cfg.Drivers.LDAP.MasterID != "" && cfg.Drivers.LDAP.UserFilter == "" {
return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name)
}
return nil
}
@@ -0,0 +1,63 @@
package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/owncloud/ocis/v2/services/groups/pkg/config"
)
func TestValidateMasterIDRequiresUserFilter(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "groups"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.Error(t, err)
}
func TestValidateMasterIDWithUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "groups"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "(ownCloudMemberOf=instance-1)",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}
func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "groups"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}
+1 -1
View File
@@ -84,7 +84,7 @@ type LDAPDriver struct {
DisableUserMechanism string `yaml:"disable_user_mechanism" env:"OCIS_LDAP_DISABLE_USER_MECHANISM;USERS_LDAP_DISABLE_USER_MECHANISM" desc:"An option to control the behavior for disabling users. Valid options are 'none', 'attribute' and 'group'. If set to 'group', disabling a user via API will add the user to the configured group for disabled users, if set to 'attribute' this will be done in the ldap user entry, if set to 'none' the disable request is not processed." introductionVersion:"pre5.0"`
UserTypeAttribute string `yaml:"user_type_attribute" env:"OCIS_LDAP_USER_SCHEMA_USER_TYPE;USERS_LDAP_USER_TYPE_ATTRIBUTE" desc:"LDAP Attribute to distinguish between 'Member' and 'Guest' users. Default is 'ownCloudUserType'." introductionVersion:"pre5.0"`
LdapDisabledUsersGroupDN string `yaml:"ldap_disabled_users_group_dn" env:"OCIS_LDAP_DISABLED_USERS_GROUP_DN;USERS_LDAP_DISABLED_USERS_GROUP_DN" desc:"The distinguished name of the group to which added users will be classified as disabled when 'disable_user_mechanism' is set to 'group'." introductionVersion:"pre5.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"`
MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance resolves every user in the shared LDAP directory." introductionVersion:"7.0"`
UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"`
UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"`
UserSchema LDAPUserSchema `yaml:"user_schema"`
@@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error {
return shared.MissingLDAPBindPassword(cfg.Service.Name)
}
if cfg.Driver == "ldap" && cfg.Drivers.LDAP.MasterID != "" && cfg.Drivers.LDAP.UserFilter == "" {
return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name)
}
return nil
}
@@ -0,0 +1,63 @@
package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/owncloud/ocis/v2/services/users/pkg/config"
)
func TestValidateMasterIDRequiresUserFilter(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "users"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.Error(t, err)
}
func TestValidateMasterIDWithUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "users"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "master-1",
UserFilter: "(ownCloudMemberOf=instance-1)",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}
func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) {
cfg := &config.Config{
Service: config.Service{Name: "users"},
TokenManager: &config.TokenManager{JWTSecret: "secret"},
Driver: "ldap",
Drivers: config.Drivers{
LDAP: config.LDAPDriver{
BindPassword: "secret",
MasterID: "",
UserFilter: "",
},
},
}
err := Validate(cfg)
assert.NoError(t, err)
}