mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-06-26 09:20:14 +02:00
Merge pull request #750 from strukturag/throttle-ipv6
Throttle /64 subnets for IPv6.
This commit is contained in:
commit
6c62f9caae
72
hub_test.go
72
hub_test.go
|
@ -3622,6 +3622,14 @@ func TestGetRealUserIP(t *testing.T) {
|
|||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-real-ip"): []string{"2002:db8::1"},
|
||||
},
|
||||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"11.12.13.14",
|
||||
http.Header{
|
||||
|
@ -3630,6 +3638,54 @@ func TestGetRealUserIP(t *testing.T) {
|
|||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"10.11.12.13",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-real-ip"): []string{"10.11.12.13"},
|
||||
},
|
||||
"2001:db8::/48",
|
||||
"[2001:db8::1]:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-real-ip"): []string{"2002:db8::1"},
|
||||
},
|
||||
"2001:db8::/48",
|
||||
"[2001:db8::1]:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"2002:db8::1, 192.168.30.32"},
|
||||
},
|
||||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"2002:db8::1, 2001:db8::1"},
|
||||
},
|
||||
"192.168.0.0/16, 2001:db8::/48",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"2002:db8::1, 192.168.30.32"},
|
||||
},
|
||||
"192.168.0.0/16, 2001:db8::/48",
|
||||
"[2001:db8::1]:23456",
|
||||
},
|
||||
{
|
||||
"2002:db8::1",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"2002:db8::1, 2001:db8::2"},
|
||||
},
|
||||
"2001:db8::/48",
|
||||
"[2001:db8::1]:23456",
|
||||
},
|
||||
// "X-Real-IP" has preference before "X-Forwarded-For"
|
||||
{
|
||||
"10.11.12.13",
|
||||
|
@ -3649,6 +3705,22 @@ func TestGetRealUserIP(t *testing.T) {
|
|||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"11.12.13.14",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"1.2.3.4", "11.12.13.14", "192.168.30.32"},
|
||||
},
|
||||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
{
|
||||
"11.12.13.14",
|
||||
http.Header{
|
||||
http.CanonicalHeaderKey("x-forwarded-for"): []string{"1.2.3.4", "2.3.4.5", "11.12.13.14", "192.168.31.32", "192.168.30.32"},
|
||||
},
|
||||
"192.168.0.0/16",
|
||||
"192.168.1.2:23456",
|
||||
},
|
||||
// Headers are ignored if coming from untrusted clients.
|
||||
{
|
||||
"10.11.12.13",
|
||||
|
|
34
throttle.go
34
throttle.go
|
@ -25,6 +25,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -53,6 +54,9 @@ const (
|
|||
|
||||
var (
|
||||
ErrBruteforceDetected = errors.New("bruteforce detected")
|
||||
|
||||
// subnet64 is the /64 subnet for IPv6 addresses.
|
||||
subnet64 = net.CIDRMask(64, 128)
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -67,6 +71,21 @@ type Throttler interface {
|
|||
CheckBruteforce(ctx context.Context, client string, action string) (ThrottleFunc, error)
|
||||
}
|
||||
|
||||
func getThrottleIp(ipString string) string {
|
||||
ip := net.ParseIP(ipString)
|
||||
// Throttle full IPv4 address.
|
||||
if l := len(ip); l == 0 || l == net.IPv4len {
|
||||
return ipString
|
||||
}
|
||||
|
||||
if i := ip.To4(); len(i) == net.IPv4len {
|
||||
return ipString
|
||||
}
|
||||
|
||||
// Throttle /64 subnet of IPv6 addresses.
|
||||
return ip.Mask(subnet64).String()
|
||||
}
|
||||
|
||||
type throttleEntry struct {
|
||||
ts time.Time
|
||||
}
|
||||
|
@ -110,7 +129,8 @@ func (t *memoryThrottler) getEntries(client string, action string) []throttleEnt
|
|||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
|
||||
actions := t.clients[client]
|
||||
toThrottle := getThrottleIp(client)
|
||||
actions := t.clients[toThrottle]
|
||||
if len(actions) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -123,14 +143,15 @@ func (t *memoryThrottler) setEntries(client string, action string, entries []thr
|
|||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
actions := t.clients[client]
|
||||
toThrottle := getThrottleIp(client)
|
||||
actions := t.clients[toThrottle]
|
||||
if len(actions) == 0 {
|
||||
if len(entries) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
actions = make(map[string][]throttleEntry)
|
||||
t.clients[client] = actions
|
||||
t.clients[toThrottle] = actions
|
||||
}
|
||||
|
||||
if len(entries) > 0 {
|
||||
|
@ -138,7 +159,7 @@ func (t *memoryThrottler) setEntries(client string, action string, entries []thr
|
|||
} else {
|
||||
delete(actions, action)
|
||||
if len(actions) == 0 {
|
||||
delete(t.clients, client)
|
||||
delete(t.clients, toThrottle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -147,9 +168,10 @@ func (t *memoryThrottler) addEntry(client string, action string, entry throttleE
|
|||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
actions, found := t.clients[client]
|
||||
toThrottle := getThrottleIp(client)
|
||||
actions, found := t.clients[toThrottle]
|
||||
if !found {
|
||||
t.clients[client] = map[string][]throttleEntry{
|
||||
t.clients[toThrottle] = map[string][]throttleEntry{
|
||||
action: {
|
||||
entry,
|
||||
},
|
||||
|
|
|
@ -102,6 +102,52 @@ func TestThrottler(t *testing.T) {
|
|||
throttle4(ctx)
|
||||
}
|
||||
|
||||
func TestThrottlerIPv6(t *testing.T) {
|
||||
timing := &throttlerTiming{
|
||||
t: t,
|
||||
now: time.Now(),
|
||||
}
|
||||
th := newMemoryThrottlerForTest(t)
|
||||
th.getNow = timing.getNow
|
||||
th.doDelay = timing.doDelay
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Make sure full /64 subnets are throttled for IPv6.
|
||||
throttle1, err := th.CheckBruteforce(ctx, "2001:db8:abcd:0012::1", "action1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
timing.expectedSleep = 100 * time.Millisecond
|
||||
throttle1(ctx)
|
||||
|
||||
timing.now = timing.now.Add(time.Millisecond)
|
||||
throttle2, err := th.CheckBruteforce(ctx, "2001:db8:abcd:0012::2", "action1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
timing.expectedSleep = 200 * time.Millisecond
|
||||
throttle2(ctx)
|
||||
|
||||
// A diffent /64 subnet is not throttled yet.
|
||||
timing.now = timing.now.Add(time.Millisecond)
|
||||
throttle3, err := th.CheckBruteforce(ctx, "2001:db8:abcd:0013::1", "action1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
timing.expectedSleep = 100 * time.Millisecond
|
||||
throttle3(ctx)
|
||||
|
||||
// A different action is not throttled.
|
||||
timing.now = timing.now.Add(time.Millisecond)
|
||||
throttle4, err := th.CheckBruteforce(ctx, "2001:db8:abcd:0012::1", "action2")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
timing.expectedSleep = 100 * time.Millisecond
|
||||
throttle4(ctx)
|
||||
}
|
||||
|
||||
func TestThrottler_Bruteforce(t *testing.T) {
|
||||
timing := &throttlerTiming{
|
||||
t: t,
|
||||
|
|
Loading…
Reference in a new issue