|
5 | 5 | package migrations
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "net" |
8 | 9 | "path/filepath"
|
9 | 10 | "testing"
|
10 | 11 |
|
@@ -74,3 +75,42 @@ func TestMigrateWhiteBlocklist(t *testing.T) {
|
74 | 75 |
|
75 | 76 | setting.ImportLocalPaths = old
|
76 | 77 | }
|
| 78 | + |
| 79 | +func TestAllowBlockList(t *testing.T) { |
| 80 | + init := func(allow, block string, local bool) { |
| 81 | + setting.Migrations.AllowedDomains = allow |
| 82 | + setting.Migrations.BlockedDomains = block |
| 83 | + setting.Migrations.AllowLocalNetworks = local |
| 84 | + assert.NoError(t, Init()) |
| 85 | + } |
| 86 | + |
| 87 | + // default, allow all external, block none, no local networks |
| 88 | + init("", "", false) |
| 89 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 90 | + assert.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")})) |
| 91 | + |
| 92 | + // allow all including local networks (it could lead to SSRF in production) |
| 93 | + init("", "", true) |
| 94 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 95 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")})) |
| 96 | + |
| 97 | + // allow wildcard, block some subdomains. if the domain name is allowed, then the local network check is skipped |
| 98 | + init("*.domain.com", "blocked.domain.com", false) |
| 99 | + assert.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 100 | + assert.NoError(t, checkByAllowBlockList("sub.domain.com", []net.IP{net.ParseIP("127.0.0.1")})) |
| 101 | + assert.Error(t, checkByAllowBlockList("blocked.domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 102 | + assert.Error(t, checkByAllowBlockList("sub.other.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 103 | + |
| 104 | + // allow wildcard (it could lead to SSRF in production) |
| 105 | + init("*", "", false) |
| 106 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 107 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")})) |
| 108 | + |
| 109 | + // local network can still be blocked |
| 110 | + init("*", "127.0.0.*", false) |
| 111 | + assert.NoError(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("1.2.3.4")})) |
| 112 | + assert.Error(t, checkByAllowBlockList("domain.com", []net.IP{net.ParseIP("127.0.0.1")})) |
| 113 | + |
| 114 | + // reset |
| 115 | + init("", "", false) |
| 116 | +} |
0 commit comments