Skip to content

Commit 0dc043f

Browse files
Merge pull request #32 from serilog-contrib/feature/mask-uri
Support masking properties that have a Uri as the value
2 parents 933db95 + d56f636 commit 0dc043f

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog for Serilog.Enrichers.Sensitive
22

3+
## 1.7.3
4+
5+
- Add support for masking property values that contain a `Uri`, reported by @yadanilov19
6+
37
## 1.7.2
48

59
- FIx issue where Microsoft.Extensions.Configuration.Binder version 7.0.0 or up was required for JSON configuration to work. [#25](https://github.com/serilog-contrib/Serilog.Enrichers.Sensitive/issues/25)

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.7.2.0</Version>
3+
<Version>1.7.3.0</Version>
44
<Authors>Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman</Authors>
55
<Company>Codenizer BV</Company>
66
<Copyright>2023 Sander van Vliet</Copyright>

src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
131131

132132
return (false, null);
133133
}
134+
// System.Uri is a built-in scalar type in Serilog:
135+
// https://github.com/serilog/serilog/blob/dev/src/Serilog/Capturing/PropertyValueConverter.cs#L23
136+
// which is why this needs special handling and isn't
137+
// caught by the string value above.
138+
case ScalarValue { Value: Uri uriValue }:
139+
{
140+
var (wasMasked, maskedValue) = ReplaceSensitiveDataFromString(uriValue.ToString());
141+
142+
if (wasMasked)
143+
{
144+
return (true, new ScalarValue(new Uri(maskedValue)));
145+
}
146+
147+
return (false, null);
148+
}
134149
case SequenceValue sequenceValue:
135150
var resultElements = new List<LogEventPropertyValue>();
136151
var anyElementMasked = false;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Serilog.Core;
2+
using Serilog.Sinks.InMemory;
3+
using System;
4+
using System.Text.RegularExpressions;
5+
using Serilog.Sinks.InMemory.Assertions;
6+
using Xunit;
7+
8+
namespace Serilog.Enrichers.Sensitive.Tests.Unit
9+
{
10+
public class WhenMaskingLogEventWithNonStringScalarValue
11+
{
12+
private readonly InMemorySink _inMemorySnk;
13+
private readonly Logger _logger;
14+
15+
[Fact]
16+
public void GivenPropertyValueIsUri_ValueIsMasked()
17+
{
18+
_logger.Information("Message {Prop}", new Uri("https://tempuri.org?someparam=SENSITIVE"));
19+
20+
_inMemorySnk
21+
.Snapshot()
22+
.Should()
23+
.HaveMessage("Message {Prop}")
24+
.Appearing()
25+
.Once()
26+
.WithProperty("Prop")
27+
.WithValue(new Uri("https://tempuri.org?***MASKED***"));
28+
}
29+
30+
[Fact]
31+
public void GivenPropertyValueIsTypeWithToStringOverride_ValueIsMasked()
32+
{
33+
_logger.Information("Message {Prop}", new TypeWithToStringOverride("my SECRET message"));
34+
35+
_inMemorySnk
36+
.Snapshot()
37+
.Should()
38+
.HaveMessage("Message {Prop}")
39+
.Appearing()
40+
.Once()
41+
.WithProperty("Prop")
42+
.WithValue("my ***MASKED*** message");
43+
}
44+
45+
public WhenMaskingLogEventWithNonStringScalarValue()
46+
{
47+
_inMemorySnk = new InMemorySink();
48+
49+
_logger = new LoggerConfiguration()
50+
.Enrich.WithSensitiveDataMasking(options =>
51+
{
52+
options.MaskingOperators.Add(new UriMaskingOperator());
53+
options.MaskingOperators.Add(new TestRegexMaskingOperator());
54+
})
55+
.WriteTo.Sink(_inMemorySnk)
56+
.CreateLogger();
57+
}
58+
}
59+
60+
public class TestRegexMaskingOperator : RegexMaskingOperator
61+
{
62+
public TestRegexMaskingOperator() : base("SECRET", RegexOptions.Compiled)
63+
{
64+
}
65+
}
66+
67+
public class TypeWithToStringOverride
68+
{
69+
private readonly string _value;
70+
71+
public TypeWithToStringOverride(string value)
72+
{
73+
_value = value;
74+
}
75+
76+
public override string ToString()
77+
{
78+
return _value;
79+
}
80+
}
81+
82+
public class UriMaskingOperator : RegexMaskingOperator
83+
{
84+
private const string SomePattern =
85+
"someparam=.*?(.(?:&|$))";
86+
87+
public UriMaskingOperator() : base(SomePattern, RegexOptions.IgnoreCase | RegexOptions.Compiled)
88+
{
89+
}
90+
91+
protected override string PreprocessInput(string input)
92+
{
93+
return input;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)