Skip to content

Commit e1f6692

Browse files
IvanHuntersgithub-actions[bot]
authored andcommitted
fix(api): address review feedback on TenantNamespace RBAC
Address review feedback from sircthulhu and CodeRabbit: 1. Return Forbidden instead of NotFound for unauthorized access - Get() now returns 403 Forbidden to follow standard K8s RBAC behavior - Previously returned 404 NotFound for security-by-obscurity - Updated test expectations to match new behavior 2. Propagate field and label selectors in Watch handler - Pass opts.FieldSelector and opts.LabelSelector to upstream Watch - Add defensive filtering before authorization to prevent RBAC bypass - Fixes potential issue with resourceNames restrictions 3. Refactor subject-matching logic to eliminate duplication - Extract matchesSubject() helper for Group/User/ServiceAccount checks - Remove duplicated code from filterAccessible and hasAccessToNamespace - Consolidates ServiceAccount namespace fallback logic All tests pass successfully. Signed-off-by: IvanHunters <[email protected]> (cherry picked from commit 7257b6a)
1 parent 79c23d8 commit e1f6692

2 files changed

Lines changed: 56 additions & 44 deletions

File tree

‎pkg/registry/core/tenantnamespace/rest.go‎

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
apierrors "k8s.io/apimachinery/pkg/api/errors"
1818
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/fields"
21+
"k8s.io/apimachinery/pkg/labels"
2022
"k8s.io/apimachinery/pkg/runtime"
2123
"k8s.io/apimachinery/pkg/runtime/schema"
2224
"k8s.io/apimachinery/pkg/types"
@@ -130,8 +132,8 @@ func (r *REST) Get(
130132
return nil, err
131133
}
132134
if !hasAccess {
133-
// Return NotFound instead of Forbidden to prevent enumeration
134-
return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name)
135+
// Return Forbidden to follow standard K8s RBAC behavior
136+
return nil, apierrors.NewForbidden(r.gvr.GroupResource(), name, fmt.Errorf("access denied"))
135137
}
136138

