This repository has been 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
Fix URL preview bugs (type error when loading cache from db, content-type including quotes) #4157
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d117e4f
some test cleanups
hawkowl dd4fee4
fix the bug & add tests
hawkowl 4ee46e3
Merge remote-tracking branch 'origin/develop' into hawkowl/content-ty…
hawkowl 0a99c16
changelog
hawkowl a54e85f
Merge remote-tracking branch 'origin/develop' into hawkowl/content-ty…
hawkowl a8acab5
changelog
hawkowl 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 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 @@ | ||
Loading URL previews from the DB cache on Postgres will no longer cause Unicode type errors when responding to the request, and URL previews will no longer fail if the remote server returns a Content-Type header with the chartype in quotes. |
This file contains 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 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 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,164 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2018 New Vector Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
from mock import Mock | ||
|
||
from twisted.internet.defer import Deferred | ||
|
||
from synapse.config.repository import MediaStorageProviderConfig | ||
from synapse.util.module_loader import load_module | ||
|
||
from tests import unittest | ||
|
||
|
||
class URLPreviewTests(unittest.HomeserverTestCase): | ||
|
||
hijack_auth = True | ||
user_id = "@test:user" | ||
|
||
def make_homeserver(self, reactor, clock): | ||
|
||
self.storage_path = self.mktemp() | ||
os.mkdir(self.storage_path) | ||
|
||
config = self.default_config() | ||
config.url_preview_enabled = True | ||
config.max_spider_size = 9999999 | ||
config.url_preview_url_blacklist = [] | ||
config.media_store_path = self.storage_path | ||
|
||
provider_config = { | ||
"module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend", | ||
"store_local": True, | ||
"store_synchronous": False, | ||
"store_remote": True, | ||
"config": {"directory": self.storage_path}, | ||
} | ||
|
||
loaded = list(load_module(provider_config)) + [ | ||
MediaStorageProviderConfig(False, False, False) | ||
] | ||
|
||
config.media_storage_providers = [loaded] | ||
|
||
hs = self.setup_test_homeserver(config=config) | ||
|
||
return hs | ||
|
||
def prepare(self, reactor, clock, hs): | ||
|
||
self.fetches = [] | ||
|
||
def get_file(url, output_stream, max_size): | ||
""" | ||
Returns tuple[int,dict,str,int] of file length, response headers, | ||
absolute URI, and response code. | ||
""" | ||
|
||
def write_to(r): | ||
data, response = r | ||
output_stream.write(data) | ||
return response | ||
|
||
d = Deferred() | ||
d.addCallback(write_to) | ||
self.fetches.append((d, url)) | ||
return d | ||
|
||
client = Mock() | ||
client.get_file = get_file | ||
|
||
self.media_repo = hs.get_media_repository_resource() | ||
preview_url = self.media_repo.children[b'preview_url'] | ||
preview_url.client = client | ||
self.preview_url = preview_url | ||
|
||
def test_cache_returns_correct_type(self): | ||
|
||
request, channel = self.make_request( | ||
"GET", "url_preview?url=matrix.org", shorthand=False | ||
) | ||
request.render(self.preview_url) | ||
self.pump() | ||
|
||
# We've made one fetch | ||
self.assertEqual(len(self.fetches), 1) | ||
|
||
end_content = ( | ||
b'<html><head>' | ||
b'<meta property="og:title" content="~matrix~" />' | ||
b'<meta property="og:description" content="hi" />' | ||
b'</head></html>' | ||
) | ||
|
||
self.fetches[0][0].callback( | ||
( | ||
end_content, | ||
( | ||
len(end_content), | ||
{ | ||
b"Content-Length": [b"%d" % (len(end_content))], | ||
b"Content-Type": [b'text/html; charset="utf8"'], | ||
}, | ||
"https://example.com", | ||
200, | ||
), | ||
) | ||
) | ||
|
||
self.pump() | ||
self.assertEqual(channel.code, 200) | ||
self.assertEqual( | ||
channel.json_body, {"og:title": "~matrix~", "og:description": "hi"} | ||
) | ||
|
||
# Check the cache returns the correct response | ||
request, channel = self.make_request( | ||
"GET", "url_preview?url=matrix.org", shorthand=False | ||
) | ||
request.render(self.preview_url) | ||
self.pump() | ||
|
||
# Only one fetch, still, since we'll lean on the cache | ||
self.assertEqual(len(self.fetches), 1) | ||
|
||
# Check the cache response has the same content | ||
self.assertEqual(channel.code, 200) | ||
self.assertEqual( | ||
channel.json_body, {"og:title": "~matrix~", "og:description": "hi"} | ||
) | ||
|
||
# Clear the in-memory cache | ||
self.assertIn("matrix.org", self.preview_url._cache) | ||
self.preview_url._cache.pop("matrix.org") | ||
self.assertNotIn("matrix.org", self.preview_url._cache) | ||
|
||
# Check the database cache returns the correct response | ||
request, channel = self.make_request( | ||
"GET", "url_preview?url=matrix.org", shorthand=False | ||
) | ||
request.render(self.preview_url) | ||
self.pump() | ||
|
||
# Only one fetch, still, since we'll lean on the cache | ||
self.assertEqual(len(self.fetches), 1) | ||
|
||
# Check the cache response has the same content | ||
self.assertEqual(channel.code, 200) | ||
self.assertEqual( | ||
channel.json_body, {"og:title": "~matrix~", "og:description": "hi"} | ||
) |
This file contains 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.
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.
Is this just a style change?
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.
No - the bugfix is the
"?
s on either side which will strip quotes fromcharset="utf-8"
, which used to break us. I had to reformat it as the line got too long.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.
Aaaaaaaaah, I'm blind. Cool 👍