-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASM] Stack trace leak vulnerability detection (#5067)
* Add sample code * Add stacktrace leak sinks and metric * Add asp net >= 5 case * Vulnerability implementation * Add integration tests * Fix error * add snapshots * Update test * add snapshot * Fix * Add nuget integration package * EveryMemberOfTypeNamesIsRepresented fix * Add integration extension * Fix unit test * stacktrace leak sink tag * update snapshot * Set developer tests flag * remove not needed file * Fix snapshots * Update autogenerated files * Update controllers code * update controllers * Update autogenerated files * Use 3 digits format in versions * Add not vulnerable test case. * Update tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/StackTraceLeak/DeveloperExceptionPageMiddlewareIntegrationBis.cs Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com> * Fix * nomenclature change. --------- Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com>
- Loading branch information
1 parent
cca9324
commit a00c0fa
Showing
51 changed files
with
1,017 additions
and
292 deletions.
There are no files selected for viewing
This file contains 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 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
49 changes: 49 additions & 0 deletions
49
...rofiler/AutoInstrumentation/StackTraceLeak/DeveloperExceptionPageMiddlewareIntegration.cs
This file contains 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,49 @@ | ||
// <copyright file="DeveloperExceptionPageMiddlewareIntegration.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> | ||
|
||
#nullable enable | ||
|
||
#if !NETFRAMEWORK | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using Datadog.Trace.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Configuration; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.StackTraceLeak; | ||
|
||
/// <summary> | ||
/// DeveloperExceptionPageMiddleware integration | ||
/// </summary> | ||
[InstrumentMethod( | ||
AssemblyName = "Microsoft.AspNetCore.Diagnostics", | ||
TypeName = "Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware", | ||
ParameterTypeNames = new[] { "Microsoft.AspNetCore.Http.HttpContext", ClrNames.Exception }, | ||
MethodName = "DisplayException", | ||
ReturnTypeName = ClrNames.Task, | ||
MinimumVersion = "2.0.0", | ||
MaximumVersion = "2.*.*", | ||
IntegrationName = nameof(IntegrationId.StackTraceLeak), | ||
InstrumentationCategory = InstrumentationCategory.Iast)] | ||
|
||
[Browsable(false)] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class DeveloperExceptionPageMiddlewareIntegration | ||
{ | ||
/// <summary> | ||
/// OnMethodBegin callback | ||
/// </summary> | ||
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param> | ||
/// <param name="context">The context of the error.</param> | ||
/// <param name="exception">The exception to be shown.</param> | ||
/// <typeparam name="TTarget">Type of the target</typeparam> | ||
/// <returns>Calltarget state value</returns> | ||
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, HttpContext context, Exception exception) | ||
{ | ||
return StackTraceLeakIntegrationCommon.OnExceptionLeak(IntegrationId.StackTraceLeak, exception); | ||
} | ||
} | ||
#endif |
70 changes: 70 additions & 0 deletions
70
...toInstrumentation/StackTraceLeak/DeveloperExceptionPageMiddlewareIntegration_Pre_3_0_0.cs
This file contains 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,70 @@ | ||
// <copyright file="DeveloperExceptionPageMiddlewareIntegration_Pre_3_0_0.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> | ||
|
||
#nullable enable | ||
|
||
#if !NETFRAMEWORK | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Reflection; | ||
using Datadog.Trace.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Configuration; | ||
using Datadog.Trace.DuckTyping; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.StackTraceLeak; | ||
|
||
/// <summary> | ||
/// DeveloperExceptionPageMiddlewareImpl integration | ||
/// </summary> | ||
[InstrumentMethod( | ||
AssemblyName = "Microsoft.AspNetCore.Diagnostics", | ||
TypeName = "Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware", | ||
ParameterTypeNames = new[] { "Microsoft.AspNetCore.Diagnostics.ErrorContext" }, | ||
MethodName = "DisplayException", | ||
ReturnTypeName = ClrNames.Task, | ||
MinimumVersion = "3.0.0", | ||
MaximumVersion = "6.*.*", | ||
IntegrationName = nameof(IntegrationId.StackTraceLeak), | ||
InstrumentationCategory = InstrumentationCategory.Iast)] | ||
[InstrumentMethod( | ||
AssemblyName = "Microsoft.AspNetCore.Diagnostics", | ||
TypeName = "Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl", | ||
ParameterTypeNames = new[] { "Microsoft.AspNetCore.Diagnostics.ErrorContext" }, | ||
MethodName = "DisplayException", | ||
ReturnTypeName = ClrNames.Task, | ||
MinimumVersion = "7.0.0", | ||
MaximumVersion = "8.*.*", | ||
IntegrationName = nameof(IntegrationId.StackTraceLeak), | ||
InstrumentationCategory = InstrumentationCategory.Iast)] | ||
|
||
[Browsable(false)] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class DeveloperExceptionPageMiddlewareIntegration_Pre_3_0_0 | ||
{ | ||
internal interface IErrorContext | ||
{ | ||
public Exception Exception { get; } | ||
} | ||
|
||
/// <summary> | ||
/// OnMethodBegin callback | ||
/// </summary> | ||
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param> | ||
/// <param name="errorContext">The context of the error.</param> | ||
/// <typeparam name="TTarget">Type of the target</typeparam> | ||
/// <typeparam name="TContext">ErrorContext type</typeparam> | ||
/// <returns>Calltarget state value</returns> | ||
internal static CallTargetState OnMethodBegin<TTarget, TContext>(TTarget instance, TContext errorContext) | ||
where TContext : IErrorContext | ||
{ | ||
// In the current implementation ErrorContext is always non-null, as is Exception | ||
// so this should be safe | ||
var exception = errorContext.Exception; | ||
return StackTraceLeakIntegrationCommon.OnExceptionLeak(IntegrationId.StackTraceLeak, exception); | ||
} | ||
} | ||
|
||
#endif |
55 changes: 55 additions & 0 deletions
55
...c/Datadog.Trace/ClrProfiler/AutoInstrumentation/StackTraceLeak/HttpResponseIntegration.cs
This file contains 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,55 @@ | ||
// <copyright file="HttpResponseIntegration.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> | ||
|
||
#nullable enable | ||
|
||
#if NETFRAMEWORK | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Web; | ||
using Datadog.Trace.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Configuration; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.StackTraceLeak; | ||
|
||
/// <summary> | ||
/// HttpResponseIntegration integration | ||
/// </summary> | ||
[InstrumentMethod( | ||
AssemblyName = "System.Web", | ||
TypeName = "System.Web.HttpResponse", | ||
ParameterTypeNames = new[] { ClrNames.Exception, ClrNames.Bool }, | ||
MethodName = "WriteErrorMessage", | ||
ReturnTypeName = ClrNames.Void, | ||
MinimumVersion = "4.0.0", | ||
MaximumVersion = "4.*.*", | ||
IntegrationName = nameof(IntegrationId.StackTraceLeak), | ||
InstrumentationCategory = InstrumentationCategory.Iast)] | ||
|
||
[Browsable(false)] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class HttpResponseIntegration | ||
{ | ||
/// <summary> | ||
/// OnMethodBegin callback | ||
/// </summary> | ||
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param> | ||
/// <param name="exception">The exception to be shown.</param> | ||
/// <param name="dontShowSensitiveErrors">The dontShowSensitiveErrors parameter of WriteErrorMessage.</param> | ||
/// <typeparam name="TTarget">Type of the target</typeparam> | ||
/// <returns>Calltarget state value</returns> | ||
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, Exception exception, bool dontShowSensitiveErrors) | ||
{ | ||
if (HttpRuntime.UsingIntegratedPipeline && !dontShowSensitiveErrors) | ||
{ | ||
return StackTraceLeakIntegrationCommon.OnExceptionLeak(IntegrationId.StackTraceLeak, exception); | ||
} | ||
|
||
return CallTargetState.GetDefault(); | ||
} | ||
} | ||
|
||
#endif |
40 changes: 40 additions & 0 deletions
40
...g.Trace/ClrProfiler/AutoInstrumentation/StackTraceLeak/StackTraceLeakIntegrationCommon.cs
This file contains 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,40 @@ | ||
// <copyright file="StackTraceLeakIntegrationCommon.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.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Configuration; | ||
using Datadog.Trace.Iast; | ||
using Datadog.Trace.Logging; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.StackTraceLeak; | ||
|
||
#nullable enable | ||
internal static class StackTraceLeakIntegrationCommon | ||
{ | ||
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(StackTraceLeakIntegrationCommon)); | ||
|
||
internal static CallTargetState OnExceptionLeak(IntegrationId integrationId, Exception exception) | ||
{ | ||
if (!Tracer.Instance.Settings.IsIntegrationEnabled(integrationId)) | ||
{ | ||
return CallTargetState.GetDefault(); | ||
} | ||
|
||
try | ||
{ | ||
if (exception is not null) | ||
{ | ||
return new CallTargetState(IastModule.OnStackTraceLeak(exception, integrationId).SingleSpan); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error(ex, $"Error in {nameof(OnExceptionLeak)}."); | ||
} | ||
|
||
return CallTargetState.GetDefault(); | ||
} | ||
} |
This file contains 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 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 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 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 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 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.