-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Fix Path Traversal and XSS Security Vulnerabilities Detected by SpotBugs #5546
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -185,7 +185,13 @@ public static boolean startModule(Module mod, ServletContext servletContext, boo | |
| log.debug("Moving file from: {} to {}", name, absPath); | ||
|
|
||
| // get the output file | ||
| File outFile = new File(absPath.toString().replace("/", File.separator)); | ||
| String safePath = absPath.toString(); | ||
|
|
||
| // basic path traversal protection | ||
| if (safePath.contains("..") || safePath.contains("\\") || safePath.contains("//")) { | ||
| throw new IllegalArgumentException("Unsafe file path detected: " + safePath); | ||
| } | ||
| File outFile = new File(safePath.replace("/", File.separator)); | ||
| if (entry.isDirectory()) { | ||
| if (!outFile.exists()) { | ||
| outFile.mkdirs(); | ||
|
|
@@ -782,7 +788,15 @@ public static void shutdownModules(ServletContext servletContext) { | |
|
|
||
| // clear the module messages | ||
| String messagesPath = realPath + "/WEB-INF/"; | ||
| File folder = new File(messagesPath.replace("/", File.separator)); | ||
| // Normalize separators | ||
| String safePath = messagesPath.replace("/", File.separator); | ||
|
|
||
| // Detect basic path traversal patterns | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| if (safePath.contains("..")) { | ||
| throw new IllegalArgumentException("Invalid path: potential path traversal detected: " + safePath); | ||
| } | ||
|
|
||
| File folder = new File(safePath); | ||
|
|
||
| File[] files = folder.listFiles(); | ||
| if (folder.exists() && files != null) { | ||
|
|
@@ -833,8 +847,22 @@ public static void stopModule(Module mod, ServletContext servletContext, boolean | |
| String realPath = getRealPath(servletContext); | ||
|
|
||
| // delete the web files from the webapp | ||
| String absPath = realPath + "/WEB-INF/view/module/" + moduleId; | ||
| File moduleWebFolder = new File(absPath.replace("/", File.separator)); | ||
| String absPath = realPath + "/WEB-INF/web/module/" + moduleId; | ||
|
|
||
| // normalize path separator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Kindly remove all the comments that starts with //. |
||
| String safePath = absPath.replace("/", File.separator); | ||
|
|
||
| // Basic path traversal protection | ||
| if (safePath.contains("..")) { | ||
| throw new UnsupportedOperationException( | ||
| "Attempted to access '" + safePath + | ||
| "' but this was rejected because it contains '..', which may indicate " + | ||
| "a path traversal or zip-slip style attack." | ||
| ); | ||
| } | ||
|
|
||
| File moduleWebFolder = new File(safePath); | ||
|
|
||
| if (moduleWebFolder.exists()) { | ||
| try { | ||
| OpenmrsUtil.deleteDirectory(moduleWebFolder); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,8 +274,10 @@ else if (REVIEW_CHANGES.equals(page)) { | |
|
|
||
| addLogLinesToResponse(result); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there away you can fix the format changes please? |
||
|
|
||
| String jsonText = toJSONString(result); | ||
| httpResponse.setContentType("application/json;charset=UTF-8"); | ||
| jsonText = org.apache.commons.text.StringEscapeUtils.escapeHtml4(jsonText); | ||
| httpResponse.getWriter().write(jsonText); | ||
| } | ||
| } | ||
|
|
||
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.
Kindly the comments here don’t seem to add extra value so it would be better to remove them and rely on the code readability instead.