|
1 | 1 | #if SYSTEM_WEB |
2 | 2 | using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Security.Claims; |
| 5 | +using System.Security.Principal; |
3 | 6 | using System.Web; |
4 | 7 | using Sentry.Extensibility; |
| 8 | +using Sentry.Protocol; |
5 | 9 |
|
6 | 10 | namespace Sentry.Internal.Web |
7 | 11 | { |
8 | 12 | internal class SystemWebRequestEventProcessor : ISentryEventProcessor |
9 | 13 | { |
| 14 | + private readonly SentryOptions _options; |
10 | 15 | internal IRequestPayloadExtractor PayloadExtractor { get; } |
11 | 16 |
|
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 | + } |
14 | 22 |
|
15 | 23 | public SentryEvent Process(SentryEvent @event) |
16 | 24 | { |
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) |
18 | 49 | { |
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) |
21 | 61 | { |
22 | | - @event.Request.Data = body; |
| 62 | + if (claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier) is Claim claim) |
| 63 | + { |
| 64 | + @event.User.Id = claim.Value; |
| 65 | + } |
23 | 66 | } |
24 | 67 | } |
| 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 | + } |
25 | 95 | return @event; |
26 | 96 | } |
27 | 97 | } |
|
0 commit comments