Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Fix CodeQL SM02211 alert - SerializationBinder#6549

Merged
tracyboehrer merged 7 commits into
mainfrom
southworks/fix/sm02211-binder-implementation
Nov 16, 2022
Merged

Fix CodeQL SM02211 alert - SerializationBinder#6549
tracyboehrer merged 7 commits into
mainfrom
southworks/fix/sm02211-binder-implementation

Conversation

@sw-joelmut
Copy link
Copy Markdown
Collaborator

Fixes #6515 #6516 #6517 #6519 #6520

Description

This PR fixes the CodeQL SM02211 alert related to unsafe JsonSerializer TypeNameHandling usage.
For these cases, it implements a custom SerializationBinder, allowing only the types that are defined in the configuration or related ones.

Specific Changes

  • Adds the AllowedTypesSerializationBinder class to allow only types that are defined in the instance.
  • Adds tests for the AllowedTypesSerializationBinder class.
  • Updated BlobsStorage, AzureBlobStorage, and CosmosDbPartitionedStorage classes to implement the binder.
  • Updated BlobsTranscriptStore setting the TypeNameHandling to None.

Testing

The following image shows the tests passing successfully and the error message when a type isn't allowed by the process.
image
image

@sw-joelmut sw-joelmut added the Automation: No parity PR does not need to be applied to other languages. label Nov 14, 2022
@sw-joelmut sw-joelmut requested a review from a team as a code owner November 14, 2022 13:21
@sw-joelmut sw-joelmut closed this Nov 14, 2022
@sw-joelmut sw-joelmut reopened this Nov 14, 2022
@tracyboehrer
Copy link
Copy Markdown
Member

@sw-joelmut This is getting some test failures in the storage tests. Here is one:

  Failed Microsoft.Bot.Builder.Azure.Tests.CosmosDbPartitionedStorageTests.WriteAsyncFailure [357 ms]
  Error Message:
   Assert.Throws() Failure
Expected: typeof(Microsoft.Azure.Cosmos.CosmosException)
Actual:   typeof(System.InvalidOperationException): Unable to find the following types in the 'AllowedTypes' collection.
  - <>f__AnonymousType0, Microsoft.Bot.Builder.Azure.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Please provide the 'AllowedTypesSerializationBinder' in the custom 'JsonSerializerSettings' instance, with the list of types to allow.

Example:
    new JsonSerializerSettings
    {
        SerializationBinder = new AllowedTypesSerializationBinder(
            new List<Type>
            {
                typeof(<>f__AnonymousType0),
            }),
    }
---- System.InvalidOperationException : Unable to find the following types in the 'AllowedTypes' collection.
  - <>f__AnonymousType0, Microsoft.Bot.Builder.Azure.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Please provide the 'AllowedTypesSerializationBinder' in the custom 'JsonSerializerSettings' instance, with the list of types to allow.

Example:
    new JsonSerializerSettings
    {
        SerializationBinder = new AllowedTypesSerializationBinder(
            new List<Type>
            {
                typeof(<>f__AnonymousType0),
            }),
    }
  Stack Trace:
     at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.ThrowDisallowedTypesError(List`1 types) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 170
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.CleanupTypes(JContainer json) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 148
   at Microsoft.Bot.Builder.Azure.CosmosDbPartitionedStorage.WriteAsync(IDictionary`2 changes, CancellationToken cancellationToken) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder.Azure/CosmosDbPartitionedStorage.cs:line 220
----- Inner Stack Trace -----
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.ThrowDisallowedTypesError(List`1 types) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 170
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.CleanupTypes(JContainer json) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 148
   at Microsoft.Bot.Builder.Azure.CosmosDbPartitionedStorage.WriteAsync(IDictionary`2 changes, CancellationToken cancellationToken) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder.Azure/CosmosDbPartitionedStorage.cs:line 220

@sw-joelmut
Copy link
Copy Markdown
Collaborator Author

