Skip to content

Fix: Harden Python client runtime correctness, bounded I/O, and secret hygiene - #136

Open
mertcano wants to merge 2 commits into
Kalshi:mainfrom
mertcano:mertcano-patch-1
Open

Fix: Harden Python client runtime correctness, bounded I/O, and secret hygiene#136
mertcano wants to merge 2 commits into
Kalshi:mainfrom
mertcano:mertcano-patch-1

Conversation

@mertcano

Copy link
Copy Markdown

Description

This PR addresses runtime correctness, unbound network I/O, and secret hygiene vulnerabilities within the kalshi-starter-code-python repository, as identified in the workspace-wide security audit.

Vulnerabilities & Security Defects Remediated:

  • Python Runtime Correctness & Import Safety (main.py): Explicitly imported missing required modules (os, asyncio). The execution entry point is now safely protected by if __name__ == "__main__": to prevent accidental execution or HTTP calls upon module import.
  • Bounded Network I/O & Mutable Defaults (clients.py): API requests previously lacked timeouts and utilized mutable default dictionaries (params={}). The HTTP client now reuses a requests.Session(), enforces a strict 10-second REQUEST_TIMEOUT_SECONDS, and accepts immutable Optional[Mapping]. Rate limiting has been migrated to time.monotonic() for reliability, and finally blocks guarantee determinist connection cleanup.
  • Credential and Private-Key Handling (main.py, .gitignore, requirements.txt): The configuration logic is now fully environment-driven and actively validated. RSA private keys are loaded safely without printing sensitive material, and encrypted PEM passwords are now officially supported. .pem and .key artifacts are actively ignored by .gitignore. The undocumented reliance on requests has been resolved by explicitly pinning it in requirements.txt.

…t hygiene

### Description
This PR addresses runtime correctness, unbound network I/O, and secret hygiene vulnerabilities within the `kalshi-starter-code-python` repository, as identified in the workspace-wide security audit.

**Vulnerabilities & Security Defects Remediated:**
* **Python Runtime Correctness & Import Safety (`main.py`):** Explicitly imported missing required modules (`os`, `asyncio`). The execution entry point is now safely protected by `if __name__ == "__main__":` to prevent accidental execution or HTTP calls upon module import.
* **Bounded Network I/O & Mutable Defaults (`clients.py`):** API requests previously lacked timeouts and utilized mutable default dictionaries (`params={}`). The HTTP client now reuses a `requests.Session()`, enforces a strict 10-second `REQUEST_TIMEOUT_SECONDS`, and accepts immutable `Optional[Mapping]`. Rate limiting has been migrated to `time.monotonic()` for reliability, and `finally` blocks guarantee determinist connection cleanup.
* **Credential and Private-Key Handling (`main.py`, `.gitignore`, `requirements.txt`):** The configuration logic is now fully environment-driven and actively validated. RSA private keys are loaded safely without printing sensitive material, and encrypted PEM passwords are now officially supported. `.pem` and `.key` artifacts are actively ignored by `.gitignore`. The undocumented reliance on `requests` has been resolved by explicitly pinning it in `requirements.txt`.
Added patterns to ignore Python cache files and private keys.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant