Skip to content

test: add test for ignoring comma-separated X-Forwarded-Host when trust proxy disabled#6893

Open
Ayoub-Mabrouk wants to merge 1 commit intoexpressjs:masterfrom
Ayoub-Mabrouk:test/add-req-host-ignore-comma-separated
Open

test: add test for ignoring comma-separated X-Forwarded-Host when trust proxy disabled#6893
Ayoub-Mabrouk wants to merge 1 commit intoexpressjs:masterfrom
Ayoub-Mabrouk:test/add-req-host-ignore-comma-separated

Conversation

@Ayoub-Mabrouk
Copy link
Copy Markdown
Contributor

Verify that req.host ignores comma-separated X-Forwarded-Host values
when trust proxy is disabled, ensuring security by using Host header
instead of potentially malicious forwarded headers

…st proxy disabled

Verify that req.host ignores comma-separated X-Forwarded-Host values
when trust proxy is disabled, ensuring security by using Host header
instead of potentially malicious forwarded headers.
@Ayoub-Mabrouk Ayoub-Mabrouk force-pushed the test/add-req-host-ignore-comma-separated branch from 475b00c to 1f860fb Compare November 10, 2025 22:33
@nidhishgajjar
Copy link
Copy Markdown

Orb Code Review (powered by GLM-4.7 on Orb Cloud)

Summary

This PR adds a test to verify that Express correctly ignores comma-separated values in the X-Forwarded-Host header. This is a security-related test that ensures malicious or malformed X-Forwarded-Host headers containing multiple comma-separated values don't override the actual Host header.

Architecture

Changes:

  • Added a new test case in test/req.host.js
  • Tests that when both Host and X-Forwarded-Host headers are present, and X-Forwarded-Host contains comma-separated values, the original Host header value is used
  • Follows existing test structure and conventions

Implementation Quality:

  • ✅ Simple, focused test addition
  • ✅ Follows existing test patterns in the file
  • ✅ Tests important security edge case
  • ✅ Clear test name describing the behavior
  • ✅ Uses consistent assertions and patterns

Issues Found

Positive Aspects:

  • ✅ Tests important security behavior
  • ✅ Prevents potential header injection attacks
  • ✅ Consistent with existing test structure
  • ✅ Clear and maintainable test
  • ✅ Good test coverage for edge case

Minor Suggestions:

  1. Test Documentation: Consider adding a comment explaining why this behavior is important (security implications, RFC compliance, etc.).

  2. Additional Test Cases: Consider adding more edge cases:

    • Empty X-Forwarded-Host after comma: X-Forwarded-Host: 'example.com, '
    • Whitespace variations: X-Forwarded-Host: 'example.com, foobar.com' vs 'example.com,foobar.com'
    • Multiple headers: What if X-Forwarded-Host is sent multiple times?
  3. RFC Reference: Consider adding a comment referencing any relevant RFC or security advisory about handling X-Forwarded-Host.

Questions:

  1. Implementation Detail: Is this behavior documented in the Express docs? If this is the intended behavior, it would be good to verify it's documented.

  2. Consistency: Are there other headers (X-Forwarded-For, X-Forwarded-Proto) that should have similar tests?

Cross-file Impact

Impact:

  • ✅ Isolated change to test file only
  • ✅ No changes to application code
  • ✅ No API changes or breaking changes
  • ✅ Improves test coverage without affecting functionality

Integration:

  • ✅ Follows existing test patterns
  • ✅ Fits well with other tests in the same file

Security & Performance

Security:

  • Improves security by testing behavior that prevents potential header injection
  • ✅ Ensures Express handles malformed/malicious headers correctly
  • ✅ Prevents attackers from using comma-separated X-Forwarded-Host to override legitimate Host header

Performance:

  • ✅ No runtime performance impact (test addition only)
  • ✅ Minimal test execution time overhead

Assessment

Overall: Excellent test addition, ready to merge

This is a valuable security-related test that ensures Express correctly handles a potentially malicious edge case in the X-Forwarded-Host header. The implementation is clean and follows existing patterns.

Recommendation: Approve

The PR is ready to merge. It's a focused, well-implemented test that improves security coverage.

Specific Strengths:

  • Tests important security edge case
  • Prevents potential header injection attacks
  • Follows existing test patterns
  • Clear and maintainable
  • Minimal and focused change
  • Good test naming

Areas for Enhancement:

  1. Consider adding test documentation/comments explaining security implications
  2. Consider additional edge case tests if desired
  3. Verify this behavior is documented in Express docs
  4. Consider similar tests for other X-Forwarded headers

Next Steps:

  1. Optionally add explanatory comment about security implications
  2. Optionally add additional edge case tests
  3. Verify documentation matches this behavior
  4. Merge

This is a valuable security test that improves Express's robustness against potential header-based attacks.

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.

2 participants