-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Only allow webhook to send requests to allowed hosts #17482
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 all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e9618db
Only allow webhook to send requests to allowed hosts
wxiaoguang d007a94
Improve documents, unit tests and comments.
wxiaoguang f4b13c2
Merge branch 'main' into fix-webhook-request
wxiaoguang b5d0fc6
fix spaces
wxiaoguang 825bb4b
Merge branch 'main' into fix-webhook-request
wxiaoguang ec5db50
Merge branch 'main' into fix-webhook-request
wxiaoguang d897184
refactor to modules/hostmatcher
wxiaoguang 2f6ce78
Merge branch 'fix-webhook-request' of github.com:wxiaoguang/gitea int…
wxiaoguang 4156a74
fix spaces
wxiaoguang c6f560d
use "*" instead of "all", improve comments
wxiaoguang d1d5ff0
fix space
wxiaoguang e134bf2
use lowercase
wxiaoguang 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
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,94 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hostmatcher | ||
|
||
import ( | ||
"net" | ||
"path/filepath" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
// HostMatchList is used to check if a host or IP is in a list. | ||
// If you only need to do wildcard matching, consider to use modules/matchlist | ||
type HostMatchList struct { | ||
hosts []string | ||
ipNets []*net.IPNet | ||
} | ||
|
||
// MatchBuiltinAll all hosts are matched | ||
const MatchBuiltinAll = "*" | ||
|
||
// MatchBuiltinExternal A valid non-private unicast IP, all hosts on public internet are matched | ||
const MatchBuiltinExternal = "external" | ||
|
||
// MatchBuiltinPrivate RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and RFC 4193 (FC00::/7). Also called LAN/Intranet. | ||
const MatchBuiltinPrivate = "private" | ||
|
||
// MatchBuiltinLoopback 127.0.0.0/8 for IPv4 and ::1/128 for IPv6, localhost is included. | ||
const MatchBuiltinLoopback = "loopback" | ||
|
||
// ParseHostMatchList parses the host list HostMatchList | ||
func ParseHostMatchList(hostList string) *HostMatchList { | ||
hl := &HostMatchList{} | ||
for _, s := range strings.Split(hostList, ",") { | ||
s = strings.ToLower(strings.TrimSpace(s)) | ||
if s == "" { | ||
continue | ||
} | ||
_, ipNet, err := net.ParseCIDR(s) | ||
if err == nil { | ||
hl.ipNets = append(hl.ipNets, ipNet) | ||
} else { | ||
hl.hosts = append(hl.hosts, s) | ||
} | ||
} | ||
return hl | ||
} | ||
|
||
// MatchesHostOrIP checks if the host or IP matches an allow/deny(block) list | ||
func (hl *HostMatchList) MatchesHostOrIP(host string, ip net.IP) bool { | ||
var matched bool | ||
host = strings.ToLower(host) | ||
ipStr := ip.String() | ||
loop: | ||
for _, hostInList := range hl.hosts { | ||
switch hostInList { | ||
case "": | ||
continue | ||
case MatchBuiltinAll: | ||
matched = true | ||
break loop | ||
case MatchBuiltinExternal: | ||
if matched = ip.IsGlobalUnicast() && !util.IsIPPrivate(ip); matched { | ||
break loop | ||
} | ||
case MatchBuiltinPrivate: | ||
if matched = util.IsIPPrivate(ip); matched { | ||
break loop | ||
} | ||
case MatchBuiltinLoopback: | ||
if matched = ip.IsLoopback(); matched { | ||
break loop | ||
} | ||
default: | ||
if matched, _ = filepath.Match(hostInList, host); matched { | ||
break loop | ||
} | ||
if matched, _ = filepath.Match(hostInList, ipStr); matched { | ||
break loop | ||
} | ||
} | ||
} | ||
if !matched { | ||
for _, ipNet := range hl.ipNets { | ||
if matched = ipNet.Contains(ip); matched { | ||
break | ||
} | ||
} | ||
} | ||
return matched | ||
} |
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,119 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hostmatcher | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHostOrIPMatchesList(t *testing.T) { | ||
type tc struct { | ||
host string | ||
ip net.IP | ||
expected bool | ||
} | ||
|
||
// for IPv6: "::1" is loopback, "fd00::/8" is private | ||
|
||
hl := ParseHostMatchList("private, External, *.myDomain.com, 169.254.1.0/24") | ||
cases := []tc{ | ||
{"", net.IPv4zero, false}, | ||
{"", net.IPv6zero, false}, | ||
|
||
{"", net.ParseIP("127.0.0.1"), false}, | ||
{"", net.ParseIP("::1"), false}, | ||
|
||
{"", net.ParseIP("10.0.1.1"), true}, | ||
{"", net.ParseIP("192.168.1.1"), true}, | ||
{"", net.ParseIP("fd00::1"), true}, | ||
|
||
{"", net.ParseIP("8.8.8.8"), true}, | ||
{"", net.ParseIP("1001::1"), true}, | ||
|
||
{"mydomain.com", net.IPv4zero, false}, | ||
{"sub.mydomain.com", net.IPv4zero, true}, | ||
|
||
{"", net.ParseIP("169.254.1.1"), true}, | ||
{"", net.ParseIP("169.254.2.2"), false}, | ||
} | ||
for _, c := range cases { | ||
assert.Equalf(t, c.expected, hl.MatchesHostOrIP(c.host, c.ip), "case %s(%v)", c.host, c.ip) | ||
} | ||
|
||
hl = ParseHostMatchList("loopback") | ||
cases = []tc{ | ||
{"", net.IPv4zero, false}, | ||
{"", net.ParseIP("127.0.0.1"), true}, | ||
{"", net.ParseIP("10.0.1.1"), false}, | ||
{"", net.ParseIP("192.168.1.1"), false}, | ||
{"", net.ParseIP("8.8.8.8"), false}, | ||
|
||
{"", net.ParseIP("::1"), true}, | ||
{"", net.ParseIP("fd00::1"), false}, | ||
{"", net.ParseIP("1000::1"), false}, | ||
|
||
{"mydomain.com", net.IPv4zero, false}, | ||
} | ||
for _, c := range cases { | ||
assert.Equalf(t, c.expected, hl.MatchesHostOrIP(c.host, c.ip), "case %s(%v)", c.host, c.ip) | ||
} | ||
|
||
hl = ParseHostMatchList("private") | ||
cases = []tc{ | ||
{"", net.IPv4zero, false}, | ||
{"", net.ParseIP("127.0.0.1"), false}, | ||
{"", net.ParseIP("10.0.1.1"), true}, | ||
{"", net.ParseIP("192.168.1.1"), true}, | ||
{"", net.ParseIP("8.8.8.8"), false}, | ||
|
||
{"", net.ParseIP("::1"), false}, | ||
{"", net.ParseIP("fd00::1"), true}, | ||
{"", net.ParseIP("1000::1"), false}, | ||
|
||
{"mydomain.com", net.IPv4zero, false}, | ||
} | ||
for _, c := range cases { | ||
assert.Equalf(t, c.expected, hl.MatchesHostOrIP(c.host, c.ip), "case %s(%v)", c.host, c.ip) | ||
} | ||
|
||
hl = ParseHostMatchList("external") | ||
cases = []tc{ | ||
{"", net.IPv4zero, false}, | ||
{"", net.ParseIP("127.0.0.1"), false}, | ||
{"", net.ParseIP("10.0.1.1"), false}, | ||
{"", net.ParseIP("192.168.1.1"), false}, | ||
{"", net.ParseIP("8.8.8.8"), true}, | ||
|
||
{"", net.ParseIP("::1"), false}, | ||
{"", net.ParseIP("fd00::1"), false}, | ||
{"", net.ParseIP("1000::1"), true}, | ||
|
||
{"mydomain.com", net.IPv4zero, false}, | ||
} | ||
for _, c := range cases { | ||
assert.Equalf(t, c.expected, hl.MatchesHostOrIP(c.host, c.ip), "case %s(%v)", c.host, c.ip) | ||
} | ||
|
||
hl = ParseHostMatchList("*") | ||
cases = []tc{ | ||
{"", net.IPv4zero, true}, | ||
{"", net.ParseIP("127.0.0.1"), true}, | ||
{"", net.ParseIP("10.0.1.1"), true}, | ||
{"", net.ParseIP("192.168.1.1"), true}, | ||
{"", net.ParseIP("8.8.8.8"), true}, | ||
|
||
{"", net.ParseIP("::1"), true}, | ||
{"", net.ParseIP("fd00::1"), true}, | ||
{"", net.ParseIP("1000::1"), true}, | ||
|
||
{"mydomain.com", net.IPv4zero, true}, | ||
} | ||
for _, c := range cases { | ||
assert.Equalf(t, c.expected, hl.MatchesHostOrIP(c.host, c.ip), "case %s(%v)", c.host, c.ip) | ||
} | ||
} |
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,19 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package util | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
// IsIPPrivate for net.IP.IsPrivate. TODO: replace with `ip.IsPrivate()` if min go version is bumped to 1.17 | ||
func IsIPPrivate(ip net.IP) bool { | ||
if ip4 := ip.To4(); ip4 != nil { | ||
return ip4[0] == 10 || | ||
(ip4[0] == 172 && ip4[1]&0xf0 == 16) || | ||
(ip4[0] == 192 && ip4[1] == 168) | ||
} | ||
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.