improve not found error

pull/2605/head
René Werner 2023-08-27 12:35:30 +02:00
parent 8ec7cec435
commit 1512997235
2 changed files with 36 additions and 1 deletions

View File

@ -6,6 +6,7 @@ package fiber
import (
"fmt"
"html"
"sort"
"strconv"
"strings"
@ -147,7 +148,7 @@ func (app *App) next(c *Ctx) (bool, error) {
}
// If c.Next() does not match, return 404
err := NewError(StatusNotFound, "Cannot "+c.method+" "+c.pathOriginal)
err := NewError(StatusNotFound, "Cannot "+c.method+" "+html.EscapeString(c.pathOriginal))
if !c.matched && app.methodExist(c) {
// If no match, scan stack again if other methods match the request
// Moved from app.handler because middleware may break the route chain

View File

@ -473,6 +473,40 @@ func Test_Route_Static_HasPrefix(t *testing.T) {
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
}
func Test_Router_NotFound(t *testing.T) {
app := New()
app.Use(func(c *Ctx) error {
return c.Next()
})
appHandler := app.Handler()
c := &fasthttp.RequestCtx{}
c.Request.Header.SetMethod("DELETE")
c.URI().SetPath("/this/route/does/not/exist")
appHandler(c)
utils.AssertEqual(t, 404, c.Response.StatusCode())
utils.AssertEqual(t, "Cannot DELETE /this/route/does/not/exist", string(c.Response.Body()))
}
func Test_Router_NotFound_HTML_Inject(t *testing.T) {
app := New()
app.Use(func(c *Ctx) error {
return c.Next()
})
appHandler := app.Handler()
c := &fasthttp.RequestCtx{}
c.Request.Header.SetMethod("DELETE")
c.URI().SetPath("/does/not/exist<script>alert('foo');</script>")
appHandler(c)
utils.AssertEqual(t, 404, c.Response.StatusCode())
utils.AssertEqual(t, "Cannot DELETE /does/not/exist&lt;script&gt;alert(&#39;foo&#39;);&lt;/script&gt;", string(c.Response.Body()))
}
//////////////////////////////////////////////
///////////////// BENCHMARKS /////////////////
//////////////////////////////////////////////