Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
fix: harden flow file uploads
mason5052263eaf56aba6
diff --git a/backend/pkg/server/response/errors.go b/backend/pkg/server/response/errors.goindex f294ddc3..89f75233 100644--- a/backend/pkg/server/response/errors.go+++ b/backend/pkg/server/response/errors.go@@ -113,7 +113,8 @@ var ErrFlowsInvalidData = NewHttpError(500, "Flows.InvalidData", "invalid flow dvar ErrFlowFilesInvalidRequest = NewHttpError(400, "FlowFiles.InvalidRequest", "invalid flow file request data")var ErrFlowFilesNotFound = NewHttpError(404, "FlowFiles.NotFound", "flow file not found")-var ErrFlowFilesInvalidData = NewHttpError(500, "FlowFiles.InvalidData", "invalid flow file data")+var ErrFlowFilesInvalidData = NewHttpError(400, "FlowFiles.InvalidData", "invalid flow file data")+var ErrFlowFilesAlreadyExists = NewHttpError(409, "FlowFiles.AlreadyExists", "flow file already exists")// tasksdiff --git a/backend/pkg/server/router.go b/backend/pkg/server/router.goindex 45bb5a53..eb4c6f15 100644--- a/backend/pkg/server/router.go+++ b/backend/pkg/server/router.go@@ -372,7 +372,9 @@ func setFlowsGroup(parent *gin.RouterGroup, svc *services.FlowService) {func setFlowFilesGroup(parent *gin.RouterGroup, svc *services.FlowFileService) {flowFilesGroup := parent.Group("/flows/:flowID/files"){+ flowFilesGroup.GET("", svc.GetFlowFiles)flowFilesGroup.GET("/", svc.GetFlowFiles)+ flowFilesGroup.POST("", svc.UploadFlowFiles)flowFilesGroup.POST("/", svc.UploadFlowFiles)flowFilesGroup.GET("/:fileName", svc.DownloadFlowFile)}diff --git a/backend/pkg/server/services/flow_files.go b/backend/pkg/server/services/flow_files.goindex b7461779..b3d20d33 100644--- a/backend/pkg/server/services/flow_files.go+++ b/backend/pkg/server/services/flow_files.go@@ -23,6 +23,8 @@ import (const flowUploadsDirName = "uploads"+var errFlowFileNotRegular = errors.New("flow file is not a regular file")+type flowFile struct {Name string `json:"name"`Path string `json:"path"`@@ -124,6 +126,25 @@ func (s *FlowFileService) UploadFlowFiles(c *gin.Context) {}dstPath := filepath.Join(uploadDir, fileName)+ exists, err := flowFileExists(dstPath)+ if err != nil {+ logger.FromContext(c).WithError(err).WithFields(map[string]any{+ "flow_id": flowID,+ "file_name": fileName,+ }).Error("error checking existing uploaded file")+ response.Error(c, response.ErrInternal, err)+ return+ }+ if exists {+ err = fmt.Errorf("flow file '%s' already exists", fileName)+ logger.FromContext(c).WithError(err).WithFields(map[string]any{+ "flow_id": flowID,+ "file_name": fileName,+ }).Error("uploaded file already exists")+ response.Error(c, response.ErrFlowFilesAlreadyExists, err)+ return+ }+if err := c.SaveUploadedFile(fileHeader, dstPath); err != nil {logger.FromContext(c).WithError(err).WithFields(map[string]any{"flow_id": flowID,@@ -133,7 +154,7 @@ func (s *FlowFileService) UploadFlowFiles(c *gin.Context) {return}- info, err := os.Stat(dstPath)+ info, err := regularFlowFileInfo(dstPath)if err != nil {logger.FromContext(c).WithError(err).WithFields(map[string]any{"flow_id": flowID,@@ -174,28 +195,19 @@ func (s *FlowFileService) DownloadFlowFile(c *gin.Context) {}filePath := filepath.Join(s.flowUploadsDir(flowID), fileName)- info, err := os.Stat(filePath)+ _, err = regularFlowFileInfo(filePath)if err != nil {logger.FromContext(c).WithError(err).WithFields(map[string]any{"flow_id": flowID,"file_name": fileName,}).Error("error reading flow file")- if errors.Is(err, os.ErrNotExist) {+ if errors.Is(err, os.ErrNotExist) || errors.Is(err, errFlowFileNotRegular) {response.Error(c, response.ErrFlowFilesNotFound, err)} else {response.Error(c, response.ErrInternal, err)}return}- if info.IsDir() {- err = fmt.Errorf("file '%s' is a directory", fileName)- logger.FromContext(c).WithError(err).WithFields(map[string]any{- "flow_id": flowID,- "file_name": fileName,- }).Error("invalid flow file type")- response.Error(c, response.ErrFlowFilesNotFound, err)- return- }c.FileAttachment(filePath, fileName)}@@ -231,7 +243,7 @@ func (s *FlowFileService) listFlowFiles(flowID uint64) ([]flowFile, error) {files := make([]flowFile, 0, len(entries))for _, entry := range entries {- if entry.IsDir() {+ if entry.IsDir() || entry.Type()&os.ModeSymlink != 0 {continue}@@ -239,6 +251,9 @@ func (s *FlowFileService) listFlowFiles(flowID uint64) ([]flowFile, error) {if err != nil {return nil, err}+ if !info.Mode().IsRegular() {+ continue+ }files = append(files, newFlowFile(info))}@@ -313,6 +328,29 @@ func sanitizeFlowFileName(fileName string) (string, error) {return cleanName, nil}+func flowFileExists(filePath string) (bool, error) {+ if _, err := os.Lstat(filePath); err != nil {+ if errors.Is(err, os.ErrNotExist) {+ return false, nil+ }+ return false, err+ }++ return true, nil+}++func regularFlowFileInfo(filePath string) (os.FileInfo, error) {+ info, err := os.Lstat(filePath)+ if err != nil {+ return nil, err+ }+ if !info.Mode().IsRegular() {+ return nil, fmt.Errorf("%w: %s", errFlowFileNotRegular, filePath)+ }++ return info, nil+}+func newFlowFile(info os.FileInfo) flowFile {return flowFile{Name: info.Name(),diff --git a/backend/pkg/server/services/flow_files_test.go b/backend/pkg/server/services/flow_files_test.goindex b40cb440..4e01f4e9 100644--- a/backend/pkg/server/services/flow_files_test.go+++ b/backend/pkg/server/services/flow_files_test.go@@ -64,9 +64,11 @@ func TestFlowFileService_ListFlowFiles(t *testing.T) {oldPath := filepath.Join(uploadDir, "old.txt")newPath := filepath.Join(uploadDir, "new.txt")+ linkPath := filepath.Join(uploadDir, "link.txt")require.NoError(t, os.WriteFile(oldPath, []byte("old"), 0644))require.NoError(t, os.WriteFile(newPath, []byte("newer content"), 0644))+ _ = os.Symlink(newPath, linkPath)oldTime := time.Now().Add(-2 * time.Hour)newTime := time.Now().Add(-1 * time.Hour)@@ -84,6 +86,10 @@ func TestFlowFileService_ListFlowFiles(t *testing.T) {assert.Equal(t, "old.txt", files[1].Name)assert.Equal(t, "/work/uploads/old.txt", files[1].Path)assert.Equal(t, int64(len("old")), files[1].Size)++ for _, file := range files {+ assert.NotEqual(t, "link.txt", file.Name)+ }}func TestFlowFileService_ListFlowFiles_MissingDirectory(t *testing.T) {@@ -93,3 +99,32 @@ func TestFlowFileService_ListFlowFiles_MissingDirectory(t *testing.T) {require.NoError(t, err)assert.Empty(t, files)}++func TestFlowFileExists(t *testing.T) {+ dir := t.TempDir()+ filePath := filepath.Join(dir, "report.txt")++ exists, err := flowFileExists(filePath)+ require.NoError(t, err)+ assert.False(t, exists)++ require.NoError(t, os.WriteFile(filePath, []byte("report"), 0644))++ exists, err = flowFileExists(filePath)+ require.NoError(t, err)+ assert.True(t, exists)+}++func TestRegularFlowFileInfoRejectsSymlink(t *testing.T) {+ dir := t.TempDir()+ filePath := filepath.Join(dir, "report.txt")+ linkPath := filepath.Join(dir, "link.txt")++ require.NoError(t, os.WriteFile(filePath, []byte("report"), 0644))+ if err := os.Symlink(filePath, linkPath); err != nil {+ t.Skipf("symlink creation is not available: %v", err)+ }++ _, err := regularFlowFileInfo(linkPath)+ require.ErrorIs(t, err, errFlowFileNotRegular)+}diff --git a/frontend/src/features/flows/files/flow-files.tsx b/frontend/src/features/flows/files/flow-files.tsxindex 9c2b05d3..fce6c5fa 100644--- a/frontend/src/features/flows/files/flow-files.tsx+++ b/frontend/src/features/flows/files/flow-files.tsx@@ -13,6 +13,11 @@ import { formatDate } from '@/lib/utils/format';import { baseUrl } from '@/models/api';import { useFlow } from '@/providers/flow-provider';+interface ApiResponse<T> {+ data?: T;+ status: string;+}+interface FlowFile {modifiedAt: string;name: string;@@ -25,6 +30,14 @@ interface FlowFilesResponse {total: number;}+const unwrapFlowFilesResponse = (response: ApiResponse<FlowFilesResponse>) => {+ if (response.status !== 'success' || !response.data) {+ throw new Error('Unexpected flow files response');+ }++ return response.data;+};+const formatFileSize = (size: number) => {if (size < 1024) {return `${size} B`;@@ -60,7 +73,8 @@ const FlowFiles = () => {setIsLoading(true);try {- const data = await axios.get<FlowFilesResponse>(`/flows/${flowId}/files`);+ const response = await axios.get<unknown, ApiResponse<FlowFilesResponse>>(`/flows/${flowId}/files`);+ const data = unwrapFlowFilesResponse(response);setFiles(data.files ?? []);} catch (error) {const description = error instanceof Error ? error.message : 'An error occurred while loading flow files';@@ -113,7 +127,16 @@ const FlowFiles = () => {setIsUploading(true);try {- const data = await axios.post<FlowFilesResponse>(`/flows/${flowId}/files`, formData);+ const response = await axios.post<unknown, ApiResponse<FlowFilesResponse>>(+ `/flows/${flowId}/files`,+ formData,+ {+ headers: {+ 'Content-Type': undefined,+ },+ },+ );+ const data = unwrapFlowFilesResponse(response);const uploadedCount = data.files?.length ?? selectedFiles.length;toast.success(uploadedCount === 1 ? 'File uploaded successfully' : 'Files uploaded successfully', {