Skip to content

Commit db8454b

Browse files
committed
Fix bug
1 parent f7c818c commit db8454b

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

web/src/main/java/org/openmrs/web/filter/StartupFilter.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.lang.annotation.Annotation;
1919
import java.lang.reflect.Field;
2020
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2123
import java.util.Collections;
2224
import java.util.HashMap;
2325
import java.util.List;
@@ -116,30 +118,27 @@ public final void doFilter(ServletRequest request, ServletResponse response, Fil
116118
// (the "/initfilter" part is needed so that the openmrs_static_context-servlet.xml file doesn't
117119
// get instantiated early, before the locale messages are all set up)
118120
if (servletPath.startsWith("/images") || servletPath.startsWith("/initfilter/scripts")) {
119-
servletPath = servletPath.replaceFirst("/initfilter", "/WEB-INF/view"); // strip out the /initfilter part
121+
// strip out the /initfilter part
122+
servletPath = servletPath.replaceFirst("/initfilter", "/WEB-INF/view");
120123
// writes the actual image file path to the response
121-
File file = new File(filterConfig.getServletContext().getRealPath(servletPath));
124+
Path filePath = Paths.get(filterConfig.getServletContext().getRealPath(servletPath)).normalize();
125+
Path fullFilePath = filePath;
122126
if (httpRequest.getPathInfo() != null) {
123-
file = new File(file, httpRequest.getPathInfo());
127+
fullFilePath = fullFilePath.resolve(httpRequest.getPathInfo());
128+
if (!(fullFilePath.normalize().startsWith(filePath))) {
129+
log.warn("Detected attempted directory traversal in request for {}", httpRequest.getPathInfo());
130+
return;
131+
}
124132
}
125133

126-
InputStream imageFileInputStream = null;
127-
try {
128-
imageFileInputStream = new FileInputStream(file);
134+
try (InputStream imageFileInputStream = new FileInputStream(fullFilePath.normalize().toFile())) {
129135
OpenmrsUtil.copyFile(imageFileInputStream, httpResponse.getOutputStream());
130136
}
131137
catch (FileNotFoundException e) {
132-
log.error("Unable to find file: " + file.getAbsolutePath());
138+
log.error("Unable to find file: {}", filePath, e);
133139
}
134-
finally {
135-
if (imageFileInputStream != null) {
136-
try {
137-
imageFileInputStream.close();
138-
}
139-
catch (IOException io) {
140-
log.warn("Couldn't close imageFileInputStream: " + io);
141-
}
142-
}
140+
catch (IOException e) {
141+
log.warn("An error occurred while handling file {}", filePath, e);
143142
}
144143
} else if (servletPath.startsWith("/scripts")) {
145144
log.error(
@@ -193,7 +192,7 @@ private void initializeVelocity() {
193192
velocityEngine.init(props);
194193
}
195194
catch (Exception e) {
196-
log.error("velocity init failed, because: " + e);
195+
log.error("velocity init failed, because: {}", e, e);
197196
}
198197
}
199198
}

0 commit comments

Comments
 (0)