mirror of
https://github.com/lunchcat/sif.git
synced 2026-07-28 14:37:01 -07:00
fix(scan): decode html entities in probe title (#354)
the probe title field is documented as an httpx-style page title, but extractTitle returned the raw regex capture, so a title like "Tom & Jerry" was reported verbatim instead of "Tom & Jerry". run it through html.UnescapeString before trimming so the reported title matches the rendered page.
This commit is contained in:
@@ -15,6 +15,7 @@ package scan
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -133,13 +134,14 @@ func Probe(targetURL string, timeout time.Duration, logdir string) (*ProbeResult
|
||||
}
|
||||
|
||||
// extractTitle returns the trimmed text of the first <title> in body, or "" when
|
||||
// there isn't one.
|
||||
// there isn't one. html entities are decoded so the title matches the rendered
|
||||
// page rather than carrying raw "&"-style markup.
|
||||
func extractTitle(body []byte) string {
|
||||
m := titleRe.FindSubmatch(body)
|
||||
if len(m) < 2 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(m[1]))
|
||||
return strings.TrimSpace(html.UnescapeString(string(m[1])))
|
||||
}
|
||||
|
||||
// ResultType identifies probe results for the result registry.
|
||||
|
||||
@@ -108,6 +108,7 @@ func TestProbe_ExtractTitle(t *testing.T) {
|
||||
{"trimmed", "<title> spaced </title>", "spaced"},
|
||||
{"attrs", `<title lang="en">attr</title>`, "attr"},
|
||||
{"multiline", "<title>line one\nline two</title>", "line one\nline two"},
|
||||
{"entities", "<title>Tom & Jerry – Home</title>", "Tom & Jerry – Home"},
|
||||
{"none", "<html><body>no title</body></html>", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user