Skip to content

Commit f8c973c

Browse files
authored
fix: prevent previewing internal network web pages. (#4421)
1 parent 2aaaef7 commit f8c973c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

plugin/httpgetter/html_meta.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httpgetter
33
import (
44
"errors"
55
"io"
6+
"net"
67
"net/http"
78
"net/url"
89

@@ -17,7 +18,7 @@ type HTMLMeta struct {
1718
}
1819

1920
func GetHTMLMeta(urlStr string) (*HTMLMeta, error) {
20-
if _, err := url.Parse(urlStr); err != nil {
21+
if err := validateURL(urlStr); err != nil {
2122
return nil, err
2223
}
2324

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

39+
// TODO: limit the size of the response body
40+
3841
htmlMeta := extractHTMLMeta(response.Body)
3942
return htmlMeta, nil
4043
}
@@ -96,3 +99,25 @@ func extractMetaProperty(token html.Token, prop string) (content string, ok bool
9699
}
97100
return content, ok
98101
}
102+
103+
func validateURL(urlStr string) error {
104+
u, err := url.Parse(urlStr)
105+
if err != nil {
106+
return errors.New("invalid URL format")
107+
}
108+
109+
if u.Scheme != "http" && u.Scheme != "https" {
110+
return errors.New("only http/https protocols are allowed")
111+
}
112+
113+
if host := u.Hostname(); host != "" {
114+
ip := net.ParseIP(host)
115+
if ip != nil {
116+
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() {
117+
return errors.New("internal IP addresses are not allowed")
118+
}
119+
}
120+
}
121+
122+
return nil
123+
}

0 commit comments

Comments
 (0)