Skip to content

Conversation

@luck9823
Copy link

@luck9823 luck9823 commented Feb 8, 2026

Fixes #1219

Problem

The SOCKS proxy parser in nettacker/core/socks_proxy.py throws IndexError when parsing malformed proxy strings that don't follow the expected format username:password@host:port.

Solution

  • Added safe parsing with length checks before accessing list elements
  • Added default values for missing components:
    • Empty strings for credentials
    • 127.0.0.1:1080 for host/port
  • Split only on first @ to handle edge cases in credentials

Changes

  • Modified set_socks_proxy() function in nettacker/core/socks_proxy.py
  • Added comments explaining the parsing logic
  • Improved error handling for both authenticated and non-authenticated proxy formats

Testing

Tested with various malformed proxy strings:

  • malformed_proxy (no separators)
  • user@host (missing password and port)
  • user:pass@host (missing port)

All cases now handle gracefully without raising IndexError.

Type of change

  • Bugfix (non-breaking change which fixes an issue)

Checklist

  • I've followed the contributing guidelines
  • Code follows project style guidelines
  • Self-review performed
  • Comments added for complex logic

---

### **Title должен остаться:**

Fix IndexError in SOCKS proxy parser for malformed credentials

- Add safe parsing for username:password@host:port format
- Add length checks before accessing list elements
- Add default values (empty string for credentials, 127.0.0.1:1080 for host)
- Fixes potential IndexError when parsing malformed proxy strings

Relates to OWASP#1219
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 8, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Improved SOCKS proxy handling so credentialed and non-credentialed proxy strings are parsed reliably, with sensible defaults for missing ports.
  • Documentation
    • Clarified proxy input format and behavior in internal documentation.

Walkthrough

Enhanced SOCKS proxy parsing in set_socks_proxy: robustly handles credentials (including @/: in passwords), validates host and port parsing with safe defaults (port 1080), preserves return types, and adds a docstring describing input format and return value.

Changes

Cohort / File(s) Summary
SOCKS proxy implementation
nettacker/core/socks_proxy.py
Rewrote parsing in set_socks_proxy to use rsplit/split with maxsplit, validate credential and host:port parts, handle missing/invalid ports by defaulting to 1080, correctly apply optional username/password to socks.set_default_proxy, and added a docstring.
Tests (proxy parsing)
tests/core/test_socks_proxy.py
(If present) Added/updated unit tests covering valid proxies with/without auth, malformed inputs (missing colon), and credentials containing @ or :.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: fixing an IndexError in the SOCKS proxy parser when handling malformed credentials.
Description check ✅ Passed The PR description is well-related to the changeset, explaining the problem, solution, changes made, and testing approach.
Linked Issues check ✅ Passed The PR addresses issue #1219 requirements: handles malformed SOCKS proxy strings, validates split results before access, uses rsplit/split with maxsplit for edge cases, and provides default values.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the SOCKS proxy parser per issue #1219. The PR modifies only nettacker/core/socks_proxy.py to address the documented objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@nettacker/core/socks_proxy.py`:
- Line 37: The port parsing currently uses int(host_parts[1]) which raises
ValueError for non-numeric ports; update both occurrences (the port assignment
that uses host_parts and the similar one at the second occurrence) to guard
conversion with a try/except ValueError (or strconv-safe parse) and fall back to
the default port 1080 when conversion fails, ensuring host_parts and port
variables are used unchanged otherwise and the code continues gracefully.
- Line 27: The parsing of proxy strings currently uses socks_proxy.split("@", 1)
which breaks when passwords contain '@'; update the split to use rsplit("@", 1)
so the last '@' is used as the delimiter (modify the line that assigns
auth_part, host_part from socks_proxy), and run/adjust any related tests or
callers that expect correct auth_part/host_part extraction.
🧹 Nitpick comments (2)
nettacker/core/socks_proxy.py (2)

31-31: Redundant len(...) > 0 guards — str.split() always returns at least one element.

auth_parts[0] and host_parts[0] are always safe to access after split()/rsplit(), so the len(...) > 0 checks on lines 31, 36, and 49 are dead branches. Not harmful, but slightly misleading.

♻️ Simplified (line 31 example; same pattern for 36 and 49)
-            socks_username = auth_parts[0] if len(auth_parts) > 0 else ""
+            socks_username = auth_parts[0]

Also applies to: 36-36, 49-49


17-17: Missing docstring and type hints on set_socks_proxy.

As per coding guidelines, "use type hints where practical, and add docstrings for public APIs." A brief docstring and a return type hint would improve discoverability and maintainability.

- Change split('@', 1) to rsplit('@', 1) to handle @ in passwords
- Add ValueError handling for non-numeric port values
- Remove redundant len() > 0 checks for split()[0]
- Add docstring for set_socks_proxy function
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@nettacker/core/socks_proxy.py`:
- Around line 18-28: The docstring for the SOCKS proxy configuration is
inaccurate—update the docstring in the function that configures SOCKS proxy (the
block that checks url.startswith('socks5://') and falls back to SOCKS4) to
mention both 'socks5://' and 'socks4://' formats, including authenticated
('socks5://username:password@host:port' or
'socks4://username:password@host:port') and unauthenticated
('socks5://host:port' or 'socks4://host:port') forms, and keep the returned
tuple description (socket_class, getaddrinfo_function) unchanged so it matches
the implementation.
🧹 Nitpick comments (2)
nettacker/core/socks_proxy.py (2)

64-73: Duplicate host:port parsing logic could be extracted into a helper.

Lines 46–54 and 65–73 are nearly identical. A small helper like _parse_host_port(host_part, default_port=1080) would reduce duplication and make future changes easier.

♻️ Suggested refactor
+def _parse_host_port(host_part, default_port=1080):
+    """Parse host:port string, returning (hostname, port) with a safe default."""
+    parts = host_part.rsplit(":", 1)
+    hostname = parts[0]
+    if len(parts) > 1:
+        try:
+            port = int(parts[1])
+        except ValueError:
+            port = default_port
+    else:
+        port = default_port
+    return hostname, port

Then in both branches:

-            host_parts = host_part.rsplit(":", 1)
-            hostname = host_parts[0]
-            if len(host_parts) > 1:
-                try:
-                    port = int(host_parts[1])
-                except ValueError:
-                    port = 1080
-            else:
-                port = 1080
+            hostname, port = _parse_host_port(host_part)

56-79: Redundant str() wrapping on hostname.

hostname is already a str (result of str.rsplit), so the str(hostname) calls on lines 58 and 77 are unnecessary.

@luck9823
Copy link
Author

Closing as duplicate of #1214.

I see that @moksha-hub's PR addresses the same SOCKS proxy issue.

Will work on a different contribution instead.

Thank you for the feedback!

@luck9823 luck9823 closed this Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants