This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Strictly enforce canonicaljson requirements in a new room version #7381
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b26c01b
Add a new room version.
clokep 04c81b1
Check strict canonical JSON if the room version calls for it.
clokep b8eb8d4
Also check incoming client events.
clokep 97cf2a9
Refactor the validation function.
clokep 6c06fa3
Handle frozendicts properly.
clokep 6ef13b6
Add some basic test cases.
clokep c9647b4
Add newsfragment.
clokep fc6f5a3
Move check from handler to validator.
clokep a4485a9
Add a comment.
clokep 534032b
Add additional comments.
clokep 673172a
Merge remote-tracking branch 'origin/develop' into clokep/strict-json
clokep e8edfec
Fix import order.
clokep 8351097
Merge remote-tracking branch 'origin/develop' into clokep/strict-json
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add an experimental room version which strictly adheres to the canonical JSON specification. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| # limitations under the License. | ||
| import collections | ||
| import re | ||
| from typing import Mapping, Union | ||
| from typing import Any, Mapping, Union | ||
|
|
||
| from six import string_types | ||
|
|
||
|
|
@@ -23,6 +23,7 @@ | |
| from twisted.internet import defer | ||
|
|
||
| from synapse.api.constants import EventTypes, RelationTypes | ||
| from synapse.api.errors import Codes, SynapseError | ||
| from synapse.api.room_versions import RoomVersion | ||
| from synapse.util.async_helpers import yieldable_gather_results | ||
|
|
||
|
|
@@ -449,3 +450,35 @@ def copy_power_levels_contents( | |
| raise TypeError("Invalid power_levels value for %s: %r" % (k, v)) | ||
|
|
||
| return power_levels | ||
|
|
||
|
|
||
| def validate_canonicaljson(value: Any): | ||
| """ | ||
| Ensure that the JSON object is valid according to the rules of canonical JSON. | ||
|
|
||
| See the appendix section 3.1: Canonical JSON. | ||
|
|
||
| This rejects JSON that has: | ||
| * An integer outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1] | ||
| * Floats | ||
| * NaN, Infinity, -Infinity | ||
| """ | ||
| if isinstance(value, int): | ||
| if value <= -(2 ** 53) or 2 ** 53 <= value: | ||
| raise SynapseError(400, "JSON integer out of range", Codes.BAD_JSON) | ||
|
|
||
| elif isinstance(value, float): | ||
| # Note that Infinity, -Infinity, and NaN are also considered floats. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CanonicalJSON indeed does aim to represent all floats as ints, just in case anyone was unsure about blocking all floats: https://matrix.org/docs/spec/appendices#canonical-json
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, as the description says all floats need to be rejected. I didn't find the specification super clear here unless you read into the grammar though. I think we could improve that. |
||
| raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON) | ||
|
|
||
| elif isinstance(value, (dict, frozendict)): | ||
| for v in value.values(): | ||
| validate_canonicaljson(v) | ||
|
|
||
| elif isinstance(value, (list, tuple)): | ||
| for i in value: | ||
| validate_canonicaljson(i) | ||
|
|
||
| elif not isinstance(value, (bool, str)) and value is not None: | ||
| # Other potential JSON values (bool, None, str) are safe. | ||
| raise SynapseError(400, "Unknown JSON value", Codes.BAD_JSON) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.