MediaWiki master
CorsUtils.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
10
16 public const CONSTRUCTOR_OPTIONS = [
23 ];
24
25 private ServiceOptions $options;
26 private ResponseFactory $responseFactory;
27 private UserIdentity $user;
28
29 public function __construct(
30 ServiceOptions $options,
31 ResponseFactory $responseFactory,
32 UserIdentity $user
33 ) {
34 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
35 $this->options = $options;
36 $this->responseFactory = $responseFactory;
37 $this->user = $user;
38 }
39
48 public function authorize( RequestInterface $request, Handler $handler ) {
49 // Handlers that need write access are by definition a cache-miss, therefore there is no
50 // need to vary by the origin.
51 if (
52 $handler->needsWriteAccess()
53 && $request->hasHeader( 'Origin' )
54 && !$this->user->isRegistered()
55 ) {
56 $origin = Origin::parseHeaderList( $request->getHeader( 'Origin' ) );
57
58 if ( !$this->allowOrigin( $origin ) ) {
59 return 'rest-cross-origin-anon-write';
60 }
61 }
62
63 return null;
64 }
65
70 private function allowOrigin( Origin $origin ): bool {
71 $allowed = array_merge( [ $this->getCanonicalDomain() ],
72 $this->options->get( MainConfigNames::CrossSiteAJAXdomains ) );
73 $excluded = $this->options->get( MainConfigNames::CrossSiteAJAXdomainExceptions );
74
75 return $origin->match( $allowed, $excluded );
76 }
77
81 private function getCanonicalDomain(): string {
82 $res = parse_url( $this->options->get( MainConfigNames::CanonicalServer ) );
83 '@phan-var array $res';
84
85 $host = $res['host'] ?? '';
86 $port = $res['port'] ?? null;
87
88 return $port ? "$host:$port" : $host;
89 }
90
101 public function modifyResponse( RequestInterface $request, ResponseInterface $response ): ResponseInterface {
102 if ( !$this->options->get( MainConfigNames::AllowCrossOrigin ) ) {
103 return $response;
104 }
105
106 $allowedOrigin = '*';
107
108 if ( $this->options->get( MainConfigNames::RestAllowCrossOriginCookieAuth ) ) {
109 // @TODO Since we only Vary the response if (1) the method is OPTIONS or (2) the user is
110 // registered, it is safe to only add the Vary: Origin when those two conditions
111 // are met since a response to a logged-in user's request is not cachable.
112 // Therefore, logged out users should always get `Access-Control-Allow-Origin: *`
113 // on all non-OPTIONS request and logged-in users *may* get
114 // `Access-Control-Allow-Origin: <requested origin>`
115
116 // Vary All Requests by the Origin header.
117 $response->addHeader( 'Vary', 'Origin' );
118
119 // If the Origin header is missing, there is nothing to check against.
120 if ( $request->hasHeader( 'Origin' ) ) {
121 $origin = Origin::parseHeaderList( $request->getHeader( 'Origin' ) );
122 if ( $this->allowOrigin( $origin ) ) {
123 // Only set the allowed origin for preflight requests, or for main requests where a registered
124 // user is authenticated. This prevents having to Vary all requests by the Origin.
125 // Anonymous users will always get '*', registered users *may* get the requested origin back.
126 if ( $request->getMethod() === 'OPTIONS' || $this->user->isRegistered() ) {
127 $allowedOrigin = $origin->getSingleOrigin();
128 }
129 }
130 }
131 }
132
133 // If the Origin was determined to be something other than *any* allow the session
134 // cookies to be sent with the main request. If this is the main request, allow the
135 // response to be read.
136 //
137 // If the client includes the credentials on a simple request (HEAD, GET, etc.), but
138 // they do not pass this check, the browser will refuse to allow the client to read the
139 // response. The client may resolve this by repeating the request without the
140 // credentials.
141 if ( $allowedOrigin !== '*' ) {
142 $response->setHeader( 'Access-Control-Allow-Credentials', 'true' );
143 }
144
145 $response->setHeader( 'Access-Control-Allow-Origin', $allowedOrigin );
146
147 return $response;
148 }
149
156 public function createPreflightResponse( array $allowedMethods ): Response {
157 $response = $this->responseFactory->createNoContent();
158 $response->setHeader( 'Access-Control-Allow-Methods', $allowedMethods );
159
160 $allowedHeaders = $this->options->get( MainConfigNames::AllowedCorsHeaders );
161 $allowedHeaders = array_merge( $allowedHeaders, array_diff( [
162 // Authorization header must be explicitly listed which prevent the use of '*'
163 'Authorization',
164 // REST must allow Content-Type to be operational
165 'Content-Type',
166 // REST relies on conditional requests for some endpoints
167 'If-Mach',
168 'If-None-Match',
169 'If-Modified-Since',
170 ], $allowedHeaders ) );
171 $response->setHeader( 'Access-Control-Allow-Headers', $allowedHeaders );
172
173 return $response;
174 }
175}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const CrossSiteAJAXdomainExceptions
Name constant for the CrossSiteAJAXdomainExceptions setting, for use with Config::get()
const CanonicalServer
Name constant for the CanonicalServer setting, for use with Config::get()
const AllowCrossOrigin
Name constant for the AllowCrossOrigin setting, for use with Config::get()
const AllowedCorsHeaders
Name constant for the AllowedCorsHeaders setting, for use with Config::get()
const CrossSiteAJAXdomains
Name constant for the CrossSiteAJAXdomains setting, for use with Config::get()
const RestAllowCrossOriginCookieAuth
Name constant for the RestAllowCrossOriginCookieAuth setting, for use with Config::get()
__construct(ServiceOptions $options, ResponseFactory $responseFactory, UserIdentity $user)
Definition CorsUtils.php:29
createPreflightResponse(array $allowedMethods)
Create a CORS preflight response.
authorize(RequestInterface $request, Handler $handler)
Only allow registered users to make unsafe cross-origin requests.
Definition CorsUtils.php:48
modifyResponse(RequestInterface $request, ResponseInterface $response)
Modify response to allow for CORS.
Base class for REST route handlers.
Definition Handler.php:24
needsWriteAccess()
Indicates whether this route requires write access.
Definition Handler.php:1099
A class to assist with the parsing of Origin header according to the RFC 6454 https://tools....
Definition Origin.php:12
match(array $allowList, array $excludeList)
Check whether all the origins match at least one of the rules in $allowList.
Definition Origin.php:77
Generates standardized response objects.
setHeader( $name, $value)
Set or replace the specified header.
Definition Response.php:94
An interface used by Router to ensure that the client has "basic" access, i.e.
A request interface similar to PSR-7's ServerRequestInterface.
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.
getHeader( $name)
Retrieves a message header value by the given case-insensitive name.
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
addHeader( $name, $value)
Append the given value to the specified header.
Interface for objects representing user identity.