| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/http/httptest" |
| 6 |
"net/url" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
) |
| 12 |
|
| 13 |
func TestValidateIssuer(t *testing.T) { |
| 14 |
type args struct { |
| 15 |
issuer string |
| 16 |
allowInsecure bool |
| 17 |
} |
| 18 |
tests := []struct { |
| 19 |
name string |
| 20 |
args args |
| 21 |
wantErr bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
"missing issuer fails", |
| 25 |
args{ |
| 26 |
issuer: "", |
| 27 |
}, |
| 28 |
true, |
| 29 |
}, |
| 30 |
{ |
| 31 |
"invalid url for issuer fails", |
| 32 |
args{ |
| 33 |
issuer: ":issuer", |
| 34 |
}, |
| 35 |
true, |
| 36 |
}, |
| 37 |
{ |
| 38 |
"host for issuer missing fails", |
| 39 |
args{ |
| 40 |
issuer: "https:///issuer", |
| 41 |
}, |
| 42 |
true, |
| 43 |
}, |
| 44 |
{ |
| 45 |
"host with fragment fails", |
| 46 |
args{ |
| 47 |
issuer: "https://issuer.com/#issuer", |
| 48 |
}, |
| 49 |
true, |
| 50 |
}, |
| 51 |
{ |
| 52 |
"host with query fails", |
| 53 |
args{ |
| 54 |
issuer: "https://issuer.com?issuer=me", |
| 55 |
}, |
| 56 |
true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
"host with http fails", |
| 60 |
args{ |
| 61 |
issuer: "http://issuer.com", |
| 62 |
}, |
| 63 |
true, |
| 64 |
}, |
| 65 |
{ |
| 66 |
"host with https ok", |
| 67 |
args{ |
| 68 |
issuer: "https://issuer.com", |
| 69 |
}, |
| 70 |
false, |
| 71 |
}, |
| 72 |
{ |
| 73 |
"custom scheme fails", |
| 74 |
args{ |
| 75 |
issuer: "custom://localhost:9999", |
| 76 |
}, |
| 77 |
true, |
| 78 |
}, |
| 79 |
{ |
| 80 |
"http with allowInsecure ok", |
| 81 |
args{ |
| 82 |
issuer: "http://localhost:9999", |
| 83 |
allowInsecure: true, |
| 84 |
}, |
| 85 |
false, |
| 86 |
}, |
| 87 |
{ |
| 88 |
"https with allowInsecure ok", |
| 89 |
args{ |
| 90 |
issuer: "https://localhost:9999", |
| 91 |
allowInsecure: true, |
| 92 |
}, |
| 93 |
false, |
| 94 |
}, |
| 95 |
{ |
| 96 |
"custom scheme with allowInsecure fails", |
| 97 |
args{ |
| 98 |
issuer: "custom://localhost:9999", |
| 99 |
allowInsecure: true, |
| 100 |
}, |
| 101 |
true, |
| 102 |
}, |
| 103 |
} |
| 104 |
for _, tt := range tests { |
| 105 |
t.Run(tt.name, func(t *testing.T) { |
| 106 |
if err := ValidateIssuer(tt.args.issuer, tt.args.allowInsecure); (err != nil) != tt.wantErr { |
| 107 |
t.Errorf("ValidateIssuer() error = %v, wantErr %v", err, tt.wantErr) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestValidateIssuerPath(t *testing.T) { |
| 114 |
type args struct { |
| 115 |
issuerPath *url.URL |
| 116 |
} |
| 117 |
tests := []struct { |
| 118 |
name string |
| 119 |
args args |
| 120 |
wantErr bool |
| 121 |
}{ |
| 122 |
{ |
| 123 |
"empty ok", |
| 124 |
args{func() *url.URL { |
| 125 |
u, _ := url.Parse("") |
| 126 |
return u |
| 127 |
}()}, |
| 128 |
false, |
| 129 |
}, |
| 130 |
{ |
| 131 |
"custom ok", |
| 132 |
args{func() *url.URL { |
| 133 |
u, _ := url.Parse("/custom") |
| 134 |
return u |
| 135 |
}()}, |
| 136 |
false, |
| 137 |
}, |
| 138 |
{ |
| 139 |
"fragment fails", |
| 140 |
args{func() *url.URL { |
| 141 |
u, _ := url.Parse("#fragment") |
| 142 |
return u |
| 143 |
}()}, |
| 144 |
true, |
| 145 |
}, |
| 146 |
{ |
| 147 |
"query fails", |
| 148 |
args{func() *url.URL { |
| 149 |
u, _ := url.Parse("?query=value") |
| 150 |
return u |
| 151 |
}()}, |
| 152 |
true, |
| 153 |
}, |
| 154 |
} |
| 155 |
for _, tt := range tests { |
| 156 |
t.Run(tt.name, func(t *testing.T) { |
| 157 |
if err := ValidateIssuerPath(tt.args.issuerPath); (err != nil) != tt.wantErr { |
| 158 |
t.Errorf("ValidateIssuerPath() error = %v, wantErr %v", err, tt.wantErr) |
| 159 |
} |
| 160 |
}) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestIssuerFromHost(t *testing.T) { |
| 165 |
type args struct { |
| 166 |
path string |
| 167 |
allowInsecure bool |
| 168 |
target string |
| 169 |
} |
| 170 |
type res struct { |
| 171 |
issuer string |
| 172 |
err error |
| 173 |
} |
| 174 |
tests := []struct { |
| 175 |
name string |
| 176 |
args args |
| 177 |
res res |
| 178 |
}{ |
| 179 |
{ |
| 180 |
"invalid issuer path", |
| 181 |
args{ |
| 182 |
path: "/#fragment", |
| 183 |
allowInsecure: false, |
| 184 |
}, |
| 185 |
res{ |
| 186 |
issuer: "", |
| 187 |
err: ErrInvalidIssuerPath, |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
"empty path secure", |
| 192 |
args{ |
| 193 |
path: "", |
| 194 |
allowInsecure: false, |
| 195 |
target: "https://issuer.com", |
| 196 |
}, |
| 197 |
res{ |
| 198 |
issuer: "https://issuer.com", |
| 199 |
err: nil, |
| 200 |
}, |
| 201 |
}, |
| 202 |
{ |
| 203 |
"custom path secure", |
| 204 |
args{ |
| 205 |
path: "/custom/", |
| 206 |
allowInsecure: false, |
| 207 |
target: "https://issuer.com", |
| 208 |
}, |
| 209 |
res{ |
| 210 |
issuer: "https://issuer.com/custom/", |
| 211 |
err: nil, |
| 212 |
}, |
| 213 |
}, |
| 214 |
{ |
| 215 |
"custom path no leading slash", |
| 216 |
args{ |
| 217 |
path: "custom/", |
| 218 |
allowInsecure: false, |
| 219 |
target: "https://issuer.com", |
| 220 |
}, |
| 221 |
res{ |
| 222 |
issuer: "https://issuer.com/custom/", |
| 223 |
err: nil, |
| 224 |
}, |
| 225 |
}, |
| 226 |
{ |
| 227 |
"empty path unsecure", |
| 228 |
args{ |
| 229 |
path: "", |
| 230 |
allowInsecure: true, |
| 231 |
target: "http://issuer.com", |
| 232 |
}, |
| 233 |
res{ |
| 234 |
issuer: "http://issuer.com", |
| 235 |
err: nil, |
| 236 |
}, |
| 237 |
}, |
| 238 |
{ |
| 239 |
"custom path insecure", |
| 240 |
args{ |
| 241 |
path: "/custom/", |
| 242 |
allowInsecure: true, |
| 243 |
target: "http://issuer.com", |
| 244 |
}, |
| 245 |
res{ |
| 246 |
issuer: "http://issuer.com/custom/", |
| 247 |
err: nil, |
| 248 |
}, |
| 249 |
}, |
| 250 |
} |
| 251 |
for _, tt := range tests { |
| 252 |
t.Run(tt.name, func(t *testing.T) { |
| 253 |
issuer, err := IssuerFromHost(tt.args.path)(tt.args.allowInsecure) |
| 254 |
if tt.res.err == nil { |
| 255 |
assert.NoError(t, err) |
| 256 |
req := httptest.NewRequest("", tt.args.target, nil) |
| 257 |
assert.Equal(t, tt.res.issuer, issuer(req)) |
| 258 |
} |
| 259 |
if tt.res.err != nil { |
| 260 |
assert.ErrorIs(t, err, tt.res.err) |
| 261 |
} |
| 262 |
}) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestIssuerFromForwardedOrHost(t *testing.T) { |
| 267 |
type args struct { |
| 268 |
path string |
| 269 |
opts []IssuerFromOption |
| 270 |
target string |
| 271 |
header map[string][]string |
| 272 |
} |
| 273 |
type res struct { |
| 274 |
issuer string |
| 275 |
} |
| 276 |
tests := []struct { |
| 277 |
name string |
| 278 |
args args |
| 279 |
res res |
| 280 |
}{ |
| 281 |
{ |
| 282 |
"header parse error", |
| 283 |
args{ |
| 284 |
path: "/custom/", |
| 285 |
target: "https://issuer.com", |
| 286 |
header: map[string][]string{"Forwarded": {"~~~~"}}, |
| 287 |
}, |
| 288 |
res{ |
| 289 |
issuer: "https://issuer.com/custom/", |
| 290 |
}, |
| 291 |
}, |
| 292 |
{ |
| 293 |
"no forwarded header", |
| 294 |
args{ |
| 295 |
path: "/custom/", |
| 296 |
target: "https://issuer.com", |
| 297 |
}, |
| 298 |
res{ |
| 299 |
issuer: "https://issuer.com/custom/", |
| 300 |
}, |
| 301 |
}, |
| 302 |
// by=<identifier>;for=<identifier>;host=<host>;proto=<http|https> |
| 303 |
{ |
| 304 |
"forwarded header without host", |
| 305 |
args{ |
| 306 |
path: "/custom/", |
| 307 |
target: "https://issuer.com", |
| 308 |
header: map[string][]string{"Forwarded": { |
| 309 |
`by=identifier;for=identifier;proto=https`, |
| 310 |
}}, |
| 311 |
}, |
| 312 |
res{ |
| 313 |
issuer: "https://issuer.com/custom/", |
| 314 |
}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
"forwarded header with host", |
| 318 |
args{ |
| 319 |
path: "/custom/", |
| 320 |
target: "https://issuer.com", |
| 321 |
header: map[string][]string{"Forwarded": { |
| 322 |
`by=identifier;for=identifier;host=first.com;proto=https`, |
| 323 |
}}, |
| 324 |
}, |
| 325 |
res{ |
| 326 |
issuer: "https://first.com/custom/", |
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
"forwarded header with multiple hosts", |
| 331 |
args{ |
| 332 |
path: "/custom/", |
| 333 |
target: "https://issuer.com", |
| 334 |
header: map[string][]string{"Forwarded": { |
| 335 |
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`, |
| 336 |
}}, |
| 337 |
}, |
| 338 |
res{ |
| 339 |
issuer: "https://first.com/custom/", |
| 340 |
}, |
| 341 |
}, |
| 342 |
{ |
| 343 |
"multiple forwarded headers hosts", |
| 344 |
args{ |
| 345 |
path: "/custom/", |
| 346 |
target: "https://issuer.com", |
| 347 |
header: map[string][]string{"Forwarded": { |
| 348 |
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`, |
| 349 |
`by=identifier;for=identifier;host=third.com;proto=https`, |
| 350 |
}}, |
| 351 |
}, |
| 352 |
res{ |
| 353 |
issuer: "https://first.com/custom/", |
| 354 |
}, |
| 355 |
}, |
| 356 |
{ |
| 357 |
"custom header first", |
| 358 |
args{ |
| 359 |
path: "/custom/", |
| 360 |
target: "https://issuer.com", |
| 361 |
header: map[string][]string{ |
| 362 |
"Forwarded": { |
| 363 |
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`, |
| 364 |
`by=identifier;for=identifier;host=third.com;proto=https`, |
| 365 |
}, |
| 366 |
"X-Custom-Forwarded": { |
| 367 |
`by=identifier;for=identifier;host=custom.com;proto=https,host=custom2.com`, |
| 368 |
}, |
| 369 |
}, |
| 370 |
opts: []IssuerFromOption{ |
| 371 |
WithIssuerFromCustomHeaders("x-custom-forwarded"), |
| 372 |
}, |
| 373 |
}, |
| 374 |
res{ |
| 375 |
issuer: "https://custom.com/custom/", |
| 376 |
}, |
| 377 |
}, |
| 378 |
} |
| 379 |
for _, tt := range tests { |
| 380 |
t.Run(tt.name, func(t *testing.T) { |
| 381 |
issuer, err := IssuerFromForwardedOrHost(tt.args.path, tt.args.opts...)(false) |
| 382 |
require.NoError(t, err) |
| 383 |
req := httptest.NewRequest("", tt.args.target, nil) |
| 384 |
for k, v := range tt.args.header { |
| 385 |
req.Header[http.CanonicalHeaderKey(k)] = v |
| 386 |
} |
| 387 |
assert.Equal(t, tt.res.issuer, issuer(req)) |
| 388 |
}) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
func TestStaticIssuer(t *testing.T) { |
| 393 |
type args struct { |
| 394 |
issuer string |
| 395 |
allowInsecure bool |
| 396 |
} |
| 397 |
type res struct { |
| 398 |
issuer string |
| 399 |
err error |
| 400 |
} |
| 401 |
tests := []struct { |
| 402 |
name string |
| 403 |
args args |
| 404 |
res res |
| 405 |
}{ |
| 406 |
{ |
| 407 |
"invalid issuer", |
| 408 |
args{ |
| 409 |
issuer: "", |
| 410 |
allowInsecure: false, |
| 411 |
}, |
| 412 |
res{ |
| 413 |
issuer: "", |
| 414 |
err: ErrInvalidIssuerNoIssuer, |
| 415 |
}, |
| 416 |
}, |
| 417 |
{ |
| 418 |
"empty path secure", |
| 419 |
args{ |
| 420 |
issuer: "https://issuer.com", |
| 421 |
allowInsecure: false, |
| 422 |
}, |
| 423 |
res{ |
| 424 |
issuer: "https://issuer.com", |
| 425 |
err: nil, |
| 426 |
}, |
| 427 |
}, |
| 428 |
{ |
| 429 |
"custom path secure", |
| 430 |
args{ |
| 431 |
issuer: "https://issuer.com/custom/", |
| 432 |
allowInsecure: false, |
| 433 |
}, |
| 434 |
res{ |
| 435 |
issuer: "https://issuer.com/custom/", |
| 436 |
err: nil, |
| 437 |
}, |
| 438 |
}, |
| 439 |
{ |
| 440 |
"unsecure", |
| 441 |
args{ |
| 442 |
issuer: "http://issuer.com", |
| 443 |
allowInsecure: true, |
| 444 |
}, |
| 445 |
res{ |
| 446 |
issuer: "http://issuer.com", |
| 447 |
err: nil, |
| 448 |
}, |
| 449 |
}, |
| 450 |
} |
| 451 |
for _, tt := range tests { |
| 452 |
t.Run(tt.name, func(t *testing.T) { |
| 453 |
issuer, err := StaticIssuer(tt.args.issuer)(tt.args.allowInsecure) |
| 454 |
if tt.res.err == nil { |
| 455 |
assert.NoError(t, err) |
| 456 |
assert.Equal(t, tt.res.issuer, issuer(nil)) |
| 457 |
} |
| 458 |
if tt.res.err != nil { |
| 459 |
assert.ErrorIs(t, err, tt.res.err) |
| 460 |
} |
| 461 |
}) |
| 462 |
} |
| 463 |
} |