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

Redact query from HttpTelemetry #104970

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Text;
using System.Threading;

namespace System.Net.Http
Expand Down Expand Up @@ -37,6 +38,14 @@ public static class Keywords
private void RequestStart(string scheme, string host, int port, string pathAndQuery, byte versionMajor, byte versionMinor, HttpVersionPolicy versionPolicy)
{
Interlocked.Increment(ref _startedRequests);
if (!GlobalHttpSettings.DiagnosticsHandler.DisableUriRedaction)
{
int queryIndex = pathAndQuery.IndexOf('?');
if (queryIndex >= 0)
{
pathAndQuery = $"{pathAndQuery.AsSpan(0, queryIndex + 1)}*";
}
}
WriteEvent(eventId: 1, scheme, host, port, pathAndQuery, versionMajor, versionMinor, versionPolicy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -399,7 +400,89 @@ await server.AcceptConnectionAsync(async connection =>
}, UseVersion.ToString(), testMethod).DisposeAsync();
}

private static void ValidateStartFailedStopEvents(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events, Version version, bool shouldHaveFailures = false, int count = 1)
[OuterLoop]
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData("/test/path?q1=a&q2=b", true)]
[InlineData("/test/path?q1=a&q2=b", false)]
[InlineData("/test/path", true)]
[InlineData("/test/path", false)]
[InlineData("?q1=a&q2=b", true)]
[InlineData("?q1=a&q2=b", false)]
[InlineData("", true)]
[InlineData("", false)]
[InlineData("/test/path?q1=a&q2=b#frag", true)]
[InlineData("/test/path?q1=a&q2=b#frag", false)]
[InlineData("/test/path#frag", true)]
[InlineData("/test/path#frag", false)]
[InlineData("?q1=a&q2=b#frag", true)]
[InlineData("?q1=a&q2=b#frag", false)]
[InlineData("#frag", true)]
[InlineData("#frag", false)]
public async Task EventSource_PathAndQueryRedaction_LogsStartStop(string uriTail, bool disableRedaction)
{
var psi = new ProcessStartInfo();
psi.Environment.Add("DOTNET_SYSTEM_NET_HTTP_DISABLEURIREDACTION", disableRedaction.ToString());
var fragIndex = uriTail.IndexOf('#');
var expectedPathAndQuery = uriTail.Substring(0, fragIndex >= 0 ? fragIndex : uriTail.Length);
if (!disableRedaction)
{
var queryIndex = expectedPathAndQuery.IndexOf('?');
expectedPathAndQuery = expectedPathAndQuery.Substring(0, queryIndex >= 0 ? queryIndex + 1 : expectedPathAndQuery.Length);
expectedPathAndQuery = queryIndex >= 0 ? expectedPathAndQuery + '*' : expectedPathAndQuery;
}
expectedPathAndQuery = expectedPathAndQuery.StartsWith('/') ? expectedPathAndQuery : '/' + expectedPathAndQuery;

await RemoteExecutor.Invoke(static async (useVersionString, uriTail, expectedPathAndQuery) =>
{
Version version = Version.Parse(useVersionString);
using var listener = new TestEventListener("System.Net.Http", EventLevel.Verbose, eventCounterInterval: 0.1d);
listener.AddActivityTracking();

var events = new ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)>();
Uri expectedUri = null;
await listener.RunWithCallbackAsync(e => events.Enqueue((e, e.ActivityId)), async () =>
{
await GetFactoryForVersion(version).CreateClientAndServerAsync(
async uri =>
{
expectedUri = uri;
using HttpClientHandler handler = CreateHttpClientHandler(version);
using HttpClient client = CreateHttpClient(handler, useVersionString);
client.BaseAddress = uri;
using var invoker = new HttpMessageInvoker(handler);

var request = new HttpRequestMessage(HttpMethod.Get, uri)
{
Version = version
};

await client.GetAsync(uriTail);
},
async server =>
{
await server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestDataAsync();
await WaitForEventCountersAsync(events);
await connection.SendResponseAsync();
});
});

await WaitForEventCountersAsync(events);
});
Assert.DoesNotContain(events, e => e.Event.EventId == 0); // errors from the EventSource itself

ValidateStartFailedStopEvents(events, version, pathAndQuery: expectedPathAndQuery);

ValidateConnectionEstablishedClosed(events, version, expectedUri);

ValidateRequestResponseStartStopEvents(events, null, 0, count: 1);

ValidateEventCounters(events, requestCount: 1, shouldHaveFailures: false, versionMajor: version.Major);
}, UseVersion.ToString(), uriTail, expectedPathAndQuery, new RemoteInvokeOptions { StartInfo = psi }).DisposeAsync();
}

private static void ValidateStartFailedStopEvents(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events, Version version, bool shouldHaveFailures = false, int count = 1, string? pathAndQuery = null)
{
(EventWrittenEventArgs Event, Guid ActivityId)[] starts = events.Where(e => e.Event.EventName == "RequestStart").ToArray();
foreach (EventWrittenEventArgs startEvent in starts.Select(e => e.Event))
Expand All @@ -409,6 +492,10 @@ private static void ValidateStartFailedStopEvents(ConcurrentQueue<(EventWrittenE
Assert.NotEmpty((string)startEvent.Payload[1]); // host
Assert.True(startEvent.Payload[2] is int port && port >= 0 && port <= 65535);
Assert.NotEmpty((string)startEvent.Payload[3]); // pathAndQuery
if (pathAndQuery is not null)
{
Assert.Equal(pathAndQuery, (string)startEvent.Payload[3]); // pathAndQuery
}
byte versionMajor = Assert.IsType<byte>(startEvent.Payload[4]);
Assert.Equal(version.Major, versionMajor);
byte versionMinor = Assert.IsType<byte>(startEvent.Payload[5]);
Expand Down
Loading