fix: tlsx hangs indefinitely for some hosts#920
fix: tlsx hangs indefinitely for some hosts#920SolariSystems wants to merge 2 commits intoprojectdiscovery:mainfrom
Conversation
Addresses projectdiscovery#819 Automated fix by Solari
Neo Security AuditNo security issues found Highlights
Hardening Notes
Comment |
WalkthroughReplaces blocking TLS handshakes with context-aware, per-call timeouts during cipher enumeration in both the standard tls and custom ztls paths; ztls uses a goroutine-based handshake wrapper to enforce timeouts and treat tls.ErrCertsOnly as success. Changes
Sequence Diagram(s)sequenceDiagram
participant Enum as EnumerateCiphers
participant Ctx as Context (timeout)
participant TLS as TLS Conn
participant Gor as Goroutine
Enum->>Ctx: create timeout context (options.Timeout || 5s)
alt Standard TLS path
Enum->>TLS: HandshakeContext(ctx)
TLS-->>Enum: success / error / ctx.Done
else ztls path
Enum->>Gor: start goroutine -> tlsConn.Handshake()
Gor->>TLS: execute Handshake()
par
TLS-->>Gor: handshake result
and
Ctx-->>Enum: ctx.Done (timeout)
Enum->>TLS: Close() on timeout
end
Gor-->>Enum: handshake result (or blocked until Close)
end
Enum->>Ctx: cancel context
Enum->>Enum: record cipher on success / skip on error
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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. Comment |
Address security hardening suggestions from Neo review: - Close tlsConn on context timeout in tlsHandshakeWithTimeout() to unblock the goroutine stuck in Handshake() and prevent goroutine accumulation under sustained timeout conditions - Add defer enumCancel() after context creation in both tls and ztls EnumerateCiphers to ensure context resources are released even if early returns occur Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Fixes #819
Fix the ztls tlsHandshakeWithTimeout() function which has a fundamental design flaw: Handshake() is called synchronously inside a select case expression, meaning the select NEVER evaluates ctx.Done() if the handshake blocks. The fix is to run the handshake in a goroutine so the select can properly race between handshake completion and context timeout. Additionally, fix cipher enumeration in both tls and ztls modes which lack proper timeouts (bare Handshake() and context.TODO()).
Changes Made
The diff correctly fixes the root cause of the indefinite hang (synchronous Handshake() in select case expression) and adds proper timeouts to all three affected code paths.
Verification
Summary by CodeRabbit
New Features
Bug Fixes
/claim #819