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

fix: Prevent previewing internal network web pages. #4421

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion plugin/httpgetter/html_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpgetter
import (
"errors"
"io"
"net"
"net/http"
"net/url"

Expand All @@ -17,7 +18,7 @@ type HTMLMeta struct {
}

func GetHTMLMeta(urlStr string) (*HTMLMeta, error) {
if _, err := url.Parse(urlStr); err != nil {
if err := validateURL(urlStr); err != nil {
return nil, err
}

Expand All @@ -35,6 +36,8 @@ func GetHTMLMeta(urlStr string) (*HTMLMeta, error) {
return nil, errors.New("not a HTML page")
}

// TODO: limit the size of the response body

htmlMeta := extractHTMLMeta(response.Body)
return htmlMeta, nil
}
Expand Down Expand Up @@ -96,3 +99,25 @@ func extractMetaProperty(token html.Token, prop string) (content string, ok bool
}
return content, ok
}

func validateURL(urlStr string) error {
u, err := url.Parse(urlStr)
if err != nil {
return errors.New("invalid URL format")
}

if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("only http/https protocols are allowed")
}

if host := u.Hostname(); host != "" {
ip := net.ParseIP(host)
if ip != nil {
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() {
return errors.New("internal IP addresses are not allowed")
}
}
}

return nil
}