|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package rbac |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + "google.golang.org/grpc/internal/grpctest" |
| 25 | + |
| 26 | + v3rbacpb "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3" |
| 27 | + v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" |
| 28 | + rpb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/rbac/v3" |
| 29 | +) |
| 30 | + |
| 31 | +type s struct { |
| 32 | + grpctest.Tester |
| 33 | +} |
| 34 | + |
| 35 | +func Test(t *testing.T) { |
| 36 | + grpctest.RunSubTests(t, s{}) |
| 37 | +} |
| 38 | + |
| 39 | +func headerMatcher(name string) *v3routepb.HeaderMatcher { |
| 40 | + return &v3routepb.HeaderMatcher{ |
| 41 | + Name: name, |
| 42 | + HeaderMatchSpecifier: &v3routepb.HeaderMatcher_PresentMatch{PresentMatch: true}, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func headerPermission(name string) *v3rbacpb.Permission { |
| 47 | + return &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_Header{Header: headerMatcher(name)}} |
| 48 | +} |
| 49 | + |
| 50 | +func headerPrincipal(name string) *v3rbacpb.Principal { |
| 51 | + return &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_Header{Header: headerMatcher(name)}} |
| 52 | +} |
| 53 | + |
| 54 | +func rbacConfig(perm *v3rbacpb.Permission, principal *v3rbacpb.Principal) *rpb.RBAC { |
| 55 | + return &rpb.RBAC{Rules: &v3rbacpb.RBAC{ |
| 56 | + Action: v3rbacpb.RBAC_ALLOW, |
| 57 | + Policies: map[string]*v3rbacpb.Policy{ |
| 58 | + "test-policy": { |
| 59 | + Permissions: []*v3rbacpb.Permission{perm}, |
| 60 | + Principals: []*v3rbacpb.Principal{principal}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }} |
| 64 | +} |
| 65 | + |
| 66 | +// TestNestedHeaderMatcherValidation checks that a header matcher for :scheme or |
| 67 | +// a grpc- prefixed name is rejected even when it is nested inside an and/or/not |
| 68 | +// rule, as A41 requires. |
| 69 | +func (s) TestNestedHeaderMatcherValidation(t *testing.T) { |
| 70 | + anyPermission := &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_Any{Any: true}} |
| 71 | + anyPrincipal := &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_Any{Any: true}} |
| 72 | + |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + cfg *rpb.RBAC |
| 76 | + }{ |
| 77 | + { |
| 78 | + name: "permission and_rules :scheme", |
| 79 | + cfg: rbacConfig(&v3rbacpb.Permission{Rule: &v3rbacpb.Permission_AndRules{AndRules: &v3rbacpb.Permission_Set{ |
| 80 | + Rules: []*v3rbacpb.Permission{headerPermission(":scheme")}, |
| 81 | + }}}, anyPrincipal), |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "permission not_rule grpc- prefix", |
| 85 | + cfg: rbacConfig(&v3rbacpb.Permission{Rule: &v3rbacpb.Permission_NotRule{NotRule: headerPermission("grpc-timeout")}}, anyPrincipal), |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "principal or_ids :scheme", |
| 89 | + cfg: rbacConfig(anyPermission, &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_OrIds{OrIds: &v3rbacpb.Principal_Set{ |
| 90 | + Ids: []*v3rbacpb.Principal{headerPrincipal(":scheme")}, |
| 91 | + }}}), |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "principal not_id grpc- prefix", |
| 95 | + cfg: rbacConfig(anyPermission, &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_NotId{NotId: headerPrincipal("grpc-encoding")}}), |
| 96 | + }, |
| 97 | + } |
| 98 | + for _, test := range tests { |
| 99 | + t.Run(test.name, func(t *testing.T) { |
| 100 | + if _, err := parseConfig(test.cfg); err == nil { |
| 101 | + t.Fatalf("parseConfig() succeeded; want error rejecting a nested :scheme/grpc- header matcher") |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestNestedHostHeaderAliasing checks that a "host" header matcher nested inside |
| 108 | +// an and/or/not rule is rewritten to ":authority", so it behaves the same as a |
| 109 | +// top-level host matcher (A41 host/:authority equivalence). |
| 110 | +func (s) TestNestedHostHeaderAliasing(t *testing.T) { |
| 111 | + perm := &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_NotRule{NotRule: headerPermission("host")}} |
| 112 | + principal := &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_AndIds{AndIds: &v3rbacpb.Principal_Set{ |
| 113 | + Ids: []*v3rbacpb.Principal{headerPrincipal("host")}, |
| 114 | + }}} |
| 115 | + |
| 116 | + if _, err := parseConfig(rbacConfig(perm, principal)); err != nil { |
| 117 | + t.Fatalf("parseConfig() failed: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + gotPerm := perm.GetRule().(*v3rbacpb.Permission_NotRule).NotRule.GetRule().(*v3rbacpb.Permission_Header).Header.GetName() |
| 121 | + if gotPerm != ":authority" { |
| 122 | + t.Errorf("Nested permission host matcher name = %q, want %q", gotPerm, ":authority") |
| 123 | + } |
| 124 | + gotPrincipal := principal.GetIdentifier().(*v3rbacpb.Principal_AndIds).AndIds.GetIds()[0].GetIdentifier().(*v3rbacpb.Principal_Header).Header.GetName() |
| 125 | + if gotPrincipal != ":authority" { |
| 126 | + t.Errorf("Nested principal host matcher name = %q, want %q", gotPrincipal, ":authority") |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TestHeaderMatcherNameIsLowercased checks that a header matcher name is |
| 131 | +// lowercased before it reaches the matching engine. gRPC lowercases every |
| 132 | +// metadata key, so a name that carries an uppercase character matches no |
| 133 | +// header at all, and a DENY policy using one fails open. |
| 134 | +func (s) TestHeaderMatcherNameIsLowercased(t *testing.T) { |
| 135 | + tests := []struct { |
| 136 | + name string |
| 137 | + wantName string |
| 138 | + }{ |
| 139 | + {name: "X-Role", wantName: "x-role"}, |
| 140 | + {name: "USER-AGENT", wantName: "user-agent"}, |
| 141 | + // The host to :authority alias must not depend on the case either. |
| 142 | + {name: "Host", wantName: ":authority"}, |
| 143 | + } |
| 144 | + for _, test := range tests { |
| 145 | + t.Run(test.name, func(t *testing.T) { |
| 146 | + // The permission matcher is nested and the principal matcher is at |
| 147 | + // the top level, so both the recursive walk and the top level are |
| 148 | + // covered. |
| 149 | + permHeader, principalHeader := headerMatcher(test.name), headerMatcher(test.name) |
| 150 | + perm := &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_NotRule{ |
| 151 | + NotRule: &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_Header{Header: permHeader}}, |
| 152 | + }} |
| 153 | + principal := &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_Header{Header: principalHeader}} |
| 154 | + |
| 155 | + if _, err := parseConfig(rbacConfig(perm, principal)); err != nil { |
| 156 | + t.Fatalf("parseConfig() failed: %v", err) |
| 157 | + } |
| 158 | + if got := permHeader.GetName(); got != test.wantName { |
| 159 | + t.Errorf("Permission header matcher name = %q, want %q", got, test.wantName) |
| 160 | + } |
| 161 | + if got := principalHeader.GetName(); got != test.wantName { |
| 162 | + t.Errorf("Principal header matcher name = %q, want %q", got, test.wantName) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// TestHeaderMatcherValidationIsCaseInsensitive checks that the A41 rejection of |
| 169 | +// :scheme and grpc- prefixed header matchers cannot be evaded by spelling the |
| 170 | +// name in another case. |
| 171 | +func (s) TestHeaderMatcherValidationIsCaseInsensitive(t *testing.T) { |
| 172 | + anyPermission := &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_Any{Any: true}} |
| 173 | + anyPrincipal := &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_Any{Any: true}} |
| 174 | + |
| 175 | + for _, name := range []string{":Scheme", "Grpc-Timeout", "GRPC-STATUS"} { |
| 176 | + t.Run(name, func(t *testing.T) { |
| 177 | + if _, err := parseConfig(rbacConfig(headerPermission(name), anyPrincipal)); err == nil { |
| 178 | + t.Errorf("parseConfig() succeeded for permission header matcher %q; want error rejecting a :scheme/grpc- header matcher", name) |
| 179 | + } |
| 180 | + if _, err := parseConfig(rbacConfig(anyPermission, headerPrincipal(name))); err == nil { |
| 181 | + t.Errorf("parseConfig() succeeded for principal header matcher %q; want error rejecting a :scheme/grpc- header matcher", name) |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
0 commit comments