Skip to content

Request body blocking for netty #377

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

Merged
merged 6 commits into from
Feb 24, 2023
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 @@ -33,4 +33,8 @@ public class AttributeKeys {
public static final AttributeKey<Map<String, String>> REQUEST_HEADERS =
io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys.attributeKey(
AttributeKeys.class.getName() + ".request-headers");

public static final AttributeKey<?> REQUEST =
io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys.attributeKey(
"io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys.http-server-request");
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.Attribute;
import io.netty.util.ReferenceCountUtil;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.AttributeKeys;
import io.opentelemetry.javaagent.instrumentation.netty.common.HttpRequestAndChannel;
import java.util.Map;
import org.hypertrace.agent.filter.FilterRegistry;

Expand Down Expand Up @@ -55,6 +57,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
return;
}
}
if (msg instanceof HttpContent) {
if (FilterRegistry.getFilter().evaluateRequestBody(span, null, null)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? It's passing null for request body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have the request body at this point, but we have the request body in span attributes. And the filter impl will use request body from span attributes

Attribute<?> requestAttr = channel.attr(AttributeKeys.REQUEST);
HttpRequest req = ((HttpRequestAndChannel) (requestAttr.get())).request();
forbidden(ctx, req);
return;
}
}
ctx.fireChannelRead(msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,39 @@ public void blocking() throws IOException, TimeoutException, InterruptedExceptio
spanData
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_BODY)));

RequestBody requestBody = blockedRequestBody(true, 3000, 75);
Request request2 =
new Request.Builder()
.url(String.format("http://localhost:%d/post", port))
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
.post(requestBody)
.build();

try (Response response = httpClient.newCall(request2).execute()) {
Assertions.assertEquals(403, response.code());
Assertions.assertTrue(response.body().string().isEmpty());
}

List<List<SpanData>> traces2 = TEST_WRITER.getTraces();
TEST_WRITER.waitForTraces(2);
Assertions.assertEquals(2, traces2.size());
List<SpanData> trace2 = traces2.get(1);
Assertions.assertEquals(1, trace2.size());
SpanData spanData2 = trace2.get(0);

Assertions.assertEquals(
REQUEST_HEADER_VALUE,
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
Assertions.assertNull(
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER_NAME)));
Assertions.assertNull(
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_BODY)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class AttributeKeys {

public static final AttributeKey<Map<String, String>> REQUEST_HEADERS =
AttributeKey.valueOf(AttributeKeys.class, "request-headers");

public static final AttributeKey<?> REQUEST =
AttributeKey.valueOf(
"io.opentelemetry.javaagent.instrumentation.netty.v4_1.server.NettyServerSingletons#http-server-request");
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.Attribute;
import io.netty.util.ReferenceCountUtil;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.AttributeKeys;
import io.opentelemetry.javaagent.instrumentation.netty.common.HttpRequestAndChannel;
import java.util.Map;
import org.hypertrace.agent.filter.FilterRegistry;

Expand Down Expand Up @@ -55,6 +57,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
return;
}
}
if (msg instanceof HttpContent) {
if (FilterRegistry.getFilter().evaluateRequestBody(span, null, null)) {
Attribute<?> requestAttr = channel.attr(AttributeKeys.REQUEST);
HttpRequest req = ((HttpRequestAndChannel) (requestAttr.get())).request();
forbidden(ctx, req);
return;
}
}
ctx.fireChannelRead(msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,39 @@ public void blocking() throws IOException, TimeoutException, InterruptedExceptio
spanData
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_BODY)));

RequestBody requestBody = blockedRequestBody(true, 3000, 75);
Request request2 =
new Request.Builder()
.url(String.format("http://localhost:%d/post", port))
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
.post(requestBody)
.build();

try (Response response = httpClient.newCall(request2).execute()) {
Assertions.assertEquals(403, response.code());
Assertions.assertTrue(response.body().string().isEmpty());
}

List<List<SpanData>> traces2 = TEST_WRITER.getTraces();
TEST_WRITER.waitForTraces(2);
Assertions.assertEquals(2, traces2.size());
List<SpanData> trace2 = traces2.get(1);
Assertions.assertEquals(1, trace2.size());
SpanData spanData2 = trace2.get(0);

Assertions.assertEquals(
REQUEST_HEADER_VALUE,
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
Assertions.assertNull(
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER_NAME)));
Assertions.assertNull(
spanData2
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_BODY)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,31 @@ public void writeTo(BufferedSink sink) throws IOException {
}
};
}

public static RequestBody blockedRequestBody(
final boolean chunked, final long size, final int writeSize) {
final byte[] buffer = new byte[writeSize];
Arrays.fill(buffer, (byte) 'x');

return new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.get("application/json; charset=utf-8");
}

@Override
public long contentLength() throws IOException {
return chunked ? -1L : size;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write("{\"block=true\":\"".getBytes());
for (int count = 0; count < size; count += writeSize) {
sink.write(buffer, 0, (int) Math.min(size - count, writeSize));
}
sink.write("\"}".getBytes());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.hypertrace.agent.testing.mockfilter;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.sdk.trace.ReadableSpan;
import java.util.Map;
import org.hypertrace.agent.filter.api.Filter;

Expand All @@ -37,6 +39,14 @@ public boolean evaluateRequestHeaders(Span span, Map<String, String> headers) {

@Override
public boolean evaluateRequestBody(Span span, String body, Map<String, String> headers) {
return body.contains("block=true");
if (body != null && body.contains("block=true")) {
return true;
}
if (span instanceof ReadableSpan) {
String spanReqBody =
((ReadableSpan) span).getAttribute(AttributeKey.stringKey("http.request.body"));
return spanReqBody != null && spanReqBody.contains("block=true");
}
return false;
}
}