Skip to content

Refactor loadFromFile to use bufio.Scanner - #1791

Merged
Mzack9999 merged 1 commit into
projectdiscovery:devfrom
nandaanomi:patch-1
Jul 2, 2026
Merged

Mzack9999 merged 1 commit into
projectdiscovery:devfrom
nandaanomi:patch-1

Conversation

@nandaanomi

@nandaanomi nandaanomi commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #1790

Two bugs fixed in pkg/runner/util.go:

1. Replaced fileutil.ReadFile (channel-based, goroutine errors not propagatable) with bufio.Scanner + os.Open. scanner.Err() is now returned so any I/O error during mid-read is surfaced to the caller instead of silently producing a partial list.

2. preprocessDomain now strips https:// and http:// prefixes and truncates at the first /, ?, or #. This prevents malformed entries like https://8.8.8.8 in resolver/domain list files from silently failing inside dnsx.

Minor: added make([]string, 0, 64) to reduce reallocs on large input files.

Proof

echo "https://8.8.8.8" > resolvers.txt
subfinder -d example.com -rL resolvers.txt -v

Before: resolver passed as-is, silently unused, no error shown.
After: entry normalized to 8.8.8.8, resolver used correctly.

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Improvements
    • Domain processing now removes leading http:// and https:// prefixes and extracts only the host portion by trimming paths, queries, and fragments for more consistent normalization and matching.
    • Input file loading behavior is validated to ensure cleaner, ordered domain results.
  • Tests
    • Added unit tests covering domain preprocessing and file-based loading, including handling of missing input files and comment/whitespace normalization.

@coderabbitai

coderabbitai Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

pkg/runner/util.go now scans resolver files directly, returns scan errors, and normalizes domain inputs by stripping schemes and URL suffixes. New tests cover domain preprocessing, file loading, and missing-file errors.

Changes

Resolver File Loading and Domain Preprocessing

Layer / File(s) Summary
File I/O and Domain Preprocessing Pipeline
pkg/runner/util.go, pkg/runner/util_test.go
loadFromFile now scans input line by line and propagates read failures, while preprocessDomain strips https:///http:// and truncates at /, ?, or # after existing normalization. Tests cover normalization cases, file loading order, and missing-file errors.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

I hop through lines with scanner light,
And trim each domain just right.
Schemes fall off, paths vanish too,
Now clean resolver hops shine through. 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes one of the main changes in the PR.
Linked Issues check ✅ Passed The PR addresses both linked requirements by surfacing read errors with bufio.Scanner and normalizing URL-based resolver entries.
Out of Scope Changes check ✅ Passed The added tests and util changes are directly related to the linked bugs and do not introduce unrelated scope.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/runner/util.go (1)

11-28: 💤 Low value

Handle the f.Close() error (or explicitly discard it) to satisfy errcheck.

golangci-lint (errcheck) flags the deferred f.Close() on line 16. For a read-only file the actual risk is negligible, but this will likely fail the CI lint gate. The simplest fix is an explicit discard.

♻️ Proposed tweak
-	defer f.Close()
+	defer func() { _ = f.Close() }()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/runner/util.go` around lines 11 - 28, The deferred file close in
loadFromFile currently ignores the returned error which trips errcheck; update
the defer to explicitly discard or handle the error from f.Close() (for example
use defer func(){ _ = f.Close() }() or capture and log/return it) so that the
close error is not ignored; change the defer f.Close() inside loadFromFile
accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/runner/util.go`:
- Around line 11-28: The deferred file close in loadFromFile currently ignores
the returned error which trips errcheck; update the defer to explicitly discard
or handle the error from f.Close() (for example use defer func(){ _ = f.Close()
}() or capture and log/return it) so that the close error is not ignored; change
the defer f.Close() inside loadFromFile accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 454d648c-6b74-4860-afb8-b5e1ce0b1a3c

📥 Commits

Reviewing files that changed from the base of the PR and between 0ed1b0b and 133c01d.

📒 Files selected for processing (1)
  • pkg/runner/util.go

@dogancanbakir
dogancanbakir requested a review from Mzack9999 July 1, 2026 08:57

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/runner/util.go (1)