137139
ns := &corev1.Namespace{}
@@ -155,10 +157,20 @@ func (r *REST) Get(
155157

156158
func (r *REST) Watch(ctx context.Context, opts *metainternal.ListOptions) (watch.Interface, error) {
157159
nsList := &corev1.NamespaceList{}
158-
nsWatch, err := r.w.Watch(ctx, nsList, &client.ListOptions{Raw: &metav1.ListOptions{
160+
161+
// Build upstream watch options with field and label selectors
162+
rawOpts := &metav1.ListOptions{
159163
Watch: true,
160164
ResourceVersion: opts.ResourceVersion,
161-
}})
165+
}
166+
if opts.FieldSelector != nil {
167+
rawOpts.FieldSelector = opts.FieldSelector.String()
168+
}
169+
if opts.LabelSelector != nil {
170+
rawOpts.LabelSelector = opts.LabelSelector.String()
171+
}
172+
173+
nsWatch, err := r.w.Watch(ctx, nsList, &client.ListOptions{Raw: rawOpts})
162174
if err != nil {
163175
return nil, err
164176
}
@@ -200,6 +212,18 @@ func (r *REST) Watch(ctx context.Context, opts *metainternal.ListOptions) (watch
200212
continue
201213
}
202214

215+
// Apply defensive filtering for field and label selectors
216+
if opts.FieldSelector != nil {
217+
if !opts.FieldSelector.Matches(fields.Set{"metadata.name": ns.Name}) {
218+
continue
219+
}
220+
}
221+
if opts.LabelSelector != nil {
222+
if !opts.LabelSelector.Matches(labels.Set(ns.Labels)) {
223+
continue
224+
}
225+
}
226+
203227
// Check if user has access to this namespace
204228
hasAccess, err := r.hasAccessToNamespace(ctx, ns.Name)
205229
if err != nil {
@@ -331,6 +355,26 @@ func (r *REST) makeList(src *corev1.NamespaceList, allowed []string) *corev1alph
331355
return out
332356
}
333357

358+
// matchesSubject checks if a RoleBinding subject matches the user's identity.
359+
// It handles Group, User, and ServiceAccount subjects with proper namespace fallback.
360+
func matchesSubject(subj rbacv1.Subject, bindingNamespace, username string, groups map[string]struct{}) bool {
361+
switch subj.Kind {
362+
case "Group":
363+
_, ok := groups[subj.Name]
364+
return ok
365+
case "User":
366+
return subj.Name == username
367+
case "ServiceAccount":
368+
saNamespace := subj.Namespace
369+
if saNamespace == "" {
370+
saNamespace = bindingNamespace
371+
}
372+
return username == fmt.Sprintf("system:serviceaccount:%s:%s", saNamespace, subj.Name)
373+
default:
374+
return false
375+
}
376+
}
377+
334378
func (r *REST) filterAccessible(
335379
ctx context.Context,
336380
names []string,
@@ -369,26 +413,9 @@ func (r *REST) filterAccessible(
369413
subjectLoop:
370414
for j := range rbs.Items[i].Subjects {
371415
subj := rbs.Items[i].Subjects[j]
372-
switch subj.Kind {
373-
case "Group":
374-
if _, ok = groups[subj.Name]; ok {
375-
allowedNameSet[rbs.Items[i].Namespace] = struct{}{}
376-
break subjectLoop
377-
}
378-
case "User":
379-
if subj.Name == u.GetName() {
380-
allowedNameSet[rbs.Items[i].Namespace] = struct{}{}
381-
break subjectLoop
382-
}
383-
case "ServiceAccount":
384-
saNamespace := subj.Namespace
385-
if saNamespace == "" {
386-
saNamespace = rbs.Items[i].Namespace
387-
}
388-
if u.GetName() == fmt.Sprintf("system:serviceaccount:%s:%s", saNamespace, subj.Name) {
389-
allowedNameSet[rbs.Items[i].Namespace] = struct{}{}
390-
break subjectLoop
391-
}
416+
if matchesSubject(subj, rbs.Items[i].Namespace, u.GetName(), groups) {
417+
allowedNameSet[rbs.Items[i].Namespace] = struct{}{}
418+
break subjectLoop
392419
}
393420
}
394421
}
@@ -434,23 +461,8 @@ func (r *REST) hasAccessToNamespace(
434461
for i := range rbs.Items {
435462
for j := range rbs.Items[i].Subjects {
436463
subj := rbs.Items[i].Subjects[j]
437-
switch subj.Kind {
438-
case "Group":
439-
if _, ok := groups[subj.Name]; ok {
440-
return true, nil
441-
}
442-
case "User":
443-
if subj.Name == u.GetName() {
444-
return true, nil
445-
}
446-
case "ServiceAccount":
447-
saNamespace := subj.Namespace
448-
if saNamespace == "" {
449-
saNamespace = rbs.Items[i].Namespace
450-
}
451-
if u.GetName() == fmt.Sprintf("system:serviceaccount:%s:%s", saNamespace, subj.Name) {
452-
return true, nil
453-
}
464+
if matchesSubject(subj, rbs.Items[i].Namespace, u.GetName(), groups) {
465+
return true, nil
454466
}
455467
}
456468
}

‎pkg/registry/core/tenantnamespace/rest_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ func TestGet_WithoutAccess(t *testing.T) {
462462
t.Errorf("expected nil object, got %v", obj)
463463
}
464464

465-
// Verify it returns NotFound (not Forbidden) to prevent enumeration
466-
if !apierrors.IsNotFound(err) {
467-
t.Errorf("expected NotFound error, got %v", err)
465+
// Verify it returns Forbidden to follow standard K8s RBAC behavior
466+
if !apierrors.IsForbidden(err) {
467+
t.Errorf("expected Forbidden error, got %v", err)
468468
}
469469
}
470470

0 commit comments

Comments
 (0)