From 2747bd581e1bae0a1436fcde984558304c005333 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 16 Mar 2026 14:40:53 +0100 Subject: fix(html): invalid parsing of arguments --- html.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++-------------- html_test.go | 24 +++++++++++++++++++++ 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/html.go b/html.go index 52460ee..463e1f3 100644 --- a/html.go +++ b/html.go @@ -97,33 +97,71 @@ func parseArgs(base string) map[string]string { var key strings.Builder var value strings.Builder var quote bool - for i < len(content) && content[i] != '>' && (i+1 == len(content) || string(content[i:i+2]) != "/>") { + next := true + finish := func() { + quote = false + sep = false + if key.Len() > 0 { + res[key.String()] = value.String() + } + key.Reset() + value.Reset() + } + for i < len(content) && next { curr := content[i] if !sep { switch curr { case '=': sep = true - case ' ': - sep = false - if key.Len() > 0 { - res[key.String()] = "" + case '/': + if i+1 >= len(content) || string(content[i:i+2]) != "/>" { + key.WriteRune(curr) + i++ + continue } - key.Reset() + fallthrough + case '>': + next = false + fallthrough + case ' ': + finish() default: key.WriteRune(curr) } } else { - if value.Len() == 0 && curr == '"' { - quote = true - } else if (curr == '"' && quote) || curr == ' ' && !quote { - quote = false - sep = false - res[key.String()] = value.String() - key.Reset() - value.Reset() - } else { - value.WriteRune(curr) + switch curr { + case '"': + if value.Len() == 0 { + quote = true + i++ + continue + } else if quote { + finish() + i++ + continue + } + case '/': + if i+1 >= len(content) || string(content[i:i+2]) != "/>" { + value.WriteRune(curr) + i++ + continue + } + fallthrough + case '>': + if !quote { + next = false + } + fallthrough + case ' ': + if !quote { + finish() + i++ + continue + } + fallthrough + default: } + value.WriteRune(curr) } i++ } diff --git a/html_test.go b/html_test.go index b7fdf1a..8051b30 100644 --- a/html_test.go +++ b/html_test.go @@ -9,6 +9,9 @@ import ( ) func TestGetHumanFromHTML(t *testing.T) { + if testing.Short() { + t.SkipNow() + } client := http.DefaultClient // using human.json author's website resp, err := client.Get(`https://robida.net/`) @@ -87,6 +90,27 @@ func TestGetURLFromHTML(t *testing.T) { if u.Host != "example.org" { t.Errorf("invalid host: %s", u.Host) } + + realSample := ` + + + + + + ` + u, err = GetURLFromHTML([]byte(realSample), base) + if err != nil { + t.Fatal(err) + } + if u == nil { + t.Fatal("not found") + } + if u.Path != "/human.json" { + t.Errorf("invalid path: %s", u.Path) + } + if u.Host != "example.org" { + t.Errorf("invalid host: %s", u.Host) + } } func TestParseArgs(t *testing.T) { -- cgit v1.2.3