Skip to content

Commit be0b9d4

Browse files
committed
Sanitize file paths of the API project traversal
1 parent bf346bd commit be0b9d4

File tree

1 file changed

+17
-2
lines changed
  • platform-api/src/internal/service

1 file changed

+17
-2
lines changed

platform-api/src/internal/service/git.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,24 @@ func (s *gitService) ValidateAPIProject(repoURL, branch, path string) (*dto.APIP
163163
return nil, fmt.Errorf("malformed api project: apis.openapi and apis.wso2Artifact fields are required")
164164
}
165165

166+
// Sanitize paths to prevent traversal attacks
167+
openAPIClean := pathpkg.Clean(api.OpenAPI)
168+
if strings.HasPrefix(openAPIClean, "..") || pathpkg.IsAbs(openAPIClean) {
169+
return nil, fmt.Errorf("malformed api project: invalid openapi path: %s", api.OpenAPI)
170+
}
171+
wso2ArtifactClean := pathpkg.Clean(api.WSO2Artifact)
172+
if strings.HasPrefix(wso2ArtifactClean, "..") || pathpkg.IsAbs(wso2ArtifactClean) {
173+
return nil, fmt.Errorf("malformed api project: invalid wso2Artifact path: %s", api.WSO2Artifact)
174+
}
175+
166176
// 4. Check if the referenced files exist in the project path
167-
openAPIPath := pathpkg.Join(path, api.OpenAPI)
177+
openAPIPath := pathpkg.Join(path, openAPIClean)
168178
_, err := s.FetchFileContent(repoURL, branch, openAPIPath)
169179
if err != nil {
170180
return nil, fmt.Errorf("invalid api project: openapi file not found: %s", api.OpenAPI)
171181
}
172182

173-
wso2ArtifactPath := pathpkg.Join(path, api.WSO2Artifact)
183+
wso2ArtifactPath := pathpkg.Join(path, wso2ArtifactClean)
174184
_, err = s.FetchFileContent(repoURL, branch, wso2ArtifactPath)
175185
if err != nil {
176186
return nil, fmt.Errorf("invalid api project: wso2 artifact file not found: %s", api.WSO2Artifact)
@@ -192,5 +202,10 @@ func (s *gitService) FetchWSO2Artifact(repoURL, branch, path string) (*dto.APIDe
192202
return nil, fmt.Errorf("failed to parse WSO2 artifact file at %s: %w", path, err)
193203
}
194204

205+
// Validate required fields
206+
if artifact.Kind == "" || artifact.Version == "" {
207+
return nil, fmt.Errorf("malformed WSO2 artifact at %s: kind and version are required", path)
208+
}
209+
195210
return &artifact, nil
196211
}

0 commit comments

Comments
 (0)