|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Http\Middleware; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Symfony\Component\HttpFoundation\Response; |
| 8 | + |
| 9 | +class HideServerHeaders |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Headers that reveal server information and should be removed |
| 13 | + * |
| 14 | + * @var array |
| 15 | + */ |
| 16 | + private $headersToRemove = [ |
| 17 | + // Server identification |
| 18 | + 'Server', |
| 19 | + 'X-Powered-By', |
| 20 | + 'X-AspNet-Version', |
| 21 | + 'X-AspNetMvc-Version', |
| 22 | + |
| 23 | + // Web technologies and frameworks |
| 24 | + 'X-Generator', |
| 25 | + 'X-Drupal-Cache', |
| 26 | + 'X-Varnish', |
| 27 | + 'X-Cache', |
| 28 | + 'X-Cache-Hits', |
| 29 | + 'X-Framework', |
| 30 | + |
| 31 | + // Load balancer and proxy information |
| 32 | + 'X-Forwarded-For', |
| 33 | + 'X-Real-IP', |
| 34 | + 'X-Forwarded-Proto', |
| 35 | + 'X-Forwarded-Host', |
| 36 | + 'X-Forwarded-Server', |
| 37 | + 'X-Forwarded-Port', |
| 38 | + |
| 39 | + // Additional server information |
| 40 | + 'X-Served-By', |
| 41 | + 'X-Cache-Status', |
| 42 | + 'X-Served-From', |
| 43 | + 'X-Content-Source', |
| 44 | + 'X-Request-ID', |
| 45 | + 'X-Request-Id', |
| 46 | + |
| 47 | + // PHP specific headers |
| 48 | + 'X-PHP-Version', |
| 49 | + 'X-PHP-Originating-Script', |
| 50 | + |
| 51 | + // Development and debugging headers |
| 52 | + 'X-Debug-Token', |
| 53 | + 'X-Debug-Token-Link', |
| 54 | + 'X-Symfony-Cache', |
| 55 | + ]; |
| 56 | + |
| 57 | + /** |
| 58 | + * Handle an incoming request. |
| 59 | + * |
| 60 | + * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next |
| 61 | + */ |
| 62 | + public function handle(Request $request, Closure $next): Response |
| 63 | + { |
| 64 | + $response = $next($request); |
| 65 | + |
| 66 | + // Only remove headers in production or when explicitly configured |
| 67 | + if ($this->shouldHideHeaders()) { |
| 68 | + // Remove all server-revealing headers |
| 69 | + foreach ($this->headersToRemove as $header) { |
| 70 | + $response->headers->remove($header); |
| 71 | + } |
| 72 | + |
| 73 | + // Set a generic server header to avoid revealing the absence |
| 74 | + $response->headers->set('Server', 'Web Server'); |
| 75 | + } |
| 76 | + |
| 77 | + return $response; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Determine if headers should be hidden based on environment |
| 82 | + * |
| 83 | + * @return bool |
| 84 | + */ |
| 85 | + private function shouldHideHeaders(): bool |
| 86 | + { |
| 87 | + // Hide headers in production or when explicitly configured |
| 88 | + return app()->environment('production') || |
| 89 | + config('app.hide_server_headers', false); |
| 90 | + } |
| 91 | +} |
0 commit comments