|
| 1 | +/* |
| 2 | +Copyright 2026 The kcp Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package authheaders |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + |
| 24 | + userinfo "k8s.io/apiserver/pkg/authentication/user" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + userHeader = "X-Remote-User" |
| 29 | + groupHeader = "X-Remote-Group" |
| 30 | + extraPrefix = "X-Remote-Extra-" |
| 31 | + |
| 32 | + warrantHeader = "X-Remote-Extra-Authorization.kcp.io%2fwarrant" |
| 33 | + scopesHeader = "X-Remote-Extra-Authentication.kcp.io%2fscopes" |
| 34 | +) |
| 35 | + |
| 36 | +// TestSetAuthHeaders_StripsForgedIdentityHeaders is the regression test for the |
| 37 | +// identity-header injection vulnerability: client-supplied X-Remote-* headers |
| 38 | +// must be removed before the authenticated identity is stamped, so a forged |
| 39 | +// value can never be forwarded to a backend that trusts request-header auth. |
| 40 | +func TestSetAuthHeaders_StripsForgedIdentityHeaders(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + forgedWarrant := `{"user":"attacker","groups":["system:masters"]}` |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + user userinfo.Info |
| 47 | + inbound http.Header |
| 48 | + wantUser []string |
| 49 | + wantGroups []string |
| 50 | + forbiddenHeaders []string |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "forged group dropped, real identity stamped", |
| 54 | + user: &userinfo.DefaultInfo{Name: "alice", Groups: []string{"system:authenticated"}}, |
| 55 | + inbound: http.Header{ |
| 56 | + groupHeader: {"system:masters"}, |
| 57 | + }, |
| 58 | + wantUser: []string{"alice"}, |
| 59 | + wantGroups: []string{"system:authenticated"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "forged user overwritten, forged warrant/scopes dropped (no user header sent)", |
| 63 | + user: &userinfo.DefaultInfo{Name: "alice", Groups: []string{"system:authenticated"}}, |
| 64 | + inbound: http.Header{ |
| 65 | + groupHeader: {"system:masters", "org:required"}, |
| 66 | + warrantHeader: {forgedWarrant}, |
| 67 | + scopesHeader: {"cluster:root"}, |
| 68 | + }, |
| 69 | + wantUser: []string{"alice"}, |
| 70 | + wantGroups: []string{"system:authenticated"}, |
| 71 | + forbiddenHeaders: []string{warrantHeader, scopesHeader}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "forged user header is overwritten with the real identity", |
| 75 | + user: &userinfo.DefaultInfo{Name: "alice", Groups: []string{"team:a"}}, |
| 76 | + inbound: http.Header{ |
| 77 | + userHeader: {"system:admin"}, |
| 78 | + groupHeader: {"system:masters"}, |
| 79 | + }, |
| 80 | + wantUser: []string{"alice"}, |
| 81 | + wantGroups: []string{"team:a"}, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "real extras are stamped (encoded), forged extras dropped", |
| 85 | + user: &userinfo.DefaultInfo{ |
| 86 | + Name: "alice", |
| 87 | + Groups: []string{"system:authenticated"}, |
| 88 | + Extra: map[string][]string{"authentication.kcp.io/cluster-name": {"root:org:ws"}}, |
| 89 | + }, |
| 90 | + inbound: http.Header{ |
| 91 | + warrantHeader: {forgedWarrant}, |
| 92 | + }, |
| 93 | + wantUser: []string{"alice"}, |
| 94 | + wantGroups: []string{"system:authenticated"}, |
| 95 | + forbiddenHeaders: []string{warrantHeader}, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tc := range tests { |
| 100 | + t.Run(tc.name, func(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + h := tc.inbound.Clone() |
| 103 | + if h == nil { |
| 104 | + h = http.Header{} |
| 105 | + } |
| 106 | + |
| 107 | + SetAuthHeaders(h, tc.user, userHeader, groupHeader, extraPrefix) |
| 108 | + |
| 109 | + if got := h.Values(userHeader); !reflect.DeepEqual(got, tc.wantUser) { |
| 110 | + t.Errorf("user header = %v, want %v", got, tc.wantUser) |
| 111 | + } |
| 112 | + if got := h.Values(groupHeader); !reflect.DeepEqual(got, tc.wantGroups) { |
| 113 | + t.Errorf("group header = %v, want %v", got, tc.wantGroups) |
| 114 | + } |
| 115 | + for _, g := range h.Values(groupHeader) { |
| 116 | + if g == "system:masters" { |
| 117 | + t.Errorf("forged group system:masters leaked: %v", h.Values(groupHeader)) |
| 118 | + } |
| 119 | + } |
| 120 | + for _, name := range tc.forbiddenHeaders { |
| 121 | + if v := h.Values(name); len(v) != 0 { |
| 122 | + t.Errorf("forged header %q leaked: %v", name, v) |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// TestSetAuthHeaders_RealExtraIsEncodedAndForwarded confirms the legitimate |
| 130 | +// extras round-trip through the PathEscape encoding the request-header |
| 131 | +// authenticator expects. |
| 132 | +func TestSetAuthHeaders_RealExtraIsEncodedAndForwarded(t *testing.T) { |
| 133 | + t.Parallel() |
| 134 | + h := http.Header{} |
| 135 | + SetAuthHeaders(h, &userinfo.DefaultInfo{ |
| 136 | + Name: "alice", |
| 137 | + Extra: map[string][]string{"authentication.kcp.io/cluster-name": {"root:org:ws"}}, |
| 138 | + }, userHeader, groupHeader, extraPrefix) |
| 139 | + |
| 140 | + const encoded = "X-Remote-Extra-Authentication.kcp.io%2Fcluster-name" |
| 141 | + if got := h.Values(encoded); !reflect.DeepEqual(got, []string{"root:org:ws"}) { |
| 142 | + t.Errorf("encoded extra header %q = %v, want [root:org:ws]; full headers=%v", encoded, got, h) |
| 143 | + } |
| 144 | +} |
0 commit comments