Skip to content

Commit e9618db

Browse files
committed
Only allow webhook to send requests to allowed hosts
1 parent e6e3b21 commit e9618db

File tree

8 files changed

+199
-23
lines changed

8 files changed

+199
-23
lines changed

cmd/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ func listen(m http.Handler, handleRedirector bool) error {
194194
listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort)
195195
}
196196
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
197+
log.Info("AppURL: %s", setting.AppURL)
197198

198199
if setting.LFS.StartServer {
199200
log.Info("LFS server enabled")

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,9 @@ PATH =
13961396
;; Deliver timeout in seconds
13971397
;DELIVER_TIMEOUT = 5
13981398
;;
1399+
;; Webhook can only call allowed hosts for security reasons. Comma separated list: loopback, private, global, or all, or CIDR list (1.2.3.0/8), or wildcard hosts (*.mydomain.com)
1400+
; ALLOWED_HOST_LIST = global
1401+
;;
13991402
;; Allow insecure certification
14001403
;SKIP_TLS_VERIFY = false
14011404
;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
581581

582582
- `QUEUE_LENGTH`: **1000**: Hook task queue length. Use caution when editing this value.
583583
- `DELIVER_TIMEOUT`: **5**: Delivery timeout (sec) for shooting webhooks.
584+
- `ALLOWED_HOST_LIST`: **global**: Webhook can only call allowed hosts for security reasons. Comma separated list: `loopback`, `private`, `global`, or `all`, or CIDR list (1.2.3.0/8), or wildcard hosts (*.mydomain.com)
584585
- `SKIP_TLS_VERIFY`: **false**: Allow insecure certification.
585586
- `PAGING_NUM`: **10**: Number of webhook history events that are shown in one page.
586587
- `PROXY_URL`: **\<empty\>**: Proxy server URL, support http://, https//, socks://, blank will follow environment http_proxy/https_proxy. If not given, will use global proxy setting.

modules/migrations/migrate.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func IsMigrateURLAllowed(remoteURL string, doer *models.User) error {
8989
return &models.ErrInvalidCloneAddr{Host: u.Host, NotResolvedIP: true}
9090
}
9191
for _, addr := range addrList {
92-
if isIPPrivate(addr) || !addr.IsGlobalUnicast() {
92+
if util.IsIPPrivate(addr) || !addr.IsGlobalUnicast() {
9393
return &models.ErrInvalidCloneAddr{Host: u.Host, PrivateNet: addr.String(), IsPermissionDenied: true}
9494
}
9595
}
@@ -474,13 +474,3 @@ func Init() error {
474474

475475
return nil
476476
}
477-
478-
// TODO: replace with `ip.IsPrivate()` if min go version is bumped to 1.17
479-
func isIPPrivate(ip net.IP) bool {
480-
if ip4 := ip.To4(); ip4 != nil {
481-
return ip4[0] == 10 ||
482-
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
483-
(ip4[0] == 192 && ip4[1] == 168)
484-
}
485-
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
486-
}

modules/setting/webhook.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@
55
package setting
66

77
import (
8+
"net"
89
"net/url"
10+
"strings"
911

1012
"code.gitea.io/gitea/modules/log"
1113
)
1214

1315
var (
1416
// Webhook settings
1517
Webhook = struct {
16-
QueueLength int
17-
DeliverTimeout int
18-
SkipTLSVerify bool
19-
Types []string
20-
PagingNum int
21-
ProxyURL string
22-
ProxyURLFixed *url.URL
23-
ProxyHosts []string
18+
QueueLength int
19+
DeliverTimeout int
20+
SkipTLSVerify bool
21+
AllowedHostList []string // loopback,private,global, or all, or CIDR list, or wildcard hosts
22+
AllowedHostIPNets []*net.IPNet
23+
Types []string
24+
PagingNum int
25+
ProxyURL string
26+
ProxyURLFixed *url.URL
27+
ProxyHosts []string
2428
}{
2529
QueueLength: 1000,
2630
DeliverTimeout: 5,
@@ -31,11 +35,29 @@ var (
3135
}
3236
)
3337

38+
// ParseWebhookAllowedHostList parses the ALLOWED_HOST_LIST value
39+
func ParseWebhookAllowedHostList(allowedHostListStr string) (allowedHostList []string, allowedHostIPNets []*net.IPNet) {
40+
for _, s := range strings.Split(allowedHostListStr, ",") {
41+
s = strings.TrimSpace(s)
42+
if s == "" {
43+
continue
44+
}
45+
_, ipNet, err := net.ParseCIDR(s)
46+
if err == nil {
47+
allowedHostIPNets = append(allowedHostIPNets, ipNet)
48+
} else {
49+
allowedHostList = append(allowedHostList, s)
50+
}
51+
}
52+
return
53+
}
54+
3455
func newWebhookService() {
3556
sec := Cfg.Section("webhook")
3657
Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000)
3758
Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
3859
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
60+
Webhook.AllowedHostList, Webhook.AllowedHostIPNets = ParseWebhookAllowedHostList(sec.Key("ALLOWED_HOST_LIST").MustString("global"))
3961
Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork"}
4062
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
4163
Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("")

modules/util/util.go

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/rand"
1010
"errors"
1111
"math/big"
12+
"net"
1213
"strconv"
1314
"strings"
1415
)
@@ -161,3 +162,13 @@ func RandomString(length int64) (string, error) {
161162
}
162163
return string(bytes), nil
163164
}
165+
166+
// IsIPPrivate for net.IP.IsPrivate. TODO: replace with `ip.IsPrivate()` if min go version is bumped to 1.17
167+
func IsIPPrivate(ip net.IP) bool {
168+
if ip4 := ip.To4(); ip4 != nil {
169+
return ip4[0] == 10 ||
170+
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
171+
(ip4[0] == 192 && ip4[1] == 168)
172+
}
173+
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
174+
}

services/webhook/deliver.go

+70-4
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ import (
1616
"net"
1717
"net/http"
1818
"net/url"
19+
"path/filepath"
1920
"strconv"
2021
"strings"
2122
"sync"
23+
"syscall"
2224
"time"
2325

2426
"code.gitea.io/gitea/models"
2527
"code.gitea.io/gitea/modules/graceful"
2628
"code.gitea.io/gitea/modules/log"
2729
"code.gitea.io/gitea/modules/proxy"
2830
"code.gitea.io/gitea/modules/setting"
31+
"code.gitea.io/gitea/modules/util"
32+
2933
"github.com/gobwas/glob"
3034
)
3135

36+
var contextKeyWebhookRequest interface{} = "contextKeyWebhookRequest"
37+
3238
// Deliver deliver hook task
3339
func Deliver(t *models.HookTask) error {
3440
w, err := models.GetWebhookByID(t.HookID)
@@ -171,7 +177,7 @@ func Deliver(t *models.HookTask) error {
171177
return fmt.Errorf("Webhook task skipped (webhooks disabled): [%d]", t.ID)
172178
}
173179

174-
resp, err := webhookHTTPClient.Do(req)
180+
resp, err := webhookHTTPClient.Do(req.WithContext(context.WithValue(req.Context(), contextKeyWebhookRequest, req)))
175181
if err != nil {
176182
t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
177183
return err
@@ -288,19 +294,79 @@ func webhookProxy() func(req *http.Request) (*url.URL, error) {
288294
}
289295
}
290296

297+
func isWebhookRequestAllowed(allowedHostList []string, allowedHostIPNets []*net.IPNet, host string, ip net.IP) bool {
298+
var allowed bool
299+
ipStr := ip.String()
300+
loop:
301+
for _, allowedHost := range allowedHostList {
302+
switch allowedHost {
303+
case "":
304+
continue
305+
case "all":
306+
allowed = true
307+
break loop
308+
case "global":
309+
if allowed = ip.IsGlobalUnicast() && !util.IsIPPrivate(ip); allowed {
310+
break loop
311+
}
312+
case "private":
313+
if allowed = util.IsIPPrivate(ip); allowed {
314+
break loop
315+
}
316+
case "loopback":
317+
if allowed = ip.IsLoopback(); allowed {
318+
break loop
319+
}
320+
default:
321+
if ok, _ := filepath.Match(allowedHost, host); ok {
322+
allowed = true
323+
break loop
324+
}
325+
if ok, _ := filepath.Match(allowedHost, ipStr); ok {
326+
allowed = true
327+
break loop
328+
}
329+
}
330+
}
331+
if !allowed {
332+
for _, allowIPNet := range allowedHostIPNets {
333+
if allowIPNet.Contains(ip) {
334+
allowed = true
335+
break
336+
}
337+
}
338+
}
339+
return allowed
340+
}
341+
291342
// InitDeliverHooks starts the hooks delivery thread
292343
func InitDeliverHooks() {
293344
timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
294345

295346
webhookHTTPClient = &http.Client{
347+
Timeout: timeout,
296348
Transport: &http.Transport{
297349
TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify},
298350
Proxy: webhookProxy(),
299-
Dial: func(netw, addr string) (net.Conn, error) {
300-
return net.DialTimeout(netw, addr, timeout) // dial timeout
351+
DialContext: func(ctx context.Context, network, addrOrHost string) (net.Conn, error) {
352+
dialer := net.Dialer{
353+
Timeout: timeout,
354+
Control: func(network, ipAddr string, c syscall.RawConn) error {
355+
// in Control func, the addr was already resolved to IP:PORT format, there is no cost to do ResolveTCPAddr here
356+
tcpAddr, err := net.ResolveTCPAddr(network, ipAddr)
357+
req := ctx.Value(contextKeyWebhookRequest).(*http.Request)
358+
if err != nil {
359+
return fmt.Errorf("webhook can only call HTTP servers via TCP, deny '%s(%s:%s)', err=%v", req.Host, network, ipAddr, err)
360+
}
361+
if !isWebhookRequestAllowed(setting.Webhook.AllowedHostList, setting.Webhook.AllowedHostIPNets, req.Host, tcpAddr.IP) {
362+
return fmt.Errorf("webhook can only call allowed HTTP servers (check your webhook.ALLOWED_HOST_LIST setting), deny '%s(%s)'", req.Host, ipAddr)
363+
}
364+
return nil
365+
},
366+
}
367+
return dialer.DialContext(ctx, network, addrOrHost)
301368
},
302369
},
303-
Timeout: timeout, // request timeout
304370
}
305371

306372
go graceful.GetManager().RunWithShutdownContext(DeliverHooks)

services/webhook/deliver_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package webhook
66

77
import (
8+
"net"
89
"net/http"
910
"net/url"
1011
"testing"
@@ -37,3 +38,84 @@ func TestWebhookProxy(t *testing.T) {
3738
}
3839
}
3940
}
41+
42+
func TestIsWebhookRequestAllowed(t *testing.T) {
43+
type tc struct {
44+
host string
45+
ip net.IP
46+
expected bool
47+
}
48+
49+
ah, an := setting.ParseWebhookAllowedHostList("private, global, *.google.com, 169.254.1.0/24")
50+
cases := []tc{
51+
{"", net.IPv4zero, false},
52+
53+
{"", net.ParseIP("127.0.0.1"), false},
54+
55+
{"", net.ParseIP("10.0.1.1"), true},
56+
{"", net.ParseIP("192.168.1.1"), true},
57+
58+
{"", net.ParseIP("8.8.8.8"), true},
59+
60+
{"google.com", net.IPv4zero, false},
61+
{"sub.google.com", net.IPv4zero, true},
62+
63+
{"", net.ParseIP("169.254.1.1"), true},
64+
{"", net.ParseIP("169.254.2.2"), false},
65+
}
66+
for _, c := range cases {
67+
assert.Equalf(t, c.expected, isWebhookRequestAllowed(ah, an, c.host, c.ip), "case %s(%v)", c.host, c.ip)
68+
}
69+
70+
ah, an = setting.ParseWebhookAllowedHostList("loopback")
71+
cases = []tc{
72+
{"", net.IPv4zero, false},
73+
{"", net.ParseIP("127.0.0.1"), true},
74+
{"", net.ParseIP("10.0.1.1"), false},
75+
{"", net.ParseIP("192.168.1.1"), false},
76+
{"", net.ParseIP("8.8.8.8"), false},
77+
{"google.com", net.IPv4zero, false},
78+
}
79+
for _, c := range cases {
80+
assert.Equalf(t, c.expected, isWebhookRequestAllowed(ah, an, c.host, c.ip), "case %s(%v)", c.host, c.ip)
81+
}
82+
83+
ah, an = setting.ParseWebhookAllowedHostList("private")
84+
cases = []tc{
85+
{"", net.IPv4zero, false},
86+
{"", net.ParseIP("127.0.0.1"), false},
87+
{"", net.ParseIP("10.0.1.1"), true},
88+
{"", net.ParseIP("192.168.1.1"), true},
89+
{"", net.ParseIP("8.8.8.8"), false},
90+
{"google.com", net.IPv4zero, false},
91+
}
92+
for _, c := range cases {
93+
assert.Equalf(t, c.expected, isWebhookRequestAllowed(ah, an, c.host, c.ip), "case %s(%v)", c.host, c.ip)
94+
}
95+
96+
ah, an = setting.ParseWebhookAllowedHostList("global")
97+
cases = []tc{
98+
{"", net.IPv4zero, false},
99+
{"", net.ParseIP("127.0.0.1"), false},
100+
{"", net.ParseIP("10.0.1.1"), false},
101+
{"", net.ParseIP("192.168.1.1"), false},
102+
{"", net.ParseIP("8.8.8.8"), true},
103+
{"google.com", net.IPv4zero, false},
104+
}
105+
for _, c := range cases {
106+
assert.Equalf(t, c.expected, isWebhookRequestAllowed(ah, an, c.host, c.ip), "case %s(%v)", c.host, c.ip)
107+
}
108+
109+
ah, an = setting.ParseWebhookAllowedHostList("all")
110+
cases = []tc{
111+
{"", net.IPv4zero, true},
112+
{"", net.ParseIP("127.0.0.1"), true},
113+
{"", net.ParseIP("10.0.1.1"), true},
114+
{"", net.ParseIP("192.168.1.1"), true},
115+
{"", net.ParseIP("8.8.8.8"), true},
116+
{"google.com", net.IPv4zero, true},
117+
}
118+
for _, c := range cases {
119+
assert.Equalf(t, c.expected, isWebhookRequestAllowed(ah, an, c.host, c.ip), "case %s(%v)", c.host, c.ip)
120+
}
121+
}

0 commit comments

Comments
 (0)