MediaWiki master
HeaderCallback.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Request;
4
8use RuntimeException;
9
15 private static $headersSentException;
17 private static $messageSent = false;
18
26 public static function register() {
27 if ( version_compare( PHP_VERSION, '8.6', '<' ) ) {
28 // This bug has been fixed in PHP 8.6:
29 // https://github.com/php/php-src/issues/20619#issuecomment-3828242097
30
31 // T261260 load some classes which will be needed in callback().
32 // Autoloading seems unreliable in header callbacks, and in the case of a web
33 // request (ie. in all cases where the request might be performance-sensitive)
34 // these classes will have to be loaded at some point anyway.
35 class_exists( WebRequest::class );
36 class_exists( LoggerFactory::class );
37 class_exists( Telemetry::class );
38 }
39
40 header_register_callback( self::callback( ... ) );
41 }
42
48 public static function callback() {
49 // Prevent caching of responses with cookies (T127993)
50 $headers = [];
51 foreach ( headers_list() as $header ) {
52 $header = explode( ':', $header, 2 );
53
54 // Note: The code below (currently) does not care about value-less headers
55 if ( isset( $header[1] ) ) {
56 $headers[ strtolower( trim( $header[0] ) ) ][] = trim( $header[1] );
57 }
58 }
59
60 if ( isset( $headers['set-cookie'] ) ) {
61 $cacheControl = isset( $headers['cache-control'] )
62 ? implode( ', ', $headers['cache-control'] )
63 : '';
64
65 if ( !preg_match( '/(?:^|,)\s*(?:private|no-cache|no-store)\s*(?:$|,)/i',
66 $cacheControl )
67 ) {
68 header( 'Expires: Thu, 01 Jan 1970 00:00:00 GMT' );
69 header( 'Cache-Control: private, max-age=0, s-maxage=0' );
70 LoggerFactory::getInstance( 'cache-cookies' )->warning(
71 'Cookies set on {url} with Cache-Control "{cache-control}"', [
73 'set-cookie' => self::sanitizeSetCookie( $headers['set-cookie'] ),
74 'cache-control' => $cacheControl ?: '<not set>',
75 ]
76 );
77 }
78 }
79
80 $telemetryHeaders = Telemetry::getInstance()->getRequestHeaders();
81 // Set the request ID/trace prams on the response, so edge infrastructure can log it.
82 // FIXME this is not an ideal place to do it, but the most reliable for now.
83 foreach ( $telemetryHeaders as $header => $value ) {
84 if ( !isset( $headers[strtolower( $header )] ) ) {
85 header( "$header: $value" );
86 }
87 }
88
89 // Save a backtrace for logging in case it turns out that headers were sent prematurely
90 self::$headersSentException = new RuntimeException( 'Headers already sent from this point' );
91 }
92
99 public static function warnIfHeadersSent() {
100 if ( !self::$messageSent && headers_sent( $filename, $line ) ) {
101 self::$messageSent = true;
102 MWDebug::warning( 'Headers already sent, should send headers earlier than ' .
103 wfGetCaller( 3 ) );
104 $logger = LoggerFactory::getInstance( 'headers-sent' );
105 $logger->error( 'Warning: headers were already sent (output started at ' . $filename . ':' . $line . ')', [
106 'exception' => self::$headersSentException,
107 'detection-trace' => new RuntimeException( 'Detected here' ),
108 ] );
109 }
110 }
111
117 public static function sanitizeSetCookie( array $values ) {
118 $sanitizedValues = [];
119 foreach ( $values as $value ) {
120 // Set-Cookie header format: <cookie-name>=<cookie-value>; <non-sensitive attributes>
121 $parts = explode( ';', $value );
122 [ $name, $value ] = explode( '=', $parts[0], 2 );
123 if ( strlen( $value ) > 8 ) {
124 $value = substr( $value, 0, 8 ) . '...';
125 $parts[0] = "$name=$value";
126 }
127 $sanitizedValues[] = implode( ';', $parts );
128 }
129 return implode( "\n", $sanitizedValues );
130 }
131}
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Debug toolbar.
Definition MWDebug.php:35
Service for handling telemetry data.
Definition Telemetry.php:15
Create PSR-3 logger objects.
static warnIfHeadersSent()
Log a warning message if headers have already been sent.
static callback()
The callback, which is called by the transport.
static sanitizeSetCookie(array $values)
Sanitize Set-Cookie headers for logging.
static getGlobalRequestURL()
Return the path and query string portion of the main request URI.