diff options
| author | James Mills <prologic@shortcircuit.net.au> | 2020-03-29 17:41:10 +1000 |
|---|---|---|
| committer | James Mills <prologic@shortcircuit.net.au> | 2020-03-29 17:41:10 +1000 |
| commit | a22a2ae031f2c02b41aa9a7c1746b2989bcf0126 (patch) | |
| tree | 8af65c4e69a6661e7554330f6d0d61c60bdde6f3 /app/app.go | |
| parent | b8aee1d2b5f09ae68f46e00f83eccff2dcf0bb55 (diff) | |
Add maximum upload size saftey and guards
Diffstat (limited to 'app/app.go')
| -rw-r--r-- | app/app.go | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -210,8 +210,7 @@ func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) { ctx := &struct{}{} a.render("upload", w, ctx) } else if r.Method == "POST" { - // TODO: Move to a constant - r.ParseMultipartForm((10 << 20) * 10) // 100MB + r.ParseMultipartForm(a.Config.Server.MaxUploadSize) file, handler, err := r.FormFile("video_file") if err != nil { @@ -414,6 +413,37 @@ func (a *App) importHandler(w http.ResponseWriter, r *http.Request) { } defer os.Remove(uf.Name()) + log.WithField("video_url", videoInfo.VideoURL).Info("requesting video size") + + res, err := http.Head(videoInfo.VideoURL) + if err != nil { + err := fmt.Errorf("error getting size of video %w", err) + log.Error(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + contentLength := utils.SafeParseInt64(res.Header.Get("Content-Length"), -1) + if contentLength == -1 { + err := fmt.Errorf("error calculating size of video") + log.WithField("contentLength", contentLength).Error(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if contentLength > a.Config.Server.MaxUploadSize { + err := fmt.Errorf( + "imported video would exceed maximum upload size of %s", + humanize.Bytes(uint64(a.Config.Server.MaxUploadSize)), + ) + log. + WithField("contentLength", contentLength). + WithField("max_upload_size", a.Config.Server.MaxUploadSize). + Error(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + log.WithField("contentLength", contentLength).Info("downloading video") + if err := utils.Download(videoInfo.VideoURL, uf.Name()); err != nil { err := fmt.Errorf("error downloading video %s: %w", url, err) log.Error(err) |
