Skip to content

fix(crtsh): drop unused certificate metadata from SQL query (#1773) - #1787

Merged
Mzack9999 merged 1 commit into
projectdiscovery:devfrom
ChrisJr404:crtsh-query-simplify
Jun 16, 2026
Merged

Mzack9999 merged 1 commit into
projectdiscovery:devfrom
ChrisJr404:crtsh-query-simplify

Conversation

@ChrisJr404

@ChrisJr404 ChrisJr404 commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1773.

Background

The Postgres query in pkg/subscraping/sources/crtsh/crtsh.go was inherited verbatim from crt.sh's web UI (which renders a certificate browser). subfinder only ever consumes NAME_VALUE, so the certificate-metadata ceremony is wasted work on crt.sh's free public database:

  • GROUP BY sub.CERTIFICATE on multi-KB DER blobs
  • four x509_* function calls per certificate group (CPU-intensive ASN.1 parsing)
  • correlated LEFT JOIN LATERAL into ct_log_entry per group
  • ORDER BY ENTRY_TIMESTAMP (subfinder dedupes downstream — ordering is irrelevant)
  • LIMIT 10000 placed on raw certificate rows (a band-aid against the ceremony costs)

@lc's issue lays this out in detail.

Change

Replace the WITH/JOIN/x509-functions form with a direct SELECT DISTINCT cai.NAME_VALUE. The downstream SplitSeq + Extractor path is unchanged, so any consumer behavior is identical.

A pleasant side-effect: LIMIT 10000 now bounds distinct subdomains rather than raw certificate hits, so the implicit cap is more useful in practice. The unlimited "All" mode is preserved.

Verification

psql directly against crt.sh for iana.org:

query wall time rows returned unique NAME_VALUEs
existing (WITH+JOIN+x509) 8.0s 225 24
simplified (DISTINCT) 6.3s 23 24

diff of the dedup'd result sets is empty — same subdomains discovered, same identity coverage. The 23-vs-24 row gap is a single *.iana.org wildcard string that the existing query repeats per cert; the new query emits it once.

CI's existing pkg/passive/sources_wo_auth_test.go skips crtsh in GitHub Actions (IP-based ban), so this change is exercised by manual psql verification rather than the integration suite.

Notes

  • Diff is +8 / -26 lines, single file.
  • No behavior change for the HTTP fallback (getSubdomainsFromHTTP).
  • No new dependencies, no public-API change.

Summary by CodeRabbit

  • Refactor
    • Optimized subdomain data retrieval to use a more efficient query structure, reducing code complexity while maintaining the same functionality.

…iscovery#1773)

The Postgres query inherited the verbatim form crt.sh's web UI uses to
render its certificate browser. subfinder consumes NAME_VALUE only, so
GROUP BY sub.CERTIFICATE on multi-KB DER blobs, the four x509_*
function calls per group, the correlated LEFT JOIN LATERAL into
ct_log_entry, and the trailing ORDER BY ENTRY_TIMESTAMP are all
discarded.

Replacing the WITH/JOIN with a direct DISTINCT NAME_VALUE preserves the
result set (verified parity on iana.org: 24 unique names from both
queries) and lets the LIMIT 10000 clause bound unique subdomains
instead of raw certificate rows. The downstream SplitSeq + Extractor
path is unchanged.
@neo-by-projectdiscovery-dev

neo-by-projectdiscovery-dev Bot commented May 3, 2026 •

Copy link
Copy Markdown

Neo - PR Security Review

No security issues found

Comment @pdneo help for available commands. · Open in Neo

@coderabbitai

coderabbitai Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

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: ad3d8b43-67f1-45e6-930e-e34a98961863

📥 Commits

Reviewing files that changed from the base of the PR and between 0ed1b0b and 68c844a.

📒 Files selected for processing (1)
  • pkg/subscraping/sources/crtsh/crtsh.go

Walkthrough

The crt.sh PostgreSQL query is simplified to directly select distinct NAME_VALUE from certificate_and_identities using full-text and ILIKE matching, eliminating unnecessary certificate metadata extraction, grouping, joins, and ordering that subfinder does not use.

Changes

Query Simplification

Layer / File(s) Summary
Query Optimization
pkg/subscraping/sources/crtsh/crtsh.go
getSubdomainsFromSQL removes the CTE, GROUP BY on certificate blobs, x509 function calls, LEFT JOIN LATERAL to ct_log_entry, and ORDER BY timestamp. The new query directly selects DISTINCT cai.NAME_VALUE from certificate_and_identities with full-text and ILIKE filters, matching the suggested fix from the linked issue.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A query so tangled with joins and with bloom,
Parsing x509 in every room!
Now DISTINCT NAME_VALUE lights the way,
Simpler and faster—hip-hop-hooray! 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: simplifying the SQL query by removing unused certificate metadata operations.
Linked Issues check ✅ Passed The PR fulfills all coding objectives from issue #1773: eliminates GROUP BY on DER blobs, removes x509_* calls, drops correlated LEFT JOIN LATERAL, uses simplified SELECT DISTINCT with plainto_tsquery and ILIKE filters, and preserves downstream behavior.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the SQL query simplification objective; no modifications to HTTP fallback, dependencies, public API, or unrelated code.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@ehsandeep
ehsandeep requested a review from dogancanbakir May 3, 2026 14:56
@dogancanbakir
dogancanbakir requested a review from Mzack9999 June 16, 2026 10:04
@Mzack9999
Mzack9999 merged commit 292032e into projectdiscovery:dev Jun 16, 2026
10 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.

[Issue] crt.sh SQL query does unnecessary work, caps results at 10k rows

3 participants