Skip to content

fix: improve error handling and fix bugs#2499

Merged
Mzack9999 merged 2 commits into
projectdiscovery:devfrom
dashitongzhi:fix/code-quality-improvements
May 20, 2026
Merged

fix: improve error handling and fix bugs#2499
Mzack9999 merged 2 commits into
projectdiscovery:devfrom
dashitongzhi:fix/code-quality-improvements

Conversation

@dashitongzhi
Copy link
Copy Markdown
Contributor

@dashitongzhi dashitongzhi commented May 20, 2026

Summary

  • Fixed bug where DecodeData failure returned wrong error
  • Added proper error handling for strconv.Atoi parsing errors
  • Changed mutable variable hammingDistanceThreshold to constant
  • Improved port parsing validation
  • Fixed redundant return statement in fileutil.go

Motivation

Improve code robustness and fix silent error handling issues.

Changes

  • Error handling improvements in httpx.go
  • Port parsing validation improvements
  • Code quality fixes across multiple files

Summary by CodeRabbit

  • Bug Fixes
    • Fixed file discovery to not report errors on successful file matches
    • Improved HTTP response error handling and Content-Length header parsing
    • Enhanced port number parsing to gracefully handle malformed or missing port values across multiple operations

Review Change Stack

- Fix bug: return err instead of closeErr in DecodeData error path (httpx.go)
- Improve error handling for strconv.Atoi calls in multiple locations
- Change hammingDistanceThreshold from var to const since it's never modified
- Fix redundant return statement in ListFilesWithPattern
- Improve port parsing to handle empty port strings explicitly
Copilot AI review requested due to automatic review settings May 20, 2026 01:35
@auto-assign auto-assign Bot requested a review from Mzack9999 May 20, 2026 01:35
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bff3ac5b-9ac7-4bbc-b639-007a2c5e0b7f

📥 Commits

Reviewing files that changed from the base of the PR and between 77b7521 and 127074e.

📒 Files selected for processing (4)
  • common/fileutil/fileutil.go
  • common/hashes/jarm/jarmhash.go
  • common/httpx/httpx.go
  • runner/runner.go

Walkthrough

This PR improves error handling and input validation across four files. HTTP globbing and response decoding now properly surface errors instead of losing them. Port extraction is hardened against parse failures in two independent codepaths. A package constant is introduced to replace a mutable variable.

Changes

Error Handling and Port Parsing Improvements

Layer / File(s) Summary
Glob and HTTP response error handling
common/fileutil/fileutil.go, common/httpx/httpx.go
ListFilesWithPattern returns nil error on successful glob matches. (*HTTPX).Do now returns actual decode errors instead of close errors, and only updates resp.ContentLength when header parsing succeeds.
Port parsing and constant refactoring
common/hashes/jarm/jarmhash.go, runner/runner.go
Jarm and pipeline detection conditionally parse port strings, defaulting on parse failure or absence. hammingDistanceThreshold is moved from mutable variable to package-level constant.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A hop through the code, errors now clear,
Ports parse with grace when numbers appear,
No silent failures, no loss in the fray,
Just defensive coding to brighten the day! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is vague and generic, using broad terms like 'improve error handling and fix bugs' without specifying which errors or which bugs are being addressed. Make the title more specific by highlighting the primary bug fix or error handling improvement, such as 'fix: correct error propagation in httpx DecodeData and improve port parsing' to better communicate the main changes.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates httpx runner behavior around deduplication/proxy defaults and persistence, adds CPE storage to DB backends, and bumps dependencies/Go version.

Changes:

  • Tighten input and response deduplication logic (thread-safe testAndSet, IP-aware simhash dedupe) and add tests.
  • Add Options.HasMatcherOrFilter() and change response storage to persist only matched outputs when matchers/filters are active.
  • Add cpe column support in Postgres/MySQL schemas and batch inserts; update proxy env fallback and dependency versions.

