Skip to content

Conversation

@clue
Copy link
Owner

@clue clue commented Jul 26, 2022

This changeset adds support for trusted proxies by reading the $remote_addr attribute for the AccessLogHandler like this:

<?php

require __DIR__ . '/../vendor/autoload.php';

class TrustedProxyMiddleware
{
    public function __invoke(Psr\Http\Message\ServerRequestInterface $request, callable $next)
    {
        // use 127.0.0.1 as trusted proxy to read from X-Forwarded-For (XFF)
        $remote_addr = $request->getAttribute('remote_addr') ?? $request->getServerParams()['REMOTE_ADDR'] ?? null;
        if ($remote_addr === '127.0.0.1' && $request->hasHeader('X-Forwarded-For')) {
            $remote_addr = preg_replace('/,.*/', '', $request->getHeaderLine('X-Forwarded-For'));
            $request = $request->withAttribute('remote_addr', $remote_addr);
        }
        return $next($request);
    }
}

$app = new FrameworkX\App(
    TrustedProxyMiddleware::class,
    FrameworkX\AccessLogHandler::class,
    FrameworkX\ErrorHandler::class
);

// Register routes here, see routing…

$app->run();

This is the next step in adding more options to control access logging in follow-up PRs as discussed in #169 and #172.

Builds on top of #174 and #175

@clue clue added the new feature New feature or request label Jul 26, 2022
@clue clue requested a review from SimonFrings July 26, 2022 11:38
@SimonFrings SimonFrings merged commit 440cf7e into clue:main Jul 26, 2022
@clue clue deleted the remote_addr branch July 26, 2022 14:32
@SimonFrings SimonFrings added this to the v0.11.0 milestone Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants