-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse json validation #16923
Parse json validation #16923
Changes from 7 commits
9608394
a040208
7797db3
96a05b2
3cee2e9
279bf93
9c5152a
9508d4d
f44b1bf
8099ea0
f5f8864
6e68a9f
0634aeb
776dadb
fa414ac
870a99a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Adds parse_json servlet function for standardized JSON parsing from query parameters, ensuring enhanced data validation and error handling. | ||
Introduces INVALID_PARAM error response for invalid JSON objects, improving parameter validation feedback. | ||
Adds validation check to prevent 500 internal server error on invalid Json Filter request. | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import json | ||
import time | ||
import urllib.parse | ||
from http import HTTPStatus | ||
from typing import List, Optional | ||
from unittest.mock import AsyncMock, Mock | ||
|
||
|
@@ -2190,6 +2191,33 @@ def test_room_messages_purge(self) -> None: | |
chunk = channel.json_body["chunk"] | ||
self.assertEqual(len(chunk), 0, [event["content"] for event in chunk]) | ||
|
||
def test_room_message_filter_query_validation(self) -> None: | ||
# Test json validation in (filter) query parameter. | ||
# Does not test the validity of the filter, only the json validation. | ||
|
||
# Check Get with valid json filter parameter, expect 200. | ||
_valid_filter_str = '{"types": ["m.room.message"]}' | ||
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. Generally you should only precede a variable name with an underscore in Python if you'd like to label the output of a function, but not actually use it. Here we are using Could you remove it, along with 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. Ahh took me a bit! But now I think I know what you mean. for _ in range(32):
print('Hello, World.') this woulde be correct. But in this case, following PEP 8, 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. Thanks for citing the PEP! It's interesting to read where this convention came from. In Synapse, we certainly do use leading underscores for internal function/method names and private class variables ( However, we don't use this convention for local variable names - so at least for this codebase, I would remove the leading underscore. |
||
channel = self.make_request( | ||
"GET", | ||
f"/_synapse/admin/v1/rooms/{self.room_id}/messages?dir=b&filter={_valid_filter_str}", | ||
access_token=self.admin_user_tok, | ||
) | ||
|
||
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body) | ||
|
||
# Check Get with invalid json filter parameter, expect 400 INVALID_PARAM. | ||
_invalid_filter_str = "}}}{}" | ||
channel = self.make_request( | ||
"GET", | ||
f"/_synapse/admin/v1/rooms/{self.room_id}/messages?dir=b&filter={_invalid_filter_str}", | ||
access_token=self.admin_user_tok, | ||
) | ||
|
||
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.json_body) | ||
self.assertEqual( | ||
channel.json_body["errcode"], Codes.INVALID_PARAM, channel.json_body | ||
) | ||
|
||
|
||
class JoinAliasRoomTestCase(unittest.HomeserverTestCase): | ||
servlets = [ | ||
|
@@ -2522,6 +2550,39 @@ def test_context_as_admin(self) -> None: | |
else: | ||
self.fail("Event %s from events_after not found" % j) | ||
|
||
def test_room_event_context_filter_query_validation(self) -> None: | ||
# Test json validation in (filter) query parameter. | ||
# Does not test the validity of the filter, only the json validation. | ||
|
||
# Create a user with room and event_id. | ||
user_id = self.register_user("test", "test") | ||
user_tok = self.login("test", "test") | ||
room_id = self.helper.create_room_as(user_id, tok=user_tok) | ||
event_id = self.helper.send(room_id, "message 1", tok=user_tok)["event_id"] | ||
|
||
# Check Get with valid json filter parameter, expect 200. | ||
_valid_filter_str = '{"types": ["m.room.message"]}' | ||
channel = self.make_request( | ||
"GET", | ||
f"/_synapse/admin/v1/rooms/{room_id}/context/{event_id}?filter={_valid_filter_str}", | ||
access_token=self.admin_user_tok, | ||
) | ||
|
||
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body) | ||
|
||
# Check Get with invalid json filter parameter, expect 400 INVALID_PARAM. | ||
_invalid_filter_str = "}}}{}" | ||
channel = self.make_request( | ||
"GET", | ||
f"/_synapse/admin/v1/rooms/{room_id}/context/{event_id}?filter={_invalid_filter_str}", | ||
access_token=self.admin_user_tok, | ||
) | ||
|
||
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.json_body) | ||
self.assertEqual( | ||
channel.json_body["errcode"], Codes.INVALID_PARAM, channel.json_body | ||
) | ||
|
||
|
||
class MakeRoomAdminTestCase(unittest.HomeserverTestCase): | ||
servlets = [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally we try to keep changelog entries short, and acknowledge that the audience is system administrators. Such an audience won't care to know the details of the implementation, but rather than user-facing impact. My suggestion would be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the Insights! - I'll keep that in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you happy to accept my suggestion here? I prefer the suggested version of the changelog for the reasons in my initial comment.