Skip to content

Commit

Permalink
fix review comments/hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Feb 27, 2024
1 parent dd198e4 commit 639f552
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 30 deletions.
4 changes: 2 additions & 2 deletions docs/api/middleware/encryptcookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ var ConfigDefault = Config{
```

## Usage With Other Middlewares That Reads Or Modify Cookies
Place the encryptcookie middleware before any other middleware that reads or modifies cookies. For example, if you are using the CSRF middleware, ensure that the encryptcookie middleware is placed before it. Failure to do so may prevent the CSRF middleware from reading the encrypted cookie.
Place the `encryptcookie` middleware before any other middleware that reads or modifies cookies. For example, if you are using the CSRF middleware, ensure that the `encryptcookie` middleware is placed before it. Failure to do so may prevent the CSRF middleware from reading the encrypted cookie.

You may also choose to exclude certain cookies from encryption. For instance, if you are using the CSRF middleware with a frontend framework like Angular, and the framework reads the token from a cookie, you should exclude that cookie from encryption. This can be achieved by adding the cookie name to the Except array in the configuration:
You may also choose to exclude certain cookies from encryption. For instance, if you are using the `CSRF` middleware with a frontend framework like Angular, and the framework reads the token from a cookie, you should exclude that cookie from encryption. This can be achieved by adding the cookie name to the Except array in the configuration:

```go
app.Use(encryptcookie.New(encryptcookie.Config{
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/gofiber/fiber/v3
go 1.20

require (
github.com/gofiber/fiber/v2 v2.52.0
github.com/gofiber/utils/v2 v2.0.0-beta.3
github.com/google/uuid v1.6.0
github.com/mattn/go-colorable v0.1.13
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/utils/v2 v2.0.0-beta.3 h1:pfOhUDDVjBJpkWv6C5jaDyYLvpui7zQ97zpyFFsUOKw=
github.com/gofiber/utils/v2 v2.0.0-beta.3/go.mod h1:jsl17+MsKfwJjM3ONCE9Rzji/j8XNbwjhUVTjzgfDCo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
9 changes: 9 additions & 0 deletions log/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (l *defaultLogger) privateLog(lv Level, fmtArgs []any) {
buf.WriteString(fmt.Sprint(fmtArgs...))

_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
Expand All @@ -54,6 +57,9 @@ func (l *defaultLogger) privateLogf(lv Level, format string, fmtArgs []any) {
_, _ = fmt.Fprint(buf, fmtArgs...)
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
Expand Down Expand Up @@ -92,6 +98,9 @@ func (l *defaultLogger) privateLogw(lv Level, format string, keysAndValues []any
}

_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
Expand Down
78 changes: 59 additions & 19 deletions log/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ func (w *byteSliceWriter) Write(p []byte) (int, error) {
return len(p), nil
}

func Test_WithContextCaller(t *testing.T) {
logger = &defaultLogger{
stdlog: log.New(os.Stderr, "", log.Lshortfile),
depth: 4,
}

var w byteSliceWriter
SetOutput(&w)
ctx := context.TODO()

WithContext(ctx).Info("")
Info("")

require.Equal(t, "default_test.go:41: [Info] \ndefault_test.go:42: [Info] \n", string(w.b))
}

func Test_DefaultLogger(t *testing.T) {
initDefaultLogger()

Expand All @@ -39,7 +55,21 @@ func Test_DefaultLogger(t *testing.T) {
Info("starting work")
Warn("work may fail")
Error("work failed")
Panic("work panic")

didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
Panic("work panic")
}()

if !didPanic {
t.Errorf("Expected a panic when the panic logger method is called")
}

require.Equal(t, "[Trace] trace work\n"+
"[Debug] received work order\n"+
"[Info] starting work\n"+
Expand All @@ -59,7 +89,20 @@ func Test_DefaultFormatLogger(t *testing.T) {
Infof("starting %s", work)
Warnf("%s may fail", work)
Errorf("%s failed", work)
Panicf("%s panic", work)

didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
Panicf("%s panic", work)
}()

if !didPanic {
t.Errorf("Expected a panic when the panic logger method is called")
}

require.Equal(t, "[Trace] trace work\n"+
"[Debug] received work order\n"+
Expand All @@ -82,7 +125,20 @@ func Test_CtxLogger(t *testing.T) {
WithContext(ctx).Infof("starting %s", work)
WithContext(ctx).Warnf("%s may fail", work)
WithContext(ctx).Errorf("%s failed %d", work, 50)
WithContext(ctx).Panicf("%s panic", work)

didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
WithContext(ctx).Panicf("%s panic", work)
}()

if !didPanic {
t.Errorf("Expected a panic when the panic logger method is called")
}

require.Equal(t, "[Trace] trace work\n"+
"[Debug] received work order\n"+
Expand Down Expand Up @@ -210,22 +266,6 @@ func BenchmarkLogfKeyAndValues(b *testing.B) {
}
}

func Test_WithContextCaller(t *testing.T) {
logger = &defaultLogger{
stdlog: log.New(os.Stderr, "", log.Lshortfile),
depth: 4,
}

var w byteSliceWriter
SetOutput(&w)
ctx := context.TODO()

WithContext(ctx).Info("")
Info("")

require.Equal(t, "default_test.go:223: [Info] \ndefault_test.go:224: [Info] \n", string(w.b))
}

func Test_SetLevel(t *testing.T) {
setLogger := &defaultLogger{
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
Expand Down
5 changes: 2 additions & 3 deletions middleware/cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func New(config ...Config) fiber.Handler {

// Validate CORS credentials configuration
if cfg.AllowCredentials && cfg.AllowOrigins == "*" {
panic("[CORS] Insecure setup, 'AllowCredentials' is set to true, and 'AllowOrigins' is set to a wildcard.")
log.Panic("[CORS] Insecure setup, 'AllowCredentials' is set to true, and 'AllowOrigins' is set to a wildcard.") //nolint:revive // we want to exit the program
}

// Validate and normalize static AllowOrigins if not using AllowOriginsFunc
Expand All @@ -121,8 +121,7 @@ func New(config ...Config) fiber.Handler {
if isValid {
validatedOrigins = append(validatedOrigins, normalizedOrigin)
} else {
log.Warnf("[CORS] Invalid origin format in configuration: %s", origin)
panic("[CORS] Invalid origin provided in configuration")
log.Panicf("[CORS] Invalid origin format in configuration: %s", origin) //nolint:revive // we want to exit the program
}
}
cfg.AllowOrigins = strings.Join(validatedOrigins, ",")
Expand Down
7 changes: 4 additions & 3 deletions middleware/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package healthcheck

import (
"github.com/gofiber/fiber/v2/utils"
"strings"

"github.com/gofiber/fiber/v3"
)

Expand Down Expand Up @@ -41,12 +42,12 @@ func New(config ...Config) fiber.Handler {
return c.Next()
}

prefixCount := len(utils.TrimRight(c.Route().Path, '/'))
prefixCount := len(strings.TrimRight(c.Route().Path, "/"))
if len(c.Path()) >= prefixCount {
checkPath := c.Path()[prefixCount:]
checkPathTrimmed := checkPath
if !c.App().Config().StrictRouting {
checkPathTrimmed = utils.TrimRight(checkPath, '/')
checkPathTrimmed = strings.TrimRight(checkPath, "/")
}
switch {
case checkPath == cfg.ReadinessEndpoint || checkPathTrimmed == cfg.ReadinessEndpoint:
Expand Down

0 comments on commit 639f552

Please sign in to comment.