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

Accept a query parameters for encrypted query values #66

Merged
merged 1 commit into from
Feb 27, 2024
Merged
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
20 changes: 14 additions & 6 deletions src/h_vialib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def url_for(
content_type: Optional[ContentType] = None,
options=None,
blocked_for=None,
query=None,
headers=None,
):
"""Generate a Via URL to display a given URL.
Expand All @@ -73,6 +74,10 @@ def url_for(
or "html")
:param options: Any additional params to add to the URL
:param blocked_for: context for the blocked pages
:param query: Any extra query params needed to make the request to `url`.
These are sent encrypted to Via.
Note that the headers are only used when the content is proxied with
python, ie only PDFs from certain sources.
:param headers: Any headers needed to make the request to `url`.
The headers are sent encrypted to Via.
Note that the headers are only used when the content is proxied with
Expand All @@ -81,21 +86,24 @@ def url_for(
"""
doc = ViaDoc(url, content_type)

query = dict(self.options)
params = dict(self.options)
if options:
query.update(options)
params.update(options)

if query:
params["via.secret.query"] = self._secure_secrets.encrypt_dict(query)

if headers:
query["via.secret.headers"] = self._secure_secrets.encrypt_dict(headers)
params["via.secret.headers"] = self._secure_secrets.encrypt_dict(headers)

if blocked_for:
query["via.blocked_for"] = blocked_for
params["via.blocked_for"] = blocked_for

if doc.content_type == ContentType.HTML:
# Optimisation to skip routing for documents we know are HTML
via_url = self._url_for_html(doc.url, query)
via_url = self._url_for_html(doc.url, params)
else:
via_url = self._secure_url.create(self._url_for(doc, query))
via_url = self._secure_url.create(self._url_for(doc, params))

return via_url

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/h_vialib/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ def test_url_for_with_headers(self, client, Encryption):
{"via.secret.headers": "secure headers"}
)

def test_url_for_with_query(self, client, Encryption):
query = {"some": "parameter"}
Encryption.return_value.encrypt_dict.return_value = "secure query"

final_url = client.url_for("http://example.com", query=query)

Encryption.return_value.encrypt_dict.assert_called_once_with(query)
assert final_url == Any.url.containing_query(
{"via.secret.query": "secure query"}
)

@pytest.mark.parametrize("content_type", (None, "pdf", "html"))
def test_url_for_raises_without_a_service_url(self, content_type):
client = ViaClient(
Expand Down