-
Notifications
You must be signed in to change notification settings - Fork 141
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
[IAST] Improve RestSharp SSRF detection #6060
Changes from all commits
be0d436
3a9d354
a8d0aa2
794a5f9
e422bd0
dbe2e12
d637718
7e72e6e
2138428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. Could you add to the 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. Done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// <copyright file="UrlEncode2Integration.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 | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using Datadog.Trace.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Iast; | ||
using Datadog.Trace.Vendors.Serilog; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.RestSharp; | ||
|
||
/// <summary> | ||
/// System.Security.Cryptography.HashAlgorithm instrumentation | ||
/// </summary> | ||
[InstrumentMethod( | ||
AssemblyName = "RestSharp", | ||
TypeName = "RestSharp.Extensions.StringExtensions", | ||
MethodName = "UrlEncode", | ||
ReturnTypeName = ClrNames.String, | ||
ParameterTypeNames = new[] { ClrNames.String, "System.Text.Encoding" }, | ||
MinimumVersion = "104.0.0", | ||
MaximumVersion = "112.*.*", | ||
InstrumentationCategory = InstrumentationCategory.Iast, | ||
IntegrationName = nameof(Configuration.IntegrationId.Ssrf))] | ||
[Browsable(false)] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public class UrlEncode2Integration | ||
{ | ||
private static bool errorLogged = false; | ||
|
||
/// <summary> | ||
/// OnMethodBegin callback | ||
/// </summary> | ||
/// <param name="value">String being escaped.</param> | ||
/// <param name="encoding">Encoding being used.</param> | ||
/// <returns>Calltarget state value</returns> | ||
internal static CallTargetState OnMethodBegin<TTarget>(string value, System.Text.Encoding encoding) | ||
{ | ||
return new CallTargetState(null, value); | ||
} | ||
|
||
/// <summary> | ||
/// OnMethodEnd callback | ||
/// </summary> | ||
/// <typeparam name="TTarget">Type of the target</typeparam> | ||
/// <typeparam name="TReturn">Type of the return value</typeparam> | ||
/// <param name="returnValue">Return value.</param> | ||
/// <param name="exception">Exception instance in case the original code threw an exception.</param> | ||
/// <param name="state">Calltarget state value</param> | ||
/// <returns>CallTargetReturn</returns> | ||
internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TReturn returnValue, Exception exception, CallTargetState state) | ||
{ | ||
try | ||
{ | ||
if (exception is null && returnValue is string value) | ||
{ | ||
if (state.State is string input) | ||
{ | ||
var newValue = IastModule.OnSsrfEscape(input, value); | ||
if (newValue is not null) | ||
{ | ||
returnValue = (TReturn)(object)newValue; | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
if (!errorLogged) | ||
{ | ||
Log.Error(e, "Error escaping Url with encoding"); | ||
errorLogged = true; | ||
} | ||
} | ||
|
||
return new CallTargetReturn<TReturn>(returnValue); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// <copyright file="UrlEncodeIntegration.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 | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using Datadog.Trace.ClrProfiler.CallTarget; | ||
using Datadog.Trace.Iast; | ||
using Datadog.Trace.Vendors.Serilog; | ||
|
||
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.RestSharp; | ||
|
||
/// <summary> | ||
/// System.Security.Cryptography.HashAlgorithm instrumentation | ||
/// </summary> | ||
[InstrumentMethod( | ||
AssemblyName = "RestSharp", | ||
TypeName = "RestSharp.Extensions.StringExtensions", | ||
MethodName = "UrlEncode", | ||
ReturnTypeName = ClrNames.String, | ||
ParameterTypeNames = new[] { ClrNames.String }, | ||
MinimumVersion = "104.0.0", | ||
MaximumVersion = "112.*.*", | ||
InstrumentationCategory = InstrumentationCategory.Iast, | ||
IntegrationName = nameof(Configuration.IntegrationId.Ssrf))] | ||
[Browsable(false)] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public class UrlEncodeIntegration | ||
{ | ||
private static bool errorLogged = false; | ||
|
||
/// <summary> | ||
/// OnMethodBegin callback | ||
/// </summary> | ||
/// <param name="value">String being escaped.</param> | ||
/// <returns>Calltarget state value</returns> | ||
internal static CallTargetState OnMethodBegin<TTarget>(string value) | ||
{ | ||
return new CallTargetState(null, value); | ||
} | ||
|
||
/// <summary> | ||
/// OnMethodEnd callback | ||
/// </summary> | ||
/// <typeparam name="TTarget">Type of the target</typeparam> | ||
/// <typeparam name="TReturn">Type of the return value</typeparam> | ||
/// <param name="returnValue">Return value.</param> | ||
/// <param name="exception">Exception instance in case the original code threw an exception.</param> | ||
/// <param name="state">Calltarget state value</param> | ||
/// <returns>CallTargetReturn</returns> | ||
internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TReturn returnValue, Exception exception, CallTargetState state) | ||
{ | ||
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. NIT: Since the code of this method is the same as the one in UrlEncode2Integration.cs, it would probably make sense to create a common method that is called from both instrumentation points. |
||
try | ||
{ | ||
if (exception is null && returnValue is string value) | ||
{ | ||
if (state.State is string input) | ||
{ | ||
var newValue = IastModule.OnSsrfEscape(input, value); | ||
if (newValue is not null) | ||
{ | ||
returnValue = (TReturn)(object)newValue; | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
if (!errorLogged) | ||
{ | ||
Log.Error(e, "Error escaping Url"); | ||
errorLogged = true; | ||
} | ||
} | ||
|
||
return new CallTargetReturn<TReturn>(returnValue); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that these changes are done automatically when compiling the solution?