Files
ARTEX/db/retarget_replace_test.go
T
Marco Azarmaneche ba7f8cb292 fix(db): match only whole hosts when retargeting a task
replaceHostCaseInsensitive replaced every case-insensitive occurrence
of the old host. Retargeting 10.0.0.1 to 10.0.0.5 also rewrote
10.0.0.10 to 10.0.0.50 in the goal, open goal nodes and constraints,
and retargeting example.com rewrote notexample.com and
example.com.evil.net. The goal then also counted as replaced, so the
"[Target: ...]" suffix was not added. Whether the goal already names
the new host was a plain substring check too, so a goal naming
10.0.0.10 got no suffix when the new target was 10.0.0.1.

wholeHostMatches keeps a match only when no host byte touches it: no
letter, digit, '-' or '_' on either side, no '.' plus label after it,
no '.' before an IP, and no ':' next to an IPv6 address. A subdomain
such as api.example.com still follows the new host, as before. Both
the replacement and the suffix check (retargetGoal) use it. The new
host is now written literally, so a '$' in it is no longer read as a
regexp group reference.
2026-12-03 16:37:42 +02:00

40 lines
1.7 KiB
Go

package db
import "testing"
func TestReplaceHostCaseInsensitiveMatchesWholeHostOnly(t *testing.T) {
cases := []struct{ text, old, new, want string }{
{"http://10.0.0.1/ and http://10.0.0.10/", "10.0.0.1", "10.0.0.5", "http://10.0.0.5/ and http://10.0.0.10/"},
{"notexample.com and Example.com:8080", "example.com", "new.com", "notexample.com and new.com:8080"},
{"example.com.evil.net", "example.com", "new.com", "example.com.evil.net"},
{"api.example.com, then example.com.", "example.com", "new.com", "api.new.com, then new.com."},
{"1.10.0.0.1 and (10.0.0.1)", "10.0.0.1", "10.0.0.5", "1.10.0.0.1 and (10.0.0.5)"},
{"http://[fe80::1]:80/ and fe80::1:5", "fe80::1", "fe80::2", "http://[fe80::2]:80/ and fe80::1:5"},
{"2001:db8::1", "db8::1", "db8::2", "2001:db8::1"},
{"see example.com", "example.com", "a$1b", "see a$1b"},
}
for _, c := range cases {
got, _ := replaceHostCaseInsensitive(c.text, c.old, c.new)
if got != c.want {
t.Errorf("replace(%q, %q→%q) = %q, want %q", c.text, c.old, c.new, got, c.want)
}
}
if _, ok := replaceHostCaseInsensitive("http://10.0.0.10/", "10.0.0.1", "10.0.0.5"); ok {
t.Errorf("a longer host reported as replaced")
}
}
func TestRetargetGoalSuffixMatchesWholeHostOnly(t *testing.T) {
cases := []struct{ goal, want string }{
{"scan 10.0.0.9", "scan 10.0.0.1"},
{"scan 10.0.0.10", "scan 10.0.0.10 [Target: http://10.0.0.1/]"},
{"scan 10.0.0.1 too", "scan 10.0.0.1 too"},
{"scan the app", "scan the app [Target: http://10.0.0.1/]"},
}
for _, c := range cases {
if got := retargetGoal(c.goal, "10.0.0.9", "10.0.0.1", "http://10.0.0.1/"); got != c.want {
t.Errorf("retargetGoal(%q) = %q, want %q", c.goal, got, c.want)
}
}
}