diff --git a/doc/dev/framework-migration.md b/doc/dev/framework-migration.md index b7008e8..1c0b70b 100644 --- a/doc/dev/framework-migration.md +++ b/doc/dev/framework-migration.md @@ -5,12 +5,12 @@ - To access `/:abc`, use `r.PathValue("abc")`. - Want to associate arbitrary value with a request? Use `r.Context.Value()`. - Don't use `r.Response`. It's `nil`. +- Only use the following HTTP status codes: + - 303 StatusSeeOther: set method to GET + - 307 StatusTemporaryRedirect: method and body not changed + - 308 StatusPermanentRedirect: method and body not changed ## todo -- correct redirect status codes. currently i put in whatever. - - 303 StatusSeeOther: set method to GET - - 307 Temporary: method and body not changed - - 308 Permanent: method and body not changed - add limiter (maybe it should be in nginx) - add caching (maybe it should be in nginx) diff --git a/main.go b/main.go index 20b09bf..85b812f 100644 --- a/main.go +++ b/main.go @@ -226,7 +226,7 @@ func defineRoutes() *mux.Router { // Legacy illust URL router.HandleFunc("/member_illust.php", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/artworks/"+utils.CompatRequest{Request: r}.Query("illust_id"), http.StatusFound) + http.Redirect(w, r, "/artworks/"+utils.CompatRequest{Request: r}.Query("illust_id"), http.StatusPermanentRedirect) }).Methods("GET") // Proxy routes diff --git a/routes/rankingCalendar.go b/routes/rankingCalendar.go index 3e63c97..63051b9 100644 --- a/routes/rankingCalendar.go +++ b/routes/rankingCalendar.go @@ -45,8 +45,7 @@ func RankingCalendarPicker(w http.ResponseWriter, r CompatRequest) error { return utils.RedirectToRoute(w, r,"/rankingCalendar", map[string]string{ "mode": mode, "date": date, - - }, http.StatusFound) + }) } func RankingCalendarPage(w http.ResponseWriter, r CompatRequest) error { diff --git a/routes/tag.go b/routes/tag.go index 3e83431..d05f932 100644 --- a/routes/tag.go +++ b/routes/tag.go @@ -69,5 +69,5 @@ func AdvancedTagPost(w http.ResponseWriter, r CompatRequest) error { "tool": r.Query("tool", r.FormValue("tool")), "scd": r.Query("scd", r.FormValue("scd")), "ecd": r.Query("ecd", r.FormValue("ecd")), - }, http.StatusFound) + }) } diff --git a/semgrep.yml b/semgrep.yml index 7118985..8b3644e 100644 --- a/semgrep.yml +++ b/semgrep.yml @@ -146,3 +146,10 @@ rules: # func $NAME(w http.ResponseWriter, r *http.Request) error { # $...I # } +- id: rule-11 + message: "Use StatusSeeOther or StatusPermanentRedirect" + languages: [go] + severity: WARNING + patterns: + - pattern: | + StatusFound diff --git a/utils/compat.go b/utils/compat.go index e36edb0..d02692c 100644 --- a/utils/compat.go +++ b/utils/compat.go @@ -55,12 +55,12 @@ func SendString(w http.ResponseWriter, text string) error { return err } -func RedirectToRoute(w http.ResponseWriter, r CompatRequest, path string, query_params map[string]string, code int) error { +func RedirectToRoute(w http.ResponseWriter, r CompatRequest, path string, query_params map[string]string) error { query := url.Values{} for k, v := range query_params { query.Add(k, v) } - http.Redirect(w, r.Request, path+query.Encode(), code) + http.Redirect(w, r.Request, path+query.Encode(), http.StatusSeeOther) return nil }