22using System . Collections . Generic ;
33using System . Linq ;
44using System . Net ;
5+ using System . Text . RegularExpressions ;
56using Microsoft . AspNetCore . Http ;
67using SecureNative . SDK . Config ;
78
@@ -17,20 +18,65 @@ public static class RequestUtils
1718 "x-forwarded-for" , "x-client-ip" , "x-real-ip" , "x-forwarded" , "x-cluster-client-ip" , "forwarded-for" ,
1819 "forwarded" , "via"
1920 } ;
21+ private static readonly List < string > PiiHeaders = new List < string >
22+ {
23+ "authorization" , "access_token" , "apikey" , "password" , "passwd" , "secret" , "api_key"
24+ } ;
2025
21- public static Dictionary < string , string > GetHeadersFromRequest ( HttpWebRequest request )
26+ public static Dictionary < string , string > GetHeadersFromRequest ( HttpWebRequest request , SecureNativeOptions options )
2227 {
2328 var headers = new Dictionary < string , string > ( ) ;
24- try
29+ if ( options ? . GetPiiHeaders ( ) . Length > 0 )
2530 {
26- foreach ( var key in request . Headers . AllKeys )
31+ try
32+ {
33+ foreach ( var key in request . Headers . AllKeys )
34+ {
35+ if ( ! options . GetPiiHeaders ( ) . Contains ( key ) )
36+ {
37+ headers . Add ( key , request . Headers [ key ] ) ;
38+ }
39+ }
40+ }
41+ catch ( Exception )
42+ {
43+ // ignored
44+ }
45+ } else if ( options != null && options . GetPiiRegexPattern ( ) != "" )
46+ {
47+ try
48+ {
49+ var pattern = new Regex ( options . GetPiiRegexPattern ( ) ) ;
50+
51+ foreach ( var key in request . Headers . AllKeys )
52+ {
53+ if ( ! pattern . Match ( key ) . Success )
54+ {
55+ headers . Add ( key , request . Headers [ key ] ) ;
56+ }
57+ }
58+ }
59+ catch ( Exception )
2760 {
28- headers . Add ( key , request . Headers [ key ] ) ;
61+ // ignored
2962 }
3063 }
31- catch ( Exception )
64+ else
3265 {
33- // ignored
66+ try
67+ {
68+ foreach ( var key in request . Headers . AllKeys )
69+ {
70+ if ( ! PiiHeaders . Contains ( key ) )
71+ {
72+ headers . Add ( key , request . Headers [ key ] ) ;
73+ }
74+ }
75+ }
76+ catch ( Exception )
77+ {
78+ // ignored
79+ }
3480 }
3581
3682 return headers ;
@@ -80,7 +126,7 @@ private static string ParseCookie(string cookieString, string cookieName)
80126
81127 public static string GetClientIpFromRequest ( HttpWebRequest request , SecureNativeOptions options )
82128 {
83- if ( options ? . GetProxyHeaders ( ) != null )
129+ if ( options ? . GetProxyHeaders ( ) . Length > 0 )
84130 {
85131 foreach ( var header in options . GetProxyHeaders ( ) )
86132 {
@@ -140,19 +186,60 @@ public static string GetRemoteIpFromRequest(HttpWebRequest request)
140186 return string . Empty ;
141187 }
142188
143- public static Dictionary < string , string > GetHeadersFromRequest ( HttpRequest request )
189+ public static Dictionary < string , string > GetHeadersFromRequest ( HttpRequest request , SecureNativeOptions options )
144190 {
145191 var headers = new Dictionary < string , string > ( ) ;
146- try
192+ if ( options ? . GetPiiHeaders ( ) . Length > 0 )
147193 {
148- foreach ( var key in request . Headers . Keys )
194+ try
195+ {
196+ foreach ( var key in request . Headers . Keys )
197+ {
198+ if ( ! options . GetPiiHeaders ( ) . Contains ( key ) )
199+ {
200+ headers . Add ( key , request . Headers [ key ] ) ;
201+ }
202+ }
203+ }
204+ catch ( Exception )
205+ {
206+ // ignored
207+ }
208+ } else if ( options != null && options . GetPiiRegexPattern ( ) != "" )
209+ {
210+ try
211+ {
212+ var pattern = new Regex ( options . GetPiiRegexPattern ( ) ) ;
213+
214+ foreach ( var key in request . Headers . Keys )
215+ {
216+ if ( ! pattern . Match ( key ) . Success )
217+ {
218+ headers . Add ( key , request . Headers [ key ] ) ;
219+ }
220+ }
221+ }
222+ catch ( Exception )
149223 {
150- headers . Add ( key , request . Headers [ key ] ) ;
224+ // ignored
151225 }
152226 }
153- catch ( Exception )
227+ else
154228 {
155- // ignored
229+ try
230+ {
231+ foreach ( var key in request . Headers . Keys )
232+ {
233+ if ( ! PiiHeaders . Contains ( key ) )
234+ {
235+ headers . Add ( key , request . Headers [ key ] ) ;
236+ }
237+ }
238+ }
239+ catch ( Exception )
240+ {
241+ // ignored
242+ }
156243 }
157244
158245 return headers ;
@@ -203,7 +290,7 @@ public static string GetCookieValueFromRequest(HttpRequest request, string cooki
203290
204291 public static string GetClientIpFromRequest ( HttpRequest request , SecureNativeOptions options )
205292 {
206- if ( options ? . GetProxyHeaders ( ) != null )
293+ if ( options ? . GetProxyHeaders ( ) . Length > 0 )
207294 {
208295 foreach ( var header in options . GetProxyHeaders ( ) )
209296 {
0 commit comments