-
Notifications
You must be signed in to change notification settings - Fork 355
Add block-outcome telemetry for Netty blocking enforcement failures #12316
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
Open
jandro996
wants to merge
3
commits into
master
Choose a base branch
from
APPSEC-62696
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...test/java/datadog/trace/instrumentation/netty41/NettyMultipartHelperBlockFailureTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package datadog.trace.instrumentation.netty41; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| import datadog.appsec.api.blocking.BlockingContentType; | ||
| import datadog.appsec.api.blocking.BlockingException; | ||
| import datadog.trace.api.appsec.AppSecContext; | ||
| import datadog.trace.api.gateway.BlockResponseFunction; | ||
| import datadog.trace.api.gateway.Flow; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.gateway.RequestContextSlot; | ||
| import datadog.trace.api.internal.TraceSegment; | ||
| import datadog.trace.bootstrap.instrumentation.api.ClientIpAddressData; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Covers the {@code tryBlock() -> AppSecContext.reportBlockFailure()} path. Hand-written test | ||
| * doubles are used because Mockito is only on this module's test runtime classpath, not its test | ||
| * compile classpath. | ||
| */ | ||
| class NettyMultipartHelperBlockFailureTest { | ||
|
|
||
| private static final Flow.Action.RequestBlockingAction RBA = | ||
| new Flow.Action.RequestBlockingAction(403, BlockingContentType.AUTO); | ||
|
|
||
| @Test | ||
| void reportsBlockFailureWhenBlockingResponseCannotBeCommitted() { | ||
| CountingAppSecContext appSecCtx = new CountingAppSecContext(); | ||
| TestRequestContext ctx = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), appSecCtx); | ||
|
|
||
| BlockingException exception = NettyMultipartHelper.tryBlock(ctx, blockingFlow(), "blocked!"); | ||
|
|
||
| assertNotNull(exception); | ||
| assertEquals("blocked!", exception.getMessage()); | ||
| assertEquals(1, appSecCtx.blockFailures); | ||
| assertSame(RBA, ctx.brf.lastAction); | ||
| assertSame(ctx.traceSegment, ctx.brf.lastSegment); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotReportBlockFailureWhenBlockingResponseIsCommitted() { | ||
| CountingAppSecContext appSecCtx = new CountingAppSecContext(); | ||
| TestRequestContext ctx = new TestRequestContext(new TestBlockResponseFunction(true), appSecCtx); | ||
|
|
||
| BlockingException exception = NettyMultipartHelper.tryBlock(ctx, blockingFlow(), "blocked!"); | ||
|
|
||
| assertNotNull(exception); | ||
| assertEquals("blocked!", exception.getMessage()); | ||
| assertEquals(0, appSecCtx.blockFailures); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotThrowWhenAppSecSlotDoesNotHoldAnAppSecContext() { | ||
| TestRequestContext nullSlot = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), null); | ||
| assertNotNull(NettyMultipartHelper.tryBlock(nullSlot, blockingFlow(), "blocked!")); | ||
|
|
||
| TestRequestContext foreignSlot = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), "not an AppSecContext"); | ||
| assertNotNull(NettyMultipartHelper.tryBlock(foreignSlot, blockingFlow(), "blocked!")); | ||
| } | ||
|
|
||
| private static Flow<Void> blockingFlow() { | ||
| return new Flow<Void>() { | ||
| @Override | ||
| public Action getAction() { | ||
| return RBA; | ||
| } | ||
|
|
||
| @Override | ||
| public Void getResult() { | ||
| return null; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static final class CountingAppSecContext implements AppSecContext { | ||
| private int blockFailures; | ||
|
|
||
| @Override | ||
| public boolean isManuallyKept() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void reportBlockFailure() { | ||
| blockFailures++; | ||
| } | ||
| } | ||
|
|
||
| private static final class TestBlockResponseFunction implements BlockResponseFunction { | ||
| private final boolean committed; | ||
| private TraceSegment lastSegment; | ||
| private Flow.Action.RequestBlockingAction lastAction; | ||
|
|
||
| private TestBlockResponseFunction(boolean committed) { | ||
| this.committed = committed; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean tryCommitBlockingResponse( | ||
| TraceSegment segment, Flow.Action.RequestBlockingAction rba) { | ||
| this.lastAction = rba; | ||
| return BlockResponseFunction.super.tryCommitBlockingResponse(segment, rba); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean tryCommitBlockingResponse( | ||
| TraceSegment segment, | ||
| int statusCode, | ||
| BlockingContentType templateType, | ||
| Map<String, String> extraHeaders, | ||
| String securityResponseId) { | ||
| this.lastSegment = segment; | ||
| return committed; | ||
| } | ||
| } | ||
|
|
||
| private static final class TestRequestContext implements RequestContext { | ||
| private final TestBlockResponseFunction brf; | ||
| private final Object appSecData; | ||
| private final TraceSegment traceSegment = TraceSegment.NoOp.INSTANCE; | ||
|
|
||
| private TestRequestContext(TestBlockResponseFunction brf, Object appSecData) { | ||
| this.brf = brf; | ||
| this.appSecData = appSecData; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <T> T getData(RequestContextSlot slot) { | ||
| return slot == RequestContextSlot.APPSEC ? (T) appSecData : null; | ||
| } | ||
|
|
||
| @Override | ||
| public TraceSegment getTraceSegment() { | ||
| return traceSegment; | ||
| } | ||
|
|
||
| @Override | ||
| public void setBlockResponseFunction(BlockResponseFunction blockResponseFunction) {} | ||
|
|
||
| @Override | ||
| public BlockResponseFunction getBlockResponseFunction() { | ||
| return brf; | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T getOrCreateMetaStructTop(String key, Function<String, T> defaultValue) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void setClientIpAddressData(ClientIpAddressData clientIpAddressData) {} | ||
|
|
||
| @Override | ||
| public ClientIpAddressData getClientIpAddressData() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.