|
| 1 | +"""Tests for security fixes - cryptographic random number generation and SSL context.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import ssl |
| 5 | +import sys |
| 6 | + |
| 7 | +# Add project root to sys.path |
| 8 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def test_wecom_crypto_uses_secrets(): |
| 14 | + """Test that WXBizJsonMsgCrypt uses secrets module instead of random.""" |
| 15 | + from astrbot.core.platform.sources.wecom_ai_bot.WXBizJsonMsgCrypt import Prpcrypt |
| 16 | + |
| 17 | + # Create an instance and test that random string generation works |
| 18 | + prpcrypt = Prpcrypt(b"test_key_32_bytes_long_value!") |
| 19 | + |
| 20 | + # Generate multiple random strings and verify they are different and valid |
| 21 | + random_strings = [prpcrypt.get_random_str() for _ in range(10)] |
| 22 | + |
| 23 | + # All strings should be 16 bytes long |
| 24 | + assert all(len(s) == 16 for s in random_strings) |
| 25 | + |
| 26 | + # All strings should be different (extremely high probability with cryptographic random) |
| 27 | + assert len(set(random_strings)) == 10 |
| 28 | + |
| 29 | + # All strings should be numeric when decoded |
| 30 | + for s in random_strings: |
| 31 | + decoded = s.decode() |
| 32 | + assert decoded.isdigit() |
| 33 | + assert 1000000000000000 <= int(decoded) <= 9999999999999999 |
| 34 | + |
| 35 | + |
| 36 | +def test_wecomai_utils_uses_secrets(): |
| 37 | + """Test that wecomai_utils uses secrets module for random string generation.""" |
| 38 | + from astrbot.core.platform.sources.wecom_ai_bot.wecomai_utils import ( |
| 39 | + generate_random_string, |
| 40 | + ) |
| 41 | + |
| 42 | + # Generate multiple random strings and verify they are different |
| 43 | + random_strings = [generate_random_string(10) for _ in range(20)] |
| 44 | + |
| 45 | + # All strings should be 10 characters long |
| 46 | + assert all(len(s) == 10 for s in random_strings) |
| 47 | + |
| 48 | + # All strings should be alphanumeric |
| 49 | + for s in random_strings: |
| 50 | + assert s.isalnum() |
| 51 | + |
| 52 | + # All strings should be different (extremely high probability with cryptographic random) |
| 53 | + assert len(set(random_strings)) >= 19 # Allow for 1 collision in 20 (very unlikely) |
| 54 | + |
| 55 | + |
| 56 | +def test_azure_tts_signature_uses_secrets(): |
| 57 | + """Test that Azure TTS signature generation uses secrets module.""" |
| 58 | + import asyncio |
| 59 | + |
| 60 | + from astrbot.core.provider.sources.azure_tts_source import OTTSProvider |
| 61 | + |
| 62 | + # Create a provider with test config |
| 63 | + config = { |
| 64 | + "OTTS_SKEY": "test_secret_key", |
| 65 | + "OTTS_URL": "https://example.com/api/tts", |
| 66 | + "OTTS_AUTH_TIME": "https://example.com/api/time", |
| 67 | + } |
| 68 | + |
| 69 | + async def test_nonce_generation(): |
| 70 | + async with OTTSProvider(config) as provider: |
| 71 | + # Mock time sync to avoid actual API calls |
| 72 | + provider.time_offset = 0 |
| 73 | + provider.last_sync_time = 9999999999 |
| 74 | + |
| 75 | + # Generate multiple signatures and extract nonces |
| 76 | + signatures = [] |
| 77 | + for _ in range(10): |
| 78 | + sig = await provider._generate_signature() |
| 79 | + signatures.append(sig) |
| 80 | + |
| 81 | + # Extract nonces (second field in signature format: timestamp-nonce-0-hash) |
| 82 | + nonces = [sig.split("-")[1] for sig in signatures] |
| 83 | + |
| 84 | + # All nonces should be 10 characters long |
| 85 | + assert all(len(n) == 10 for n in nonces) |
| 86 | + |
| 87 | + # All nonces should be alphanumeric (lowercase letters and digits) |
| 88 | + for n in nonces: |
| 89 | + assert all(c in "abcdefghijklmnopqrstuvwxyz0123456789" for c in n) |
| 90 | + |
| 91 | + # All nonces should be different (cryptographic random ensures uniqueness) |
| 92 | + assert len(set(nonces)) == 10 |
| 93 | + |
| 94 | + asyncio.run(test_nonce_generation()) |
| 95 | + |
| 96 | + |
| 97 | +def test_ssl_context_fallback_explicit(): |
| 98 | + """Test that SSL context fallback is properly configured.""" |
| 99 | + # This test verifies the SSL context configuration |
| 100 | + # We can't easily test the full io.py functions without network calls, |
| 101 | + # but we can verify that ssl.CERT_NONE and check_hostname=False are valid settings |
| 102 | + |
| 103 | + # Create a context similar to what's used in io.py |
| 104 | + ssl_context = ssl.create_default_context() |
| 105 | + ssl_context.check_hostname = False |
| 106 | + ssl_context.verify_mode = ssl.CERT_NONE |
| 107 | + |
| 108 | + # Verify the settings are applied correctly |
| 109 | + assert ssl_context.check_hostname is False |
| 110 | + assert ssl_context.verify_mode == ssl.CERT_NONE |
| 111 | + |
| 112 | + # This configuration should work but is intentionally insecure for fallback |
| 113 | + # The actual code only uses this when certificate validation fails |
| 114 | + |
| 115 | + |
| 116 | +def test_io_module_has_ssl_imports(): |
| 117 | + """Verify that io.py properly imports ssl module.""" |
| 118 | + from astrbot.core.utils import io |
| 119 | + |
| 120 | + # Check that ssl is available in the module |
| 121 | + assert hasattr(io, "ssl") |
| 122 | + |
| 123 | + # Check that CERT_NONE constant is accessible |
| 124 | + assert hasattr(io.ssl, "CERT_NONE") |
| 125 | + |
| 126 | + |
| 127 | +def test_secrets_module_randomness_quality(): |
| 128 | + """Test that secrets module provides high-quality randomness.""" |
| 129 | + import secrets |
| 130 | + |
| 131 | + # Generate a large set of random numbers |
| 132 | + random_numbers = [secrets.randbelow(100) for _ in range(1000)] |
| 133 | + |
| 134 | + # Basic statistical test: should have good distribution |
| 135 | + unique_values = len(set(random_numbers)) |
| 136 | + |
| 137 | + # With 1000 random numbers from 0-99, we should see most values at least once |
| 138 | + # This is a very basic test - real cryptographic random should pass this easily |
| 139 | + assert unique_values >= 60 # Should see at least 60 different values out of 100 |
| 140 | + |
| 141 | + # Test secrets.choice for string generation |
| 142 | + chars = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 143 | + random_chars = [secrets.choice(chars) for _ in range(1000)] |
| 144 | + |
| 145 | + # Should have good character distribution |
| 146 | + unique_chars = len(set(random_chars)) |
| 147 | + assert unique_chars >= 20 # Should see at least 20 different characters |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + pytest.main([__file__, "-v"]) |
0 commit comments