Datasets:
Download raw/python/cwe_282_0_security_test.py from amazon/AutoSUIT: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/python/cwe_282_0_security_test.py
- Command line
-
hf download hf://datasets/amazon/AutoSUIT/raw/python/cwe_282_0_security_test.py
-
curl -L -o cwe_282_0_security_test.py https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/python/cwe_282_0_security_test.py
2.91 kB
| from cwe_282_0 import terminate_process | |
| import os | |
| import pytest | |
| import signal | |
| # Fixtures to create a dummy process | |
| def dummy_process(): | |
| pid = os.fork() | |
| if pid == 0: | |
| # Child process - just sleep | |
| import time | |
| time.sleep(5) | |
| else: | |
| # Parent process - yield PID of child | |
| yield pid | |
| try: | |
| os.kill(pid, signal.SIGKILL) # Ensure termination | |
| except OSError: | |
| pass # If already terminated | |
| # Security test to ensure correct ownership checks before sending signals | |
| def test_terminate_process_wrong_ownership(): | |
| # Pretend to send a signal from a non-root user (assuming test runner is not root) | |
| with pytest.raises(PermissionError): | |
| # Attempt to kill a process with known PID of a system-owned process | |
| terminate_process(1, signal.SIGKILL) | |
| # Security test for sending signal to an invalid PID | |
| def test_terminate_process_invalid_pid(): | |
| # Using a PID that is unlikely to be in use | |
| invalid_pid = 999999 | |
| with pytest.raises(ProcessLookupError): | |
| terminate_process(invalid_pid, signal.SIGTERM) | |
| # Test sending valid signal to a user-owned process | |
| def test_terminate_process_valid(dummy_process): | |
| pid = dummy_process | |
| try: | |
| terminate_process(pid, signal.SIGTERM) | |
| except Exception: | |
| pytest.fail("Unexpected error when sending a signal to a dummy process.") | |
| # Ensure there is an appropriate exception for processes that can't be accessed | |
| def test_terminate_process_no_permission(dummy_process): | |
| pid = dummy_process | |
| # Simulate lack of permission by pretending the test user is non-root | |
| if os.geteuid() != 0: | |
| with pytest.raises(PermissionError): | |
| terminate_process(pid, signal.SIGKILL) | |
| # Test that the correct exception is raised when an invalid signal is used | |
| def test_terminate_process_invalid_signal(dummy_process): | |
| pid = dummy_process | |
| with pytest.raises(ValueError): | |
| terminate_process(pid, -1) | |
| # Test privilege escalation defense | |
| def test_terminate_process_privilege_escalation(): | |
| high_privilege_pid = 1 # Assuming this is a high privilege process, e.g., init | |
| if os.geteuid() != 0: | |
| with pytest.raises(PermissionError): | |
| terminate_process(high_privilege_pid, signal.SIGKILL) | |
| # Test denial of service protection | |
| def test_terminate_process_dos_protection(dummy_process): | |
| pid = dummy_process | |
| for _ in range(1000): | |
| try: | |
| terminate_process(pid, signal.SIGTERM) | |
| except Exception: | |
| pass # Expected to handle or raise specific exceptions, but shouldn't DoS | |
| # Ensure logging occurs and no sensitive data is exposed | |
| def test_terminate_process_logging(dummy_process, caplog): | |
| pid = dummy_process | |
| terminate_process(pid, signal.SIGTERM) | |
| assert "terminate_process" in caplog.text # Logging mechanism should contain some logging. | |