Skip to content

feat(appsec): add Forwarded as a source for client IP#3911

Merged
RomainMuller merged 8 commits into
mainfrom
romain.marcadier/APPSEC-58265/clientip.forwarded
Aug 26, 2025
Merged

feat(appsec): add Forwarded as a source for client IP#3911
RomainMuller merged 8 commits into
mainfrom
romain.marcadier/APPSEC-58265/clientip.forwarded

Conversation

@RomainMuller

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds the Forwarded header as a source of information for the Client IP detection.

Motivation

The Forwarded header is used by API Gateway to inform handlers about the actual forwarding route. This header has a somewhat different format compared to other IP headers (it's a semicolon-delimited sequence of directives, not a comma-separated list of IP addresses) that requires its own parsing logic.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running ./scripts/lint.sh locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@RomainMuller
RomainMuller requested a review from a team as a code owner August 25, 2025 10:14
@RomainMuller
RomainMuller requested a review from Copilot August 25, 2025 10:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the Forwarded header as a source for client IP detection in the AppSec module. The Forwarded header uses a different format than other IP headers, requiring specialized parsing logic to extract IP addresses from semicolon-delimited directives.

  • Replaces x-forwarded with forwarded in the default IP headers list
  • Implements parsing logic for the RFC 7239 Forwarded header format
  • Adds comprehensive test coverage for various Forwarded header scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/appsec/listener/httpsec/request.go Updates the default IP headers list to include forwarded instead of x-forwarded
internal/appsec/listener/httpsec/clientip.go Implements parseForwardedHeader function with specialized parsing logic for the Forwarded header format
internal/appsec/listener/httpsec/clientip_test.go Adds comprehensive test cases for Forwarded header parsing and excludes it from generic IP header tests

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread internal/appsec/listener/httpsec/clientip_test.go Outdated
Comment thread internal/appsec/listener/httpsec/clientip.go Outdated
@RomainMuller
RomainMuller force-pushed the romain.marcadier/APPSEC-58265/clientip.forwarded branch from 7b73009 to 49d57f1 Compare August 25, 2025 10:17
@pr-commenter

pr-commenter Bot commented Aug 25, 2025

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2025-08-25 13:44:43

Comparing candidate commit 75e125e in PR branch romain.marcadier/APPSEC-58265/clientip.forwarded with baseline commit b5536ad in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics.

The `Forwarded` header is used by API Gateway to inform handlers about the actual
forwarding route. This header has a somewhat different format compared to other
IP headers (it's a semicolon-delimited sequence of directives, not a comma-separated
list of IP addresses) that requires its own parsing logic.
@RomainMuller
RomainMuller force-pushed the romain.marcadier/APPSEC-58265/clientip.forwarded branch from 49d57f1 to 6af4f59 Compare August 25, 2025 10:18
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Aug 25, 2025

Copy link
Copy Markdown

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 75e125e | Docs | Was this helpful? Give us feedback!

@eliottness eliottness left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good except minor tweaks and maybe what looks like an overly complex implementation compared to this one for example: https://github.com/slashid/httpforwarded/blob/0bf9cfcea9fa/parse.go#L70

Comment thread internal/appsec/listener/httpsec/clientip.go Outdated
Comment thread internal/appsec/listener/httpsec/clientip.go Outdated
Comment thread internal/appsec/listener/httpsec/clientip.go Outdated
Comment thread internal/appsec/listener/httpsec/clientip_test.go Outdated
Comment thread internal/appsec/listener/httpsec/clientip_test.go Outdated
@RomainMuller

Copy link
Copy Markdown
Contributor Author

NB -- This is pending a ST update that enacts removal of the X-Forwarded header from expected parsed headers.

@RomainMuller
RomainMuller merged commit c921cdc into main Aug 26, 2025
330 of 333 checks passed
@RomainMuller
RomainMuller deleted the romain.marcadier/APPSEC-58265/clientip.forwarded branch August 26, 2025 09:00
RomainMuller added a commit to DataDog/system-tests that referenced this pull request Aug 26, 2025
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