@Test public void readUserNetworkAddressTestMultipleXForward() throws PwmUnrecoverableException { final Configuration conf = new Configuration( StoredConfigurationFactory.newConfig() ); final HttpServletRequest mockRequest = Mockito.mock( HttpServletRequest.class ); Mockito.when( mockRequest.getRemoteAddr() ).thenReturn( "10.1.1.1" ); Mockito.when( mockRequest.getHeader( HttpHeader.XForwardedFor.getHttpName() ) ).thenReturn( "10.1.1.2, 10.1.1.3" ); final String resultIP = RequestInitializationFilter.readUserNetworkAddress( mockRequest, conf ); Assert.assertEquals( "10.1.1.2", resultIP ); }
public static String readUserNetworkAddress( final HttpServletRequest request, final Configuration config ) { final List<String> candidateAddresses = new ArrayList<>(); final boolean useXForwardedFor = config != null && config.readSettingAsBoolean( PwmSetting.USE_X_FORWARDED_FOR_HEADER ); if ( useXForwardedFor ) { final String xForwardedForValue = request.getHeader( HttpHeader.XForwardedFor.getHttpName() ); if ( !StringUtil.isEmpty( xForwardedForValue ) ) { Collections.addAll( candidateAddresses, xForwardedForValue.split( "," ) ); } } final String sourceIP = request.getRemoteAddr(); if ( !StringUtil.isEmpty( sourceIP ) ) { candidateAddresses.add( sourceIP ); } for ( final String candidateAddress : candidateAddresses ) { final String trimAddr = candidateAddress.trim(); if ( InetAddressValidator.getInstance().isValid( trimAddr ) ) { return trimAddr; } else { LOGGER.warn( () -> "discarding bogus source network address '" + trimAddr + "'" ); } } return ""; }
RequestInitializationFilter implements Filter { public static String readUserNetworkAddress( final HttpServletRequest request, final Configuration config ) { final List<String> candidateAddresses = new ArrayList<>(); final boolean useXForwardedFor = config != null && config.readSettingAsBoolean( PwmSetting.USE_X_FORWARDED_FOR_HEADER ); if ( useXForwardedFor ) { final String xForwardedForValue = request.getHeader( HttpHeader.XForwardedFor.getHttpName() ); if ( !StringUtil.isEmpty( xForwardedForValue ) ) { Collections.addAll( candidateAddresses, xForwardedForValue.split( "," ) ); } } final String sourceIP = request.getRemoteAddr(); if ( !StringUtil.isEmpty( sourceIP ) ) { candidateAddresses.add( sourceIP ); } for ( final String candidateAddress : candidateAddresses ) { final String trimAddr = candidateAddress.trim(); if ( InetAddressValidator.getInstance().isValid( trimAddr ) ) { return trimAddr; } else { LOGGER.warn( () -> "discarding bogus source network address '" + trimAddr + "'" ); } } return ""; } }
RequestInitializationFilter implements Filter { public static String readUserNetworkAddress( final HttpServletRequest request, final Configuration config ) { final List<String> candidateAddresses = new ArrayList<>(); final boolean useXForwardedFor = config != null && config.readSettingAsBoolean( PwmSetting.USE_X_FORWARDED_FOR_HEADER ); if ( useXForwardedFor ) { final String xForwardedForValue = request.getHeader( HttpHeader.XForwardedFor.getHttpName() ); if ( !StringUtil.isEmpty( xForwardedForValue ) ) { Collections.addAll( candidateAddresses, xForwardedForValue.split( "," ) ); } } final String sourceIP = request.getRemoteAddr(); if ( !StringUtil.isEmpty( sourceIP ) ) { candidateAddresses.add( sourceIP ); } for ( final String candidateAddress : candidateAddresses ) { final String trimAddr = candidateAddress.trim(); if ( InetAddressValidator.getInstance().isValid( trimAddr ) ) { return trimAddr; } else { LOGGER.warn( () -> "discarding bogus source network address '" + trimAddr + "'" ); } } return ""; }  }
RequestInitializationFilter implements Filter { public static String readUserNetworkAddress( final HttpServletRequest request, final Configuration config ) { final List<String> candidateAddresses = new ArrayList<>(); final boolean useXForwardedFor = config != null && config.readSettingAsBoolean( PwmSetting.USE_X_FORWARDED_FOR_HEADER ); if ( useXForwardedFor ) { final String xForwardedForValue = request.getHeader( HttpHeader.XForwardedFor.getHttpName() ); if ( !StringUtil.isEmpty( xForwardedForValue ) ) { Collections.addAll( candidateAddresses, xForwardedForValue.split( "," ) ); } } final String sourceIP = request.getRemoteAddr(); if ( !StringUtil.isEmpty( sourceIP ) ) { candidateAddresses.add( sourceIP ); } for ( final String candidateAddress : candidateAddresses ) { final String trimAddr = candidateAddress.trim(); if ( InetAddressValidator.getInstance().isValid( trimAddr ) ) { return trimAddr; } else { LOGGER.warn( () -> "discarding bogus source network address '" + trimAddr + "'" ); } } return ""; }  @Override void init( final FilterConfig filterConfig ); @Override void destroy( ); void doFilter(
            final ServletRequest servletRequest,
            final ServletResponse servletResponse,
            final FilterChain filterChain ); static void addStaticResponseHeaders(
            final PwmApplication pwmApplication,
            final HttpServletResponse resp
    ); static String readUserHostname( final HttpServletRequest request, final Configuration config ); static String readUserNetworkAddress(
            final HttpServletRequest request,
            final Configuration config
    ); }
RequestInitializationFilter implements Filter { public static String readUserNetworkAddress( final HttpServletRequest request, final Configuration config ) { final List<String> candidateAddresses = new ArrayList<>(); final boolean useXForwardedFor = config != null && config.readSettingAsBoolean( PwmSetting.USE_X_FORWARDED_FOR_HEADER ); if ( useXForwardedFor ) { final String xForwardedForValue = request.getHeader( HttpHeader.XForwardedFor.getHttpName() ); if ( !StringUtil.isEmpty( xForwardedForValue ) ) { Collections.addAll( candidateAddresses, xForwardedForValue.split( "," ) ); } } final String sourceIP = request.getRemoteAddr(); if ( !StringUtil.isEmpty( sourceIP ) ) { candidateAddresses.add( sourceIP ); } for ( final String candidateAddress : candidateAddresses ) { final String trimAddr = candidateAddress.trim(); if ( InetAddressValidator.getInstance().isValid( trimAddr ) ) { return trimAddr; } else { LOGGER.warn( () -> "discarding bogus source network address '" + trimAddr + "'" ); } } return ""; }  @Override void init( final FilterConfig filterConfig ); @Override void destroy( ); void doFilter(
            final ServletRequest servletRequest,
            final ServletResponse servletResponse,
            final FilterChain filterChain ); static void addStaticResponseHeaders(
            final PwmApplication pwmApplication,
            final HttpServletResponse resp
    ); static String readUserHostname( final HttpServletRequest request, final Configuration config ); static String readUserNetworkAddress(
            final HttpServletRequest request,
            final Configuration config
    );  }