MediaWiki master
CorsUtils.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
10
15
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
66 private function allowOrigin( Origin $origin ): bool {
67 $allowed = array_merge( [ $this->getCanonicalDomain() ],
68 $this->options->get( MainConfigNames::CrossSiteAJAXdomains ) );
69 $excluded = $this->options->get( MainConfigNames::CrossSiteAJAXdomainExceptions );
70
71 return $origin->match( $allowed, $excluded );
72 }
73
74 private function getCanonicalDomain(): string {
75 $res = parse_url( $this->options->get( MainConfigNames::CanonicalServer ) );
76 '@phan-var array $res';
77
78 $host = $res['host'] ?? '';
79 $port = $res['port'] ?? null;
80
81 return $port ? "$host:$port" : $host;
82 }
83
94 public function modifyResponse( RequestInterface $request, ResponseInterface $response ): ResponseInterface {
95 if ( !$this->options->get( MainConfigNames::AllowCrossOrigin ) ) {
96 return $response;
97 }
98
99 $allowedOrigin = '*';
100
101 if ( $this->options->get( MainConfigNames::RestAllowCrossOriginCookieAuth ) ) {
102 // @TODO Since we only Vary the response if (1) the method is OPTIONS or (2) the user is
103 // registered, it is safe to only add the Vary: Origin when those two conditions
104 // are met since a response to a logged-in user's request is not cachable.
105 // Therefore, logged out users should always get `Access-Control-Allow-Origin: *`
106 // on all non-OPTIONS request and logged-in users *may* get
107 // `Access-Control-Allow-Origin: <requested origin>`
108
109 // Vary All Requests by the Origin header.
110 $response->addHeader( 'Vary', 'Origin' );
111
112 // If the Origin header is missing, there is nothing to check against.
113 if ( $request->hasHeader( 'Origin' ) ) {
114 $origin = Origin::parseHeaderList( $request->getHeader( 'Origin' ) );
115 if ( $this->allowOrigin( $origin ) ) {
116 // Only set the allowed origin for preflight requests, or for main requests where a registered
117 // user is authenticated. This prevents having to Vary all requests by the Origin.
118 // Anonymous users will always get '*', registered users *may* get the requested origin back.
119 if ( $request->getMethod() === 'OPTIONS' || $this->user->isRegistered() ) {
120 $allowedOrigin = $origin->getSingleOrigin();
121 }
122 }
123 }
124 }
125
126 // If the Origin was determined to be something other than *any* allow the session
127 // cookies to be sent with the main request. If this is the main request, allow the
128 // response to be read.
129 //
130 // If the client includes the credentials on a simple request (HEAD, GET, etc.), but
131 // they do not pass this check, the browser will refuse to allow the client to read the
132 // response. The client may resolve this by repeating the request without the
133 // credentials.
134 if ( $allowedOrigin !== '*' ) {
135 $response->setHeader( 'Access-Control-Allow-Credentials', 'true' );
136 }
137
138 $response->setHeader( 'Access-Control-Allow-Origin', $allowedOrigin );
139
140 return $response;
141 }
142
149 public function createPreflightResponse( array $allowedMethods ): Response {
150 $response = $this->responseFactory->createNoContent();
151 $response->setHeader( 'Access-Control-Allow-Methods', $allowedMethods );
152
153 $allowedHeaders = $this->options->get( MainConfigNames::AllowedCorsHeaders );
154 $allowedHeaders = array_merge( $allowedHeaders, array_diff( [
155 // Authorization header must be explicitly listed which prevent the use of '*'
156 'Authorization',
157 // REST must allow Content-Type to be operational
158 'Content-Type',
159 // REST relies on conditional requests for some endpoints
160 'If-Match',
161 'If-None-Match',
162 'If-Modified-Since',
163 ], $allowedHeaders ) );
164 $response->setHeader( 'Access-Control-Allow-Headers', $allowedHeaders );
165
166 return $response;
167 }
168}
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.
Definition CorsUtils.php:94
Base class for REST route handlers.
Definition Handler.php:25
needsWriteAccess()
Indicates whether this route requires write access to the wiki.
Definition Handler.php:1198
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:70
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.