Skip to content

Commit

Permalink
fix(middleware/csrf): update refererMatchesHost()
Browse files Browse the repository at this point in the history
  • Loading branch information
sixcolors committed Mar 25, 2024
1 parent 54eae2a commit ec6c81d
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions middleware/csrf/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/url"
"reflect"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -220,7 +221,7 @@ func isCsrfFromCookie(extractor interface{}) bool {
// returns an error if the referer header is not present or is invalid
// returns nil if the referer header is valid
func refererMatchesHost(c *fiber.Ctx) error {
referer := c.Get(fiber.HeaderReferer)
referer := strings.ToLower(c.Get(fiber.HeaderReferer))
if referer == "" {
return ErrNoReferer
}
Expand All @@ -230,9 +231,9 @@ func refererMatchesHost(c *fiber.Ctx) error {
return ErrBadReferer
}

if refererURL.Scheme+"://"+refererURL.Host != c.Protocol()+"://"+c.Hostname() {
return ErrBadReferer
if refererURL.Scheme == c.Protocol() && refererURL.Host == c.Hostname() {
return nil
}

return nil
return ErrBadReferer
}

0 comments on commit ec6c81d

Please sign in to comment.