Skip to content
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

removed trailing comma from the jsonl exporter #5861

Merged
merged 2 commits into from
Dec 1, 2024

Conversation

bf-rbrown
Copy link
Contributor

@bf-rbrown bf-rbrown commented Nov 27, 2024

Proposed changes

This PR removes the trailing comma from the jsonl exporter (-jsonl-export flag) to comply with jsonlines formatting.

This addresses #5860

The current implementation creates a file that is comma and newline terminated. While this works as an output file, the file does not work when ingested into other programs:

$ nuclei --version
[INF] Nuclei Engine Version: v3.3.6
...omitted...

$ nuclei -t ./template.yaml -jsonl-export output.jsonl -l targets.txt
...omited...

$ cat output.jsonl
{...omitted..., "matcher-status":true},
{...omitted..., "matcher-status":true},
{...omitted..., "matcher-status":true},


$ cat output.jsonl | jq -c '."matcher-status"'                                                    
true
jq: parse error: Expected value before ',' at line 1, column 34721

This PR removes the comma from each row in the output:

go run cmd/nuclei/main.go -duc -t ./template.yaml -jsonl-export output.jsonl -l targets.txt
...omitted...

$ cat output.jsonl
{...omitted..., "matcher-status":true}
{...omitted..., "matcher-status":true}
{...omitted..., "matcher-status":true}

$ cat output.jsonl | jq -c '."matcher-status"'
true
true
true

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

  • New Features

    • Improved formatting of JSON Lines in the output file for better compliance with JSONL standards.
  • Bug Fixes

    • Corrected the output format by ensuring each JSON object is followed only by a newline character.
    • Enhanced file handling to clear existing content before writing.

@auto-assign auto-assign bot requested a review from dogancanbakir November 27, 2024 22:42
Copy link

coderabbitai bot commented Nov 27, 2024

Walkthrough

The changes in this pull request involve a modification to the WriteRows method of the Exporter struct in the jsonl.go file. The update adjusts the file opening logic to include the os.O_TRUNC flag, ensuring the file is cleared before writing. Additionally, the formatting of JSON lines has been updated to append only a newline character after each JSON object, aligning the output with the correct JSON Lines format.

Changes

File Change Summary
pkg/reporting/exporters/jsonl/jsonl.go Modified WriteRows method to include os.O_TRUNC for file opening and updated formatting to append only a newline character after each JSON object.

Possibly related issues

Poem

🐇 In the land of JSON lines,
A rabbit hops and brightly shines.
No more commas, just a newline,
Each object dances, oh so fine!
With every write, the format's right,
JSONL's joy, a pure delight! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between aac0780 and be1cb0f.

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

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

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: 0

🧹 Outside diff range and nitpick comments (1)
pkg/reporting/exporters/jsonl/jsonl.go (1)

Line range hint 71-75: Consider adding O_TRUNC flag for file creation

While the current implementation works correctly, consider adding the O_TRUNC flag when opening the file to explicitly indicate that the file should be truncated if it exists. This makes the behavior more explicit.

- exporter.outputFile, err = os.OpenFile(exporter.options.File, os.O_WRONLY|os.O_CREATE, 0644)
+ exporter.outputFile, err = os.OpenFile(exporter.options.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1f98545 and aac0780.

📒 Files selected for processing (1)
  • pkg/reporting/exporters/jsonl/jsonl.go (1 hunks)
🔇 Additional comments (1)
pkg/reporting/exporters/jsonl/jsonl.go (1)

89-89: LGTM! Change correctly implements JSONL format

The modification to append only a newline character (\n) instead of comma+newline properly aligns with the JSON Lines specification, which requires each JSON object to be on its own line without trailing commas.

Let's verify the output format:

✅ Verification successful

Let's gather more information by checking the test files to verify the JSONL export functionality:


Let's verify the implementation by checking the WriteRows method:


Based on the code inspection, I can now provide a final response about the JSONL format change:

LGTM! The JSONL format implementation is correct

The implementation in WriteRows() properly handles JSONL format by:

  1. Converting each record to JSON using json.Marshal()
  2. Appending only a newline character (\n) after each JSON object
  3. Writing each line sequentially to the file

This aligns with the JSONL (JSON Lines) specification which requires:

  • One complete JSON object per line
  • No trailing commas between objects
  • Lines separated by newline characters
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify JSONL output format compliance
# Expected: Each line should be a valid JSON object without trailing commas

# Search for any test files that might contain sample JSONL output
fd -e jsonl -x sh -c 'echo "=== {} ==="; cat {} | jq -c "." || echo "Invalid JSONL format"'

# Also check if there are any tests validating the output format
rg -A 5 "TestJSONLExporter|jsonl.*export" 

Length of output: 3479


Script:

#!/bin/bash
# Search for test files related to JSONL exporter
fd -e go -x grep -l "jsonl" {} | xargs cat

# Specifically look for test cases in the same directory
ast-grep --pattern 'func Test$_($_) {
  $$$
}'  pkg/reporting/exporters/jsonl/jsonl.go

# Check if there are any example or test data files
fd -e json -e jsonl -e txt -x sh -c 'echo "=== {} ==="; head -n 5 {}'

Length of output: 100960


Script:

#!/bin/bash
# Look for WriteRows implementation
ast-grep --pattern 'func (exporter *Exporter) WriteRows() error {
  $$$
}'  pkg/reporting/exporters/jsonl/jsonl.go

# Also check if there are any tests that verify the output format
ast-grep --pattern 'func Test$_($_) {
  $$$
  jsonl$_
  $$$
}'  pkg/reporting/exporters/jsonl/jsonl_test.go

Length of output: 3065

…that the file should be truncated if it exists.
@ehsandeep ehsandeep linked an issue Nov 28, 2024 that may be closed by this pull request
1 task
@GeorginaReeder
Copy link

Thanks for your contribution @bf-rbrown !

@ehsandeep ehsandeep merged commit 557b4fb into projectdiscovery:dev Dec 1, 2024
15 of 17 checks passed
@ehsandeep ehsandeep removed the request for review from dogancanbakir December 1, 2024 13:18
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] JSONL exporter adds trailing comma
3 participants