Enforce valid URLs

This commit is contained in:
adnano 2020-09-21 22:21:51 -04:00
parent 86e7fe4355
commit 6297d1e3ca
3 changed files with 7 additions and 20 deletions

View File

@ -13,10 +13,6 @@ type Client struct{}
// Request makes a request for the provided URL. The host is inferred from the URL.
func (c *Client) Request(url string) (*Response, error) {
if len(url) > 1024 {
return nil, ErrInvalidURL
}
req, err := NewRequest(url)
if err != nil {
return nil, err
@ -26,10 +22,6 @@ func (c *Client) Request(url string) (*Response, error) {
// ProxyRequest requests the provided URL from the provided host.
func (c *Client) ProxyRequest(host, url string) (*Response, error) {
if len(url) > 1024 {
return nil, ErrInvalidURL
}
req, err := NewProxyRequest(host, url)
if err != nil {
return nil, err

View File

@ -71,11 +71,6 @@ func NewRequest(rawurl string) (*Request, error) {
return nil, err
}
// UserInfo is invalid
if u.User != nil {
return nil, ErrInvalidURL
}
return &Request{
Host: u.Host,
URL: u,
@ -89,11 +84,6 @@ func NewProxyRequest(host, rawurl string) (*Request, error) {
return nil, err
}
// UserInfo is invalid
if u.User != nil {
return nil, ErrInvalidURL
}
return &Request{
Host: host,
URL: u,
@ -102,7 +92,12 @@ func NewProxyRequest(host, rawurl string) (*Request, error) {
// Write writes the Gemini request to the provided io.Writer.
func (r *Request) Write(w io.Writer) error {
request := r.URL.String() + "\r\n"
url := r.URL.String()
// UserInfo is invalid
if r.URL.User != nil || len(url) > 1024 {
return ErrInvalidURL
}
request := url + "\r\n"
_, err := w.Write([]byte(request))
return err
}

View File

@ -52,7 +52,7 @@ func (s *Server) Serve(ln net.Listener) error {
Status: StatusBadRequest,
Meta: "URL exceeds 1024 bytes",
}
} else if url, err := url.Parse(rawurl); err != nil {
} else if url, err := url.Parse(rawurl); err != nil || url.User != nil {
resp = &Response{
Status: StatusBadRequest,
Meta: "Invalid URL",