r55400 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55399‎ | r55400 | r55401 >
Date:00:22, 21 August 2009
Author:mrzman
Status:ok
Tags:
Comment:
Tweak Access-Control-Allow-Origin stuff per comments on r54127.
Use a wildcard syntax rather than string/regex options
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/api.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/DefaultSettings.php
@@ -4142,21 +4142,30 @@
41434143 * Newer browsers support cross-site AJAX when the target resource allows requests
41444144 * from the origin domain by the Access-Control-Allow-Origin header.
41454145 * This is currently only used by the API (requests to api.php)
4146 - * $wgCrossSiteAJAXdomains can be set as follows:
 4146+ * $wgCrossSiteAJAXdomains can be set using a wildcard syntax:
41474147 *
4148 - * - the string '*' to allow requests from any domain
4149 - * - an array of domains to allow AJAX requests from, e.g.
4150 - * array( 'http://en.wikipedia.org', 'http://en.wikibooks.org' );
4151 - * - if $wgCrossSiteAJAXdomainsRegex is true, an array of regexes to be
4152 - * matched against the request origin. Anything that matches will be allowed
 4148+ * '*' matches any number of characters
 4149+ * '?' matches any 1 character
 4150+ *
 4151+ * Example:
 4152+ $wgCrossSiteAJAXdomains = array(
 4153+ 'www.mediawiki.org',
 4154+ '*.wikipedia.org',
 4155+ '*.wikimedia.org',
 4156+ '*.wiktionary.org',
 4157+ );
 4158+ *
41534159 */
41544160 $wgCrossSiteAJAXdomains = array();
41554161
41564162 /**
4157 - * Set to true to treat $wgCrossSiteAJAXdomains as regexes instead of strings
 4163+ * Domains that should not be allowed to make AJAX requests,
 4164+ * even if they match one of the domains allowed by $wgCrossSiteAJAXdomains
 4165+ * Uses the same syntax as $wgCrossSiteAJAXdomains
41584166 */
4159 -$wgCrossSiteAJAXdomainsRegex = false;
41604167
 4168+$wgCrossSiteAJAXdomainExceptions = array();
 4169+
41614170 /**
41624171 * The minimum amount of memory that MediaWiki "needs"; MediaWiki will try to raise PHP's memory limit if it's below this amount.
41634172 */
Index: trunk/phase3/api.php
@@ -64,21 +64,37 @@
6565 }
6666
6767 // Selectively allow cross-site AJAX
68 -if ( $wgCrossSiteAJAXdomains && isset($_SERVER['HTTP_ORIGIN']) ) {
69 - if ( $wgCrossSiteAJAXdomains == '*' ) {
70 - header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
71 - header( 'Access-Control-Allow-Credentials: true' );
72 - } elseif ( $wgCrossSiteAJAXdomainsRegex ) {
73 - foreach ( $wgCrossSiteAJAXdomains as $regex ) {
74 - if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
75 - header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
76 - header( 'Access-Control-Allow-Credentials: true' );
77 - break;
 68+
 69+/*
 70+ * Helper function to convert wildcard string into a regex
 71+ * '*' => '.*?'
 72+ * '?' => '.'
 73+ * @ return string
 74+ */
 75+function convertWildcard( $search ) {
 76+ $search = preg_quote( $search, '/' );
 77+ $search = str_replace(
 78+ array( '\*', '\?' ),
 79+ array( '.*?', '.' ),
 80+ $search
 81+ );
 82+ return "/$search/";
 83+}
 84+
 85+if ( $wgCrossSiteAJAXdomains && isset($_SERVER['HTTP_ORIGIN']) ) {
 86+ $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
 87+ $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
 88+ foreach ( $regexes as $regex ) {
 89+ if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
 90+ foreach ( $exceptions as $exc ) { // Check against exceptions
 91+ if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
 92+ break 2;
 93+ }
7894 }
 95+ header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
 96+ header( 'Access-Control-Allow-Credentials: true' );
 97+ break;
7998 }
80 - } elseif ( in_array( $_SERVER['HTTP_ORIGIN'], $wgCrossSiteAJAXdomains ) ) {
81 - header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
82 - header( 'Access-Control-Allow-Credentials: true' );
8399 }
84100 }
85101
Index: trunk/phase3/RELEASE-NOTES
@@ -75,7 +75,7 @@
7676 PHP and database version.
7777 * $wgSecondaryGoNamespaces allows an arry of namespaces to be checked when the
7878 GO button is pressed, in addition to the main namespace.
79 -* (bug 19907) $wgCrossSiteAJAXdomains and $wgCrossSiteAJAXdomainsRegex added
 79+* (bug 19907) $wgCrossSiteAJAXdomains and $wgCrossSiteAJAXdomainExceptions added
8080 to control which external domains may access the API via cross-site AJAX.
8181 * $wgMaintenanceScripts for extensions to add their scripts to the default list
8282 * $wgMemoryLimit has been added, default value '50M'

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r54127(bug 19907) Adds support for cross-domain AJAX requests to the API....mrzman21:56, 31 July 2009

Status & tagging log