Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public void setHeader(String name, String value) {
if (isContentLengthHeader(name)) {
return;
}
checkForContentType(name, value);
checkForContentSecurityPolicy(name);
}
super.setHeader(name, value);
Expand All @@ -118,6 +119,7 @@ public void addHeader(String name, String value) {
if (isContentLengthHeader(name)) {
return;
}
checkForContentType(name, value);
checkForContentSecurityPolicy(name);
}
super.addHeader(name, value);
Expand All @@ -133,6 +135,12 @@ private void checkForContentSecurityPolicy(String name) {
}
}

private void checkForContentType(String name, String value) {
if ("content-type".equalsIgnoreCase(name)) {
handleContentType(value);
}
}

@Override
public void setContentLength(int len) {
// don't set it since we don't know if we will inject
Expand Down Expand Up @@ -182,15 +190,20 @@ public void onInjected() {
}
}

@Override
public void setContentType(String type) {
private void handleContentType(String type) {
final boolean wasInjecting = shouldInject;
if (shouldInject) {
shouldInject = type != null && type.contains("text/html");
}
if (!shouldInject) {
if (wasInjecting && !shouldInject) {
commit();
stopFiltering();
}
}

@Override
public void setContentType(String type) {
handleContentType(type);
super.setContentType(type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,36 @@ class RumHttpServletResponseWrapperTest extends InstrumentationSpecification {
1 * mockResponse.getOutputStream()
}

void 'getWriter with non-HTML content reports skipped'() {
setup:
void 'getWriter with non-HTML content reports skipped (setContentType)'() {
when:
wrapper.setContentType("text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.setContentType("text/plain")
1 * mockResponse.getWriter()
}

void 'getWriter with non-HTML content reports skipped (setHeader)'() {
when:
wrapper.setHeader("Content-Type", "text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.setHeader("Content-Type", "text/plain")
1 * mockResponse.getWriter()
}

void 'getWriter with non-HTML content reports skipped (addHeader)'() {
when:
wrapper.addHeader("Content-Type", "text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.addHeader("Content-Type", "text/plain")
1 * mockResponse.getWriter()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void setHeader(String name, String value) {
if (isContentLengthHeader(name)) {
return;
}
checkForContentType(name, value);
checkForContentSecurityPolicy(name);
}
super.setHeader(name, value);
Expand All @@ -137,6 +138,7 @@ public void addHeader(String name, String value) {
if (isContentLengthHeader(name)) {
return;
}
checkForContentType(name, value);
checkForContentSecurityPolicy(name);
}
super.addHeader(name, value);
Expand Down Expand Up @@ -205,15 +207,26 @@ public void onInjected() {
}
}

@Override
public void setContentType(String type) {
private void handleContentType(String type) {
final boolean wasInjecting = shouldInject;
if (shouldInject) {
shouldInject = type != null && type.contains("text/html");
}
if (!shouldInject) {
if (wasInjecting && !shouldInject) {
commit();
stopFiltering();
}
}

private void checkForContentType(String name, String value) {
if ("content-type".equalsIgnoreCase(name)) {
handleContentType(value);
}
}

@Override
public void setContentType(String type) {
handleContentType(type);
super.setContentType(type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,36 @@ class RumHttpServletResponseWrapperTest extends InstrumentationSpecification {
1 * mockResponse.getOutputStream()
}

void 'getWriter with non-HTML content reports skipped'() {
setup:
void 'getWriter with non-HTML content reports skipped (setContentType)'() {
when:
wrapper.setContentType("text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.setContentType("text/plain")
1 * mockResponse.getWriter()
}

void 'getWriter with non-HTML content reports skipped (setHeader)'() {
when:
wrapper.setHeader("Content-Type", "text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.setHeader("Content-Type", "text/plain")
1 * mockResponse.getWriter()
}

void 'getWriter with non-HTML content reports skipped (addHeader)'() {
when:
wrapper.addHeader("Content-Type", "text/plain")
wrapper.getWriter()

then:
1 * mockTelemetryCollector.onInjectionSkipped(SERVLET_VERSION)
1 * mockResponse.addHeader("Content-Type", "text/plain")
1 * mockResponse.getWriter()
}

Expand Down