Skip to content

Commit 58a51b6

Browse files
committed
fix: Read request data on System.Web
1 parent 947f98c commit 58a51b6

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

src/Sentry/Internal/Web/SystemWebRequestEventProcessor.cs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,97 @@
11
#if SYSTEM_WEB
22
using System;
3+
using System.Collections.Generic;
4+
using System.Security.Claims;
5+
using System.Security.Principal;
36
using System.Web;
47
using Sentry.Extensibility;
8+
using Sentry.Protocol;
59

610
namespace Sentry.Internal.Web
711
{
812
internal class SystemWebRequestEventProcessor : ISentryEventProcessor
913
{
14+
private readonly SentryOptions _options;
1015
internal IRequestPayloadExtractor PayloadExtractor { get; }
1116

12-
public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor)
13-
=> PayloadExtractor = payloadExtractor ?? throw new ArgumentNullException(nameof(payloadExtractor));
17+
public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor, SentryOptions options)
18+
{
19+
_options = options ?? throw new ArgumentNullException(nameof(options));
20+
PayloadExtractor = payloadExtractor ?? throw new ArgumentNullException(nameof(payloadExtractor));
21+
}
1422

1523
public SentryEvent Process(SentryEvent @event)
1624
{
17-
if (@event != null && HttpContext.Current?.Request is HttpRequest request)
25+
var context = HttpContext.Current;
26+
if (context == null || @event == null)
27+
{
28+
return @event;
29+
}
30+
31+
@event.Request.Method = context.Request.HttpMethod;
32+
@event.Request.Url = context.Request.Path;
33+
@event.Request.QueryString = context.Request.QueryString.ToString();
34+
35+
foreach (var key in context.Request.Headers.AllKeys)
36+
{
37+
38+
if (!_options.SendDefaultPii
39+
// Don't add headers which might contain PII
40+
&& (key == "Cookie"
41+
|| key == "Authorization"))
42+
{
43+
continue;
44+
}
45+
@event.Request.Headers[key] = context.Request.Headers[key];
46+
}
47+
48+
if (_options?.SendDefaultPii == true)
1849
{
19-
var body = PayloadExtractor.ExtractPayload(new SystemWebHttpRequest(request));
20-
if (body != null)
50+
@event.User.IpAddress = context.Request.UserHostAddress;
51+
if (context.User.Identity is IIdentity identity)
52+
{
53+
@event.User.Username = identity.Name;
54+
var other = new Dictionary<string, string>
55+
{
56+
{ "IsAuthenticated", identity.IsAuthenticated.ToString() }
57+
};
58+
@event.User.Other = other;
59+
}
60+
if (context.User is ClaimsPrincipal claimsPrincipal)
2161
{
22-
@event.Request.Data = body;
62+
if (claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier) is Claim claim)
63+
{
64+
@event.User.Id = claim.Value;
65+
}
2366
}
2467
}
68+
69+
@event.ServerName = Environment.MachineName;
70+
71+
// Move 'runtime' under key 'server-runtime' as User-Agent parsing done at
72+
// Sentry will represent the client's
73+
if (@event.Contexts.TryRemove(Runtime.Type, out var runtime))
74+
{
75+
@event.Contexts["server-runtime"] = runtime;
76+
}
77+
78+
if (@event.Contexts.TryRemove(Protocol.OperatingSystem.Type, out var os))
79+
{
80+
@event.Contexts["server-os"] = os;
81+
}
82+
83+
if (_options?.SendDefaultPii == true && @event.User.Username == Environment.UserName)
84+
{
85+
// if SendDefaultPii is true, Sentry SDK will send the current logged on user
86+
// which doesn't make sense in a server apps
87+
@event.User.Username = null;
88+
}
89+
90+
var body = PayloadExtractor.ExtractPayload(new SystemWebHttpRequest(context.Request));
91+
if (body != null)
92+
{
93+
@event.Request.Data = body;
94+
}
2595
return @event;
2696
}
2797
}

src/Sentry/SentryOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@ public SentryOptions()
349349
new DefaultRequestPayloadExtractor()
350350
},
351351
this,
352-
() => MaxRequestBodySize)));
352+
() => MaxRequestBodySize),
353+
this));
353354
#endif
355+
354356
ExceptionProcessors
355357
= ImmutableList.Create<ISentryEventExceptionProcessor>(
356358
new MainExceptionProcessor(this, sentryStackTraceFactory));

test/Sentry.Tests/Internals/Web/SystemWebRequestEventProcessorTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SystemWebRequestEventProcessorTests
1414
private class Fixture
1515
{
1616
public IRequestPayloadExtractor RequestPayloadExtractor { get; set; } = Substitute.For<IRequestPayloadExtractor>();
17+
public SentryOptions SentryOptions { get; set; } = new SentryOptions();
1718
public object MockBody { get; set; } = new object();
1819
public HttpContext HttpContext { get; set; }
1920

@@ -26,7 +27,7 @@ public Fixture()
2627
public SystemWebRequestEventProcessor GetSut()
2728
{
2829
HttpContext.Current = HttpContext;
29-
return new SystemWebRequestEventProcessor(RequestPayloadExtractor);
30+
return new SystemWebRequestEventProcessor(RequestPayloadExtractor, SentryOptions);
3031
}
3132
}
3233

@@ -77,7 +78,7 @@ public void Process_NoBodyExtracted_NoRequestData()
7778

7879
var actual = sut.Process(expected);
7980
Assert.Same(expected, actual);
80-
Assert.Null(expected.InternalRequest);
81+
Assert.Null(expected.Request.Data);
8182
}
8283
}
8384
}

0 commit comments

Comments
 (0)