-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Cache DNS lookups by hostname #317
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
Changes from all commits
3520639
7ba3b20
e90fe14
4c6812d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
|
|
||
| import socket | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| import main | ||
|
|
||
| def test_validate_hostname_caching(): | ||
| """ | ||
| Verify that validate_hostname caches results and avoids redundant DNS lookups. | ||
| """ | ||
| # Mock socket.getaddrinfo | ||
| with patch("socket.getaddrinfo") as mock_dns: | ||
| # Setup mock return value (valid IP) | ||
| mock_dns.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 443))] | ||
|
|
||
| # Clear cache to start fresh | ||
| main.validate_hostname.cache_clear() | ||
|
|
||
| # First call - should trigger DNS lookup | ||
| assert main.validate_hostname("example.com") is True | ||
| assert mock_dns.call_count == 1 | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # Second call - should use cache | ||
| assert main.validate_hostname("example.com") is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert mock_dns.call_count == 1 # Still 1 | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # different hostname - should trigger DNS lookup | ||
| assert main.validate_hostname("google.com") is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert mock_dns.call_count == 2 | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| def test_validate_hostname_security(): | ||
| """ | ||
| Verify security checks in validate_hostname. | ||
| """ | ||
| # Localhost | ||
| assert main.validate_hostname("localhost") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert main.validate_hostname("127.0.0.1") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert main.validate_hostname("::1") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # Private IP | ||
| assert main.validate_hostname("192.168.1.1") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # Domain resolving to private IP | ||
| with patch("socket.getaddrinfo") as mock_dns: | ||
| # Return private IP | ||
| mock_dns.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('192.168.1.1', 443))] | ||
| main.validate_hostname.cache_clear() | ||
|
|
||
| assert main.validate_hostname("private.local") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| def test_validate_folder_url_uses_validate_hostname(): | ||
| """ | ||
| Verify that validate_folder_url calls validate_hostname. | ||
| """ | ||
| with patch("main.validate_hostname") as mock_validate: | ||
| mock_validate.return_value = True | ||
|
|
||
| # Clear cache | ||
| main.validate_folder_url.cache_clear() | ||
|
|
||
| url = "https://example.com/data.json" | ||
| assert main.validate_folder_url(url) is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| mock_validate.assert_called_with("example.com") | ||
|
|
||
| # Invalid hostname | ||
| mock_validate.return_value = False | ||
|
|
||
| # Clear cache again because URL is the same | ||
| main.validate_folder_url.cache_clear() | ||
|
|
||
| assert main.validate_folder_url(url) is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test