@@ -308,3 +308,111 @@ def test_extraction_priority_without_x_forwarded_for(self):
308308 client_ip = RequestUtils .get_client_ip_from_request (request , options )
309309
310310 self .assertEqual ("203.0.113.1" , client_ip )
311+
312+ def test_strip_down_pii_data_from_headers (self ):
313+ headers = {
314+ 'Host' : 'net.example.com' ,
315+ 'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)' ,
316+ 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' ,
317+ 'Accept-Language' : 'en-us,en;q=0.5' ,
318+ 'Accept-Encoding' : 'gzip,deflate' ,
319+ 'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' ,
320+ 'Keep-Alive' : '300' ,
321+ 'Connection' : 'keep-alive' ,
322+ 'Cookie' : 'PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120' ,
323+ 'Pragma' : 'no-cache' ,
324+ 'Cache-Control' : 'no-cache' ,
325+ 'authorization' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
326+ 'access_token' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
327+ 'apikey' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
328+ 'password' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
329+ 'passwd' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
330+ 'secret' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
331+ 'api_key' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z'
332+ }
333+
334+ with requests_mock .Mocker (real_http = True ) as request :
335+ request .headers = headers
336+
337+ h = RequestUtils .get_headers_from_request (request .headers )
338+
339+ self .assertEqual (h .get ('authorization' ), None )
340+ self .assertEqual (h .get ('access_token' ), None )
341+ self .assertEqual (h .get ('apikey' ), None )
342+ self .assertEqual (h .get ('password' ), None )
343+ self .assertEqual (h .get ('passwd' ), None )
344+ self .assertEqual (h .get ('secret' ), None )
345+ self .assertEqual (h .get ('api_key' ), None )
346+
347+ def test_strip_down_pii_data_from_custom_headers (self ):
348+ headers = {
349+ 'Host' : 'net.example.com' ,
350+ 'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)' ,
351+ 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' ,
352+ 'Accept-Language' : 'en-us,en;q=0.5' ,
353+ 'Accept-Encoding' : 'gzip,deflate' ,
354+ 'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' ,
355+ 'Keep-Alive' : '300' ,
356+ 'Connection' : 'keep-alive' ,
357+ 'Cookie' : 'PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120' ,
358+ 'Pragma' : 'no-cache' ,
359+ 'Cache-Control' : 'no-cache' ,
360+ 'authorization' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
361+ 'access_token' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
362+ 'apikey' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
363+ 'password' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
364+ 'passwd' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
365+ 'secret' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
366+ 'api_key' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z'
367+ }
368+
369+ with requests_mock .Mocker (real_http = True ) as request :
370+ request .headers = headers
371+
372+ options = SecureNativeOptions (pii_headers = ['authorization' , 'access_token' , 'apikey' , 'password' ,
373+ 'passwd' , 'secret' , 'api_key' ])
374+ h = RequestUtils .get_headers_from_request (request .headers , options )
375+
376+ self .assertEqual (h .get ('authorization' ), None )
377+ self .assertEqual (h .get ('access_token' ), None )
378+ self .assertEqual (h .get ('apikey' ), None )
379+ self .assertEqual (h .get ('password' ), None )
380+ self .assertEqual (h .get ('passwd' ), None )
381+ self .assertEqual (h .get ('secret' ), None )
382+ self .assertEqual (h .get ('api_key' ), None )
383+
384+ def test_strip_down_pii_data_from_regex_pattern (self ):
385+ headers = {
386+ 'Host' : 'net.example.com' ,
387+ 'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)' ,
388+ 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' ,
389+ 'Accept-Language' : 'en-us,en;q=0.5' ,
390+ 'Accept-Encoding' : 'gzip,deflate' ,
391+ 'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' ,
392+ 'Keep-Alive' : '300' ,
393+ 'Connection' : 'keep-alive' ,
394+ 'Cookie' : 'PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120' ,
395+ 'Pragma' : 'no-cache' ,
396+ 'Cache-Control' : 'no-cache' ,
397+ 'http_auth_authorization' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
398+ 'http_auth_access_token' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
399+ 'http_auth_apikey' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
400+ 'http_auth_password' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
401+ 'http_auth_passwd' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
402+ 'http_auth_secret' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z' ,
403+ 'http_auth_api_key' : 'ylSkZIjbdWybfs4fUQe9BqP0LH5Z'
404+ }
405+
406+ with requests_mock .Mocker (real_http = True ) as request :
407+ request .headers = headers
408+
409+ options = SecureNativeOptions (pii_regex_pattern = '((?i)(http_auth_)(\w+)?)' )
410+ h = RequestUtils .get_headers_from_request (request .headers , options )
411+
412+ self .assertEqual (h .get ('http_auth_authorization' ), None )
413+ self .assertEqual (h .get ('http_auth_access_token' ), None )
414+ self .assertEqual (h .get ('http_auth_apikey' ), None )
415+ self .assertEqual (h .get ('http_auth_password' ), None )
416+ self .assertEqual (h .get ('http_auth_passwd' ), None )
417+ self .assertEqual (h .get ('http_auth_secret' ), None )
418+ self .assertEqual (h .get ('http_auth_api_key' ), None )
0 commit comments