Skip to content
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

Fully URL-encode the URL for webmentions #271

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 16 additions & 5 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Datastore model classes."""
import logging
import urllib.parse
import urllib.parse as urlparse
from urllib.parse import urlencode

from Crypto.PublicKey import RSA
from django_salmon import magicsigs
Expand Down Expand Up @@ -108,10 +109,20 @@ def proxy_url(self):
"""Returns the Bridgy Fed proxy URL to render this response as HTML."""
if self.source_mf2 or self.source_as2 or self.source_atom:
source, target = self.key.id().split(' ')
return f'{request.host_url}render?' + urllib.parse.urlencode({
'source': source,
'target': target,
})
# via https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python
url = f'{request.host_url}render'
params = {
'source': source,
'target': target,
}

url_parts = list(urlparse.urlparse(url))
query = dict(urlparse.parse_qsl(url_parts[4]))
query.update(params)

url_parts[4] = urlencode(query)

return urlparse.urlunparse(url_parts)

@classmethod
def _id(cls, source, target):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ def test_proxy_url(self):
self.assertIsNone(resp.proxy_url())

resp.source_as2 = 'as2'
self.assertEqual('http://localhost/render?source=abc&target=xyz',
self.assertEqual('http://localhost/render?source=abc%26target=xyz',
resp.proxy_url())