Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing CanHaveTitleTag Function for MIME Type Validation #1608

Merged
merged 14 commits into from
Mar 23, 2024
Merged
20 changes: 17 additions & 3 deletions common/httpx/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ import (

stringsutil "github.com/projectdiscovery/utils/strings"
"golang.org/x/net/html"
"slices"
)

var (
cutset = "\n\t\v\f\r"
reTitle = regexp.MustCompile(`(?im)<\s*title.*>(.*?)<\s*/\s*title>`)
reContentType = regexp.MustCompile(`(?im)\s*charset="(.*?)"|charset=(.*?)"\s*`)
cutset = "\n\t\v\f\r"
reTitle = regexp.MustCompile(`(?im)<\s*title.*>(.*?)<\s*/\s*title>`)
reContentType = regexp.MustCompile(`(?im)\s*charset="(.*?)"|charset=(.*?)"\s*`)
supportedTitleMimeTypes = []string{
"text/html",
"application/xhtml+xml",
"application/xml",
"application/rss+xml",
"application/atom+xml",
"application/xhtml+xml",
"application/vnd.wap.xhtml+xml",
}
)

// ExtractTitle from a response
Expand All @@ -40,6 +50,10 @@ func ExtractTitle(r *Response) (title string) {
return title
}

func CanHaveTitleTag(mimeType string) bool {
return slices.Contains(supportedTitleMimeTypes, mimeType)
}

func getTitleWithDom(r *Response) (*html.Node, error) {
var title *html.Node
var crawler func(*html.Node)
Expand Down
8 changes: 6 additions & 2 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,12 @@ retry:
builder.WriteRune(']')
}

title := httpx.ExtractTitle(resp)
if scanopts.OutputTitle {
var title string
if httpx.CanHaveTitleTag(resp.GetHeaderPart("Content-Type", ";")) {
title = httpx.ExtractTitle(resp)
}

if scanopts.OutputTitle && title != "" {
builder.WriteString(" [")
if !scanopts.OutputWithNoColor {
builder.WriteString(aurora.Cyan(title).String())
Expand Down
Loading