Skip to content

Potentially unsound trimming suppression in DefaultAntiforgeryStateProvider #50536

Open
@halter73

Description

@halter73

[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = $"{nameof(DefaultAntiforgeryStateProvider)} uses the {nameof(PersistentComponentState)} APIs to deserialize the token, which are already annotated.")]
public DefaultAntiforgeryStateProvider(PersistentComponentState state)

The justification for the trimmer warning suppression does not make sense to me. Yes, PersistentComponentState's PersistAsJson and TryTakeFromJson are "already annotated." That's why it correctly raises a warning when the DefaultAntiforgeryStateProvider constructor calls these methods.

This might be okay if the static JsonSerializerOptionsProvider.Options used by PersistentComponentState had a TypeInfoResolver backed by a JsonSerializerContext statically generated using [JsonSerializable(typeof(AntiforgeryRequestToken))]. Otherwise, don't we risk trimming the AntiforgeryRequestToken constructor which has no callers other indirectly via the JsonSerializer calls in PersistentComponentState?

Or better yet, we should probably update PersistentComponentState to use the JsonOptions in DI, and register its statically generated JsonSerializerContext with the TypeInfoResolverChain similar to what we do for MapIdentityApi and ProblemDetails.

@javiercn @eerhardt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions