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

View File

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

View File

@ -52,7 +52,7 @@ func (s *Server) Serve(ln net.Listener) error {
Status: StatusBadRequest, Status: StatusBadRequest,
Meta: "URL exceeds 1024 bytes", 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{ resp = &Response{
Status: StatusBadRequest, Status: StatusBadRequest,
Meta: "Invalid URL", Meta: "Invalid URL",