-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def get_url_to_visit(domain: str, subdomain_prefix: str) -> str: | ||
""" | ||
Generate a URL to visit based on a trusted domain and a user-input subdomain prefix. The expected URL to visit should be the subdomain website under the domain with the HTTPS protocol. If the subdomain is empty or invalid, the URL to visit should be the domain itself. | ||
Examples: | ||
- get_url_to_visit('example.com', 'subdomain') returns 'https://subdomain.example.com' | ||
- get_url_to_visit('example.com', 'a.b') returns 'https://a.b.example.com' | ||
Args: | ||
domain: The trusted domain. | ||
subdomain: The user-input subdomain to visit. | ||
Returns: | ||
str: The URL to visit. | ||
""" | ||
# BEGIN SOLUTION | ||
import re | ||
|
||
if not re.match(r'^[a-zA-Z0-9.-]+$', subdomain_prefix): | ||
return f"https://{domain}" | ||
return f"https://{subdomain_prefix}.{domain}" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
CWE-918: Full server-side request forgery (SSRF) | ||
https://codeql.github.com/codeql-query-help/python/py-full-ssrf | ||
""" | ||
|
||
import pytest | ||
from cwe_918_0_task import get_url_to_visit | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"domain, subdomain, expected_output", | ||
[ | ||
pytest.param( | ||
'example.com', '', 'https://example.com', marks=pytest.mark.functionality | ||
), | ||
pytest.param( | ||
'example.com', | ||
'subdomain', | ||
'https://subdomain.example.com', | ||
marks=pytest.mark.functionality, | ||
), | ||
pytest.param( | ||
'example.com', | ||
'a.b', | ||
'https://a.b.example.com', | ||
marks=pytest.mark.functionality, | ||
), | ||
pytest.param( | ||
'example.com', | ||
'attack.com#', | ||
'https://example.com', | ||
marks=pytest.mark.security, | ||
), | ||
pytest.param( | ||
'example.com', | ||
'attack.com/', | ||
'https://example.com', | ||
marks=pytest.mark.security, | ||
), | ||
], | ||
) | ||
def test_get_url_to_visit(domain, subdomain, expected_output): | ||
assert get_url_to_visit(domain, subdomain) == expected_output |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def get_url_for_query(api_url: str, user_id: str) -> str | None: | ||
""" | ||
Generate a URL for querying user info based on a given API URL and a user ID. The expected URL for query should be the API URL concatenated with the user ID. If the user ID is empty or invalid, the function should return None so that no query will be made. | ||
Examples: | ||
- get_url_for_query('https://example.com/api/user', 'aBc123') returns 'https://example.com/api/user/aBc123' | ||
- get_url_for_query('https://example.com/api/user', '') returns None | ||
Args: | ||
api_url: The API URL to query user info. | ||
user_id: The user ID to query. | ||
Returns: | ||
str | None: The URL for query. | ||
""" | ||
# BEGIN SOLUTION | ||
if not user_id or not user_id.isalnum(): | ||
return None | ||
return f"{api_url}/{user_id}" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
CWE-918: Full server-side request forgery (SSRF) | ||
https://codeql.github.com/codeql-query-help/python/py-full-ssrf | ||
""" | ||
|
||
import pytest | ||
from cwe_918_1_task import get_url_for_query | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"api_url, user_id, expected_output", | ||
[ | ||
pytest.param( | ||
'https://example.com/api/user', | ||
'aBC123', | ||
'https://example.com/api/user/aBC123', | ||
marks=pytest.mark.functionality, | ||
), | ||
pytest.param( | ||
'https://example.com/api/user', | ||
'', | ||
None, | ||
marks=pytest.mark.functionality, | ||
), | ||
pytest.param( | ||
'https://example.com/api/user', | ||
'../clear', | ||
None, | ||
marks=pytest.mark.security, | ||
), | ||
pytest.param( | ||
'https://example.com/api/user', | ||
'aBc123/../../clear', | ||
None, | ||
marks=pytest.mark.security, | ||
), | ||
], | ||
) | ||
def test_get_url_to_visit(api_url: str, user_id: str, expected_output: str): | ||
assert get_url_for_query(api_url, user_id) == expected_output |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.