Skip to content

Security implications of using IHttpContextAccessor in Blazor Server #27944

@Bouke

Description

@Bouke

The documentation note the following on IHttpContextAccessor:

Additionally, again for security reasons, you must not use IHttpContextAccessor within Blazor apps. Blazor apps run outside of the context of the ASP.NET Core pipeline. The HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app.

The default HttpContextAccessor is implemented using AsyncLocal:

https://github.com/dotnet/aspnetcore/blob/589aa11b5c631ce719e0530d66be8324a6d79169/src/Http/Http/src/HttpContextAccessor.cs#LL11C108-L11C108

I fail to understand the security implications of using IHttpContextAccessor in Blazor Server. AsyncLocal in general must flow with the user's SignalR connection†. So that would also be the case for the HttpContext stored in this AsyncLocal. This is also what we're seeing in our tests.

  • Under what situations would IHttpContextAccessor return a different HttpContext than the one that started the Blazor app? (i.e. another user)
  • How is this use of AsyncLocal any different from other uses of AsyncLocal and (apparently) a security consideration?
  • How can we pass the originating HttpContext from the "context of the ASP.NET Core pipeline" safely to the Blazor Server app?

† If that isn't the case, then all bets are off.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions