question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpLogging Middleware option to log all headers

See original GitHub issue

Background and Motivation

By default request/response headers which are not added to the collections of the RequestHeaders and ResponseHeaders properties of the HttpLoggingOptions configuration object are logged with a redacted value.

It would be good to add an option, in scenarios controlled by the developer, to enable the logging of any request or response header.

Examples of scenarios:

  • Debugging
  • Sensitive data by design of the application never ends up in the headers
  • Not predictable headers that must be logged

Proposed API

It can be achieved by allowing new values for the HttpLoggingFields enum. For example:

namespace Microsoft.AspNetCore.HttpLogging;

[Flags]
public enum HttpLoggingFields : long
{
   //Existent enum values
   None = 0x0,
   RequestPath = 0x1,
   ....
+  RequestHeadersIncludeSensitive = 0x1000,
+  ResponseHeadersIncludeSensitive = 0x2000,
+  RequestPropertiesAndHeadersIncludeSensitive = RequestProperties | RequestHeadersIncludeSensitive,
+  ResponsePropertiesAndHeadersIncludeSensitive = ResponseStatusCode | ResponseHeadersIncludeSensitive,
+  RequestIncludeSensitive = RequestPropertiesAndHeadersIncludeSensitive | RequestBody,
+  ResponseIncludeSensitive = ResponseStatusCode | ResponseHeadersIncludeSensitive | ResponseBody,
+  AllIncludeSensitive = RequestIncludeSensitive | ResponseIncludeSensitive
   ....
   All = Request | Response
}

Usage Examples

The developer can opt-in for the new beahvior:

using Microsoft.AspNetCore.HttpLogging;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(logging =>
{
    logging.LoggingFields = HttpLoggingFields.AllIncludeSensitive; //Usage of one of the new enum values
});
var app = builder.Build();
app.UseHttpLogging();
app.Run();

This configuration will log all the request/response headers without the redacted value.

Risks

It doesn’t affect nor change the current API behavior since the developer must explicitly configure the logging with one of the new values in order to enable the logging for all the headers.

Implementation

The following commit shows how could look like the implementation: https://github.com/liguori/aspnetcore/commit/9f35fa91fdea4404f5028789dbf239edc8d2fc8a

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
liguoricommented, Apr 1, 2022

There are context in which headers are not predictable because dynamics. I am thinking about “X-” prefixed header that can arrive on the application depending on any intermediate infrastructural objects or cases in which you would like to log “abusive user/client behavior” that can inject every kind of “garbage” headers and it must be logged for auditing purpose.

In order to be still secure, when those new Enum values are set, it could be added an excluson list of well-known standard headers (from HTTP specs) in some new properties RequestExcluedHeaders and ResponseExcluedHeaders of the HttpLoggingOptions configuration object and initialized by default from the framework (i.e. Authentication, Cookie , etc), still giving the user choice to remove it.

The point is to be secure by design because default framework initialization but give the user (developer) the choice.

Another consideration is that the most of the PII or sensitive data is in the body and once you decide to enable logging with HttpLoggingFields.All or HttpLoggingFields.RequestBody (current possible configuration), you are logging everything.

0reactions
liguoricommented, Apr 2, 2022

Thank you @Tratcher for pointing to the #33346, your suggestion about expose a boolean switch would fit the example scenarios that I have mentioned in the first message and that I would like to be covered (not only debugging).

I agree with @blowdart as well about the responsibility of security decisions of the ASP.NET team but I think that currently are already shipped with the logging middleware options for log information that could be potentially more sensitive than what we could find in all the headers (except some few exceptions similar to the one I have mentioned) like the Body that if we think about Rest API could expose all the possible data of this world.

@adityamandaleeka it is fine to close this issues I will follow the #39200 although I cannot figure out which selector could be used in order to enable in future the logging of ALL the headers (with the possibility to exclude someone like Authorization, Cookie, etc).

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP Logging in ASP.NET Core
HTTP Logging is a middleware that logs information about incoming HTTP requests and ... HTTP request information; Common properties; Headers ...
Read more >
How to use HTTP logging in ASP.NET Core 6
The HTTP logging middleware included in ASP.NET Core allows you to log request and response data including headers, body, and common properties.
Read more >
Log http requests/responses using ASP.NET Core ...
To "prove" this I built a middleware that stored all unique headers that we received and sent out. I then created an endpoint...
Read more >
HTTP Logs With 'UseHttpLogging' Middleware[.NET6 ...
So this middleware provide log information on 'HTTP Request and Response Information', 'Headers' and 'Request Body Information', etc.
Read more >
Logging HTTP Request and Response in .NET Web API
HttpLogging.HttpLoggingMiddleware. This property provides the log level that the Web API will log the HTTP requests and response data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found