TT-14891 - adds client ip from XFF by depth#7063
Conversation
|
💔 The detected issue is not in one of the allowed statuses 💔
Please ensure your jira story is in one of the allowed statuses |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
API Changes --- prev.txt 2025-11-12 17:19:36.671171359 +0000
+++ current.txt 2025-11-12 17:19:27.025137028 +0000
@@ -6838,6 +6838,14 @@
// https://tyk.io/docs/api-management/traffic-transformation/#request-size-limits
MaxRequestBodySize int64 `json:"max_request_body_size"`
+ // XFFDepth controls which position in the X-Forwarded-For chain to use for determining client IP address.
+ // A value of 0 means using the first IP (default). this is way the Gateway has calculated the client IP historically,
+ // the most common case, and will be used when this config is not set.
+ // However, any non-zero value will use that position from the right in the X-Forwarded-For chain.
+ // This is a security feature to prevent against IP spoofing attacks, and is recommended to be set to a non-zero value.
+ // A value of 1 means using the last IP, 2 means second to last, and so on.
+ XFFDepth int `json:"xff_depth"`
+
// MaxResponseBodySize configures an upper limit for the size of the response body (payload) in bytes.
//
// This limit is currently applied only if the Response Body Transform middleware is enabled.
@@ -12953,6 +12961,10 @@
package request // import "github.com/TykTechnologies/tyk/request"
+VARIABLES
+
+var Global func() config.Config
+
FUNCTIONS
func RealIP(r *http.Request) string |
PR Code Suggestions ✨Explore these optional code suggestions:
|
🔍 Code Analysis ResultsThis PR introduces a configurable Files Changed AnalysisThe changes are spread across 5 files, with a total of 243 additions and 14 deletions. The most significant change is in Architecture & Impact AssessmentWhat this PR accomplishes: Key technical changes introduced:
Affected system components:
Client IP Selection Flow: graph TD
A[Incoming Request] --> B{Check Context for 'remote_addr'};
B -- Yes --> C[Use Context IP];
B -- No --> D{Check X-Real-IP Header};
D -- Yes --> E[Parse IP];
E -- Valid --> F[Use X-Real-IP];
E -- Invalid --> G{Check X-Forwarded-For Header};
D -- No --> G;
G -- No --> H[Use RemoteAddr];
G -- Yes --> I[Split XFF Header into IP list];
I --> J{Get XFFDepth from Config};
J -- depth <= 0 --> K[Select first IP];
J -- depth > 0 --> L[Select nth IP from right];
K --> M{Is IP valid?};
L --> M;
M -- Yes --> N[Use Selected IP];
M -- No --> H;
C --> Z[End];
F --> Z;
H --> Z;
N --> Z;
Scope Discovery & Context ExpansionThe impact of this change is system-wide, as Metadata
Powered by Visor from Probelabs Last updated: 2025-11-12T17:21:50.428Z | Triggered by: synchronize | Commit: a3d051d 💡 TIP: You can chat with Visor using |
🔍 Code Analysis ResultsSecurity Issues (1)
Architecture Issues (1)
Performance Issues (1)
Quality Issues (3)
Dependency Issues (2)
✅ Connectivity Check PassedNo connectivity issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2025-11-12T17:21:51.815Z | Triggered by: synchronize | Commit: a3d051d 💡 TIP: You can chat with Visor using |
🎯 Recommended Merge TargetsBased on JIRA ticket TT-14891: [Innersource] Dynamic client-IP selection. (XFF Depth Selection Improvement) Fix Version: Tyk 5.11.0
Required:
📋 Workflow
|
|



User description
Description
Related Issue
Motivation and Context
How This Has Been Tested
Screenshots (if appropriate)
Types of changes
Checklist
PR Type
Enhancement, Tests
Description
Add configurable XFF depth for client IP extraction
Update real IP logic to support XFF depth
Extend and add tests for XFF depth scenarios
Document new XFF depth config in code
Changes walkthrough 📝
config.go
Add and document XFFDepth config optionconfig/config.go
XFFDepthfield inHttpServerOptionsConfigXFFDepthserver.go
Initialize request.Global for config accessgateway/server.go
request.Globalfunction to provide gateway configreal_ip.go
Support XFF depth in RealIP extractionrequest/real_ip.go
Globalconfig accessor for dynamic configRealIPto use XFF depth from configreal_ip_test.go
Add and extend tests for XFF depth logicrequest/real_ip_test.go
Ticket Details
TT-14891
Generated at: 2025-11-12 17:18:40