Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
f96b85ec
Unverified
Commit
f96b85ec
authored
2 years ago
by
Brendan Abolivier
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Ensure the type of URL attributes is always str when matching against preview blacklist (#12333)
parent
c31c1091
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
changelog.d/12333.bugfix
+1
-0
1 addition, 0 deletions
changelog.d/12333.bugfix
synapse/rest/media/v1/preview_url_resource.py
+7
-2
7 additions, 2 deletions
synapse/rest/media/v1/preview_url_resource.py
tests/rest/media/v1/test_url_preview.py
+41
-2
41 additions, 2 deletions
tests/rest/media/v1/test_url_preview.py
with
49 additions
and
4 deletions
changelog.d/12333.bugfix
0 → 100644
+
1
−
0
View file @
f96b85ec
Fix a long-standing bug affecting URL previews that would generate a 500 response instead of a 403 if the previewed URL includes a port that isn't allowed by the relevant blacklist.
This diff is collapsed.
Click to expand it.
synapse/rest/media/v1/preview_url_resource.py
+
7
−
2
View file @
f96b85ec
...
...
@@ -200,12 +200,17 @@ class PreviewUrlResource(DirectServeJsonResource):
match
=
False
continue
# Some attributes might not be parsed as strings by urlsplit (such as the
# port, which is parsed as an int). Because we use match functions that
# expect strings, we want to make sure that's what we give them.
value_str
=
str
(
value
)
if
pattern
.
startswith
(
"
^
"
):
if
not
re
.
match
(
pattern
,
getattr
(
url_tuple
,
attrib
)
):
if
not
re
.
match
(
pattern
,
value_str
):
match
=
False
continue
else
:
if
not
fnmatch
.
fnmatch
(
getattr
(
url_tuple
,
attrib
)
,
pattern
):
if
not
fnmatch
.
fnmatch
(
value_str
,
pattern
):
match
=
False
continue
if
match
:
...
...
This diff is collapsed.
Click to expand it.
tests/rest/media/v1/test_url_preview.py
+
41
−
2
View file @
f96b85ec
...
...
@@ -17,7 +17,7 @@ import json
import
os
import
re
from
typing
import
Any
,
Dict
,
Optional
,
Sequence
,
Tuple
,
Type
from
urllib.parse
import
urlencode
from
urllib.parse
import
quote
,
urlencode
from
twisted.internet._resolver
import
HostResolution
from
twisted.internet.address
import
IPv4Address
,
IPv6Address
...
...
@@ -69,7 +69,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
"
2001:800::/21
"
,
)
config
[
"
url_preview_ip_range_whitelist
"
]
=
(
"
1.1.1.1
"
,)
config
[
"
url_preview_url_blacklist
"
]
=
[]
config
[
"
url_preview_accept_language
"
]
=
[
"
en-UK
"
,
"
en-US;q=0.9
"
,
...
...
@@ -1123,3 +1122,43 @@ class URLPreviewTests(unittest.HomeserverTestCase):
os
.
path
.
exists
(
path
),
f
"
{
os
.
path
.
relpath
(
path
,
self
.
media_store_path
)
}
was not deleted
"
,
)
@unittest.override_config
({
"
url_preview_url_blacklist
"
:
[{
"
port
"
:
"
*
"
}]})
def
test_blacklist_port
(
self
)
->
None
:
"""
Tests that blacklisting URLs with a port makes previewing such URLs
fail with a 403 error and doesn
'
t impact other previews.
"""
self
.
lookups
[
"
matrix.org
"
]
=
[(
IPv4Address
,
"
10.1.2.3
"
)]
bad_url
=
quote
(
"
http://matrix.org:8888/foo
"
)
good_url
=
quote
(
"
http://matrix.org/foo
"
)
channel
=
self
.
make_request
(
"
GET
"
,
"
preview_url?url=
"
+
bad_url
,
shorthand
=
False
,
await_result
=
False
,
)
self
.
pump
()
self
.
assertEqual
(
channel
.
code
,
403
,
channel
.
result
)
channel
=
self
.
make_request
(
"
GET
"
,
"
preview_url?url=
"
+
good_url
,
shorthand
=
False
,
await_result
=
False
,
)
self
.
pump
()
client
=
self
.
reactor
.
tcpClients
[
0
][
2
].
buildProtocol
(
None
)
server
=
AccumulatingProtocol
()
server
.
makeConnection
(
FakeTransport
(
client
,
self
.
reactor
))
client
.
makeConnection
(
FakeTransport
(
server
,
self
.
reactor
))
client
.
dataReceived
(
b
"
HTTP/1.0 200 OK
\r\n
Content-Length: %d
\r\n
Content-Type: text/html
\r\n\r\n
"
%
(
len
(
self
.
end_content
),)
+
self
.
end_content
)
self
.
pump
()
self
.
assertEqual
(
channel
.
code
,
200
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment