Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choer(Redirect): change redirect content #4049

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.`)
}


c.Render(-1, render.Redirect{
Code: code,
Location: location,
Expand Down
1 change: 1 addition & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,7 @@ func TestContextRenderRedirectAll(t *testing.T) {
assert.Panics(t, func() { c.Redirect(309, "/resource") })
assert.NotPanics(t, func() { c.Redirect(http.StatusMultipleChoices, "/resource") })
assert.NotPanics(t, func() { c.Redirect(http.StatusPermanentRedirect, "/resource") })
assert.NotPanics(t, func() { c.Redirect(http.StatusTemporaryRedirect, "")})
}

func TestContextNegotiationWithJSON(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions docs/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,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/")
Expand Down
Loading