<commit_before>package edu.tamu.weaver.auth.whitelist;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("whitelist")
public class Whitelist {

    private static final Logger logger = LoggerFactory.getLogger(Whitelist.class);

    private static final String HEADER_X_REAL_IP = "x-real-ip";

    private static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for";

    @Value("${app.whitelist:127.0.0.1}")
    private String[] whitelist;

    @PostConstruct
    public void info() {
        logger.info("Component whitelist initialized");
    }

    public boolean isAllowed(HttpServletRequest req) throws UnknownHostException {
        String ip = req.getRemoteAddr();
        logger.info("Request ip: " + ip);

        String realIp = req.getHeader(HEADER_X_REAL_IP);
        logger.info("Request real ip: " + realIp);

        String forwardForIps = req.getHeader(HEADER_X_FORWARDED_FOR);
        logger.info("Request forwarded for ip: " + forwardForIps);

        Enumeration<String> headerNames = req.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                String headerValue = req.getHeader(headerName);
                logger.info(headerName + ": " + headerValue);
            }
        }

        boolean allowed = false;

        for (String entry : whitelist) {
            if (ip != null && entry.trim().equals(ip.trim())) {
                logger.info("Allowing whitelist ip " + entry + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
            if (realIp != null && entry.trim().equals(realIp.trim())) {
                logger.info("Allowing whitelist real ip " + realIp + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
            if (forwardForIps != null && forwardForIps.contains(entry.trim())) {
                logger.info("Allowing whitelist forwarded for ips " + forwardForIps + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
        }

        String realHost = null;
        String forwardForHosts = null;

        if (!allowed) {
            if (realIp != null) {
                realHost = getHostFromIp(realIp);
                if (realHost != null) {
                    logger.info("Request real host: " + realHost);
                }
            }

            for (String entry : whitelist) {
                if (realHost != null && entry.trim().equals(realHost.trim())) {
                    logger.info("Allowing whitelist real host " + realHost + " for " + req.getRequestURI());
                    allowed = true;
                    break;
                }
            }
        }

        if (!allowed) {
            if (forwardForIps != null) {
                forwardForHosts = getForwardedForHosts(forwardForIps);
            }

            for (String entry : whitelist) {
                if (forwardForHosts != null && forwardForHosts.contains(entry.trim())) {
                    logger.info("Allowing whitelist forwarded for hosts " + forwardForHosts + " for " + req.getRequestURI());
                    allowed = true;
                    break;
                }
            }
        }

        if (!allowed) {
            logger.warn("Disallowing request for " + req.getRequestURI());
            if (ip != null) {
                logger.warn("  ip: " + ip);<commit_msg>update logging and removed debugging code<commit_after>package edu.tamu.weaver.auth.whitelist;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("whitelist")
public class Whitelist {

    private static final Logger logger = LoggerFactory.getLogger(Whitelist.class);

    private static final String HEADER_X_REAL_IP = "x-real-ip";

    private static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for";

    @Value("${app.whitelist:127.0.0.1}")
    private String[] whitelist;

    @PostConstruct
    public void info() {
        logger.debug("Component whitelist initialized");
    }

    public boolean isAllowed(HttpServletRequest req) throws UnknownHostException {
        String ip = req.getRemoteAddr();
        logger.debug("Request ip: " + ip);

        String realIp = req.getHeader(HEADER_X_REAL_IP);
        logger.debug("Request real ip: " + realIp);

        String forwardForIps = req.getHeader(HEADER_X_FORWARDED_FOR);
        logger.debug("Request forwarded for ip: " + forwardForIps);

        boolean allowed = false;

        for (String entry : whitelist) {
            if (ip != null && entry.trim().equals(ip.trim())) {
                logger.debug("Allowing whitelist ip " + entry + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
            if (realIp != null && entry.trim().equals(realIp.trim())) {
                logger.debug("Allowing whitelist real ip " + realIp + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
            if (forwardForIps != null && forwardForIps.contains(entry.trim())) {
                logger.debug("Allowing whitelist forwarded for ips " + forwardForIps + " for " + req.getRequestURI());
                allowed = true;
                break;
            }
        }

        String realHost = null;
        String forwardForHosts = null;

        if (!allowed) {
            if (realIp != null) {
                realHost = getHostFromIp(realIp);
                if (realHost != null) {
                    logger.debug("Request real host: " + realHost);
                }
            }

            for (String entry : whitelist) {
                if (realHost != null && entry.trim().equals(realHost.trim())) {
                    logger.debug("Allowing whitelist real host " + realHost + " for " + req.getRequestURI());
                    allowed = true;
                    break;
                }
            }
        }

        if (!allowed) {
            if (forwardForIps != null) {
                forwardForHosts = getForwardedForHosts(forwardForIps);
            }

            for (String entry : whitelist) {
                if (forwardForHosts != null && forwardForHosts.contains(entry.trim())) {
                    logger.debug("Allowing whitelist forwarded for hosts " + forwardForHosts + " for " + req.getRequestURI());
                    allowed = true;
                    break;
                }
            }
        }

        if (!allowed) {
            logger.warn("Disallowing request for " + req.getRequestURI());
            if (ip != null) {
                logger.warn("  ip: " + ip);
{'fraction_non_alphanumeric': 0.11160653965680313, 'fraction_numerical': 0.002161870017565194, 'mean_word_length': 1.9164696611505123, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '417248', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1947, 'n_words': 436}