Commit efe1444
[AAP] Collect DataContract JSON response body schemas (#8706)
## Summary of changes
Adds API Security response body schema collection for ASP.NET MVC/Web
API responses serialized with
`DataContractJsonSerializer.WriteObject(response.OutputStream, data)`
([doc](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer.writeobject?view=netframework-4.8.1)).
This adds a .NET Framework AppSec instrumentation for
`DataContractJsonSerializer.WriteObject(Stream, object)`. The hook only
reports when the serializer writes to the current ASP.NET response
stream, AppSec is enabled, response body parsing is enabled, and an
ASP.NET MVC/Web API scope is active.
It also adds a `DataContractObjectExtractor` for this path. For
`[DataContract]` types, it reports only `[DataMember]` members, uses
`DataMember(Name = ...)`, and skips ignored or unmarked members.
## Reason for change
A case seen writes the response with `DataContractJsonSerializer`
directly to `HttpResponse.OutputStream`, which bypasses the existing
`JsonResult.Data` response body extraction.
Without this instrumentation, those endpoints skipped AAP API Security
traces.
## Implementation details
- Instruments
`System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject(Stream,
object)`
- Captures after successful serialization, using the original graph
object instead of buffering response bytes.
- Requires the stream to be the current
`HttpContext.Response.OutputStream`, so serializing to `MemoryStream`,
files, or other streams are ignored.
## Test coverage
- Added unit coverage for DataContract member naming and over-capture
prevention.
- Added an MVC5 sample endpoint that returns a custom `ActionResult` and
calls `DataContractJsonSerializer.WriteObject(response.OutputStream,
data)`.
- Added API Security snapshots for the new MVC5 scenario with API
Security enabled and disabled.
## Other details
https://datadoghq.atlassian.net/browse/APPSEC-68123
---------
Co-authored-by: Daniel Romano <daniel.romanolorente@datadoghq.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 4937b53 commit efe1444
18 files changed
Lines changed: 2151 additions & 178 deletions
File tree
- tracer
- build
- _build/Honeypot
- src
- Datadog.Tracer.Native
- Generated
- Datadog.Trace
- AppSec
- ClrProfiler/AutoInstrumentation/AspNet
- Generated/net461/Datadog.Trace.SourceGenerators/InstrumentationDefinitionsGenerator
- test
- Datadog.Trace.Security.IntegrationTests/ApiSecurity
- Datadog.Trace.Security.Unit.Tests
- snapshots
- test-applications/security/aspnet/Samples.Security.AspNetMvc5
- Controllers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
857 | 857 | | |
858 | 858 | | |
859 | 859 | | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
860 | 935 | | |
861 | 936 | | |
862 | 937 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
60 | 67 | | |
61 | 68 | | |
62 | 69 | | |
| |||
1550 | 1557 | | |
1551 | 1558 | | |
1552 | 1559 | | |
1553 | | - | |
| 1560 | + | |
0 commit comments