Skip to content

Conversation

@shargon
Copy link
Member

@shargon shargon commented Jan 8, 2025

Description

Fix #2612
Based on https://github.com/neo-project/neo/pull/2050/files

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • JsonTest_Object

Test Configuration:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@roman-khimov
Copy link
Contributor

This can lead to some state diff, needs to be checked.

Copy link
Member

@cschuchardt88 cschuchardt88 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how you do it Check out https://www.unicode.org/charts/PDF/U0000.pdf for Basic Latin Unicode block (U+0021-U+007F).

Copy link
Member

@cschuchardt88 cschuchardt88 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shargon
I fixed the issue on the branch for you. Check and test changes.

internal override void Write(Utf8JsonWriter writer)
{
writer.WriteStringValue(Value);
writer.WriteRawValue($"\"{DefaultEncoder.Encode(Value).Replace("\\u002B", "+")}\"");
Copy link
Member

@cschuchardt88 cschuchardt88 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind allowing the + (plus) sign is a security risk, reference: dotnet/runtime#35281
Only for XSS injection though.

Note: After searching for hours with no luck for solution. I came up with this solution on my own. Its dirty and could have bugs or exploits, however am 99% sure we don't. Need more tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it works with newtonsoft (according your reference)

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newtonsoft uses it own custom library. It doesn't use dotnet built-in JSON libraries.

Assert.AreEqual(@"{""test"":""+""}", parsed.ToString());

Assert.AreEqual(@"{""\uAAAA"":true}", parsed.ToString());
json = @" {""测试"": true}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to allow UnicodeRanges.CjkUnifiedIdeographs for these characters for json DefaultEncoder.

Assert.AreEqual(@"{""test"":true}", parsed.ToString());

json = @" {""\uAAAA"": true}";
json = @" {""test"":""+""} ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this test-case with \uAAAA? It's a nice compatibility test for NeoGo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I think this will be dropped, no way to do it without unsafe relaxing if we use native json

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesnt use unsafe relaxing currently. And its working fine now.

Copy link
Member Author

@shargon shargon Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace is a dirty fix, performance is affected always, not only when it contains +

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


static JToken()
{
DefaultEncoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.CjkUnifiedIdeographs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it change the old behaviour except the + symbol?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout #3663 (comment), but it should be the exactly the same with the addition of the + (plus sign).

@shargon shargon added the Discussion Initial issue state - proposed but not yet accepted label Jan 23, 2025
@erikzhang
Copy link
Member

@cschuchardt88
Copy link
Member

cschuchardt88 commented Oct 10, 2025

Maybe we can use UnsafeRelaxedJsonEscaping? https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#serialize-all-characters

We tried that and works great. However it allows for XSS Injection and other security problems.

@shargon
Copy link
Member Author

shargon commented Oct 10, 2025

Maybe we can use UnsafeRelaxedJsonEscaping? https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#serialize-all-characters

I remember that we tried with bad results, other characters are not scaped, maybe the '+' I don't remember very well, maybe @roman-khimov yes

@roman-khimov
Copy link
Contributor

See #2050. While the problem is valid and it'd be nice to fix it, I fear changing anything can break things.

@cschuchardt88
Copy link
Member

cschuchardt88 commented Oct 11, 2025

Maybe we can use UnsafeRelaxedJsonEscaping? https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#serialize-all-characters

I remember that we tried with bad results, other characters are not scaped, maybe the '+' I don't remember very well, maybe @roman-khimov yes

I based off the things I tested on my local machine it worked for + sign.. But now I remember that Escape characters and Strict UTf-8 didnt work.

@erikzhang
Copy link
Member

We tried that and works great. However it allows for XSS Injection and other security problems.

Putting strings directly into HTML without processing will always bring security issues. I don't think solving the XSS injection problem should be implemented by the contract interaction interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Discussion Initial issue state - proposed but not yet accepted Work in Progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'+' still replaced by \u002B

7 participants