-
Notifications
You must be signed in to change notification settings - Fork 165
[ASM] Blocking a request on .NET Core #3066
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
00aa6d3
add basic blocking
anna-git 0033450
blocking implementation
anna-git 5ba4486
add an endpoint to test if exception
anna-git 166e0dc
throw if should be blocked!
anna-git 7cb48e2
Change to just everything at start and call finish after
anna-git 0d454e0
dont forget the addresponseheaders before firing
anna-git 2f71c83
Fixing tests
anna-git 22ea8c4
Running waf several times per request
anna-git 37ecfab
Add blocking templates
anna-git b13b341
fix unit tests
anna-git fb9db61
fix 404 inte tests
anna-git b4a32a9
update launch settings
anna-git 8e5a950
fixes added info on spans
anna-git 37f2292
fix lifecycle events
anna-git d48ff41
adding new address httpclient ip to waf
anna-git 8e5041f
addin snapshot files
anna-git e6e0365
allow synchronous operatoin for net core >=3.1
anna-git 0131cb3
change snapshot
anna-git 802b812
dispose differently
anna-git b70ea82
Fix integration tests regex scrubber because different bytes on linux
anna-git 973f0f4
Change in configuration keys naming
anna-git 5f7c1a1
Response to comments 1
anna-git 5a488de
response dani's comments
anna-git f6f98e9
default to json template for blocked response
anna-git f437137
remove probes definitions.json fix to come
anna-git 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // <copyright file="BlockException.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
|
|
||
| namespace Datadog.Trace.AppSec | ||
| { | ||
| internal class BlockException : Exception | ||
| { | ||
| internal BlockException() | ||
| { | ||
| } | ||
|
|
||
| internal BlockException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
|
|
||
| internal BlockException(string message, Exception inner) | ||
| : base(message, inner) | ||
| { | ||
| } | ||
|
|
||
| internal BlockException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) | ||
| : base(info, context) | ||
| { | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| using System.Web.Routing; | ||
| #endif | ||
| using Datadog.Trace.AppSec.Transports.Http; | ||
| using Datadog.Trace.Configuration; | ||
| using Datadog.Trace.Logging; | ||
| using Datadog.Trace.Util.Http; | ||
| using Datadog.Trace.Vendors.Serilog.Events; | ||
|
|
@@ -26,35 +27,42 @@ internal partial class InstrumentationGateway | |
|
|
||
| public event EventHandler<InstrumentationGatewaySecurityEventArgs> PathParamsAvailable; | ||
|
|
||
| public event EventHandler<InstrumentationGatewaySecurityEventArgs> StartRequest; | ||
|
|
||
| public event EventHandler<InstrumentationGatewaySecurityEventArgs> EndRequest; | ||
|
|
||
| public event EventHandler<InstrumentationGatewaySecurityEventArgs> BodyAvailable; | ||
|
|
||
| public event EventHandler<InstrumentationGatewayEventArgs> LastChanceToWriteTags; | ||
|
|
||
| public void RaiseEndRequest(HttpContext context, HttpRequest request, Span relatedSpan) | ||
| public event EventHandler<InstrumentationGatewayBlockingEventArgs> BlockingOpportunity; | ||
|
|
||
| public void RaiseRequestEnd(HttpContext context, HttpRequest request, Span relatedSpan) | ||
| { | ||
| var getEventData = () => | ||
| { | ||
| var eventData = request.PrepareArgsForWaf(); | ||
| var eventData = request.PrepareArgsForWaf(relatedSpan); | ||
| eventData.Add(AddressesConstants.ResponseStatus, context.Response.StatusCode.ToString()); | ||
| return eventData; | ||
| }; | ||
|
|
||
| RaiseEvent(context, relatedSpan, getEventData, EndRequest); | ||
| } | ||
|
|
||
| public void RaiseRequestStart(HttpContext context, HttpRequest request, Span relatedSpan) | ||
| { | ||
| var getEventData = () => request.PrepareArgsForWaf(relatedSpan); | ||
| RaiseEvent(context, relatedSpan, getEventData, StartRequest); | ||
| } | ||
|
|
||
| public void RaisePathParamsAvailable(HttpContext context, Span relatedSpan, IDictionary<string, object> pathParams, bool eraseExistingAddress = true) => RaiseEvent(context, relatedSpan, () => new Dictionary<string, object> { { AddressesConstants.RequestPathParams, pathParams } }, PathParamsAvailable, eraseExistingAddress); | ||
|
|
||
| public void RaiseBodyAvailable(HttpContext context, Span relatedSpan, object body) | ||
| { | ||
| var getEventData = () => | ||
| { | ||
| var keysAndValues = BodyExtractor.Extract(body); | ||
| var eventData = new Dictionary<string, object> | ||
| { | ||
| { AddressesConstants.RequestBody, keysAndValues } | ||
| }; | ||
| var eventData = new Dictionary<string, object> { { AddressesConstants.RequestBody, keysAndValues } }; | ||
| return eventData; | ||
| }; | ||
|
|
||
|
|
@@ -95,10 +103,15 @@ private void RaiseEvent(HttpContext context, Span relatedSpan, Func<IDictionary< | |
| LogAddressIfDebugEnabled(eventData); | ||
| eventHandler.Invoke(this, new InstrumentationGatewaySecurityEventArgs(eventData, transport, relatedSpan, eraseExistingAddress)); | ||
| } | ||
| catch (Exception ex) | ||
| catch (Exception ex) when (ex is not BlockException) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elegant and not common solution 👍 |
||
| { | ||
| Log.Error(ex, "DDAS-0004-00: AppSec failed to process request."); | ||
| } | ||
| } | ||
|
|
||
| internal void RaiseBlockingOpportunity(HttpContext context, Scope scope, ImmutableTracerSettings tracerSettings, Action<InstrumentationGatewayBlockingEventArgs> doBeforeActualBlocking = null) | ||
| { | ||
| BlockingOpportunity?.Invoke(this, new InstrumentationGatewayBlockingEventArgs(context, scope, tracerSettings, doBeforeActualBlocking)); | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
tracer/src/Datadog.Trace/AppSec/InstrumentationGatewayBlockingEventArgs.cs
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,48 @@ | ||
| // <copyright file="InstrumentationGatewayBlockingEventArgs.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using Datadog.Trace.AppSec.Transports; | ||
| using Datadog.Trace.AppSec.Transports.Http; | ||
| using Datadog.Trace.Configuration; | ||
| #if NETFRAMEWORK | ||
| using System.Web; | ||
| using System.Web.Routing; | ||
| #else | ||
| using Microsoft.AspNetCore.Http; | ||
| #endif | ||
|
|
||
| namespace Datadog.Trace.AppSec | ||
| { | ||
| internal class InstrumentationGatewayBlockingEventArgs : EventArgs | ||
| { | ||
| internal InstrumentationGatewayBlockingEventArgs(HttpContext context, Scope scope, ImmutableTracerSettings settings, Action<InstrumentationGatewayBlockingEventArgs> doBeforeBlocking = null) | ||
| { | ||
| Context = context; | ||
| Scope = scope; | ||
| TracerSettings = settings; | ||
| DoBeforeBlocking = doBeforeBlocking; | ||
| Transport = new HttpTransport(context); | ||
| } | ||
|
|
||
| internal HttpContext Context { get; } | ||
|
|
||
| internal ITransport Transport { get; } | ||
|
|
||
| internal Scope Scope { get; } | ||
|
|
||
| internal ImmutableTracerSettings TracerSettings { get; } | ||
|
|
||
| private Action<InstrumentationGatewayBlockingEventArgs> DoBeforeBlocking { get; } | ||
|
|
||
| internal void InvokeDoBeforeBlocking() | ||
| { | ||
| if (Scope != null && DoBeforeBlocking != null) | ||
| { | ||
| DoBeforeBlocking(this); | ||
| } | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.