@sw-joelmut This is getting some test failures in the storage tests. Here is one:

  Failed Microsoft.Bot.Builder.Azure.Tests.CosmosDbPartitionedStorageTests.WriteAsyncFailure [357 ms]
  Error Message:
   Assert.Throws() Failure
Expected: typeof(Microsoft.Azure.Cosmos.CosmosException)
Actual:   typeof(System.InvalidOperationException): Unable to find the following types in the 'AllowedTypes' collection.
  - <>f__AnonymousType0, Microsoft.Bot.Builder.Azure.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Please provide the 'AllowedTypesSerializationBinder' in the custom 'JsonSerializerSettings' instance, with the list of types to allow.

Example:
    new JsonSerializerSettings
    {
        SerializationBinder = new AllowedTypesSerializationBinder(
            new List<Type>
            {
                typeof(<>f__AnonymousType0),
            }),
    }
---- System.InvalidOperationException : Unable to find the following types in the 'AllowedTypes' collection.
  - <>f__AnonymousType0, Microsoft.Bot.Builder.Azure.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Please provide the 'AllowedTypesSerializationBinder' in the custom 'JsonSerializerSettings' instance, with the list of types to allow.

Example:
    new JsonSerializerSettings
    {
        SerializationBinder = new AllowedTypesSerializationBinder(
            new List<Type>
            {
                typeof(<>f__AnonymousType0),
            }),
    }
  Stack Trace:
     at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.ThrowDisallowedTypesError(List`1 types) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 170
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.CleanupTypes(JContainer json) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 148
   at Microsoft.Bot.Builder.Azure.CosmosDbPartitionedStorage.WriteAsync(IDictionary`2 changes, CancellationToken cancellationToken) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder.Azure/CosmosDbPartitionedStorage.cs:line 220
----- Inner Stack Trace -----
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.ThrowDisallowedTypesError(List`1 types) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 170
   at Microsoft.Bot.Builder.AllowedTypesSerializationBinder.CleanupTypes(JContainer json) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder/AllowedTypesSerializationBinder.cs:line 148
   at Microsoft.Bot.Builder.Azure.CosmosDbPartitionedStorage.WriteAsync(IDictionary`2 changes, CancellationToken cancellationToken) in /Users/runner/work/1/s/libraries/Microsoft.Bot.Builder.Azure/CosmosDbPartitionedStorage.cs:line 220

@tracyboehrer, we will review them and let you know once we fix them.

Thanks!

@coveralls
Copy link
Copy Markdown
Collaborator

@BruceHaley
Copy link
Copy Markdown
Contributor

✔️ No Binary Compatibility issues for Microsoft.Bot.Builder.dll
✔️ No Binary Compatibility issues for Microsoft.Bot.Builder.Azure.dll

@sw-joelmut
Copy link
Copy Markdown
Collaborator Author

@tracyboehrer, we updated the failing tests to include the new custom SerializationBinder, and now they are all passing.

@tracyboehrer tracyboehrer merged commit 1ee5f6a into main Nov 16, 2022
@tracyboehrer tracyboehrer deleted the southworks/fix/sm02211-binder-implementation branch November 16, 2022 14:03
tracyboehrer pushed a commit that referenced this pull request Feb 21, 2023
tracyboehrer added a commit that referenced this pull request Feb 23, 2023
* Revert "[#6582, #6584] Incorrect serialization of state when using Blob or CosmosDB storage in v4.19.x (#6585)"

This reverts commit e15b38d.

* Revert "Fix CodeQL SM02211 alert - SerializationBinder (#6549)"

This reverts commit 1ee5f6a.

* Skip TypeNameHandling CodeQL issue from Azure and Blobs storage (#6592)

---------

Co-authored-by: Tracy Boehrer <trboehre@microsoft.com>
Co-authored-by: Joel Mut <62260472+sw-joelmut@users.noreply.github.com>
This was referenced May 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Automation: No parity PR does not need to be applied to other languages.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeQL alert SM02211: Unsafe TypeNameHandling in microsoft/microsoft/botbuilder-dotnet/botbuilder-dotnet

4 participants