Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Treat "\u0000" as "\u0020" for the purposes of message search (message indexing) #10820

Merged
merged 27 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
177ca9a
add test to check if null code points are being inserted
H-Shay Sep 13, 2021
7ae5ccf
add logic to detect and replace null code points before insertion int…
H-Shay Sep 13, 2021
c86d8b5
lints
H-Shay Sep 13, 2021
f681896
add license to test
H-Shay Sep 14, 2021
534f141
change approach to null substitution
H-Shay Sep 14, 2021
4574d66
add type hint for SearchEntry
H-Shay Sep 14, 2021
fb1f9a0
Merge branch 'develop' into test_homeserver
H-Shay Sep 14, 2021
0628de7
Add changelog entry
H-Shay Sep 14, 2021
b0b5792
updated changelog
H-Shay Sep 14, 2021
49fc935
update chanelog message
H-Shay Sep 14, 2021
8de418a
remove duplicate changelog
H-Shay Sep 14, 2021
b2c7c88
Update synapse/storage/databases/main/events.py remove extra space
H-Shay Sep 14, 2021
163f569
rename and move test file, update tests, delete old test file
H-Shay Sep 16, 2021
0d2c316
fix typo in comments
H-Shay Sep 16, 2021
7e7de36
update _find_highlights_in_postgres to replace null byte with space
H-Shay Sep 16, 2021
584ccd5
replace null byte in sqlite search insertion
H-Shay Sep 17, 2021
e922659
beef up and reorganize test for this pr
H-Shay Sep 17, 2021
1db26b6
Merge branch 'test_homeserver' of https://github.com/H-Shay/synapse i…
H-Shay Sep 17, 2021
baf2250
update changelog
H-Shay Sep 17, 2021
54ca415
add type hints and update docstring
H-Shay Sep 17, 2021
0cad2fe
check db engine directly vs using env variable
H-Shay Sep 17, 2021
c6f6fc2
refactor tests to be less repetetive
H-Shay Sep 20, 2021
3247106
move rplace logic into seperate function
H-Shay Sep 20, 2021
103125f
requested changes
H-Shay Sep 21, 2021
2639029
Fix typo.
clokep Sep 21, 2021
7c1679b
Update synapse/storage/databases/main/search.py
H-Shay Sep 22, 2021
88ad982
Update changelog.d/10820.misc
H-Shay Sep 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.d/10820.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fix a bug where sending an `m.room.text` event containing a null byte would cause an internal server error.
Fix a long-standing bug where an `m.room.text` event containing a null byte would cause an internal server error.
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 9 additions & 3 deletions synapse/storage/databases/main/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
)


def _clean_value_for_search(value: str) -> str:
"""Replaces null code points in string as postgres/sqlite do not
like the insertion of strings with null code points"""
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
return value.replace("\u0000", " ")


class SearchWorkerStore(SQLBaseStore):
def store_search_entries_txn(
self, txn: LoggingTransaction, entries: Iterable[SearchEntry]
Expand All @@ -56,7 +62,7 @@ def store_search_entries_txn(
entry.event_id,
entry.room_id,
entry.key,
entry.value.replace("\u0000", " "),
_clean_value_for_search(entry.value),
entry.stream_ordering,
entry.origin_server_ts,
)
Expand All @@ -75,7 +81,7 @@ def store_search_entries_txn(
entry.event_id,
entry.room_id,
entry.key,
entry.value.replace("\u0000", " "),
_clean_value_for_search(entry.value),
)
for entry in entries
)
Expand Down Expand Up @@ -652,7 +658,7 @@ def f(txn):
for key in ("body", "name", "topic"):
v = event.content.get(key, None)
if v:
v = v.replace("\u0000", " ")
v = _clean_value_for_search(v)
values.append(v)

if not values:
Expand Down
52 changes: 23 additions & 29 deletions tests/storage/test_room_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,41 @@ def test_null_byte(self):
Ensure this doesn't break anything.
"""

# register a user and create a room, create some messages
# Register a user and create a room, create some messages
self.register_user("alice", "password")
access_token = self.login("alice", "password")
room_id = self.helper.create_room_as("alice", True, "1", access_token)
body1 = "hi\u0000bob"
body2 = "another message"
body3 = "hi alice"
room_id = self.helper.create_room_as("alice", tok=access_token)

# send messages and ensure they don't cause an internal server
# Send messages and ensure they don't cause an internal server
# error
resp1 = self.helper.send(room_id, body1, "1", access_token)
resp2 = self.helper.send(room_id, body2, "2", access_token)
resp3 = self.helper.send(room_id, body3, "3", access_token)
self.assertTrue("event_id" in resp1)
self.assertTrue("event_id" in resp2)
self.assertTrue("event_id" in resp3)
for body in ["hi\u0000bob", "another message", "hi alice"]:
response = self.helper.send(room_id, body, tok=access_token)
self.assertIn("event_id", response)

# check that search still works with the message where the null byte was replaced
# Check that search works for the message where the null byte was replaced
store = self.hs.get_datastore()
res1 = self.get_success(
result = self.get_success(
store.search_msgs([room_id], "hi bob", ["content.body"])
)
self.assertEquals(res1.get("count"), 1)
self.assertEquals(result.get("count"), 1)
if isinstance(store.database_engine, PostgresEngine):
self.assertIn("hi", result.get("highlights"))
self.assertIn("bob", result.get("highlights"))

# check that search still works with another unrelated message
res2 = self.get_success(
# Check that search works for an unrelated message
result = self.get_success(
store.search_msgs([room_id], "another", ["content.body"])
)
self.assertEquals(res2.get("count"), 1)
self.assertEquals(result.get("count"), 1)
if isinstance(store.database_engine, PostgresEngine):
self.assertIn("another", result.get("highlights"))

# check that search still works when given a search term that overlaps
# with the message that we replaced the null byte in and an unrelated one
res3 = self.get_success(store.search_msgs([room_id], "hi", ["content.body"]))
self.assertEquals(res3.get("count"), 2)
res4 = self.get_success(
# Check that search works for a search term that overlaps with the message
# containing a null byte and an unrelated message.
result = self.get_success(store.search_msgs([room_id], "hi", ["content.body"]))
self.assertEquals(result.get("count"), 2)
result = self.get_success(
store.search_msgs([room_id], "hi alice", ["content.body"])
)

# check content of highlights if we are using postgres
if isinstance(store.database_engine, PostgresEngine):
self.assertIn("bob", res1.get("highlights"))
self.assertIn("hi", res1.get("highlights"))
self.assertIn("another", res2.get("highlights"))
self.assertIn("alice", res4.get("highlights"))
self.assertIn("alice", result.get("highlights"))