Address github security report (#3492)
This commit is contained in:
parent
34eb47aba6
commit
23cb65af25
|
@ -33,7 +33,7 @@ public abstract class RestfulResponse<T extends RequestDetails> implements IRest
|
||||||
|
|
||||||
private IIdType myOperationResourceId;
|
private IIdType myOperationResourceId;
|
||||||
private IPrimitiveType<Date> myOperationResourceLastUpdated;
|
private IPrimitiveType<Date> myOperationResourceLastUpdated;
|
||||||
private Map<String, List<String>> theHeaders = new HashMap<>();
|
private final Map<String, List<String>> myHeaders = new HashMap<>();
|
||||||
private T theRequestDetails;
|
private T theRequestDetails;
|
||||||
|
|
||||||
public RestfulResponse(T requestDetails) {
|
public RestfulResponse(T requestDetails) {
|
||||||
|
@ -51,7 +51,7 @@ public abstract class RestfulResponse<T extends RequestDetails> implements IRest
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Map<String, List<String>> getHeaders() {
|
public Map<String, List<String>> getHeaders() {
|
||||||
return theHeaders;
|
return myHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,6 +24,7 @@ import ca.uhn.fhir.rest.api.Constants;
|
||||||
import ca.uhn.fhir.rest.api.MethodOutcome;
|
import ca.uhn.fhir.rest.api.MethodOutcome;
|
||||||
import ca.uhn.fhir.rest.api.server.ParseAction;
|
import ca.uhn.fhir.rest.api.server.ParseAction;
|
||||||
import ca.uhn.fhir.rest.server.RestfulResponse;
|
import ca.uhn.fhir.rest.server.RestfulResponse;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
||||||
|
|
||||||
import javax.servlet.ServletOutputStream;
|
import javax.servlet.ServletOutputStream;
|
||||||
|
@ -80,9 +81,12 @@ public class ServletRestfulResponse extends RestfulResponse<ServletRequestDetail
|
||||||
HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
|
HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
|
||||||
getRequestDetails().getServer().addHeadersToResponse(theHttpResponse);
|
getRequestDetails().getServer().addHeadersToResponse(theHttpResponse);
|
||||||
for (Entry<String, List<String>> header : getHeaders().entrySet()) {
|
for (Entry<String, List<String>> header : getHeaders().entrySet()) {
|
||||||
final String key = header.getKey();
|
String key = header.getKey();
|
||||||
|
key = sanitizeHeaderField(key);
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (String value : header.getValue()) {
|
for (String value : header.getValue()) {
|
||||||
|
value = sanitizeHeaderField(value);
|
||||||
|
|
||||||
// existing headers should be overridden
|
// existing headers should be overridden
|
||||||
if (first) {
|
if (first) {
|
||||||
theHttpResponse.setHeader(key, value);
|
theHttpResponse.setHeader(key, value);
|
||||||
|
@ -94,6 +98,10 @@ public class ServletRestfulResponse extends RestfulResponse<ServletRequestDetail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String sanitizeHeaderField(String theKey) {
|
||||||
|
return StringUtils.replaceChars(theKey, "\r\n", null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final Writer sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) {
|
public final Writer sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) {
|
||||||
return theWriter;
|
return theWriter;
|
||||||
|
|
|
@ -13,6 +13,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
@ -52,4 +53,12 @@ public class ServletRestfulResponseTest {
|
||||||
orderVerifier.verify(servletResponse).addHeader(eq("Authorization"), eq("Bearer"));
|
orderVerifier.verify(servletResponse).addHeader(eq("Authorization"), eq("Bearer"));
|
||||||
verify(servletResponse).setHeader(eq("Cache-Control"), eq("no-cache, no-store"));
|
verify(servletResponse).setHeader(eq("Cache-Control"), eq("no-cache, no-store"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSanitizeHeaderField() {
|
||||||
|
assertEquals("AB", ServletRestfulResponse.sanitizeHeaderField("A\nB"));
|
||||||
|
assertEquals("AB", ServletRestfulResponse.sanitizeHeaderField("A\r\r\rB"));
|
||||||
|
assertEquals("AB", ServletRestfulResponse.sanitizeHeaderField("AB"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue