0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

allow setting max height and width

This is primarily added to prevent a denial of service attack where
insanely large images are requested, eating up CPU.
This commit is contained in:
Will Norris 2013-12-06 14:17:39 -08:00
parent 8310f6eb79
commit 036d0c51c4
2 changed files with 12 additions and 0 deletions

View file

@ -21,6 +21,8 @@ func main() {
p := proxy.NewProxy(nil)
p.Cache = cache.NewMemoryCache()
p.MaxWidth = 2000
p.MaxHeight = 2000
if *whitelist != "" {
p.Whitelist = strings.Split(*whitelist, ",")
}

View file

@ -69,6 +69,9 @@ type Proxy struct {
// Whitelist specifies a list of remote hosts that images can be proxied from. An empty list means all hosts are allowed.
Whitelist []string
MaxWidth int
MaxHeight int
}
// NewProxy constructs a new proxy. The provided http Client will be used to
@ -88,6 +91,13 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if p.MaxWidth > 0 && req.Options.Width > p.MaxWidth {
req.Options.Width = p.MaxWidth
}
if p.MaxHeight > 0 && req.Options.Height > p.MaxHeight {
req.Options.Height = p.MaxHeight
}
u := req.URL.String()
glog.Infof("request for image: %v", u)