Skip to content

refactor: isolate session state, fix concurrency races and protocol/POC anti-patterns - #620

Merged
ZacharyZcR merged 3 commits into
mainfrom
refactor/session-isolation-and-antipattern-fixes
Dec 1, 2026
Merged

ZacharyZcR merged 3 commits into
mainfrom
refactor/session-isolation-and-antipattern-fixes

Conversation

@ZacharyZcR

Copy link
Copy Markdown
Collaborator

Summary

Resolves several concurrency, session-isolation, networking, and performance anti-patterns across common, core, plugins, webscan, web, and pkg/fscan:

1. Session Isolation & Concurrency Safety

  • Session-scoped HTTP/GM-TLS clients (webscan/lib/Client.go, common/session.go, core/scanner.go, webscan/lib/Eval.go, plugins/web/webtitle.go):
    • Introduce HTTPClientSet + InitSessionHTTP / GetSessionHTTPClient so concurrent RunScan calls maintain independent transports, proxy configs, and timeouts, and release idle connections on exit.
    • Remove the process-wide os.Stdout redirection hack during GM-TLS handshakes; GM-TLS now reuses tr.DialContext to inherit proxy settings and context.Context cancellation without serializing handshakes or swallowing stdout.
    • Guard global HTTP client updates with sync.RWMutex and close idle connections on replaced transports.
  • Session-isolated proxy probing (common/proxy/manager.go, common/session.go):
    • Add NewIsolatedProxyManager so each ScanSession probes and caches its own SOCKS5 reliability without mutating global proxy state.
  • Logger & state race fixes (common/logger.go, core/web_scanner.go, plugins/web/webtitle.go):
    • Protect global logger reads/writes against concurrent PushSilentLogger resets with sync.RWMutex.
    • Switch core globalState to atomic.Pointer[common.State] and update cached ServiceInfo.Extras via copy-on-write (SetWebServiceProtocolWithState) to prevent concurrent map writes.
  • Web API & WebSocket fixes (web/ws/hub.go, web/api/scan.go, web/api/result.go, web/api/project.go):
    • Guard Client.send writes/closes to eliminate send on closed channel panics in readPump; add Hub.Stop() for clean shutdown.
    • Replace the global SetGlobalConfig/SetGlobalState/SetResultCallback bridge in web/api/scan.go with session.ResultSink, and fix unlocked h.startTime reads.
    • Lazy-initialize ResultStore and ProjectStore via sync.Once instead of performing filesystem I/O and panicking inside package init().

2. Unified Dialing & Context Cancellation

  • Route mongodb, oracle, mssql, cassandra, kafka, netbios, elasticsearch, and DetectHTTPSchemeContext TLS/GM-TLS handshakes through session.DialTCP/session.DialUDP so proxy settings, rate limits, packet counters, and ctx cancellation are honored consistently.
  • Wire CLI signal handling (main_cli.go) via signal.NotifyContext (graceful cancel on first signal, force exit on second), add missing signal.Stop in core/scanner.go, and propagate ctx.Done() checks to core/port_scan.go, core/icmp.go, webscan/lib/poc_executor.go, and plugins/services/credential_tester.go.

3. Fingerprint Performance, Nuclei Semantics & SDK Fixes

  • Fingerprint hot path (webscan/fingerprint/enhanced.go, webscan/fingerprint_scanner.go): Precompute lowercased body/headers and string(data.Body) once per page instead of thousands of times per scan, and collect worker matches into per-worker slices without allocating large buffered channels.
  • Nuclei adapter semantics (webscan/lib/poc_adapter.go): Default single-matcher condition to OR per Nuclei spec (fixing always-false expressions such as status: [200, 302]), and map multi-path single-request templates to OR rule groups (poc.Groups) instead of AND rules.
  • SDK & service plugins (pkg/fscan/scanner.go, plugins/services/redis.go, plugins/services/credential_tester.go):
    • Inject typed Usernames/Passwords directly into cfg.Credentials so credentials containing commas are not split; enforce DisablePocScan when AllowUnsafePlugins is false.
    • Respect configured ModuleThreadNum in Redis plugin instead of hardcoding concurrency to 20.
    • Prefer errors.Is/errors.As (context.DeadlineExceeded, io.EOF, net.Error) in ClassifyError and replace custom case-insensitive substring loops with strings.

Verification

  • go test -race ./common/... ./core ./plugins/... ./webscan/... ./pkg/fscan
  • go test -tags web ./web/...
  • go build -o /dev/null .
  • go build -tags web -o /dev/null .

@ZacharyZcR
ZacharyZcR merged commit 7c007f6 into main Dec 1, 2026
3 checks passed
@ZacharyZcR
ZacharyZcR deleted the refactor/session-isolation-and-antipattern-fixes branch December 1, 2026 17:40
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