policy: keep capability destinations out of the exit test

An app grant carries no traffic, so it offers no exit routes, as on SaaS
(via-grant-v101..v104).
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 17:23:53 +01:00
1 parent 1fa8061e2c
commit f924bf2527
9 files changed
+81253 -15

No files matched your search

+1 -1
View File
@@ -6,5 +6,5 @@ package matcher
// wider. Callers that need strict validation should pre-validate
// their inputs via [util.ParseIPSet].
func MatchFromStrings(sources, destinations []string) Match {
return newMatch(buildIPSet(sources), buildIPSet(destinations))
return newMatch(buildIPSet(sources), buildIPSet(destinations), buildIPSet(nil))
}
+34 -13
View File
@@ -16,14 +16,28 @@ type Match struct {
srcRanges []netipx.IPRange
destRanges []netipx.IPRange
// Kept apart because only port destinations carry traffic to an exit
// node; a capability to the internet changes who the exit node lists.
internet bool
capInternet bool
}
func newMatch(srcs, dests *netipx.IPSet) Match {
// newMatch unions the port and capability destinations into one set.
func newMatch(srcs, ports, caps *netipx.IPSet) Match {
var b netipx.IPSetBuilder
b.AddSet(ports)
b.AddSet(caps)
dests, _ := b.IPSet()
return Match{
srcs: srcs,
dests: dests,
srcRanges: srcs.Ranges(),
destRanges: dests.Ranges(),
srcs: srcs,
dests: dests,
srcRanges: srcs.Ranges(),
destRanges: dests.Ranges(),
internet: util.HasInternetDestination(ports),
capInternet: util.HasInternetDestination(caps),
}
}
@@ -70,22 +84,23 @@ func MatchFromFilterRule(rule tailcfg.FilterRule) Match {
// are already parsed as srcs, see [ParseSrcs]: rules sharing one source
// list, such as a via grant's on every router, parse it once.
func MatchFromFilterRuleSrcs(rule tailcfg.FilterRule, srcs *netipx.IPSet) Match {
dests := new(netipx.IPSetBuilder)
var ports, caps netipx.IPSetBuilder
for _, dp := range rule.DstPorts {
set, _ := util.ParseIPSet(dp.IP, nil)
dests.AddSet(set)
ports.AddSet(set)
}
for _, cg := range rule.CapGrant {
for _, pref := range cg.Dsts {
dests.AddPrefix(pref)
caps.AddPrefix(pref)
}
}
destsSet, _ := dests.IPSet()
portsSet, _ := ports.IPSet()
capsSet, _ := caps.IPSet()
return newMatch(srcs, destsSet)
return newMatch(srcs, portsSet, capsSet)
}
// ParseSrcs parses SrcIPs as [MatchFromFilterRule] does.
@@ -163,8 +178,14 @@ func overlaps(ranges []netipx.IPRange, r netipx.IPRange) bool {
return i < len(ranges) && ranges[i].Overlaps(r)
}
// DestsIsTheInternet reports whether any destination is in [util.TheInternet].
// It lets clients select exit nodes for that traffic.
// DestsIsTheInternet reports whether any port destination is in
// [util.TheInternet]. It lets clients select exit nodes for that traffic.
func (m *Match) DestsIsTheInternet() bool {
return util.HasInternetDestination(m.dests)
return m.internet
}
// CapDestsIsTheInternet reports whether any capability destination is in
// [util.TheInternet].
func (m *Match) CapDestsIsTheInternet() bool {
return m.capInternet
}
+19
View File
@@ -575,6 +575,25 @@ func TestDestsIsTheInternet(t *testing.T) {
}
}
// A capability grant carries no traffic, so a public capability
// destination must not select exit nodes; SaaS shows no exit node to the
// grant's sources (via-grant-v100).
func TestCapGrantToPublicIP(t *testing.T) {
t.Parallel()
m := MatchFromFilterRule(tailcfg.FilterRule{
SrcIPs: []string{"100.64.0.1/32"},
CapGrant: []tailcfg.CapGrant{{
Dsts: []netip.Prefix{netip.MustParsePrefix("8.8.8.8/32")},
CapMap: tailcfg.PeerCapMap{"example.com/cap/review": nil},
}},
})
assert.True(t, m.DestsContainsIP(netip.MustParseAddr("8.8.8.8")))
assert.False(t, m.DestsIsTheInternet())
assert.True(t, m.CapDestsIsTheInternet())
}
func TestDebugString(t *testing.T) {
t.Parallel()
+6 -1
View File
@@ -1891,8 +1891,13 @@ func isPreferredViaPrimary(
// to dsts, reach the internet by the test the grant's filter rule gets
// ([matcher.Match.DestsIsTheInternet]). Neither the wildcard nor
// autogroup:internet resolves to the internet here, so check those
// aliases themselves.
// aliases themselves. That test reads port destinations only, so an
// app-only grant never reaches the internet.
func grantReachesInternet(grant Grant, dsts []netip.Prefix) bool {
if len(grant.InternetProtocols) == 0 && len(grant.App) > 0 {
return false
}
internetAlias := slices.ContainsFunc(grant.Destinations, func(d Alias) bool {
if ag, ok := d.(*AutoGroup); ok {
return ag.Is(AutoGroupInternet)
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+3
View File
@@ -95,6 +95,9 @@ var viaCompatTests = []struct {
{"via-grant-v96", "one member in two via exit grants, plus a rule to a private subnet"},
{"via-grant-v97", "one member in two via exit grants, plus a rule to everything"},
{"via-grant-v98", "via exit steering plus a capability grant to *"},
{"via-grant-v101", "approved exit nodes, only a member capability grant to a private address"},
{"via-grant-v103", "unapproved exit nodes, only a member capability grant to a public address"},
{"via-grant-v104", "approved exit nodes, only a member capability grant to the wildcard"},
{"via-grant-v105", "a grant and an ACL with the same sources but different IPProto"},
{"via-grant-v106", "rule order across grants sharing sources"},
{"via-grant-v107", "two via grants from one source through one router"},