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

Utf8JsonReader: Do not allow non-standard line endings in single-line comments - #38036

Merged
krwq merged 6 commits into
dotnet:masterfrom
krwq:fix-33321
Jun 26, 2019
Merged

krwq merged 6 commits into
dotnet:masterfrom
krwq:fix-33321

Conversation

@krwq

@krwq krwq commented May 29, 2019 •

Copy link
Copy Markdown
Member

Fixes: https://github.com/dotnet/corefx/issues/33321
Fixes: https://github.com/dotnet/corefx/issues/37931
(also some other fixes - see below)

Some Unicode characters (i.e. U+2028, U+2029) are considered line endings and are sometimes used in some attack vectors when coming over the wire.

This change causes that parser will throw an exception if such character is encountered in a comment.

Also fixes couple of issues w.r.t to single-line comment handling with both mult- and single segment case around EOF/final block which I haven't reported. I.e.:

  • end of json on \r
  • end of json on empty comment in the end (i.e. {}// would cause parser to throw)

Perf results are here: dotnet/performance#543

@krwq
krwq requested review from WinCPP and ahsonkhan May 29, 2019 23:42
@krwq krwq changed the title Utf8JsonReader: Do not allow non-standard line endings in comments Utf8JsonReader: Do not allow non-standard line endings in single-line comments May 29, 2019
@ahsonkhan ahsonkhan added this to the 3.0 milestone May 29, 2019

@ahsonkhan ahsonkhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We need to throw if we see the custom separator characters in both Skip and Allow modes.

Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.cs
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.cs
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
@krwq

krwq commented May 31, 2019

Copy link
Copy Markdown
Member Author

@ahsonkhan I've rewrote SkipComment logic for multi-segment almost completely and have unified Consume/Skip. Haven't did anything similar for single-segment yet.

@ahsonkhan

Copy link
Copy Markdown

@krwq, reviewing this would take some time and there is a bit of a churn, can this wait till next week? Do you have a sense of perf impact here?

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
@krwq

krwq commented May 31, 2019 •

Copy link
Copy Markdown
Member Author

Yes, this can wait. Since there is currently no perf tests related to comments I'll let you know when I write them 😄

@krwq

krwq commented Jun 6, 2019

Copy link
Copy Markdown
Member Author

Perf results are here: dotnet/performance#543
Multi-segment multiline comment specifically seems to be 2x faster for the common scenario, everything else is mostly the same. For some reason single-segment multiline comment regressed a bit (I have not changed that code path or anything related and have tried re-running tests)

@ahsonkhan

ahsonkhan commented Jun 8, 2019 •

Copy link
Copy Markdown

Multi-segment multiline comment specifically seems to be 2x faster for the common scenario, everything else is mostly the same. For some reason single-segment multiline comment regressed a bit (I have not changed that code path or anything related and have tried re-running tests)

I ran the perf tests locally (between master and your branch) and was not able to reproduce your results:

summary:
better: 1, geomean: 1.032
worse: 9, geomean: 1.073
total diff: 10

Slower diff/base Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.15 169.56 194.15
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.14 920.21 1048.72
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.08 118.35 127.87
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.07 143.18 152.90
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.07 172.37 183.75
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.05 150.26 157.17
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.04 142.63 148.78
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.04 147.42 153.37
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.04 121.93 126.25
Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing( 1.03 131.92 127.82

@adamsitnik - it looks like we are hitting a character limit on the ResultComparer output. Also, can the ResultComparer store the result to disk somewhere (by default - maybe md and csv) just like the actual perf runs (rather than us having to specify a path - maybe in one of the base/diff paths)?

@adamsitnik

Copy link
Copy Markdown
Member

it looks like we are hitting a character limit on the ResultComparer output

I am not sure what would be the best way to solve it. The default output is console, which has a limited width. Most probably the easiest thing to do right now would be to shorten the ID.

System.Text.Json.Tests.Utf8JsonReaderCommentsTests.Utf8JsonReaderCommentParsing
System.Text.Json.Tests.CommentsTests.Parsing

can the ResultComparer store the result to disk somewhere

yes, you can pass the path to output csv file via --csv $filePath (full docs)

@ahsonkhan

ahsonkhan commented Jun 18, 2019 •

Copy link
Copy Markdown

yes, you can pass the path to output csv file via --csv $filePath

Also, can the ResultComparer store the result to disk somewhere (by default - maybe md and csv) just like the actual perf runs (rather than us having to specify a path - maybe in one of the base/diff paths)?

I am aware of the --csv option. I was asking, by default, without having to pass that option in.

@krwq

krwq commented Jun 21, 2019

Copy link
Copy Markdown
Member Author

@ahsonkhan ping, is there anything blocking here? I'm not personally concerned about tiny (< 20%) regression in perf here considering comments are not expected to be used in high perf scenarios and there have been couple of reliability fixes here

return SkipMultiLineCommentMultiSegment(localBuffer.Slice(1), leftOver);
}
ReadOnlySequence<byte> commentSequence = _sequence.Slice(start, end);
commentSequence = commentSequence.Slice(2, commentSequence.Length - 2 - tailBytesToIgnore);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we try to avoid the double slicing of the ReadOnlySequence?
Maybe we can already have the start SequencePosition be off-set by 2. and change end to account for the - 2 - tailBytesToIgnore

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't believe this is possible. SequencePosition doesn't know how to move by 2 since it may end up in different object which it doesn't know about (where ReadOnlySequence does)

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.cs Outdated
@ahsonkhan

Copy link
Copy Markdown

is there anything blocking here?

Nope, nothing blocking.

I'm not personally concerned about tiny (< 20%) regression in perf here considering comments are not expected to be used in high perf scenarios and there have been couple of reliability fixes here

Agreed. I am still a bit concerned about the regression (especially for the "skip" mode), but we can work on perf optimizations separately.

Do you have the latest numbers based on your changes? We should double check if we have 10-20% regression on the "skip" case, because that is a bit too high.

@ahsonkhan

Copy link
Copy Markdown

@krwq, any updates to this?

@krwq

krwq commented Jun 25, 2019

Copy link
Copy Markdown
Member Author

@ahsonkhan, I have not had a chance to re-run this yet. Nothing has changes since you've run it yourself last time

@ahsonkhan

Copy link
Copy Markdown

I have not had a chance to re-run this yet. Nothing has changes since you've run it yourself last time

OK. Let me know when you have addressed the leftover feedback so we can merge this change in :)
Thanks.

@ahsonkhan ahsonkhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please resolve the merge conflict. Otherwise, LGTM.

@krwq

krwq commented Jun 26, 2019

Copy link
Copy Markdown
Member Author

@ahsonkhan seems the conflict is that we repeated this fix:
https://github.com/dotnet/corefx/pull/38036/files#diff-b12b49879ec5ef146f3373b69e66216bL3007

I'm building locally to double check my tests still build and run correctly after rebasing.

@krwq
krwq merged commit fc8bd8e into dotnet:master Jun 26, 2019
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
… comments (dotnet/corefx#38036)

* Fix handling comments

* Apply feedback, unify Consume and Skip for multi-segment

* address some missed comments

* micro changes

* replace tab with spaces

* apply feedback


Commit migrated from dotnet/corefx@fc8bd8e
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

3 participants