26-43: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Scheme/path stripping logic looks correct.

Order of operations is sound: normalization lowercases first, so TrimPrefix reliably matches mixed-case schemes (e.g., HTTPS://), and IndexAny correctly truncates at the first path/query/fragment delimiter after scheme removal. Test cases in util_test.go corroborate the expected behavior for scheme, path, query, fragment, and IP normalization.

One optional improvement: hardcoding two TrimPrefix calls works today but doesn't scale if additional schemes (e.g., ftp://) need support later.

♻️ Optional refactor for extensibility
-	s = strings.TrimPrefix(s, "https://")
-	s = strings.TrimPrefix(s, "http://")
+	for _, scheme := range []string{"https://", "http://"} {
+		s = strings.TrimPrefix(s, scheme)
+	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/runner/util.go` around lines 26 - 43, The current scheme/path stripping
in preprocessDomain works, but it is hardcoded to only handle http and https.
Refactor the trimming logic in preprocessDomain to use a more extensible
scheme-removal approach so future schemes can be supported without adding more
TrimPrefix calls, while preserving the existing normalization and
path/query/fragment truncation behavior verified by util_test.go.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/runner/util.go`:
- Around line 26-43: The current scheme/path stripping in preprocessDomain
works, but it is hardcoded to only handle http and https. Refactor the trimming
logic in preprocessDomain to use a more extensible scheme-removal approach so
future schemes can be supported without adding more TrimPrefix calls, while
preserving the existing normalization and path/query/fragment truncation
behavior verified by util_test.go.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3156e3f5-83b9-4d90-8986-013bcf2a59b5

📥 Commits

Reviewing files that changed from the base of the PR and between 553f45a and 44dfb90.

📒 Files selected for processing (2)
  • pkg/runner/util.go
  • pkg/runner/util_test.go

@Mzack9999
Mzack9999 merged commit d0ea102 into projectdiscovery:dev Jul 2, 2026
9 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
subfinder 2.15.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed

### 🎉 New Features
* Added shodanct passive source by @dogancanbakir in projectdiscovery/subfinder#1796
* Added Option `-vR`, `-virustotal-res` for VirusTotal source by @Bundy01 in projectdiscovery/subfinder#1719
* Added machine-readable source listing with `-ls -oJ` by @Gerijacki in projectdiscovery/subfinder#1812

### 🐞 Bug Fixes
* Fixed source leaks by @Mzack9999 in projectdiscovery/subfinder#1801
* Fixed drain source channels on cancel so source goroutines exit instead of leaking on a blocked send by @Mzack9999 in projectdiscovery/subfinder#1799
* Fixed crtsh's query by dropping unused certificate metadata from SQL query by @ChrisJr404 in projectdiscovery/subfinder#1787
* Fixed VirusTotal source by capping pagination and providing clearer 429 message by @ChrisJr404 in projectdiscovery/subfinder#1788
* Fixed Anubis source by switching subdomain API to anubisdb.com by @jatinder14 in projectdiscovery/subfinder#1815

### Other Changes
* Fixed lint issues by @dogancanbakir in projectdiscovery/subfinder#1820
* Refactored loadFromFile to use bufio.Scanner by @nandaanomi in projectdiscovery/subfinder#1791


## New Contributors
* @ChrisJr404 made their first contribution in projectdiscovery/subfinder#1787
* @Bundy01 made their first contribution in projectdiscovery/subfinder#1719
* @nandaanomi made their first contribution in projectdiscovery/subfinder#1791
* @jatinder14 made their first contribution in projectdiscovery/subfinder#1815
* @Gerijacki made their first contribution in projectdiscovery/subfinder#1812

**Full Changelog**: https://github.com/projectdiscovery/subfinder/compare/v2.14.0...v2.15.0</pre>
  <p>View the full release notes at <a href="https://github.com/projectdiscovery/subfinder/releases/tag/v2.15.0">https://github.com/projectdiscovery/subfinder/releases/tag/v2.15.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!16007
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.

[bug] loadFromFile silently drops mid-read I/O errors and does not sanitize URL schemes in pkg/runner/util.go

2 participants