optimize: Limit file load size and clean up server logging
This commit is contained in:
@@ -15,7 +15,9 @@ type PageVFS struct {
|
||||
commitID string
|
||||
}
|
||||
|
||||
// todo: 限制最大文件加载大小
|
||||
// MaxFileLoadSize limits the maximum size of file loaded into memory (10MB)
|
||||
const MaxFileLoadSize = 10 * 1024 * 1024
|
||||
|
||||
func NewPageVFS(
|
||||
backend Backend,
|
||||
org string,
|
||||
@@ -69,7 +71,19 @@ func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer open.Close()
|
||||
return io.ReadAll(open)
|
||||
|
||||
// Use LimitReader to prevent reading too much data
|
||||
limitReader := io.LimitReader(open, MaxFileLoadSize+1)
|
||||
data, err := io.ReadAll(limitReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(data) > MaxFileLoadSize {
|
||||
return nil, &os.PathError{Op: "read", Path: path, Err: os.ErrInvalid} // Or a specific "file too large" error
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (p *PageVFS) ReadString(ctx context.Context, path string) (string, error) {
|
||||
|
||||
@@ -218,6 +218,7 @@ func (s *Server) Serve(writer *utils.WrittenResponseWriter, request *http.Reques
|
||||
if !ok {
|
||||
value, err = glob.Compile(filter.Path)
|
||||
if err != nil {
|
||||
zap.L().Warn("invalid glob pattern", zap.String("pattern", filter.Path), zap.Error(err))
|
||||
continue
|
||||
}
|
||||
s.globCache.Add(filter.Path, value)
|
||||
@@ -239,10 +240,13 @@ func (s *Server) Serve(writer *utils.WrittenResponseWriter, request *http.Reques
|
||||
slices.Reverse(activeFiltersCall)
|
||||
slices.Reverse(activeFilters)
|
||||
|
||||
// Build the visual call stack for logging (e.g., A -> B -> C -> B -> A)
|
||||
l := len(filtersRoute)
|
||||
if l > 1 {
|
||||
for i := l - 2; i >= 0; i-- {
|
||||
filtersRoute = append(filtersRoute, filtersRoute[i])
|
||||
}
|
||||
}
|
||||
zap.L().Debug("active filters", zap.String("filters", strings.Join(filtersRoute, " -> ")))
|
||||
|
||||
var stack core.NextCall = core.NotFountNextCall
|
||||
|
||||
Reference in New Issue
Block a user