Skip to content

Commit

Permalink
Merge pull request #665 from rohanpm/add-missing-encoding
Browse files Browse the repository at this point in the history
Add missing URL encoding during CDN redirect [RHELDST-22493]
  • Loading branch information
rohanpm authored Jan 18, 2024
2 parents 834e20a + 09cdcab commit 97e0614
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion exodus_gw/routers/cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from collections import OrderedDict
from datetime import datetime, timedelta, timezone
from urllib.parse import urlparse
from urllib.parse import quote, urlparse

from botocore.utils import datetime2timestamp
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -200,6 +200,7 @@ def cdn_redirect(
or call_context.user.internalUsername
or "<unknown user>"
)
url = quote(url)
signed_url = sign_url(url, settings, env, username)
return Response(
content=None, headers={"location": signed_url}, status_code=302
Expand Down
24 changes: 24 additions & 0 deletions tests/routers/test_cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def test_cdn_redirect_(monkeypatch, dummy_private_key, caplog):
}


@freeze_time("2022-02-16")
def test_cdn_redirect_encoding(monkeypatch, dummy_private_key, caplog):
"""Paths involving special characters get encoded during redirect."""
caplog.set_level(logging.DEBUG, "exodus-gw")
monkeypatch.setenv("EXODUS_GW_CDN_PRIVATE_KEY_TEST", dummy_private_key)

with TestClient(app) as client:
r = client.get(
"/test/cdn/some/url-with-^-character", follow_redirects=False
)

expected_cookies = "WyJDbG91ZEZyb250LUtleS1QYWlyLUlkPVhYWFhYWFhYWFhYWFhYOyBTZWN1cmU7IEh0dHBPbmx5OyBTYW1lU2l0ZT1sYXg7IERvbWFpbj1sb2NhbGhvc3Q6ODA0OTsgUGF0aD0vY29udGVudC87IE1heC1BZ2U9NDMyMDAiLCAiQ2xvdWRGcm9udC1Qb2xpY3k9ZXlKVGRHRjBaVzFsYm5RaU9sdDdJbEpsYzI5MWNtTmxJam9pYUhSMGNEb3ZMMnh2WTJGc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7IEh0dHBPbmx5OyBTYW1lU2l0ZT1sYXg7IERvbWFpbj1sb2NhbGhvc3Q6ODA0OTsgUGF0aD0vb3JpZ2luLzsgTWF4LUFnZT00MzIwMCIsICJDbG91ZEZyb250LVNpZ25hdHVyZT1NaW8za2w5enpCZXE2WUtjREY0aFdHNGlIRFhnLWRwSnV-VmtkWklYZVhPM0lsZzE3OTZUWlFBZGpLLWN6Tm5aQzBUNWVmVzNEbGlKQWVMSmhYd351MVZoTkpSQ0lvUTZmTGJDVnV4MVRHMzAtUC1FVzR-a1JmU2dlWjV2RVcydTBNWXpsQ0pNZndZSUoxQ1ZlejlMdTJ3a2NIMjFQTkNjc2liS25tTmZjbk1fOyBTZWN1cmU7IEh0dHBPbmx5OyBTYW1lU2l0ZT1sYXg7IERvbWFpbj1sb2NhbGhvc3Q6ODA0OTsgUGF0aD0vb3JpZ2luLzsgTWF4LUFnZT00MzIwMCJd"
expected_url = (
"http://localhost:8049/_/cookie/some/"
f"url-with-%5E-character?CloudFront-Cookies={expected_cookies}"
"&Expires=1644971400"
"&Signature=AKw-NwPUavLgMsk2j5fikOvCOIWXqnaFtaabzMNeXyY5VMkSd1Rt226m6VaVEj~dQpf71GUAe1dKlvPXqCUYXXHlcFPAktES7IXvg7O88c-MDMak2q2lUy0~-l6Q2kTJWJeWvGtHTBnRXEM215U5SqODnZtVW98F4mEgJGg6vEU_"
"&Key-Pair-Id=XXXXXXXXXXXXXX"
)

assert r.status_code == 302
assert r.headers["location"] == expected_url


def test_sign_url_without_private_key():
env = get_environment("test")

Expand Down

0 comments on commit 97e0614

Please sign in to comment.