@@ -3596,59 +3596,159 @@ func TestJoinRoomSwitchClient(t *testing.T) {
35963596}
35973597
35983598func TestGetRealUserIP (t * testing.T ) {
3599- REMOTE_ATTR := "192.168.1.2"
3600-
3601- trustedProxies , err := ParseAllowedIps ("192.168.0.0/16" )
3602- if err != nil {
3603- t .Fatal (err )
3604- }
3605-
3606- request := & http.Request {
3607- RemoteAddr : REMOTE_ATTR + ":23456" ,
3608- }
3609- if ip := GetRealUserIP (request , trustedProxies ); ip != REMOTE_ATTR {
3610- t .Errorf ("Expected %s but got %s" , REMOTE_ATTR , ip )
3611- }
3612-
3613- X_REAL_IP := "192.168.10.11"
3614- request .Header = http.Header {
3615- http .CanonicalHeaderKey ("x-real-ip" ): []string {X_REAL_IP },
3616- }
3617- if ip := GetRealUserIP (request , trustedProxies ); ip != X_REAL_IP {
3618- t .Errorf ("Expected %s but got %s" , X_REAL_IP , ip )
3619- }
3620-
3621- // "X-Real-IP" has preference before "X-Forwarded-For"
3622- X_FORWARDED_FOR_IP := "192.168.20.21"
3623- X_FORWARDED_FOR := X_FORWARDED_FOR_IP + ":12345, 192.168.30.32"
3624- request .Header = http.Header {
3625- http .CanonicalHeaderKey ("x-real-ip" ): []string {X_REAL_IP },
3626- http .CanonicalHeaderKey ("x-forwarded-for" ): []string {X_FORWARDED_FOR },
3627- }
3628- if ip := GetRealUserIP (request , trustedProxies ); ip != X_REAL_IP {
3629- t .Errorf ("Expected %s but got %s" , X_REAL_IP , ip )
3630- }
3631-
3632- request .Header = http.Header {
3633- http .CanonicalHeaderKey ("x-forwarded-for" ): []string {X_FORWARDED_FOR },
3634- }
3635- if ip := GetRealUserIP (request , trustedProxies ); ip != X_FORWARDED_FOR_IP {
3636- t .Errorf ("Expected %s but got %s" , X_FORWARDED_FOR_IP , ip )
3599+ testcases := []struct {
3600+ expected string
3601+ headers http.Header
3602+ trusted string
3603+ addr string
3604+ }{
3605+ {
3606+ "192.168.1.2" ,
3607+ nil ,
3608+ "192.168.0.0/16" ,
3609+ "192.168.1.2:23456" ,
3610+ },
3611+ {
3612+ "10.11.12.13" ,
3613+ nil ,
3614+ "192.168.0.0/16" ,
3615+ "10.11.12.13:23456" ,
3616+ },
3617+ {
3618+ "10.11.12.13" ,
3619+ http.Header {
3620+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"10.11.12.13" },
3621+ },
3622+ "192.168.0.0/16" ,
3623+ "192.168.1.2:23456" ,
3624+ },
3625+ {
3626+ "11.12.13.14" ,
3627+ http.Header {
3628+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 192.168.30.32" },
3629+ },
3630+ "192.168.0.0/16" ,
3631+ "192.168.1.2:23456" ,
3632+ },
3633+ // "X-Real-IP" has preference before "X-Forwarded-For"
3634+ {
3635+ "10.11.12.13" ,
3636+ http.Header {
3637+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"10.11.12.13" },
3638+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 192.168.30.32" },
3639+ },
3640+ "192.168.0.0/16" ,
3641+ "192.168.1.2:23456" ,
3642+ },
3643+ // Multiple "X-Forwarded-For" headers are merged.
3644+ {
3645+ "11.12.13.14" ,
3646+ http.Header {
3647+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14" , "192.168.30.32" },
3648+ },
3649+ "192.168.0.0/16" ,
3650+ "192.168.1.2:23456" ,
3651+ },
3652+ // Headers are ignored if coming from untrusted clients.
3653+ {
3654+ "10.11.12.13" ,
3655+ http.Header {
3656+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"11.12.13.14" },
3657+ },
3658+ "192.168.0.0/16" ,
3659+ "10.11.12.13:23456" ,
3660+ },
3661+ {
3662+ "10.11.12.13" ,
3663+ http.Header {
3664+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 192.168.30.32" },
3665+ },
3666+ "192.168.0.0/16" ,
3667+ "10.11.12.13:23456" ,
3668+ },
3669+ // X-Forwarded-For is filtered for trusted proxies.
3670+ {
3671+ "1.2.3.4" ,
3672+ http.Header {
3673+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 1.2.3.4" },
3674+ },
3675+ "192.168.0.0/16" ,
3676+ "192.168.1.2:23456" ,
3677+ },
3678+ {
3679+ "1.2.3.4" ,
3680+ http.Header {
3681+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 1.2.3.4, 192.168.2.3" },
3682+ },
3683+ "192.168.0.0/16" ,
3684+ "192.168.1.2:23456" ,
3685+ },
3686+ {
3687+ "10.11.12.13" ,
3688+ http.Header {
3689+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 1.2.3.4" },
3690+ },
3691+ "192.168.0.0/16" ,
3692+ "10.11.12.13:23456" ,
3693+ },
3694+ // Invalid IPs are ignored.
3695+ {
3696+ "192.168.1.2" ,
3697+ http.Header {
3698+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"this-is-not-an-ip" },
3699+ },
3700+ "192.168.0.0/16" ,
3701+ "192.168.1.2:23456" ,
3702+ },
3703+ {
3704+ "11.12.13.14" ,
3705+ http.Header {
3706+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"this-is-not-an-ip" },
3707+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 192.168.30.32" },
3708+ },
3709+ "192.168.0.0/16" ,
3710+ "192.168.1.2:23456" ,
3711+ },
3712+ {
3713+ "11.12.13.14" ,
3714+ http.Header {
3715+ http .CanonicalHeaderKey ("x-real-ip" ): []string {"this-is-not-an-ip" },
3716+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"11.12.13.14, 192.168.30.32, proxy1" },
3717+ },
3718+ "192.168.0.0/16" ,
3719+ "192.168.1.2:23456" ,
3720+ },
3721+ {
3722+ "192.168.1.2" ,
3723+ http.Header {
3724+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"this-is-not-an-ip" },
3725+ },
3726+ "192.168.0.0/16" ,
3727+ "192.168.1.2:23456" ,
3728+ },
3729+ {
3730+ "192.168.2.3" ,
3731+ http.Header {
3732+ http .CanonicalHeaderKey ("x-forwarded-for" ): []string {"this-is-not-an-ip, 192.168.2.3" },
3733+ },
3734+ "192.168.0.0/16" ,
3735+ "192.168.1.2:23456" ,
3736+ },
36373737 }
36383738
3639- PUBLIC_IP := "1.2.3.4"
3640- request . RemoteAddr = PUBLIC_IP + ":1234"
3641- request . Header = http. Header {
3642- http . CanonicalHeaderKey ( "x-real-ip" ): [] string { X_REAL_IP },
3643- }
3644- if ip := GetRealUserIP ( request , trustedProxies ); ip != PUBLIC_IP {
3645- t . Errorf ( "Expected %s but got %s" , PUBLIC_IP , ip )
3646- }
3647- request . Header = http. Header {
3648- http . CanonicalHeaderKey ( "x-forwarded-for" ): [] string { X_FORWARDED_FOR },
3649- }
3650- if ip := GetRealUserIP ( request , trustedProxies ); ip != PUBLIC_IP {
3651- t . Errorf ( "Expected %s but got %s" , PUBLIC_IP , ip )
3739+ for _ , tc := range testcases {
3740+ trustedProxies , err := ParseAllowedIps ( tc . trusted )
3741+ if err != nil {
3742+ t . Errorf ( "invalid trusted proxies in %+v: %s" , tc , err )
3743+ continue
3744+ }
3745+ request := & http. Request {
3746+ RemoteAddr : tc . addr ,
3747+ Header : tc . headers ,
3748+ }
3749+ if ip := GetRealUserIP ( request , trustedProxies ); ip != tc . expected {
3750+ t . Errorf ( "Expected %s for %+v but got %s" , tc . expected , tc , ip )
3751+ }
36523752 }
36533753}
36543754
0 commit comments