fix(server): seed a goal URL that is followed by punctuation

parseTarget pulls the first URL out of the goal with https?://[^\s'"]+,
which keeps any punctuation that follows it. "Assess http://juiceshop:3000."
matches "http://juiceshop:3000.", url.Parse rejects the port, and the seed
logs "could not parse a target host/IP" even though goalGateCheck accepted
the same goal. A dotted host only survived through the domain fallback,
which takes the scheme from the port, and a sentence-final period stayed
in the host.

Trim trailing punctuation before parsing the URL, including full-width
punctuation from pasted text. A closing bracket is trimmed only when the
URL has no matching opener, so a bracketed IPv6 host like http://[::1]
still parses.
This commit is contained in:
Phantom-IN
2026-11-23 20:09:03 +05:30
parent d235be850e
commit 2efc2aca68
3 changed files with 62 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package server
import "testing"
func TestParseTargetURLFollowedByPunctuation(t *testing.T) {
cases := []struct {
text string
scheme string
host string
port int
}{
{"Assess http://juiceshop:3000, then report.", "http", "juiceshop", 3000},
{"Assess http://juiceshop:3000.", "http", "juiceshop", 3000},
{"Scope: http://juiceshop:3000; nothing else", "http", "juiceshop", 3000},
{"The lab (http://juiceshop:3000) is ours", "http", "juiceshop", 3000},
{"Test https://app.example.com:8080, only the API", "https", "app.example.com", 8080},
{"Target is https://example.com.", "https", "example.com", 443},
{"Start at https://example.com/login?next=/a, then pivot", "https", "example.com", 443},
{"http://juiceshop:3000", "http", "juiceshop", 3000},
{"Assess http://juiceshop.", "http", "juiceshop", 80},
{"Assess http://10.0.0.5.", "http", "10.0.0.5", 80},
{"Assess http://juiceshop:3000。", "http", "juiceshop", 3000},
{"Assess “http://juiceshop:3000”", "http", "juiceshop", 3000},
{"See https://en.wikipedia.org/wiki/Foo_(bar).", "https", "en.wikipedia.org", 443},
{"(https://en.wikipedia.org/wiki/Foo_(bar))", "https", "en.wikipedia.org", 443},
{"http://[::1]", "http", "::1", 80},
{"http://[::1]:8080", "http", "::1", 8080},
{"Assess http://[fd00::10].", "http", "fd00::10", 80},
{"[http://[::1]:8080]", "http", "::1", 8080},
}
for _, c := range cases {
scheme, host, port, ok := parseTarget(c.text)
if !ok || scheme != c.scheme || host != c.host || port != c.port {
t.Errorf("parseTarget(%q) = %q, %q, %d, %v; want %q, %q, %d, true",
c.text, scheme, host, port, ok, c.scheme, c.host, c.port)
}
}
}
+23
View File
@@ -23,6 +23,7 @@ import (
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/archivestore"
@@ -1945,11 +1946,33 @@ var (
reDomain = regexp.MustCompile(`\b((?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(?::(\d{1,5}))?`)
)
// trimURLTail drops sentence punctuation that free text puts after a URL
// ("Assess http://host:3000."). A closing bracket is dropped only when it has no
// opener in the URL, so "http://[::1]" keeps its "]".
func trimURLTail(s string) string {
for s != "" {
r, size := utf8.DecodeLastRuneInString(s)
switch r {
case '.', ',', ';', ':', '!', '?', '。', ',', '、', ';', ':', '!', '?', '”', '’':
case ')', ']', '}', '>':
open := string("([{<"[strings.IndexRune(")]}>", r)])
if strings.Count(s, open) >= strings.Count(s, string(r)) {
return s
}
default:
return s
}
s = s[:len(s)-size]
}
return s
}
// parseTarget extracts a target (scheme, host, port) from free text, supporting
// full URLs, IP[:port] and domain[:port]. ok=false when nothing parseable.
func parseTarget(text string) (scheme, host string, port int, ok bool) {
text = strings.TrimSpace(text)
if m := reURL.FindString(text); m != "" {
m = trimURLTail(m)
if u, err := url.Parse(m); err == nil && u.Hostname() != "" {
scheme = strings.ToLower(u.Scheme)
host = strings.ToLower(u.Hostname())