-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Set subdomains to AllowOrigins with wildcard #1301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
751ecd7
Set subdomains to AllowOrigins with wildcard
atsushi-ishibashi e9ccb1e
Create IsSubDomain
atsushi-ishibashi 552d08d
Avoid panic when pattern length smaller than domain length
atsushi-ishibashi cf1aac1
Change names, improve formula
atsushi-ishibashi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package middleware | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func equalScheme(domain, pattern string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
didx := strings.Index(domain, ":") | ||
pidx := strings.Index(pattern, ":") | ||
return didx != -1 && pidx != -1 && domain[:didx] == pattern[:pidx] | ||
} | ||
|
||
// isSubDomain compares authority with wildcard | ||
func isSubDomain(domain, pattern string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above |
||
didx := strings.Index(domain, "://") | ||
pidx := strings.Index(pattern, "://") | ||
if didx == -1 || pidx == -1 { | ||
return false | ||
} | ||
domAuth := domain[didx+3:] | ||
// to avoid long loop by invalid long domain | ||
if len(domAuth) > 253 { | ||
return false | ||
} | ||
patAuth := pattern[pidx+3:] | ||
|
||
domComp := strings.Split(domAuth, ".") | ||
patComp := strings.Split(patAuth, ".") | ||
for i := len(domComp)/2 - 1; i >= 0; i-- { | ||
opp := len(domComp) - 1 - i | ||
domComp[i], domComp[opp] = domComp[opp], domComp[i] | ||
} | ||
for i := len(patComp)/2 - 1; i >= 0; i-- { | ||
opp := len(patComp) - 1 - i | ||
patComp[i], patComp[opp] = patComp[opp], patComp[i] | ||
} | ||
|
||
for i, v := range domComp { | ||
if len(patComp) <= i { | ||
return false | ||
} | ||
p := patComp[i] | ||
if p == "*" { | ||
return true | ||
} | ||
if p != v { | ||
return false | ||
} | ||
} | ||
return false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package middleware | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_equalScheme(t *testing.T) { | ||
tests := []struct { | ||
domain, pattern string | ||
expected bool | ||
}{ | ||
{ | ||
domain: "http://example.com", | ||
pattern: "http://example.com", | ||
expected: true, | ||
}, | ||
{ | ||
domain: "https://example.com", | ||
pattern: "https://example.com", | ||
expected: true, | ||
}, | ||
{ | ||
domain: "http://example.com", | ||
pattern: "https://example.com", | ||
expected: false, | ||
}, | ||
{ | ||
domain: "https://example.com", | ||
pattern: "http://example.com", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range tests { | ||
assert.Equal(t, v.expected, equalScheme(v.domain, v.pattern)) | ||
} | ||
} | ||
|
||
func Test_isSubDomain(t *testing.T) { | ||
tests := []struct { | ||
domain, pattern string | ||
expected bool | ||
}{ | ||
{ | ||
domain: "http://aaa.example.com", | ||
pattern: "http://*.example.com", | ||
expected: true, | ||
}, | ||
{ | ||
domain: "http://bbb.aaa.example.com", | ||
pattern: "http://*.example.com", | ||
expected: true, | ||
}, | ||
{ | ||
domain: "http://bbb.aaa.example.com", | ||
pattern: "http://*.aaa.example.com", | ||
expected: true, | ||
}, | ||
{ | ||
domain: "http://aaa.example.com:8080", | ||
pattern: "http://*.example.com:8080", | ||
expected: true, | ||
}, | ||
|
||
{ | ||
domain: "http://fuga.hoge.com", | ||
pattern: "http://*.example.com", | ||
expected: false, | ||
}, | ||
{ | ||
domain: "http://ccc.bbb.example.com", | ||
pattern: "http://*.aaa.example.com", | ||
expected: false, | ||
}, | ||
{ | ||
domain: `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ | ||
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ | ||
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ | ||
.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com`, | ||
pattern: "http://*.example.com", | ||
expected: false, | ||
}, | ||
{ | ||
domain: "http://ccc.bbb.example.com", | ||
pattern: "http://example.com", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range tests { | ||
assert.Equal(t, v.expected, isSubDomain(v.domain, v.pattern)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o != "*"
equalScheme()
part of isSubdomain()`