Skip to content

Conversation

@sheikhshaheerimran
Copy link
Contributor

@sheikhshaheerimran sheikhshaheerimran commented Dec 30, 2025

Summary

  • Fixes JSON output being empty when running host discovery scans
  • Fixes CSV output being empty when running host discovery scans
  • Applies to both console and file output

Closes #1611

Test plan

  • sudo ./naabu -host hackerone.com -json -sn shows JSON output
  • sudo ./naabu -host hackerone.com -csv -sn shows CSV output
  • sudo ./naabu -host hackerone.com -json -sn -o out.json writes to file
  • sudo ./naabu -host hackerone.com -csv -sn -o out.csv writes to file
  • Regular port scanning still works: ./naabu -host hackerone.com -p 80,443 -json
  • All existing tests pass

Summary by CodeRabbit

  • Bug Fixes
    • JSON and CSV outputs now reliably produce a result even when no ports are detected — JSON emits a single object with a trailing newline, and CSV emits at least one row.
    • CSV headers are initialized once per output sequence and rows are consistently written and flushed, improving formatting and consistency across result sets.

✏️ Tip: You can customize this high-level summary in your review settings.

…#1611)

  Console and file output were empty when running host discovery
  scans with -json or -csv flags. The data was being created but
  never serialized to the output buffer.

  - Fix console JSON output by using data.JSON() and writing to buffer
  - Fix console CSV output by initializing headers and writing row
  - Fix file JSON/CSV output by handling empty ports case
@auto-assign auto-assign bot requested a review from dwisiswant0 December 30, 2025 22:33
@coderabbitai
Copy link

coderabbitai bot commented Dec 30, 2025

Walkthrough

Output logic in pkg/runner/output.go and pkg/runner/runner.go was changed so zero-port results now emit a single JSON object or CSV row instead of producing no output; JSON and CSV streaming paths were adjusted to batch-format output into a buffer before printing.

Changes

Cohort / File(s) Summary
Zero-port output handling
pkg/runner/output.go
When ports list is empty, WriteJSONOutputWithMac writes one JSON object plus newline; CSV functions emit one pre-initialized CSV row instead of skipping emission.
Streaming & buffering
pkg/runner/runner.go
handleOutput now marshals per-item JSON bytes into a buffer and prints them; CSV path initializes headers once and writes rows via writeCSVRow with buffered flush instead of per-port direct emission.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I’m a rabbit in the code-lined glen,
Hop-scanning hosts again and then.
Where silence lived with ports of none,
A JSON line now greets the sun. 🐇
Hooray—each host’s small song is done.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title clearly and specifically describes the main change: fixing empty JSON/CSV output for host discovery scans, matching the core issue being addressed.
Linked Issues check ✅ Passed The code changes ensure host-discovery results are now serialized in JSON/CSV formats by modifying output handling to emit data even when ports list is empty, directly addressing issue #1611's requirement.
Out of Scope Changes check ✅ Passed The changes are focused solely on fixing JSON/CSV output serialization for host discovery scans in output.go and runner.go with no unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 39a50c1 and 8c75aa4.

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

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

@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: 2

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b96cd90 and 39a50c1.

📒 Files selected for processing (2)
  • pkg/runner/output.go
  • pkg/runner/runner.go
🧰 Additional context used
🧬 Code graph analysis (1)
pkg/runner/output.go (2)
pkg/port/port.go (1)
  • Port (10-17)
pkg/protocol/protocol.go (1)
  • Protocol (7-7)
🔇 Additional comments (2)
pkg/runner/output.go (2)

210-217: LGTM! Host discovery JSON output correctly handled.

The zero-port case now properly marshals the result and writes it with a newline, ensuring JSON output is produced for host discovery scans where no ports are found.


276-286: LGTM! Host discovery CSV output correctly handled.

The zero-port case now writes a single CSV row using the pre-initialized data, ensuring CSV output is produced for host discovery scans. The else block maintains the existing per-port row emission for regular port scans.

Comment on lines 1160 to 1163
if r.options.JSON {
b, _ := data.JSON(r.options.ExcludeOutputFields)
buffer.Write(b)
gologger.Silent().Msgf("%s", buffer.String())
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fix JSON formatting and error handling.

Two issues with the JSON console output for host discovery:

  1. Line 1162 writes JSON bytes without a newline, inconsistent with the pattern used elsewhere (e.g., line 1085 uses fmt.Sprintf("%s\n", b)).
  2. Line 1161 ignores the error from data.JSON(), which could hide marshaling failures.
🔎 Proposed fix
 if r.options.JSON {
-    b, _ := data.JSON(r.options.ExcludeOutputFields)
-    buffer.Write(b)
+    b, err := data.JSON(r.options.ExcludeOutputFields)
+    if err != nil {
+        gologger.Error().Msgf("Could not marshal JSON for %s: %s\n", host, err)
+        continue
+    }
+    buffer.Write([]byte(fmt.Sprintf("%s\n", b)))
     gologger.Silent().Msgf("%s", buffer.String())
🤖 Prompt for AI Agents
In pkg/runner/runner.go around lines 1160 to 1163, the JSON output to the
console neglects error handling from data.JSON(...) and writes bytes without a
trailing newline; change to capture and check the error returned by
data.JSON(r.options.ExcludeOutputFields), handle/report the error via gologger
(and avoid ignoring it), and when successful write the JSON bytes followed by a
newline (or use fmt.Sprintf("%s\n", b) / buffer.Write with an explicit '\n')
before calling gologger.Silent().Msgf so output formatting matches other JSON
paths.

@dogancanbakir
Copy link
Member

Thanks for the PR, @sheikhshaheerimran habibi!

@sheikhshaheerimran
Copy link
Contributor Author

sheikhshaheerimran commented Dec 31, 2025

@dogancanbakir Most welcome. I Iook forward to actively contribute to this project.

@dogancanbakir dogancanbakir merged commit 9976c32 into projectdiscovery:dev Dec 31, 2025
10 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.

JSON output is empty when running scans with only host discovery

3 participants