Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Proxy Laravel Package

fideloper/proxy

Laravel trusted proxy middleware that correctly detects HTTPS, host, and client IP behind load balancers and reverse proxies by handling X-Forwarded-* headers. Fixes URL generation and request data when running behind ELB, Cloudflare, Nginx, etc.

Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Core Use Case Alignment: Perfectly addresses the critical need for accurate request handling in Laravel applications deployed behind reverse proxies (e.g., Nginx, Cloudflare, AWS ELB). Directly mitigates common pitfalls like:
    • Incorrect client IP logging (e.g., showing proxy IP instead of end-user IP).
    • Mixed-content warnings (e.g., http:// URLs generated on https:// sites).
    • Broken redirects or asset paths due to misconfigured scheme/host.
  • Middleware-Based Design: Leverages Laravel’s middleware stack, requiring minimal architectural changes. No need for monolithic refactoring—ideal for incremental adoption.
  • Configurability: Supports dynamic trust rules (e.g., IP ranges, CIDR blocks) via config/trustedproxies.php, enabling granular control over proxy validation.

Integration Feasibility

  • Drop-in Compatibility: Designed for Laravel 5.5+ (tested up to Laravel 8.x). Works seamlessly with:
    • Laravel’s built-in Request object (extends it to normalize headers).
    • Existing middleware pipelines (e.g., app/Http/Kernel.php).
    • Third-party packages relying on accurate request data (e.g., Spatie’s rate-limiting, Laravel Debugbar).
  • Zero Database Changes: Purely configuration-driven; no schema migrations or ORM hooks required.
  • Header Normalization: Automatically parses and validates X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-Host, reducing manual header management.

Technical Risk

  • Proxy Misconfiguration: Risk of trusting untrusted proxies (e.g., exposing internal IPs). Mitigated by:
    • Explicit trustedProxies configuration (default: empty array).
    • Network-based trust rules (e.g., ['192.168.1.0/24']).
  • Header Injection Attacks: If proxies forward malformed headers, Laravel may process invalid data. The package includes basic validation but relies on proxy integrity.
  • Laravel Version Skew: Last release in 2022; potential compatibility gaps with Laravel 10+. Risk: Low if using Laravel 8/9, but requires testing for newer versions.
  • Performance Overhead: Minimal (header parsing is lightweight), but adds a middleware layer. Benchmark if latency is critical.

Key Questions

  1. Proxy Topology:
    • How many layers of proxies/load balancers exist? Does the package need to support nested X-Forwarded-For headers (e.g., client, proxy1, proxy2)?
    • Are proxies static (e.g., AWS ELB) or dynamic (e.g., serverless edge functions)?
  2. Trust Boundaries:
    • Should all proxies be trusted, or only specific ones (e.g., internal vs. CDN)?
    • Are there compliance requirements (e.g., GDPR) for logging client IPs accurately?
  3. Existing Middleware:
    • Does the app use custom middleware that relies on raw Request data (e.g., IP-based auth)? Conflicts may arise.
  4. CI/CD Pipeline:
    • How are proxy headers tested in staging vs. production? Need to validate trustedProxies config across environments.
  5. Fallback Behavior:
    • What happens if a trusted proxy fails to forward headers? Should the app degrade gracefully (e.g., fall back to REMOTE_ADDR)?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Native support for Laravel’s middleware system, Request object, and configuration files. Integrates with:
    • Routing: Automatically corrects URL generation in url(), route(), and asset() helpers.
    • Logging: Ensures Request::ip() and Request::userAgent() reflect the client, not proxies.
    • Security: Works with Laravel’s TrustProxies middleware (if using older versions) or replaces it entirely.
  • Proxy Types:
    • Reverse Proxies: Nginx, Apache, Caddy.
    • Load Balancers: AWS ELB, Cloudflare, HAProxy, Traefik.
    • CDNs: Cloudflare (with CF-Connecting-IP support via custom headers).
  • Non-Laravel Stacks: Not directly applicable, but could inspire similar middleware for other frameworks (e.g., Symfony, Express).

Migration Path

  1. Assessment Phase:
    • Audit current proxy setup: Identify all proxies/load balancers and their X-Forwarded-* behavior.
    • Log sample requests to verify REMOTE_ADDR vs. X-Forwarded-For discrepancies.
  2. Configuration:
    • Publish the package’s config: php artisan vendor:publish --provider="Fideloper\Proxy\ProxyServiceProvider".
    • Define trustedProxies in config/trustedproxies.php:
      'proxies' => [
          '192.168.1.0/24', // Internal proxy subnet
          '10.0.0.1',       // Specific ELB IP
      ],
      'headers' => [
          'X-Forwarded-For',
          'X-Forwarded-Host',
          'X-Forwarded-Proto',
      ],
      
  3. Middleware Registration:
    • Add to $middleware in app/Http/Kernel.php:
      \Fideloper\Proxy\TrustProxies::class,
      
    • Place it early in the pipeline (before auth, logging, or IP-dependent middleware).
  4. Testing:
    • Unit Tests: Mock Request with proxied headers to validate Request::ip(), Request::getHost(), etc.
    • Integration Tests: Use tools like ProxyMock to simulate proxies.
    • Load Testing: Verify performance impact under traffic (expect negligible overhead).
  5. Rollout:
    • Staging First: Deploy behind a staging proxy mirroring production.
    • Feature Flag: Optionally wrap in a feature flag for gradual rollout.
    • Monitor: Watch for:
      • Mixed-content warnings (Chrome DevTools).
      • Logged IPs (ensure they match expected client IPs).
      • Redirect loops (e.g., http://https:// failures).

Compatibility

  • Laravel Versions:
    • Supported: 5.5–8.x (tested). Unsupported: 9.x+ (may require patches).
    • Workaround for Newer Laravel: Use the underlying logic (e.g., Request::trustedProxy()) or fork the package.
  • Proxy Headers:
    • Supports standard X-Forwarded-* headers. For non-standard headers (e.g., Cloudflare’s CF-Connecting-IP), extend the package or pre-process headers in middleware.
  • Existing Packages:
    • Conflicts: Packages using raw Request data (e.g., laravel-debugbar, spatie/rate-limiter) will benefit but may need re-testing.
    • Synergy: Works with spatie/activitylog (for accurate IP logging) and laravel/sanctum (for IP-based auth).

Sequencing

  1. Phase 1: Configure trustedProxies and validate basic header parsing.
  2. Phase 2: Test URL generation (url(), asset()) and HTTPS detection.
  3. Phase 3: Integrate with logging/rate-limiting systems.
  4. Phase 4: Monitor production for edge cases (e.g., malformed headers).

Operational Impact

Maintenance

  • Configuration-Driven: Minimal ongoing maintenance if proxy infrastructure is stable. Updates required for:
    • New Laravel versions (if compatibility breaks).
    • Changes to proxy IPs/subnets (update trustedProxies).
  • Dependency Management:
    • Monitor for security advisories (MIT license; community-driven).
    • Consider pinning the version in composer.json if using unsupported Laravel versions.
  • Documentation:
    • Update internal docs to reflect new Request behavior (e.g., "IPs are now client-facing").
    • Train ops teams on trustedProxies configuration for new environments.

Support

  • Troubleshooting:
    • Common Issues:
      • "Wrong IP logged": Verify trustedProxies includes all intermediate proxies.
      • "Mixed-content errors": Ensure X-Forwarded-Proto is correctly set to https.
      • "Redirect loops": Check X-Forwarded-Host matches the app’s expected host.
    • Debugging Tools:
      • Dump headers in middleware: dd($request->header()).
      • Use Request::getClientIp() vs. Request::ip() to compare behaviors.
  • Support Matrix:
    • L1 Support: Handle config updates and basic header issues.
    • L2 Support: Debug proxy misconfigurations (e.g., missing headers).
    • L3 Support: Custom header
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport