<commit_before><?php
namespace RKA\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class SchemeAndHost
{
    /**
     * List of trusted proxy IP addresses
     *
     * If not empty, then one of these IP addresses must be in $_SERVER['REMOTE_ADDR']
     * in order for the proxy headers to be looked at.
     *
     * @var array
     */
    protected $trustedProxies;

    /**
     * Name of the IP address attribute added by rka-ip-address-middleware
     *
     * Defaults to 'ip_address'
     *
     * @var string
     */
    protected $ipAddressAttributeName;

    /**
     * Constructor
     *
     * @param array $trustedProxies   List of IP addresses of trusted proxies
     */
    public function __construct($trustedProxies = [], $ipAddressAttributeName = 'ip_address')
    {
        $this->trustedProxies = $trustedProxies;
        $this->ipAddressAttributeName = $ipAddressAttributeName;
    }

    /**
     * Set the "$attributeName" attribute to the client's IP address as determined from
     * the proxy header (X-Forwarded-For or from $_SERVER['REMOTE_ADDR']
     *
     * @param ServerRequestInterface $request PSR7 request
     * @param ResponseInterface $response     PSR7 response
     * @param callable $next                  Next middleware
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
    {
        if (!$next) {
            return $response;
        }
        
        if (!empty($this->trustedProxies)) {
            // get IP address from REMOTE_ADDR or from the Request attribute
            $ipAddress = null;
            $serverParams = $request->getServerParams();
            if (isset($serverParams['REMOTE_ADDR']) && $this->isValidIpAddress($serverParams['REMOTE_ADDR'])) {
                $ipAddress = $serverParams['REMOTE_ADDR'];
            }
            $ip = $request->getAttribute($this->ipAddressAttributeName);
            if ($this->isValidIpAddress($ip)) {
                $ipAddress = $ip;
            }

            if (!in_array($ipAddress, $this->trustedProxies)) {
                return $response = $next($request, $response);
            }
        }
        
        $uri = $request->getUri();

        // scheme
        if ($request->hasHeader('X-Forwarded-Proto')) {
            $scheme = $request->getHeaderLine('X-Forwarded-Proto');
            if (in_array($scheme, ['http', 'https'])) {
                $uri = $uri->withScheme($scheme);
            }
        }

        // host (& maybe port too)
        if ($request->hasHeader('X-Forwarded-Host')) {
            $host = trim(current(explode(',', $request->getHeaderLine('X-Forwarded-Host'))));
            
            $port = null;<commit_msg>Custom property "ip_address" should not be used for proxy validation<commit_after><?php
namespace RKA\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class SchemeAndHost
{
    /**
     * List of trusted proxy IP addresses
     *
     * If not empty, then one of these IP addresses must be in $_SERVER['REMOTE_ADDR']
     * in order for the proxy headers to be looked at.
     *
     * @var array
     */
    protected $trustedProxies;

    /**
     * Name of the IP address attribute added by rka-ip-address-middleware
     *
     * Defaults to 'ip_address'
     *
     * @var string
     */
    protected $ipAddressAttributeName;

    /**
     * Constructor
     *
     * @param array $trustedProxies   List of IP addresses of trusted proxies
     */
    public function __construct($trustedProxies = [], $ipAddressAttributeName = 'ip_address')
    {
        $this->trustedProxies = $trustedProxies;
        $this->ipAddressAttributeName = $ipAddressAttributeName;
    }

    /**
     * Set the "$attributeName" attribute to the client's IP address as determined from
     * the proxy header (X-Forwarded-For or from $_SERVER['REMOTE_ADDR']
     *
     * @param ServerRequestInterface $request PSR7 request
     * @param ResponseInterface $response     PSR7 response
     * @param callable $next                  Next middleware
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
    {
        if (!$next) {
            return $response;
        }
        
        if (!empty($this->trustedProxies)) {
            // get IP address from REMOTE_ADDR
            $ipAddress = null;
            $serverParams = $request->getServerParams();
            if (isset($serverParams['REMOTE_ADDR']) && $this->isValidIpAddress($serverParams['REMOTE_ADDR'])) {
                $ipAddress = $serverParams['REMOTE_ADDR'];
            }

            if (!in_array($ipAddress, $this->trustedProxies)) {
                return $response = $next($request, $response);
            }
        }
        
        $uri = $request->getUri();

        // scheme
        if ($request->hasHeader('X-Forwarded-Proto')) {
            $scheme = $request->getHeaderLine('X-Forwarded-Proto');
            if (in_array($scheme, ['http', 'https'])) {
                $uri = $uri->withScheme($scheme);
            }
        }

        // host (& maybe port too)
        if ($request->hasHeader('X-Forwarded-Host')) {
            $host = trim(current(explode(',', $request->getHeaderLine('X-Forwarded-Host'))));
            
            $port = null;
{'fraction_non_alphanumeric': 0.11983168679107208, 'fraction_numerical': 0.0007317965605561653, 'mean_word_length': 2.1784883720930233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2447462', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1503, 'n_words': 363}