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.

cannot use safe JSON deserialization for Dialog State object

See original GitHub issue

Version

4.5.1

Describe the bug

To save/load the dialog state, the bot need to serialize/deserialize the dialog states with JSON. Currently, the dialog states contains a list of dictionaries of objects, which is black box to the bot author programmer. To deserialize the string to the dialog state , the type information must be put into the JSON when searlizing the object ot string. For example, with Newtonsoft.Json, we need to specify TypeNameHandling = TypeNameHandling.All for serializer, just like the botbuilder sample code

But it is not safe to put the type information into the serialized JSON string, because an attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. There are some published document about it:

Per the above documents, when we serialize/deserialize the dialog state, we should specify specify TypeNameHandling = TypeNameHandling.All for JSON serializer. But currently it will cause exception in Bot Builder.

Bot Framework should provide a way to allow the bot to serialize/deserialize the dialog state safely.

To Reproduce

Steps to reproduce the behavior:

  1. download the sample code of BotBuilder: 42.scaleout
  2. change the code in the line from
private static readonly JsonSerializer StateJsonSerializer = new JsonSerializer() { TypeNameHandling = TypeNameHandling.All };

to

private static readonly JsonSerializer StateJsonSerializer = new JsonSerializer() { TypeNameHandling = TypeNameHandling.None };
  1. Compile the project and run it
  2. Use the bot emulator to talk to the bot

Actuall behavior

Exception happens:

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Microsoft.Bot.Builder.Dialogs.DialogState'.

   at Microsoft.Bot.Builder.Dialogs.ComponentDialog.ContinueDialogAsync(DialogContext outerDc, CancellationToken cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\ComponentDialog.cs:line 75
   at Microsoft.Bot.Builder.Dialogs.DialogContext.ContinueDialogAsync(CancellationToken cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\DialogContext.cs:line 160
   at Microsoft.Bot.Builder.Dialogs.DialogExtensions.RunAsync(Dialog dialog, ITurnContext turnContext, IStatePropertyAccessor`1 accessor, CancellationToken cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\DialogExtensions.cs:line 18
   at Microsoft.BotBuilderSamples.DialogHost.RunTurnAsync(Dialog dialog, ITurnContext turnContext, JObject state, CancellationToken cancellationToken) in C:\src\others\BotBuilder-Samples\samples\csharp_dotnetcore\42.scaleout\DialogHost.cs:line 67

Expected behavior

There is no error.

Screenshots

image

[bug]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
kvbreddycommented, Jan 25, 2020

I have fixed this issue for OAuthPrompt Dialog in this PR. May be someone can do a similar fix for other Dialogs as well.

1reaction
kvbreddycommented, Jan 17, 2020

@EricDahlvang , Can this issue be reopened.

The above issue is still not resolved. DialogState serialization and deserialization works only if TypeNameHandling = TypeNameHandling.All. TypeNameHandling = TypeNameHandling.None still does not work, because in most places DialogInstance.State is used and directly casted to different types which requires $type to work.

For example, in our case : var promptState = (IDictionary<string, object>)state[PersistedState]; throws an exception when using TypeNameHandling = TypeNameHandling.None as the state[PersistedState] is a JObject.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' ...
I am trying to deserialize the above JSON using the code below: var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>( ...
Read more >
CVE-2019-18935: Remote Code Execution via Insecure…
It insecurely deserializes JSON objects in a manner that results in arbitrary remote code execution on the software's underlying host. The ...
Read more >
CA2329: Do not deserialize with JsonSerializer using an ...
This rule finds Newtonsoft.Json.JsonSerializer instances that are configured to deserialize types specified from input, but not configured to ...
Read more >
JSON functions | BigQuery
Creates a JSON object. JSON_QUERY, Extracts a JSON value and converts it to a SQL JSON-formatted STRING or JSON value.
Read more >
Request and Response JSON Reference | Alexa Skills Kit
For example, your code for deserializing a JSON request must not break when it encounters a new, unknown property.
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