-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_origin_test.go
111 lines (104 loc) · 2.99 KB
/
check_origin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package corsx
import (
"net/http"
"testing"
"github.com/rs/cors"
"github.com/stretchr/testify/assert"
)
func TestCheckOrigin(t *testing.T) {
for _, tc := range []struct {
name string
allowedOrigins []string
expect, expectOther bool
}{
{
name: "empty",
allowedOrigins: []string{},
expect: true,
expectOther: true,
},
{
name: "wildcard",
allowedOrigins: []string{"https://example.com", "*"},
expect: true,
expectOther: true,
},
{
name: "exact",
allowedOrigins: []string{"https://www.ory.sh"},
expect: true,
},
{
name: "wildcard in the beginning",
allowedOrigins: []string{"*.ory.sh"},
expect: true,
},
{
name: "wildcard in the middle",
allowedOrigins: []string{"https://*.ory.sh"},
expect: true,
},
{
name: "wildcard in the end",
allowedOrigins: []string{"https://www.ory.*"},
expect: true,
},
{
name: "second wildcard is ignored",
allowedOrigins: []string{"https://*.ory.*"},
expect: false,
},
{
name: "multiple exact",
allowedOrigins: []string{"https://example.com", "https://www.ory.sh"},
expect: true,
},
{
name: "multiple wildcard",
allowedOrigins: []string{"https://*.example.com", "https://*.ory.sh"},
expect: true,
},
{
name: "wildcard and exact origins 1",
allowedOrigins: []string{"https://*.example.com", "https://www.ory.sh"},
expect: true,
},
{
name: "wildcard and exact origins 2",
allowedOrigins: []string{"https://example.com", "https://*.ory.sh"},
expect: true,
},
{
name: "multiple unrelated exact",
allowedOrigins: []string{"https://example.com", "https://example.org"},
expect: false,
},
{
name: "multiple unrelated with wildcard",
allowedOrigins: []string{"https://*.example.com", "https://*.example.org"},
expect: false,
},
{
name: "uppercase exact",
allowedOrigins: []string{"https://www.ORY.sh"},
expect: true,
},
{
name: "uppercase wildcard",
allowedOrigins: []string{"https://*.ORY.sh"},
expect: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expect, CheckOrigin(tc.allowedOrigins, "https://www.ory.sh"))
assert.Equal(t, tc.expectOther, CheckOrigin(tc.allowedOrigins, "https://google.com"))
// check for consistency with rs/cors
assert.Equal(t, tc.expect, cors.New(cors.Options{AllowedOrigins: tc.allowedOrigins}).
OriginAllowed(&http.Request{Header: http.Header{"Origin": []string{"https://www.ory.sh"}}}))
assert.Equal(t, tc.expectOther, cors.New(cors.Options{AllowedOrigins: tc.allowedOrigins}).
OriginAllowed(&http.Request{Header: http.Header{"Origin": []string{"https://google.com"}}}))
})
}
}