Skip to content

Conversation

Isaac0616
Copy link
Contributor

@Isaac0616 Isaac0616 commented Aug 1, 2025

Proposed changes

This fix the bug mentioned in #6362
The change makes the passive mode handles header the same as active mode here:

k = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(k), "-", "_"))

I didn't add test or documentation since this should be a small bug fix.

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

  • Refactor
    • Improved normalization of HTTP response header keys by replacing hyphens with underscores, in addition to trimming whitespace and converting to lowercase. This may change the appearance of header keys in outputs.

@auto-assign auto-assign bot requested a review from dogancanbakir August 1, 2025 01:34
Copy link
Contributor

coderabbitai bot commented Aug 1, 2025

Walkthrough

The code now further normalizes HTTP response header keys by replacing hyphens with underscores, in addition to previously trimming whitespace and converting to lowercase. This affects the format of header keys in the resulting DSL map, changing keys like "content-type" to "content_type". Correspondingly, the related test was updated to reflect this new key format.

Changes

Cohort / File(s) Change Summary
Header Key Normalization Update
pkg/protocols/offlinehttp/operators.go
Updated logic to replace hyphens with underscores in HTTP response header keys during normalization.
Test Adjustment for Header Key Format
pkg/protocols/offlinehttp/operators_test.go
Modified test to use underscore-separated header keys (test_header) instead of hyphen-separated (test-header).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

In headers once dashed, now underscores reign,
Transforming each key for clarity’s gain.
From "content-type" to "content_type" anew,
The map is more tidy, the logic more true.
A hop and a skip—change simple, not deep,
This bunny approves, with a smile and a leap! 🐇

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ca6d97b and f822f84.

📒 Files selected for processing (1)
  • pkg/protocols/offlinehttp/operators_test.go (2 hunks)
🔇 Additional comments (2)
pkg/protocols/offlinehttp/operators_test.go (2)

137-137: LGTM! Test correctly updated for header normalization.

The test assertion properly reflects the new header key normalization where "Test-Header" (line 130) is now accessible as "test_header" in the DSL map, consistent with the hyphen-to-underscore replacement implemented in the operators.


156-156: LGTM! KVal extractor updated for normalized header keys.

The KVal extractor configuration correctly uses "test_header" to match the normalized header key format, ensuring the extractor can properly access the header value set as "Test-Header" on line 130.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 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 generate unit tests to generate unit tests for 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.

@dwisiswant0
Copy link
Member

@coderabbitai can you take a look at /pkg/protocols/*/operators.go and make sure it’s not affected too?

Copy link
Contributor

coderabbitai bot commented Aug 1, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

I'll examine all the operators.go files in the /pkg/protocols/*/ directories to ensure they handle header normalization consistently with the fix applied in this PR.


🏁 Script executed:

#!/bin/bash
# Find all operators.go files in pkg/protocols subdirectories
echo "Finding all operators.go files in pkg/protocols/:"
fd -t f "operators.go" pkg/protocols/

echo -e "\n=== Examining each operators.go file ==="

# Examine each operators.go file for header processing logic
fd -t f "operators.go" pkg/protocols/ --exec sh -c 'echo "=== File: {} ==="; grep -n -A 10 -B 5 -i "header\|Header" {} || echo "No header-related code found"'

Length of output: 7027


@dwisiswant0 I've examined all the operators.go files across the protocols directory. Here's my analysis:

Good news - no other files are affected! Here's the breakdown:

Files with HTTP header processing:

  1. pkg/protocols/http/operators.goAlready correct

    • Line 117: k = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(k), "-", "_"))
    • This was the reference implementation that the current PR is aligning with
  2. pkg/protocols/offlinehttp/operators.goFixed in this PR

    • Line 112: Now correctly replaces hyphens with underscores
    • Previously only did strings.ToLower(strings.TrimSpace(k))

Files that don't need the fix:

  1. pkg/protocols/headless/operators.go - Not affected

    • Only handles headers as a single consolidated string value
    • Doesn't process individual header keys for DSL variables
  2. pkg/protocols/dns/operators.go - Not affected

    • Only references DNS record headers, not HTTP headers
    • Uses resourceRecord.Header() for DNS-specific functionality
  3. pkg/protocols/file/operators.go - Not affected

    • No header-related code
  4. pkg/protocols/network/operators.go - Not affected

    • No header-related code

Conclusion:

The current PR successfully brings offlinehttp/operators.go in line with the existing correct implementation in http/operators.go. All other protocol operators either don't process HTTP headers individually or handle them differently, so they don't require similar fixes.

The consistency issue has been properly resolved! 🎯

@dwisiswant0 dwisiswant0 changed the title Replace "-" in headers with "_" for DSL variables in passive mode fix(offlinehttp): Replace "-" in headers with "_" for DSL variables Aug 1, 2025
@dwisiswant0 dwisiswant0 merged commit 3ac3146 into projectdiscovery:dev Aug 1, 2025
19 checks passed
Ice3man543 pushed a commit that referenced this pull request Aug 2, 2025
…6363)

* Replace "-" in headers with "_" for DSL variables in passive mode

* test(offlinehttp): adjust haystack & needle in `TestHTTPOperatorExtract`

Signed-off-by: Dwi Siswanto <[email protected]>

---------

Signed-off-by: Dwi Siswanto <[email protected]>
Co-authored-by: Dwi Siswanto <[email protected]>
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.

3 participants