Fix code scanning alert: bind socket to 127.0.0.1 instead of all interfaces#40
Open
tschm wants to merge 2 commits into
Open
Fix code scanning alert: bind socket to 127.0.0.1 instead of all interfaces#40tschm wants to merge 2 commits into
tschm wants to merge 2 commits into
Conversation
…network interfaces Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
find_free_port now binds to 127.0.0.1 (security alert fix), but
test_raises_on_no_free_port still occupied its port on all interfaces
(""). On Windows 0.0.0.0:port and 127.0.0.1:port don't conflict, so
find_free_port bound successfully and the expected RuntimeError was
never raised. Occupy the port on 127.0.0.1 so the conflict is seen.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a security/code-scanning finding by ensuring find_free_port no longer binds to all network interfaces, and adjusts the corresponding unit test so port-collision behavior remains consistent on Windows.
Changes:
- Update
find_free_portto bind to127.0.0.1instead of all interfaces when probing ports. - Update the “no free port” test to occupy the port on
127.0.0.1so the conflict is detected on Windows.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/pycharting/core/server.py | Changes port-probing binds from all interfaces to loopback to satisfy the code-scanning alert. |
| tests/test_server.py | Updates test socket binding to loopback so the occupied-port test remains valid on Windows. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
78
to
82
| if start_port is None: | ||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
| s.bind(("", 0)) | ||
| s.bind(("127.0.0.1", 0)) | ||
| return s.getsockname()[1] |
Comment on lines
89
to
92
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.bind(("", port)) | ||
| s.bind(("127.0.0.1", port)) | ||
| s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
| return port |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves the "Binding a socket to all network interfaces" code scanning alert in
find_free_port.src/pycharting/core/server.py: bind to127.0.0.1instead of""(all interfaces).tests/test_server.py: occupy the test port on127.0.0.1so the no-free-port case still conflicts (without this, the test fails on Windows, where0.0.0.0:portand127.0.0.1:portdon't collide).🤖 Generated with Claude Code