Skip to content

fix(extractor): match the target domain literally and reject partial hosts - #1811

Merged
Mzack9999 merged 2 commits into
projectdiscovery:devfrom
Gerijacki:fix/extractor-domain-anchoring
Aug 5, 2026
Merged

Mzack9999 merged 2 commits into
projectdiscovery:devfrom
Gerijacki:fix/extractor-domain-anchoring

Conversation

@Gerijacki

@Gerijacki Gerijacki commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

NewSubdomainExtractor builds its regex by interpolating the raw domain:

regexp.Compile(`(?i)[a-zA-Z0-9\*_.-]+\.` + domain)

This has two problems:

  1. Dots in the domain are treated as "any character". For example.com the regex also matches text like exampleXcom, producing false positives.
  2. The match is unbounded on the right. Text such as foo.example.com.evil.org yields the bogus subdomain foo.example.com, which then passes the runner's HasSuffix("."+domain) check and reaches the output.

This affects every source that extracts hosts from free text (crtsh, waybackarchive, commoncrawl, hackertarget, digitorus, rapiddns, urlscan, sitedossier, ...).

Fix

  • Escape the domain with regexp.QuoteMeta so its dots (and any other metacharacters) match literally.
  • Drop matches that are only a prefix of a longer hostname, while still accepting the trailing-dot FQDN form (host.example.com.).

Tests

Added table-driven tests for the extractor covering literal dots, right-boundary rejection, wildcards, trailing dots, and case-insensitivity. The package had no extractor test before.

Summary by CodeRabbit

  • Bug Fixes
    • Improved subdomain extraction accuracy, including correct handling of domains with special characters.
    • Reduced false positives by tightening hostname boundary matching and rejecting mismatched apex/overlong label patterns.
    • Ensured consistent lowercasing of extracted subdomains, with correct behavior for wildcards and trailing-dot FQDNs.
  • Tests
    • Added unit tests covering domain matching, case normalization, wildcard preservation, trailing-dot handling, and false-positive scenarios.

…hosts

NewSubdomainExtractor interpolated the raw domain into the regex, so the
dots in the domain were treated as "any character" (e.g. "example.com"
also matched "exampleXcom"). The pattern was also unbounded on the right,
so text such as "foo.example.com.evil.org" yielded the bogus subdomain
"foo.example.com", which passes the runner's HasSuffix check and reaches
the output. This affects every source that extracts hosts from free text
(crtsh, waybackarchive, commoncrawl, hackertarget, digitorus, rapiddns,
urlscan, sitedossier, ...).

Escape the domain with regexp.QuoteMeta and drop matches that are only a
prefix of a longer hostname, while still accepting the trailing-dot FQDN
form. Add table-driven tests for the extractor.
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 43716150-81b9-441b-bf6c-7c20c265ffc8

📥 Commits

Reviewing files that changed from the base of the PR and between 13fda40 and 5bf5db4.

📒 Files selected for processing (1)
  • pkg/subscraping/extractor.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/subscraping/extractor.go

Walkthrough

The subdomain extractor now treats supplied domains literally, detects hostname continuation boundaries, and lowercases indexed matches. New table-driven tests cover valid extraction, normalization, trailing dots, wildcard prefixes, and false-positive rejection.

Changes

Subdomain Extraction

Layer / File(s) Summary
Regex matching and hostname boundaries
pkg/subscraping/extractor.go
Domain regex input is escaped, matches are extracted by index and lowercased, and hostname continuation helpers filter incomplete matches.
Extraction behavior tests
pkg/subscraping/extractor_test.go
Table-driven tests validate normal, wildcard, multi-level, case-normalized, trailing-dot, and negative extraction scenarios.

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

Poem

A rabbit checks each dotted trail,
Keeps false hostnames out of the vale.
Lowercase names hop into view,
With quoted dots behaving true.
Tests nibble cases, both old and new—
Hop, hop, extractors through!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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 clearly reflects the main extractor fix: literal domain matching and rejection of partial host matches.
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.
✨ 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/subscraping/extractor.go (1)

37-37: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Clone the extracted substring before appending it.
strings.ToLower can return the original slice when the match is already lowercase, which keeps the full text backing array alive. Wrapping the result in strings.Clone(...) breaks that reference for large response bodies.

🤖 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/subscraping/extractor.go` at line 37, Update the match append logic in
the extractor to wrap the strings.ToLower result with strings.Clone before
appending to matches, ensuring extracted substrings do not retain the full text
backing array.
🤖 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/subscraping/extractor.go`:
- Line 37: Update the match append logic in the extractor to wrap the
strings.ToLower result with strings.Clone before appending to matches, ensuring
extracted substrings do not retain the full text backing array.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8cdb345b-c382-4c0d-89f4-c10bf7ffb913

📥 Commits

Reviewing files that changed from the base of the PR and between d0ea102 and 13fda40.

📒 Files selected for processing (2)
  • pkg/subscraping/extractor.go
  • pkg/subscraping/extractor_test.go

@Gerijacki

Copy link
Copy Markdown
Contributor Author

Thanks — wrapped the extracted match in strings.Clone so a short subdomain no longer keeps the full response body alive. Done in 5bf5db4.

@dogancanbakir
dogancanbakir requested a review from Mzack9999 August 5, 2026 15:15
@Mzack9999
Mzack9999 merged commit 3f5641b into projectdiscovery:dev Aug 5, 2026
8 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
subfinder 2.16.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><!-- Release notes generated using configuration in .github/release.yml at main -->

## What's Changed

* feat(sources): honor -max-results across paginating sources by @Gerijacki in projectdiscovery/subfinder#1810
* fix(extractor): match the target domain literally and reject partial hosts by @Gerijacki in projectdiscovery/subfinder#1811
* fix(subscraping): cap untrusted source response bodies with a size limit by @adamsjack711-ux in projectdiscovery/subfinder#1809
* fix(subscraping): use DialContext instead of the deprecated Dial by @jonaslejon in projectdiscovery/subfinder#1835
* feat: add scanmalware passive source by @jonaslejon in projectdiscovery/subfinder#1834
* fix(crtsh): set statement_timeout via SQL query to avoid PgBouncer handshake rejection (#1828) by @gcoinstash-cmd in projectdiscovery/subfinder#1832

## New Contributors
* @J12934 made their first contribution in projectdiscovery/subfinder#1823
* @adamsjack711-ux made their first contribution in projectdiscovery/subfinder#1809
* @gcoinstash-cmd made their first contribution in projectdiscovery/subfinder#1832

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

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

2 participants