| 1 |
package op_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/golang/mock/gomock" |
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
|
| 9 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 10 |
"github.com/zitadel/oidc/v3/pkg/op/mock" |
| 11 |
) |
| 12 |
|
| 13 |
func TestValidateEndSessionPostLogoutRedirectURI(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
name string |
| 16 |
registered []string |
| 17 |
registeredGlobs []string |
| 18 |
applicationType op.ApplicationType |
| 19 |
postLogoutRedirectURI string |
| 20 |
useGlobsClient bool |
| 21 |
wantErr bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "exact match (web)", |
| 25 |
registered: []string{"https://logged-out"}, |
| 26 |
applicationType: op.ApplicationTypeWeb, |
| 27 |
postLogoutRedirectURI: "https://logged-out", |
| 28 |
wantErr: false, |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "no match", |
| 32 |
registered: []string{"https://logged-out"}, |
| 33 |
applicationType: op.ApplicationTypeWeb, |
| 34 |
postLogoutRedirectURI: "https://other", |
| 35 |
wantErr: true, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "native loopback v4 dynamic port ok", |
| 39 |
registered: []string{"http://127.0.0.1/callback"}, |
| 40 |
applicationType: op.ApplicationTypeNative, |
| 41 |
postLogoutRedirectURI: "http://127.0.0.1:54321/callback", |
| 42 |
wantErr: false, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "native loopback v6 dynamic port ok", |
| 46 |
registered: []string{"http://[::1]/callback"}, |
| 47 |
applicationType: op.ApplicationTypeNative, |
| 48 |
postLogoutRedirectURI: "http://[::1]:54321/callback", |
| 49 |
wantErr: false, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "native localhost dynamic port ok", |
| 53 |
registered: []string{"http://localhost/callback"}, |
| 54 |
applicationType: op.ApplicationTypeNative, |
| 55 |
postLogoutRedirectURI: "http://localhost:54321/callback", |
| 56 |
wantErr: false, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "native https loopback v4 dynamic port ok", |
| 60 |
registered: []string{"https://127.0.0.1/callback"}, |
| 61 |
applicationType: op.ApplicationTypeNative, |
| 62 |
postLogoutRedirectURI: "https://127.0.0.1:54321/callback", |
| 63 |
wantErr: false, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "native loopback wrong path fails", |
| 67 |
registered: []string{"http://127.0.0.1/callback"}, |
| 68 |
applicationType: op.ApplicationTypeNative, |
| 69 |
postLogoutRedirectURI: "http://127.0.0.1:54321/other", |
| 70 |
wantErr: true, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "native loopback exact (no port) ok", |
| 74 |
registered: []string{"http://127.0.0.1/callback"}, |
| 75 |
applicationType: op.ApplicationTypeNative, |
| 76 |
postLogoutRedirectURI: "http://127.0.0.1/callback", |
| 77 |
wantErr: false, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "web loopback dynamic port fails", |
| 81 |
registered: []string{"http://127.0.0.1/callback"}, |
| 82 |
applicationType: op.ApplicationTypeWeb, |
| 83 |
postLogoutRedirectURI: "http://127.0.0.1:54321/callback", |
| 84 |
wantErr: true, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "user-agent loopback dynamic port fails", |
| 88 |
registered: []string{"http://127.0.0.1/callback"}, |
| 89 |
applicationType: op.ApplicationTypeUserAgent, |
| 90 |
postLogoutRedirectURI: "http://127.0.0.1:54321/callback", |
| 91 |
wantErr: true, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "native non-loopback exact still ok", |
| 95 |
registered: []string{"https://example.com/lo"}, |
| 96 |
applicationType: op.ApplicationTypeNative, |
| 97 |
postLogoutRedirectURI: "https://example.com/lo", |
| 98 |
wantErr: false, |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "native non-loopback dynamic port fails", |
| 102 |
registered: []string{"https://example.com/lo"}, |
| 103 |
applicationType: op.ApplicationTypeNative, |
| 104 |
postLogoutRedirectURI: "https://example.com:8443/lo", |
| 105 |
wantErr: true, |
| 106 |
}, |
| 107 |
{ |
| 108 |
// Matches the existing auth-side behavior: equalURI compares only |
| 109 |
// path + raw query, so any registered loopback host is interchangeable |
| 110 |
// with any incoming loopback host as long as the path matches. |
| 111 |
name: "native cross-loopback host ok (v4 registered, v6 request)", |
| 112 |
registered: []string{"http://127.0.0.1/callback"}, |
| 113 |
applicationType: op.ApplicationTypeNative, |
| 114 |
postLogoutRedirectURI: "http://[::1]:54321/callback", |
| 115 |
wantErr: false, |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: "glob match (HasRedirectGlobs)", |
| 119 |
registered: []string{"https://logged-out"}, |
| 120 |
registeredGlobs: []string{"https://*.example.com"}, |
| 121 |
applicationType: op.ApplicationTypeWeb, |
| 122 |
postLogoutRedirectURI: "https://a.example.com", |
| 123 |
useGlobsClient: true, |
| 124 |
wantErr: false, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "glob no match (HasRedirectGlobs)", |
| 128 |
registered: []string{"https://logged-out"}, |
| 129 |
registeredGlobs: []string{"https://*.example.com"}, |
| 130 |
applicationType: op.ApplicationTypeWeb, |
| 131 |
postLogoutRedirectURI: "https://a.other.com", |
| 132 |
useGlobsClient: true, |
| 133 |
wantErr: true, |
| 134 |
}, |
| 135 |
|
| 136 |
// RFC 8252 §7.3 endorsed examples (verbatim). |
| 137 |
// "An example redirect using the IPv4 loopback interface with a randomly |
| 138 |
// assigned port: http://127.0.0.1:51004/oauth2redirect/example-provider" |
| 139 |
{ |
| 140 |
name: "RFC 8252 §7.3 IPv4 example", |
| 141 |
registered: []string{"http://127.0.0.1/oauth2redirect/example-provider"}, |
| 142 |
applicationType: op.ApplicationTypeNative, |
| 143 |
postLogoutRedirectURI: "http://127.0.0.1:51004/oauth2redirect/example-provider", |
| 144 |
wantErr: false, |
| 145 |
}, |
| 146 |
// "An example redirect using the IPv6 loopback interface with a randomly |
| 147 |
// assigned port: http://[::1]:61023/oauth2redirect/example-provider" |
| 148 |
{ |
| 149 |
name: "RFC 8252 §7.3 IPv6 example", |
| 150 |
registered: []string{"http://[::1]/oauth2redirect/example-provider"}, |
| 151 |
applicationType: op.ApplicationTypeNative, |
| 152 |
postLogoutRedirectURI: "http://[::1]:61023/oauth2redirect/example-provider", |
| 153 |
wantErr: false, |
| 154 |
}, |
| 155 |
// RFC 8252 §7.3: "Clients SHOULD NOT assume that the device supports a |
| 156 |
// particular version of the Internet Protocol. It is RECOMMENDED that |
| 157 |
// clients attempt to bind to the loopback interface using both IPv4 and |
| 158 |
// IPv6 and use whichever is available." A client that registered the IPv4 |
| 159 |
// form must therefore be able to log out via the IPv6 form (and vice versa). |
| 160 |
{ |
| 161 |
name: "RFC 8252 §7.3 IPv6 request against IPv4 registration", |
| 162 |
registered: []string{"http://127.0.0.1/oauth2redirect/example-provider"}, |
| 163 |
applicationType: op.ApplicationTypeNative, |
| 164 |
postLogoutRedirectURI: "http://[::1]:61023/oauth2redirect/example-provider", |
| 165 |
wantErr: false, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "RFC 8252 §7.3 IPv4 request against IPv6 registration", |
| 169 |
registered: []string{"http://[::1]/oauth2redirect/example-provider"}, |
| 170 |
applicationType: op.ApplicationTypeNative, |
| 171 |
postLogoutRedirectURI: "http://127.0.0.1:51004/oauth2redirect/example-provider", |
| 172 |
wantErr: false, |
| 173 |
}, |
| 174 |
|
| 175 |
// RFC 8252 §7.3: "The authorization server MUST allow any port to be |
| 176 |
// specified at the time of the request for loopback IP redirect URIs." |
| 177 |
// Exercise the full ephemeral port range — minimum, max, and the port the |
| 178 |
// client would have if it bound to the system default. |
| 179 |
{ |
| 180 |
name: "RFC 8252 §7.3 MUST allow any port — port 1", |
| 181 |
registered: []string{"http://127.0.0.1/cb"}, |
| 182 |
applicationType: op.ApplicationTypeNative, |
| 183 |
postLogoutRedirectURI: "http://127.0.0.1:1/cb", |
| 184 |
wantErr: false, |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "RFC 8252 §7.3 MUST allow any port — port 80", |
| 188 |
registered: []string{"http://127.0.0.1/cb"}, |
| 189 |
applicationType: op.ApplicationTypeNative, |
| 190 |
postLogoutRedirectURI: "http://127.0.0.1:80/cb", |
| 191 |
wantErr: false, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "RFC 8252 §7.3 MUST allow any port — port 65535", |
| 195 |
registered: []string{"http://127.0.0.1/cb"}, |
| 196 |
applicationType: op.ApplicationTypeNative, |
| 197 |
postLogoutRedirectURI: "http://127.0.0.1:65535/cb", |
| 198 |
wantErr: false, |
| 199 |
}, |
| 200 |
{ |
| 201 |
// Registration with an explicit port should still accept any other |
| 202 |
// port at request time — the spec says "any port", not "any port that |
| 203 |
// differs from the registered one". |
| 204 |
name: "RFC 8252 §7.3 registered explicit port, request different port", |
| 205 |
registered: []string{"http://127.0.0.1:8080/cb"}, |
| 206 |
applicationType: op.ApplicationTypeNative, |
| 207 |
postLogoutRedirectURI: "http://127.0.0.1:51004/cb", |
| 208 |
wantErr: false, |
| 209 |
}, |
| 210 |
|
| 211 |
// RFC 8252 §8.3: "While redirect URIs using localhost (i.e., |
| 212 |
// http://localhost:{port}/{path}) function similarly to loopback IP |
| 213 |
// redirects described in Section 7.3, the use of localhost is NOT |
| 214 |
// RECOMMENDED." Functional must still hold even though it's discouraged. |
| 215 |
{ |
| 216 |
name: "RFC 8252 §8.3 localhost variant (NOT RECOMMENDED but functional)", |
| 217 |
registered: []string{"http://localhost/oauth2redirect/example-provider"}, |
| 218 |
applicationType: op.ApplicationTypeNative, |
| 219 |
postLogoutRedirectURI: "http://localhost:51004/oauth2redirect/example-provider", |
| 220 |
wantErr: false, |
| 221 |
}, |
| 222 |
} |
| 223 |
for _, tt := range tests { |
| 224 |
t.Run(tt.name, func(t *testing.T) { |
| 225 |
ctrl := gomock.NewController(t) |
| 226 |
var client op.Client |
| 227 |
if tt.useGlobsClient { |
| 228 |
m := mock.NewMockHasRedirectGlobs(ctrl) |
| 229 |
m.EXPECT().PostLogoutRedirectURIs().AnyTimes().Return(tt.registered) |
| 230 |
m.EXPECT().PostLogoutRedirectURIGlobs().AnyTimes().Return(tt.registeredGlobs) |
| 231 |
m.EXPECT().ApplicationType().AnyTimes().Return(tt.applicationType) |
| 232 |
client = m |
| 233 |
} else { |
| 234 |
m := mock.NewMockClient(ctrl) |
| 235 |
m.EXPECT().PostLogoutRedirectURIs().AnyTimes().Return(tt.registered) |
| 236 |
m.EXPECT().ApplicationType().AnyTimes().Return(tt.applicationType) |
| 237 |
client = m |
| 238 |
} |
| 239 |
err := op.ValidateEndSessionPostLogoutRedirectURI(tt.postLogoutRedirectURI, client) |
| 240 |
if tt.wantErr { |
| 241 |
assert.Error(t, err) |
| 242 |
} else { |
| 243 |
assert.NoError(t, err) |
| 244 |
} |
| 245 |
}) |
| 246 |
} |
| 247 |
} |