Skip to content
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

[ASM] IAST events in span's meta_struct #5803

Merged
merged 9 commits into from
Dec 3, 2024
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
76 changes: 76 additions & 0 deletions tracer/src/Datadog.Trace/AppSec/Rasp/MetaStructHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using Datadog.Trace.Iast;
using Datadog.Trace.Vendors.MessagePack.Formatters;
using Datadog.Trace.Vendors.MessagePack.Resolvers;

Expand Down Expand Up @@ -81,6 +82,81 @@ public static Dictionary<string, object> StackFrameToDictionary(uint id, string?
return dict;
}

public static Dictionary<string, object> VulnerabilityBatchToDictionary(VulnerabilityBatch vulnerabilityBatch)
{
var result = new Dictionary<string, object>(2);

var truncationMaxValueLength = vulnerabilityBatch.GetTruncationMaxValueLength();
var redactionEnabled = vulnerabilityBatch.IsRedactionEnabled();

if (vulnerabilityBatch.Vulnerabilities.Count > 0)
{
var vulnerabilitiesList = new List<Dictionary<string, object>>(vulnerabilityBatch.Vulnerabilities.Count);
foreach (var vulnerability in vulnerabilityBatch.Vulnerabilities)
{
var vulnerabilityDictionary = new Dictionary<string, object>();

vulnerabilityDictionary["type"] = vulnerability.Type;
vulnerabilityDictionary["hash"] = vulnerability.Hash;

if (vulnerability.Location.HasValue)
{
var locationDict = new Dictionary<string, object>();
var location = vulnerability.Location.Value;

if (location.SpanId.HasValue)
{
locationDict["spanId"] = location.SpanId.Value;
}

if (location.Path is { Length: > 0 })
{
locationDict["path"] = location.Path;
}

if (location.Method is { Length: > 0 })
{
locationDict["method"] = location.Method;
}

if (location.Line.HasValue)
{
locationDict["line"] = location.Line.Value;
}

if (location.StackId is { Length: > 0 })
{
locationDict["stackId"] = location.StackId;
}

vulnerabilityDictionary["location"] = locationDict;
}

if (vulnerability.Evidence.HasValue)
{
vulnerabilityDictionary["evidence"] = new EvidenceDictionaryConverter(truncationMaxValueLength, redactionEnabled).EvidenceToDictionary(vulnerability.Evidence.Value);
}

vulnerabilitiesList.Add(vulnerabilityDictionary);
}

result["vulnerabilities"] = vulnerabilitiesList;
}

if (vulnerabilityBatch.Sources != null)
{
var sourcesList = new List<Dictionary<string, object>>(vulnerabilityBatch.Sources.Count);
foreach (var source in vulnerabilityBatch.Sources)
{
sourcesList.Add(source.ToDictionary(truncationMaxValueLength));
}

result["sources"] = sourcesList;
}

return result;
}

