Skip to content

Commit 9c50f85

Browse files
committed
feat: add file size validation in content workflow results
- Introduced a constant `MaxResultFileSize` to limit the size of result files to 10MB, preventing potential memory issues. - Implemented file size checks before reading files in the `ContentWorkflowResults` function, ensuring that files exceeding the limit are handled gracefully with appropriate error messages. These changes enhance the robustness of the content workflow by preventing excessive memory usage when processing large files.
1 parent e120fb9 commit 9c50f85

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

components/backend/handlers/content.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424
// Set by main during initialization
2525
var StateBaseDir string
2626

27+
// MaxResultFileSize is the maximum size for result files to prevent memory issues
28+
const MaxResultFileSize = 10 * 1024 * 1024 // 10MB
29+
2730
// Git operation functions - set by main package during initialization
2831
// These are set to the actual implementations from git package
2932
var (
@@ -699,14 +702,29 @@ func ContentWorkflowResults(c *gin.Context) {
699702

700703
for _, matchedPath := range matches {
701704
relPath, _ := filepath.Rel(workspaceBase, matchedPath)
702-
content, readErr := os.ReadFile(matchedPath)
703-
705+
704706
result := ResultFile{
705707
DisplayName: displayName,
706708
Path: relPath,
707709
Exists: true,
708710
}
709711

712+
// Check file size before reading
713+
fileInfo, statErr := os.Stat(matchedPath)
714+
if statErr != nil {
715+
result.Error = fmt.Sprintf("Failed to stat file: %v", statErr)
716+
results = append(results, result)
717+
continue
718+
}
719+
720+
if fileInfo.Size() > MaxResultFileSize {
721+
result.Error = fmt.Sprintf("File too large (%d bytes, max %d)", fileInfo.Size(), MaxResultFileSize)
722+
results = append(results, result)
723+
continue
724+
}
725+
726+
// Read file content
727+
content, readErr := os.ReadFile(matchedPath)
710728
if readErr != nil {
711729
result.Error = fmt.Sprintf("Failed to read: %v", readErr)
712730
} else {

0 commit comments

Comments
 (0)