Skip to content

Commit

Permalink
improve not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Aug 27, 2023
1 parent 8ec7cec commit 1512997
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fiber

import (
"fmt"
"html"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 /////////////////
//////////////////////////////////////////////
Expand Down

0 comments on commit 1512997

Please sign in to comment.