Reviewed changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
runner/runner.go Adds mutex to testAndSet, changes simhash cache value type and dedupe logic, updates input streaming, and modifies response storing behavior.
runner/runner_test.go Adds unit and concurrency tests for testAndSet, matcher/filter detection, and duplicate detection semantics.
runner/options.go Adds HasMatcherOrFilter() for detecting active output matchers/filters and updates resolver file reading.
runner/headless.go Falls back to proxy settings from environment variables for headless browser.
common/httpx/httpx.go Uses protocol constants, adds proxy-from-env default, adjusts retryable http2 fallback behavior, and fixes error handling.
common/httpx/httpx_test.go Adds tests validating HTTP/1.1 disables HTTP/2 fallback client.
internal/db/postgres.go Adds cpe column to schema and inserts it in batch writes; adds upgrade-time ALTER for back-compat.
internal/db/mysql.go Adds cpe column to schema and inserts it in batch writes; adds upgrade-time column ensure helper.
common/hashes/jarm/jarmhash.go Makes port parsing robust when URL has no/invalid port.
common/fileutil/fileutil.go Changes ListFilesWithPattern to always return nil error when files exist.
README.md Minor wording/punctuation fixes.
go.mod / go.sum Bumps Go version and updates multiple dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread runner/runner.go
Comment on lines +645 to +657
for storedHash, storedIPs := range r.simHashes.GetALL(false) {
if simhash.Compare(storedHash, respSimHash) > 3 {
continue
}
if ip == "" || sliceutil.Contains(storedIPs, ip) {
gologger.Debug().Msgf("Skipping duplicate response (simhash %d, ip %s) for URL %s\n", respSimHash, ip, result.URL)
return true
}
_ = r.simHashes.Set(storedHash, append(storedIPs, ip))
return false
}
_ = r.simHashes.Set(respSimHash, struct{}{})

_ = r.simHashes.Set(respSimHash, []string{ip})
Comment thread runner/options.go
Comment on lines +699 to +721
func (options *Options) HasMatcherOrFilter() bool {
return len(options.matchStatusCode) > 0 ||
len(options.matchContentLength) > 0 ||
len(options.filterStatusCode) > 0 ||
len(options.filterContentLength) > 0 ||
len(options.matchRegexes) > 0 ||
len(options.filterRegexes) > 0 ||
len(options.matchLinesCount) > 0 ||
len(options.matchWordsCount) > 0 ||
len(options.filterLinesCount) > 0 ||
len(options.filterWordsCount) > 0 ||
len(options.OutputMatchString) > 0 ||
len(options.OutputFilterString) > 0 ||
len(options.OutputMatchFavicon) > 0 ||
len(options.OutputFilterFavicon) > 0 ||
len(options.OutputMatchCdn) > 0 ||
len(options.OutputFilterCdn) > 0 ||
len(options.OutputFilterPageType) > 0 ||
options.OutputMatchCondition != "" ||
options.OutputFilterCondition != "" ||
options.OutputMatchResponseTime != "" ||
options.OutputFilterResponseTime != ""
}
return nil, errors.New("no files found")
}
return files, err
return files, nil
Comment thread internal/db/postgres.go
Comment on lines +159 to +165
-- Back-compat for databases whose schema was created before CPE support.
-- New installs already get this column via the CREATE TABLE above; this
-- statement only matters for in-place upgrades.
-- TODO: replace these ad-hoc ALTER TABLE statements with a proper
-- migration framework (e.g. golang-migrate / goose) once more schema
-- changes accumulate.
ALTER TABLE %s ADD COLUMN IF NOT EXISTS cpe JSONB;
@Mzack9999 Mzack9999 changed the base branch from main to dev May 20, 2026 04:06
Revert the defensive strconv.Atoi guard around the SkipDedupe branches:
values stored in r.hm are always integer strings written by this same
code path, so the parseErr/cnt>0 check is unreachable in practice, and
when taken literally it skipped the numHosts/numTargets increment for
duplicate inputs.
@Mzack9999 Mzack9999 merged commit d0081af into projectdiscovery:dev May 20, 2026
14 checks passed
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.

3 participants