-
Notifications
You must be signed in to change notification settings - Fork 695
Fix issues in throttling policy import API #13560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1225,13 +1225,32 @@ public Response exportThrottlingPolicy(String policyId, String policyName, Strin | |||||
| */ | ||||||
| public static ExportThrottlePolicyDTO getImportedPolicy(InputStream uploadedInputStream, Attachment fileDetail) | ||||||
| throws ParseException, APIImportExportException, IOException { | ||||||
|
|
||||||
| File importFolder = CommonUtil.createTempDirectory(null); | ||||||
| String uploadFileName = fileDetail.getContentDisposition().getFilename(); | ||||||
|
PasanT9 marked this conversation as resolved.
|
||||||
| String fileType = (uploadFileName.contains(ImportExportConstants.YAML_EXTENSION)) ? | ||||||
| if (StringUtils.isEmpty(uploadFileName)) { | ||||||
| throw new APIImportExportException("Invalid file name. File name cannot be null or empty."); | ||||||
| } | ||||||
|
Comment on lines
+1231
to
+1233
|
||||||
| // Validate file extension to prevent uploading unauthorized file types | ||||||
| String lowerCaseFileName = uploadFileName.toLowerCase(); | ||||||
|
||||||
| boolean isYamlFile = | ||||||
| lowerCaseFileName.endsWith(ImportExportConstants.YAML_EXTENSION) || lowerCaseFileName.endsWith( | ||||||
| ImportExportConstants.YML_EXTENSION); | ||||||
| boolean isJsonFile = lowerCaseFileName.endsWith(ImportExportConstants.JSON_EXTENSION); | ||||||
| if (!isYamlFile && !isJsonFile) { | ||||||
| throw new APIImportExportException("Invalid file type. Only YAML and JSON files are allowed."); | ||||||
| } | ||||||
| String fileType = isYamlFile ? | ||||||
| ImportExportConstants.EXPORT_POLICY_TYPE_YAML : | ||||||
| ImportExportConstants.EXPORT_POLICY_TYPE_JSON; | ||||||
| // Validating the canonical path | ||||||
| String absolutePath = importFolder.getAbsolutePath() + File.separator + uploadFileName; | ||||||
| File targetFile = new File(absolutePath); | ||||||
| String canonicalPath = targetFile.getCanonicalPath(); | ||||||
| String canonicalImportPath = importFolder.getCanonicalPath(); | ||||||
| if (!canonicalPath.startsWith(canonicalImportPath + File.separator)) { | ||||||
| throw new APIImportExportException("Invalid file name."); | ||||||
|
||||||
| throw new APIImportExportException("Invalid file name."); | |
| throw new APIImportExportException("Invalid file name. File path is outside the target directory."); |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The security enhancements in this method (file name validation, file type validation, and path traversal protection) should be covered by tests to ensure they work correctly and prevent regressions. Consider adding tests that verify: 1) null/empty file names are rejected, 2) only YAML/YML/JSON extensions are accepted, 3) path traversal attempts are blocked, 4) valid files are accepted.
Uh oh!
There was an error while loading. Please reload this page.