Skip to content

Commit

Permalink
attack: support IPv6 only in DNSCaching
Browse files Browse the repository at this point in the history
The previous code had a bug that assumed we could always find an IPv4
first.

Fixes #649
  • Loading branch information
tsenart committed Oct 2, 2023
1 parent e0155bd commit 6fbe391
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,14 @@ func DNSCaching(ttl time.Duration) func(*Attacker) {
for i := 0; i < len(ips) && j < 2; i++ {
ip := net.ParseIP(ips[i])
switch {
case len(ip.To4()) == net.IPv4len && j == 0:
case len(ip) == net.IPv4len && (j == 0 || len(ips[j-1]) == net.IPv6len):
fallthrough
case len(ip) == net.IPv6len && j == 1:
case len(ip) == net.IPv6len && (j == 0 || len(ips[j-1]) == net.IPv4len):
ips[j] = ips[i]
j++
}
}

ips = ips[:j]

type result struct {
Expand Down
13 changes: 13 additions & 0 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,16 @@ func TestVegetaHeaders(t *testing.T) {
}
}
}

// https://github.com/tsenart/vegeta/issues/649
func TestDNSCaching_Issue649(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Fatalf("panic: %v", err)
}
}()

tr := NewStaticTargeter(Target{Method: "GET", URL: "https://[2a00:1450:4005:802::200e]"})
atk := NewAttacker(DNSCaching(0))
_ = atk.hit(tr, &attack{name: "TEST", began: time.Now()})
}

0 comments on commit 6fbe391

Please sign in to comment.