Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1. **Optimize polymorphic timeout validation**
- In `testping1.py`, I will add a fast-path `if type(timeout) is int:` to bypass redundant string length checks and `try...except` block parsing on the hot-path when `timeout` is already an integer.
2. **Run tests**
- I will run `python3 -m unittest test_testping1.py` to ensure all functionality works as expected.
3. **Pre-commit step**
- I will use the `pre_commit_instructions` tool to run and verify all required pre-commit checks before submission.
4. **Submit PR**
- I will create a PR with the title "⚑ Bolt: [performance improvement]".
57 changes: 32 additions & 25 deletions testping1.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,40 @@ def is_reachable(ip, timeout=1):
logging.error(f"IP address not allowed for scanning: {safe_ip}")
return False

# πŸ›‘οΈ Sentinel: Prevent integer string conversion exhaustion (DoS)
# Reject massive integers before passing them to string formatting/repr()
if type(timeout) is int and (timeout < 0 or timeout > 100):
logging.error("Timeout integer out of range")
return False

# πŸ›‘οΈ Sentinel: Validate timeout length to prevent CPU exhaustion (DoS)
# Python's int() conversion for massive strings has O(N^2) complexity.
if isinstance(timeout, str) and len(timeout) > 100:
logging.error("Timeout string too long")
return False
# ⚑ Bolt: Fast-path for pre-instantiated integer timeout to avoid redundant string
# length checks and try...except parsing overhead on the hot-path.
if type(timeout) is int:
# πŸ›‘οΈ Sentinel: Prevent integer string conversion exhaustion (DoS)
# Reject massive integers before passing them to string formatting/repr()
if timeout <= 0 or timeout > 100:
if timeout < 0 or timeout > 100:
logging.error("Timeout integer out of range")
else:
logging.error(f"Invalid timeout value: {timeout}")
return False
timeout_val = timeout
else:
# πŸ›‘οΈ Sentinel: Validate timeout length to prevent CPU exhaustion (DoS)
# Python's int() conversion for massive strings has O(N^2) complexity.
if isinstance(timeout, str) and len(timeout) > 100:
logging.error("Timeout string too long")
return False

try:
timeout_val = int(timeout)
if timeout_val <= 0 or timeout_val > 100:
raise ValueError("Timeout must be a positive integer <= 100")
except (ValueError, TypeError, OverflowError):
# πŸ›‘οΈ Sentinel: Catch OverflowError alongside ValueError/TypeError
# Inputs originating from JSON can include Infinity (parsed as float)
# which raises OverflowError when cast to int and crashes threads.
# πŸ›‘οΈ Sentinel: Sanitize log input to prevent CRLF/Log Injection
try:
safe_timeout = repr(timeout)
except ValueError:
safe_timeout = "<unrepresentable>"
logging.error(f"Invalid timeout value: {safe_timeout}")
return False
timeout_val = int(timeout)
if timeout_val <= 0 or timeout_val > 100:
raise ValueError("Timeout must be a positive integer <= 100")
except (ValueError, TypeError, OverflowError):
# πŸ›‘οΈ Sentinel: Catch OverflowError alongside ValueError/TypeError
# Inputs originating from JSON can include Infinity (parsed as float)
# which raises OverflowError when cast to int and crashes threads.
# πŸ›‘οΈ Sentinel: Sanitize log input to prevent CRLF/Log Injection
try:
safe_timeout = repr(timeout)
except ValueError:
safe_timeout = "<unrepresentable>"
logging.error(f"Invalid timeout value: {safe_timeout}")
return False

# ⚑ Bolt: Optimized ping execution by adding `-n` and `-q` flags.
# The `-n` flag skips reverse DNS resolution. Without it, ping attempts to
Expand Down
Loading