Last active
April 3, 2025 13:14
-
-
Save jangia/d3a28032bf1a9461cab3b9dca44954f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import re | |
import pytest | |
REDIRECT_REGEX = r"(^/([A-Za-z0-9#&?=]+|$)|^https://my.domain\.com/[A-Za-z0-9#&?=/]*$)" | |
def is_valid_success_url(url: str) -> bool: | |
return bool(re.match(REDIRECT_REGEX, url)) | |
@pytest.mark.parametrize( | |
"success_url", | |
[ | |
pytest.param( | |
"/", | |
id="relative_root", | |
), | |
pytest.param( | |
"/some/path", | |
id="relative_path", | |
), | |
pytest.param( | |
"/?foo=bar", | |
id="relative_root_with_query_param", | |
), | |
pytest.param( | |
"/?foo=bar", | |
id="relative_path_with_query_param", | |
) | |
] | |
) | |
def test_is_valid_success_url(success_url: str): | |
assert is_valid_success_url(success_url) is True | |
@pytest.mark.parametrize( | |
"success_url", | |
[ | |
pytest.param( | |
"https://somewhere-else.com", | |
id="non_whitelisted_domain", | |
), | |
pytest.param( | |
"https://somewhere-else.com", | |
id="non_whitelisted_domain", | |
), | |
pytest.param( | |
"https://[email protected]/", | |
id="domain_change", | |
), | |
pytest.param( | |
"https://mydomain.com/settings>\r\nBCC:[email protected]\r\npda: m", | |
id="malicious_url", | |
), | |
pytest.param( | |
"//settings", | |
id="double_slash", | |
), | |
] | |
) | |
def test_not_valid_success_url(success_url: str): | |
assert is_valid_success_url(success_url) is False |
This file contains hidden or 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
test_named_parametrized_examples.py::test_is_valid_success_url[relative_root] PASSED [ 11%] | |
test_named_parametrized_examples.py::test_is_valid_success_url[relative_path] PASSED [ 22%] | |
test_named_parametrized_examples.py::test_is_valid_success_url[relative_root_with_query_param] PASSED [ 33%] | |
test_named_parametrized_examples.py::test_is_valid_success_url[relative_path_with_query_param] PASSED [ 44%] | |
test_named_parametrized_examples.py::test_not_valid_success_url[non_whitelisted_domain0] PASSED [ 55%] | |
test_named_parametrized_examples.py::test_not_valid_success_url[non_whitelisted_domain1] PASSED [ 66%] | |
test_named_parametrized_examples.py::test_not_valid_success_url[domain_change] PASSED [ 77%] | |
test_named_parametrized_examples.py::test_not_valid_success_url[malicious_url] PASSED [ 88%] | |
test_named_parametrized_examples.py::test_not_valid_success_url[double_slash] PASSED [100%] | |
======================================================================================================== 9 passed in 0.01s ======================================================================================================== | |
(venv) someone@Rhodes some_folder % |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment