这是indexloc提供的服务,不要输入任何密码
Skip to content

add apikey support for hackertarget #1622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

dogancanbakir
Copy link
Member

@dogancanbakir dogancanbakir commented Jul 23, 2025

closes #1576

Summary by CodeRabbit

  • New Features

    • Added support for using API keys with the HackerTarget data source, including random selection of keys when available.
    • The system now skips the HackerTarget source if no API keys are provided, and this is reflected in the reported statistics.
  • Bug Fixes

    • Improved handling of API key assignment and usage for sources requiring them.

@dogancanbakir dogancanbakir requested a review from ehsandeep July 23, 2025 16:09
@dogancanbakir dogancanbakir self-assigned this Jul 23, 2025
Copy link

coderabbitai bot commented Jul 23, 2025

Walkthrough

The changes introduce optional API key support for the HackerTarget source, enabling random API key selection and skipping execution if no key is provided. The configuration logic for adding API keys to sources is also simplified, now always attempting to add keys if any are present, regardless of whether the source explicitly requires them.

Changes

File(s) Change Summary
pkg/runner/config.go Simplified logic in UnmarshalFrom to add API keys to sources if any are present, regardless of NeedsKey().
pkg/subscraping/sources/hackertarget/hackertarget.go Added apiKeys and skipped fields to Source; updated Run, AddApiKeys, and Statistics for API key use and skipping logic.

Sequence Diagram(s)

sequenceDiagram
    participant Config as Config Loader
    participant Source as HackerTarget Source
    participant User as User

    Config->>Source: AddApiKeys(apiKeys)
    User->>Source: Run(domain, session)
    alt apiKeys present
        Source->>Source: PickRandom(apiKey)
        Source->>Source: Construct URL with &apikey=<key>
        Source->>Source: HTTP GET to HackerTarget
        Source-->>User: Return results
    else No apiKeys
        Source->>Source: Set skipped = true
        Source-->>User: Return (no results)
    end
Loading

Estimated code review effort

2 (~15 minutes)

Possibly related PRs

  • read key from env if provided #1607: Both PRs modify the handling of API keys by interacting with the NeedsKey() and AddApiKeys() methods on sources, with the main PR simplifying the key addition logic in UnmarshalFrom and the retrieved PR adding environment variable reading to supply keys to sources that require them.

Suggested reviewers

  • ehsandeep

Poem

In the warren of code, a new path appears,
HackerTarget now hops with keys, it cheers!
If no key is found, it quietly skips,
But with one in paw, it joyfully zips.
A rabbit’s delight in features anew—
API magic, just for you!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dogancanbakir dogancanbakir changed the title 1576 add apikey support for hackertarget add apikey support for hackertarget Jul 23, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8feb51f and 31d5508.

📒 Files selected for processing (2)
  • pkg/runner/config.go (1 hunks)
  • pkg/subscraping/sources/hackertarget/hackertarget.go (3 hunks)
🧠 Learnings (2)
pkg/runner/config.go (1)

Learnt from: 0x4500
PR: #1612
File: pkg/subscraping/agent.go:0-0
Timestamp: 2025-07-17T12:07:51.521Z
Learning: In subfinder subscraping sources, when a source needs to handle specific HTTP status codes differently (like treating 204 as success), the check should be implemented within the individual source's code rather than modifying the global httpRequestWrapper in agent.go. This keeps the special handling localized and avoids affecting other sources.

pkg/subscraping/sources/hackertarget/hackertarget.go (3)

Learnt from: 0x4500
PR: #1612
File: pkg/subscraping/agent.go:0-0
Timestamp: 2025-07-17T12:07:51.521Z
Learning: In subfinder subscraping sources, when a source needs to handle specific HTTP status codes differently (like treating 204 as success), the check should be implemented within the individual source's code rather than modifying the global httpRequestWrapper in agent.go. This keeps the special handling localized and avoids affecting other sources.

Learnt from: x-stp
PR: projectdiscovery/subfinder#0
File: :0-0
Timestamp: 2025-06-24T16:03:30.467Z
Learning: When fixing HTTP response handling bugs in subfinder sources, the correct pattern is to use defer session.DiscardHTTPResponse(resp) after successful requests to ensure the response body remains open for reading, and call session.DiscardHTTPResponse(resp) immediately in error cases.

Learnt from: x-stp
PR: #1608
File: v2/pkg/subscraping/sources/shrewdeye/shrewdeye.go:32-38
Timestamp: 2025-06-20T19:02:59.053Z
Learning: The DiscardHTTPResponse method in subfinder's Session already includes a built-in nil check for the response parameter, so it's safe to call with a potentially nil http.Response without additional nil checking.

🧰 Additional context used
🧠 Learnings (2)
pkg/runner/config.go (1)

Learnt from: 0x4500
PR: #1612
File: pkg/subscraping/agent.go:0-0
Timestamp: 2025-07-17T12:07:51.521Z
Learning: In subfinder subscraping sources, when a source needs to handle specific HTTP status codes differently (like treating 204 as success), the check should be implemented within the individual source's code rather than modifying the global httpRequestWrapper in agent.go. This keeps the special handling localized and avoids affecting other sources.

pkg/subscraping/sources/hackertarget/hackertarget.go (3)

Learnt from: 0x4500
PR: #1612
File: pkg/subscraping/agent.go:0-0
Timestamp: 2025-07-17T12:07:51.521Z
Learning: In subfinder subscraping sources, when a source needs to handle specific HTTP status codes differently (like treating 204 as success), the check should be implemented within the individual source's code rather than modifying the global httpRequestWrapper in agent.go. This keeps the special handling localized and avoids affecting other sources.

Learnt from: x-stp
PR: projectdiscovery/subfinder#0
File: :0-0
Timestamp: 2025-06-24T16:03:30.467Z
Learning: When fixing HTTP response handling bugs in subfinder sources, the correct pattern is to use defer session.DiscardHTTPResponse(resp) after successful requests to ensure the response body remains open for reading, and call session.DiscardHTTPResponse(resp) immediately in error cases.

Learnt from: x-stp
PR: #1608
File: v2/pkg/subscraping/sources/shrewdeye/shrewdeye.go:32-38
Timestamp: 2025-06-20T19:02:59.053Z
Learning: The DiscardHTTPResponse method in subfinder's Session already includes a built-in nil check for the response parameter, so it's safe to call with a potentially nil http.Response without additional nil checking.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Test Builds (windows-latest)
  • GitHub Check: Test Builds (ubuntu-latest)
  • GitHub Check: Test Builds (macOS-13)
  • GitHub Check: release-test
  • GitHub Check: Analyze (go)
🔇 Additional comments (5)
pkg/runner/config.go (1)

49-49: LGTM! Simplified logic aligns with optional API key pattern.

The removal of the source.NeedsKey() check enables sources to receive API keys even when they don't strictly require them, allowing for optional API key usage. The nil check removal is safe since len() on a nil slice returns 0.

pkg/subscraping/sources/hackertarget/hackertarget.go (4)

15-15: LGTM! Struct fields properly support optional API key functionality.

The addition of apiKeys and skipped fields enables tracking of API key state and execution status as intended.

Also applies to: 19-19


43-49: LGTM! HTTP request handling follows established patterns.

The HTTP request implementation correctly uses session.SimpleGet() and proper error handling with session.DiscardHTTPResponse() as per subfinder conventions.


92-95: LGTM! API key storage implementation is correct.

The AddApiKeys method now properly stores the provided keys, and the TODO comment appropriately highlights the broader refactoring needs for API key management.


102-102: LGTM! Statistics correctly report skip status.

The addition of the Skipped field to statistics provides proper visibility into when the source was skipped due to missing API keys.

Comment on lines +34 to +42
htSearchUrl := fmt.Sprintf("https://api.hackertarget.com/hostsearch/?q=%s", domain)
randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
if randomApiKey == "" {
s.skipped = true
return
}

htSearchUrl = fmt.Sprintf("%s&apikey=%s", htSearchUrl, randomApiKey)

Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify that HackerTarget API requires authentication.

The implementation skips execution entirely when no API key is available, but this may be overly restrictive if HackerTarget's API supports unauthenticated requests with rate limits.

Please verify if HackerTarget's API works without authentication:


🌐 Web query:

Does HackerTarget's hostsearch API work without an API key, and what are the rate limits for authenticated vs unauthenticated requests?

💡 Result:

HackerTarget's hostsearch API is accessible without an API key, allowing users to perform queries without authentication. However, unauthenticated users are subject to certain rate limits to ensure fair usage and maintain service stability.

Rate Limits for Unauthenticated Users:

  • Daily Limit: Unauthenticated users are permitted up to 50 API calls per day from a single IP address. (hackertarget.com)

  • Per-Second Limit: The API enforces a rate limit of 2 requests per second from a single IP address. Exceeding this limit will result in an HTTP 429 response code. (hackertarget.com)

Rate Limits for Authenticated Users:

Authenticated users, who have a HackerTarget membership and possess an API key, enjoy enhanced access:

  • Unlimited Results: Authenticated users can retrieve unlimited results, whereas free queries are limited to 500 results. (hackertarget.com)

  • Generous Daily Quota: The daily quota for authenticated users is based on their membership plan, offering a higher allowance compared to unauthenticated users. (hackertarget.com)

  • Additional Features: Membership provides access to advanced tools like Attack Surface Mapping with Domain Profiler and full access to 28 vulnerability scanners and tools. (hackertarget.com)

To access the API as an authenticated user, you need to sign up for a HackerTarget membership. Once registered, you can find your API key in the user dashboard. This key can be included in your API requests either as a query parameter (&apikey=) or as a custom header (X-API-Key). (hackertarget.com)

By adhering to these rate limits and utilizing the appropriate authentication, you can effectively use HackerTarget's hostsearch API for your network security assessments.

Citations:


Allow Unauthenticated HackerTarget API Requests

HackerTarget’s hostsearch endpoint supports unauthenticated queries (up to 50 calls/day and 2 calls/sec per IP). Skipping entirely when no API key is present is overly restrictive—let’s fall back to an unauthenticated request instead.

Locations to update:

  • pkg/subscraping/sources/hackertarget/hackertarget.go: lines 34–42

Suggested diff:

-    randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
-    if randomApiKey == "" {
-        s.skipped = true
-        return
-    }
-
-    htSearchUrl = fmt.Sprintf("%s&apikey=%s", htSearchUrl, randomApiKey)
+    randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
+    if randomApiKey != "" {
+        // Authenticated request: unlimited results per plan
+        htSearchUrl = fmt.Sprintf("%s&apikey=%s", htSearchUrl, randomApiKey)
+    } else {
+        // Unauthenticated fallback: limited to 50 calls/day and 2 calls/sec
+        // Proceed without &apikey parameter
+    }

• Remove s.skipped = true and return to allow unauthenticated queries
• Optionally, add logging to warn when falling back to unauthenticated mode
• Be mindful of rate limits (50/day, 2/sec) to avoid HTTP 429 errors

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
htSearchUrl := fmt.Sprintf("https://api.hackertarget.com/hostsearch/?q=%s", domain)
randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
if randomApiKey == "" {
s.skipped = true
return
}
htSearchUrl = fmt.Sprintf("%s&apikey=%s", htSearchUrl, randomApiKey)
htSearchUrl := fmt.Sprintf("https://api.hackertarget.com/hostsearch/?q=%s", domain)
randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
if randomApiKey != "" {
// Authenticated request: unlimited results per plan
htSearchUrl = fmt.Sprintf("%s&apikey=%s", htSearchUrl, randomApiKey)
} else {
// Unauthenticated fallback: limited to 50 calls/day and 2 calls/sec
// Proceed without &apikey parameter
}
🤖 Prompt for AI Agents
In pkg/subscraping/sources/hackertarget/hackertarget.go around lines 34 to 42,
the current code skips the request entirely if no API key is available, which is
too restrictive since HackerTarget allows unauthenticated queries with limits.
Remove the lines setting s.skipped to true and the return statement to allow the
request to proceed without an API key. Optionally, add a log warning indicating
fallback to unauthenticated mode. Ensure the URL is constructed without the
apikey parameter when no key is present, and consider implementing rate limiting
to respect the 50 calls/day and 2 calls/sec limits.

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.

Add HackerTarget paid API support
1 participant