Browse Source

Validate url format after adding BASE_URL (#97)

printercu 6 years ago
parent
commit
7104622843
2 changed files with 8 additions and 8 deletions
  1. 8 2
      download.go
  2. 0 6
      processing_options.go

+ 8 - 2
download.go

@@ -11,6 +11,7 @@ import (
 	"io"
 	"io/ioutil"
 	"net/http"
+	"net/url"
 	"sync"
 	"time"
 
@@ -29,6 +30,7 @@ var (
 	errSourceDimensionsTooBig      = errors.New("Source image dimensions are too big")
 	errSourceResolutionTooBig      = errors.New("Source image resolution are too big")
 	errSourceImageTypeNotSupported = errors.New("Source image type not supported")
+	errInvalidImageURL             = errors.New("Invalid image url")
 )
 
 var downloadBufPool = sync.Pool{
@@ -134,9 +136,13 @@ func readAndCheckImage(ctx context.Context, res *http.Response) (context.Context
 }
 
 func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, error) {
-	url := fmt.Sprintf("%s%s", conf.BaseURL, getImageURL(ctx))
+	imageURL := fmt.Sprintf("%s%s", conf.BaseURL, getImageURL(ctx))
 
-	res, err := downloadClient.Get(url)
+	if _, urlErr := url.ParseRequestURI(imageURL); urlErr != nil {
+		return ctx, func() {}, errInvalidImageURL
+	}
+
+	res, err := downloadClient.Get(imageURL)
 	if err != nil {
 		return ctx, func() {}, err
 	}

+ 0 - 6
processing_options.go

@@ -11,7 +11,6 @@ import (
 	"encoding/base64"
 	"errors"
 	"fmt"
-	"net/url"
 	"regexp"
 	"strconv"
 	"strings"
@@ -111,7 +110,6 @@ const (
 var (
 	errInvalidURLEncoding                 = errors.New("Invalid url encoding")
 	errInvalidPath                        = errors.New("Invalid path")
-	errInvalidImageURL                    = errors.New("Invalid image url")
 	errResultingImageFormatIsNotSupported = errors.New("Resulting image format is not supported")
 )
 
@@ -656,10 +654,6 @@ func parsePath(ctx context.Context, rctx *fasthttp.RequestCtx) (context.Context,
 		return ctx, err
 	}
 
-	if _, err = url.ParseRequestURI(imageURL); err != nil {
-		return ctx, errInvalidImageURL
-	}
-
 	ctx = context.WithValue(ctx, imageURLCtxKey, imageURL)
 	ctx = context.WithValue(ctx, processingOptionsCtxKey, po)