-
Notifications
You must be signed in to change notification settings - Fork 269
BAH-5041 | Fix path traversal on v2 patientImage endpoint #349
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 |
|---|---|---|
|
|
@@ -89,6 +89,17 @@ public void shouldCreateRightDirectoryAccordingToPatientId() { | |
| absoluteFileDirectory.delete(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn404WhenPathTraversalAttemptedViaPatientUuidOnV2() { | ||
| PowerMockito.mockStatic(BahmniCoreProperties.class); | ||
| when(BahmniCoreProperties.getProperty("bahmnicore.images.directory")).thenReturn("/bahmni_data/patient_images"); | ||
| patientDocumentService = new PatientDocumentServiceImpl(); | ||
|
|
||
| ResponseEntity<Object> responseEntity = patientDocumentService.retriveImageWithoutDefault("../../../../tmp/secret"); | ||
|
|
||
| assertEquals(404, responseEntity.getStatusCode().value()); | ||
|
Comment on lines
+98
to
+100
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Make the traversal test create an outside target. The old vulnerable implementation also returns 404 when Proposed test adjustment- public void shouldReturn404WhenPathTraversalAttemptedViaPatientUuidOnV2() {
+ public void shouldReturn404WhenPathTraversalAttemptedViaPatientUuidOnV2() throws Exception {
+ File imagesDirectory = temporaryFolder.newFolder("patient_images");
+ temporaryFolder.newFile("secret.jpeg");
PowerMockito.mockStatic(BahmniCoreProperties.class);
- when(BahmniCoreProperties.getProperty("bahmn icore.images.directory")).thenReturn("/bahmni_data/patient_images");
+ when(BahmniCoreProperties.getProperty("bahmnicore.images.directory"))
+ .thenReturn(imagesDirectory.getAbsolutePath());
...
- patientDocumentService.retriveImageWithoutDefault("../../../../tmp/secret");
+ patientDocumentService.retriveImageWithoutDefault("../secret");🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| @Test | ||
| public void shouldGetImageNotFoundForIfNoImageCapturedForPatientAndNoDefaultImageNotPresent() throws Exception { | ||
| final FileInputStream fileInputStreamMock = PowerMockito.mock(FileInputStream.class); | ||
|
|
||
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.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Path Traversal (CWE-22): Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Reachability: External · Exploitability: Difficult
Block symbolic-link escapes from the image directory.
The lexical
startsWith(base)check does not prevent an in-directory symbolic link from targeting an external file. Resolve the real path before opening the file, or reject symbolic-link components. Add a regression test that expects HTTP 404.🤖 Prompt for AI Agents