From c99d328d500ac8e537add7ceefa780bc66bf01f0 Mon Sep 17 00:00:00 2001 From: Ghost Date: Sun, 7 Jul 2024 23:39:46 +0800 Subject: [PATCH] chore: add Debugging location is "" redirect to "/" --- context.go | 7 +++++++ docs/doc.md | 2 ++ 2 files changed, 9 insertions(+) diff --git a/context.go b/context.go index baa4b0f9..396f8ac6 100644 --- a/context.go +++ b/context.go @@ -1066,7 +1066,14 @@ func (c *Context) String(code int, format string, values ...any) { } // Redirect returns an HTTP redirect to the specific location. +// When the 'location' parameter is empty, it poses a potential security risk. +// Avoid bringing potential security risks into the production environment. func (c *Context) Redirect(code int, location string) { + if IsDebugging() && location == "" { + debugPrint(`[WARNING] When the 'location' parameter is empty, it poses a potential security risk. Please input a secure redirection URL to ensure safe operation.`) + location = "/" + } + c.Render(-1, render.Redirect{ Code: code, Location: location, diff --git a/docs/doc.md b/docs/doc.md index 51366409..6cfc3fb3 100644 --- a/docs/doc.md +++ b/docs/doc.md @@ -1442,6 +1442,8 @@ Gin allow by default use only one html.Template. Check [a multitemplate render]( Issuing a HTTP redirect is easy. Both internal and external locations are supported. +Note: When the location is empty, there is a security risk. Please do not bring it to production + ```go r.GET("/test", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")