From 899caf9aaa92aca3f30a47021c5e0756da4f195d Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 22 Nov 2020 20:30:02 +0100 Subject: [PATCH] Add missing error returns --- micropub.go | 25 +++++++++++++++---------- micropubMedia.go | 1 + render.go | 1 + sitemap.go | 1 + 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/micropub.go b/micropub.go index c8fea15..05b88f4 100644 --- a/micropub.go +++ b/micropub.go @@ -116,18 +116,20 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) { var p *post if ct := r.Header.Get(contentType); strings.Contains(ct, contentTypeWWWForm) || strings.Contains(ct, contentTypeMultipartForm) { var err error - r.ParseForm() if strings.Contains(ct, contentTypeMultipartForm) { - err := r.ParseMultipartForm(0) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + err = r.ParseMultipartForm(0) + } else { + err = r.ParseForm() + } + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return } if action := micropubAction(r.Form.Get("action")); action != "" { u, err := url.Parse(r.Form.Get("url")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + return } if action == actionDelete { micropubDelete(w, r, u) @@ -152,6 +154,7 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) { u, err := url.Parse(parsedMfItem.URL) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + return } if parsedMfItem.Action == actionDelete { micropubDelete(w, r, u) @@ -175,6 +178,7 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) { } if !strings.Contains(r.Context().Value("scope").(string), "create") { http.Error(w, "create scope missing", http.StatusForbidden) + return } err := p.create() if err != nil { @@ -412,19 +416,20 @@ func (p *post) computeExtraPostParameters() error { func micropubDelete(w http.ResponseWriter, r *http.Request, u *url.URL) { if !strings.Contains(r.Context().Value("scope").(string), "delete") { http.Error(w, "delete scope missing", http.StatusForbidden) + return } - err := deletePost(u.Path) - if err != nil { + if err := deletePost(u.Path); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) - } else { - w.WriteHeader(http.StatusNoContent) + return } + w.WriteHeader(http.StatusNoContent) return } func micropubUpdate(w http.ResponseWriter, r *http.Request, u *url.URL, mf *microformatItem) { if !strings.Contains(r.Context().Value("scope").(string), "update") { http.Error(w, "update scope missing", http.StatusForbidden) + return } p, err := getPost(u.Path) if err != nil { diff --git a/micropubMedia.go b/micropubMedia.go index bb74071..ad93137 100644 --- a/micropubMedia.go +++ b/micropubMedia.go @@ -23,6 +23,7 @@ const micropubMediaSubPath = "/media" func serveMicropubMedia(w http.ResponseWriter, r *http.Request) { if !strings.Contains(r.Context().Value("scope").(string), "media") { http.Error(w, "media scope missing", http.StatusForbidden) + return } if appConfig.Micropub.MediaStorage == nil { http.Error(w, "Not configured", http.StatusNotImplemented) diff --git a/render.go b/render.go index fd097bf..901088a 100644 --- a/render.go +++ b/render.go @@ -234,6 +234,7 @@ func render(w http.ResponseWriter, template string, data *renderData) { err := templates[template].ExecuteTemplate(&buffer, template, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) + return } // Set content type (needed for minification middleware w.Header().Set(contentType, contentTypeHTMLUTF8) diff --git a/sitemap.go b/sitemap.go index cb8064b..01f8964 100644 --- a/sitemap.go +++ b/sitemap.go @@ -14,6 +14,7 @@ func serveSitemap(w http.ResponseWriter, r *http.Request) { posts, err := getPosts(&postsRequestConfig{}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) + return } sm := sitemap.New() sm.Minify = true