[bugfix] return 400 Bad Request on more cases of malformed AS data (#2399)

This commit is contained in:
kim
2023-11-30 16:22:34 +00:00
committed by GitHub
parent 5fd2e427bb
commit eb170003b8
47 changed files with 1493 additions and 1013 deletions

View File

@ -20,6 +20,7 @@ package middleware
import (
"fmt"
"net/http"
"runtime"
"time"
"codeberg.org/gruf/go-bytesize"
@ -56,7 +57,10 @@ func Logger(logClientIP bool) gin.HandlerFunc {
_ = c.Error(err)
// Dump a stacktrace to error log
callers := errors.GetCallers(3, 10)
pcs := make([]uintptr, 10)
n := runtime.Callers(3, pcs)
iter := runtime.CallersFrames(pcs[:n])
callers := errors.Callers(gatherFrames(iter, n))
log.WithContext(c.Request.Context()).
WithField("stacktrace", callers).Error(err)
}
@ -80,8 +84,9 @@ func Logger(logClientIP bool) gin.HandlerFunc {
}
// Create log entry with fields
l := log.WithContext(c.Request.Context()).
WithFields(fields...)
l := log.New()
l = l.WithContext(c.Request.Context())
l = l.WithFields(fields...)
// Default is info
lvl := level.INFO
@ -119,3 +124,19 @@ func Logger(logClientIP bool) gin.HandlerFunc {
c.Next()
}
}
// gatherFrames gathers runtime frames from a frame iterator.
func gatherFrames(iter *runtime.Frames, n int) []runtime.Frame {
if iter == nil {
return nil
}
frames := make([]runtime.Frame, 0, n)
for {
f, ok := iter.Next()
if !ok {
break
}
frames = append(frames, f)
}
return frames
}