public static byte[] ObjectToByteArray(object? value)
{
// 256 is the size that the serializer would reserve initially for empty arrays, so we create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ partial class IastTags
{
// IastJsonBytes = MessagePack.Serialize("_dd.iast.json");
private static ReadOnlySpan<byte> IastJsonBytes => new byte[] { 173, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110 };
// IastJsonTagSizeExceededBytes = MessagePack.Serialize("_dd.iast.json.tag.size.exceeded");
private static ReadOnlySpan<byte> IastJsonTagSizeExceededBytes => new byte[] { 191, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110, 46, 116, 97, 103, 46, 115, 105, 122, 101, 46, 101, 120, 99, 101, 101, 100, 101, 100 };
// IastEnabledBytes = MessagePack.Serialize("_dd.iast.enabled");
private static ReadOnlySpan<byte> IastEnabledBytes => new byte[] { 176, 95, 100, 100, 46, 105, 97, 115, 116, 46, 101, 110, 97, 98, 108, 101, 100 };

Expand All @@ -26,7 +24,6 @@ partial class IastTags
return key switch
{
"_dd.iast.json" => IastJson,
"_dd.iast.json.tag.size.exceeded" => IastJsonTagSizeExceeded,
"_dd.iast.enabled" => IastEnabled,
_ => base.GetTag(key),
};
Expand All @@ -39,9 +36,6 @@ public override void SetTag(string key, string value)
case "_dd.iast.json":
IastJson = value;
break;
case "_dd.iast.json.tag.size.exceeded":
IastJsonTagSizeExceeded = value;
break;
case "_dd.iast.enabled":
IastEnabled = value;
break;
Expand All @@ -58,11 +52,6 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
processor.Process(new TagItem<string>("_dd.iast.json", IastJson, IastJsonBytes));
}

if (IastJsonTagSizeExceeded is not null)
{
processor.Process(new TagItem<string>("_dd.iast.json.tag.size.exceeded", IastJsonTagSizeExceeded, IastJsonTagSizeExceededBytes));
}

if (IastEnabled is not null)
{
processor.Process(new TagItem<string>("_dd.iast.enabled", IastEnabled, IastEnabledBytes));
Expand All @@ -80,13 +69,6 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
.Append(',');
}

if (IastJsonTagSizeExceeded is not null)
{
sb.Append("_dd.iast.json.tag.size.exceeded (tag):")
.Append(IastJsonTagSizeExceeded)
.Append(',');
}

if (IastEnabled is not null)
{
sb.Append("_dd.iast.enabled (tag):")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ partial class IastTags
{
// IastJsonBytes = MessagePack.Serialize("_dd.iast.json");
private static ReadOnlySpan<byte> IastJsonBytes => new byte[] { 173, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110 };
// IastJsonTagSizeExceededBytes = MessagePack.Serialize("_dd.iast.json.tag.size.exceeded");
private static ReadOnlySpan<byte> IastJsonTagSizeExceededBytes => new byte[] { 191, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110, 46, 116, 97, 103, 46, 115, 105, 122, 101, 46, 101, 120, 99, 101, 101, 100, 101, 100 };
// IastEnabledBytes = MessagePack.Serialize("_dd.iast.enabled");
private static ReadOnlySpan<byte> IastEnabledBytes => new byte[] { 176, 95, 100, 100, 46, 105, 97, 115, 116, 46, 101, 110, 97, 98, 108, 101, 100 };

Expand All @@ -26,7 +24,6 @@ partial class IastTags
return key switch
{
"_dd.iast.json" => IastJson,
"_dd.iast.json.tag.size.exceeded" => IastJsonTagSizeExceeded,
"_dd.iast.enabled" => IastEnabled,
_ => base.GetTag(key),
};
Expand All @@ -39,9 +36,6 @@ public override void SetTag(string key, string value)
case "_dd.iast.json":
IastJson = value;
break;
case "_dd.iast.json.tag.size.exceeded":
IastJsonTagSizeExceeded = value;
break;
case "_dd.iast.enabled":
IastEnabled = value;
break;
Expand All @@ -58,11 +52,6 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
processor.Process(new TagItem<string>("_dd.iast.json", IastJson, IastJsonBytes));
}

if (IastJsonTagSizeExceeded is not null)
{
processor.Process(new TagItem<string>("_dd.iast.json.tag.size.exceeded", IastJsonTagSizeExceeded, IastJsonTagSizeExceededBytes));
}

if (IastEnabled is not null)
{
processor.Process(new TagItem<string>("_dd.iast.enabled", IastEnabled, IastEnabledBytes));
Expand All @@ -80,13 +69,6 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
.Append(',');
}

if (IastJsonTagSizeExceeded is not null)
{
sb.Append("_dd.iast.json.tag.size.exceeded (tag):")
.Append(IastJsonTagSizeExceeded)
.Append(',');
}

if (IastEnabled is not null)
{
sb.Append("_dd.iast.enabled (tag):")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ partial class IastTags
{
// IastJsonBytes = MessagePack.Serialize("_dd.iast.json");
private static ReadOnlySpan<byte> IastJsonBytes => new byte[] { 173, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110 };
// IastJsonTagSizeExceededBytes = MessagePack.Serialize("_dd.iast.json.tag.size.exceeded");
private static ReadOnlySpan<byte> IastJsonTagSizeExceededBytes => new byte[] { 191, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110, 46, 116, 97, 103, 46, 115, 105, 122, 101, 46, 101, 120, 99, 101, 101, 100, 101, 100 };
// IastEnabledBytes = MessagePack.Serialize("_dd.iast.enabled");
private static ReadOnlySpan<byte> IastEnabledBytes => new byte[] { 176, 95, 100, 100, 46, 105, 97, 115, 116, 46, 101, 110, 97, 98, 108, 101, 100 };

Expand All @@ -26,7 +24,6 @@ partial class IastTags
return key switch
{
"_dd.iast.json" => IastJson,
"_dd.iast.json.tag.size.exceeded" => IastJsonTagSizeExceeded,
"_dd.iast.enabled" => IastEnabled,
_ => base.GetTag(key),
};
Expand All @@ -39,9 +36,6 @@ public override void SetTag(string key, string value)
case "_dd.iast.json":
IastJson = value;
break;
case "_dd.iast.json.tag.size.exceeded":
IastJsonTagSizeExceeded = value;
break;
case "_dd.iast.enabled":
IastEnabled = value;
break;
Expand All @@ -58,11 +52,6 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
processor.Process(new TagItem<string>("_dd.iast.json", IastJson, IastJsonBytes));
}

if (IastJsonTagSizeExceeded is not null)
{
processor.Process(new TagItem<string>("_dd.iast.json.tag.size.exceeded", IastJsonTagSizeExceeded, IastJsonTagSizeExceededBytes));
}

if (IastEnabled is not null)
{
processor.Process(new TagItem<string>("_dd.iast.enabled", IastEnabled, IastEnabledBytes));
Expand All @@ -80,13 +69,6 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
.Append(',');
}

if (IastJsonTagSizeExceeded is not null)
{
sb.Append("_dd.iast.json.tag.size.exceeded (tag):")
.Append(IastJsonTagSizeExceeded)
.Append(',');
}

if (IastEnabled is not null)
{
sb.Append("_dd.iast.enabled (tag):")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ partial class IastTags
{
// IastJsonBytes = MessagePack.Serialize("_dd.iast.json");
private static ReadOnlySpan<byte> IastJsonBytes => new byte[] { 173, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110 };
// IastJsonTagSizeExceededBytes = MessagePack.Serialize("_dd.iast.json.tag.size.exceeded");
private static ReadOnlySpan<byte> IastJsonTagSizeExceededBytes => new byte[] { 191, 95, 100, 100, 46, 105, 97, 115, 116, 46, 106, 115, 111, 110, 46, 116, 97, 103, 46, 115, 105, 122, 101, 46, 101, 120, 99, 101, 101, 100, 101, 100 };
// IastEnabledBytes = MessagePack.Serialize("_dd.iast.enabled");
private static ReadOnlySpan<byte> IastEnabledBytes => new byte[] { 176, 95, 100, 100, 46, 105, 97, 115, 116, 46, 101, 110, 97, 98, 108, 101, 100 };

Expand All @@ -26,7 +24,6 @@ partial class IastTags
return key switch
{
"_dd.iast.json" => IastJson,
"_dd.iast.json.tag.size.exceeded" => IastJsonTagSizeExceeded,
"_dd.iast.enabled" => IastEnabled,
_ => base.GetTag(key),
};
Expand All @@ -39,9 +36,6 @@ public override void SetTag(string key, string value)
case "_dd.iast.json":
IastJson = value;
break;
case "_dd.iast.json.tag.size.exceeded":
IastJsonTagSizeExceeded = value;
break;
case "_dd.iast.enabled":
IastEnabled = value;
break;
Expand All @@ -58,11 +52,6 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
processor.Process(new TagItem<string>("_dd.iast.json", IastJson, IastJsonBytes));
}

if (IastJsonTagSizeExceeded is not null)
{
processor.Process(new TagItem<string>("_dd.iast.json.tag.size.exceeded", IastJsonTagSizeExceeded, IastJsonTagSizeExceededBytes));
}

if (IastEnabled is not null)
{
processor.Process(new TagItem<string>("_dd.iast.enabled", IastEnabled, IastEnabledBytes));
Expand All @@ -80,13 +69,6 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
.Append(',');
}

if (IastJsonTagSizeExceeded is not null)
{
sb.Append("_dd.iast.json.tag.size.exceeded (tag):")
.Append(IastJsonTagSizeExceeded)
.Append(',');
}

if (IastEnabled is not null)
{
sb.Append("_dd.iast.enabled (tag):")
Expand Down
Loading
Loading