Merge "mediawiki.debug: Move internal footHovzer to the same module"
[lhc/web/wiklou.git] / includes / OutputPage.php
index 56df0f0..c7028db 100644 (file)
@@ -304,6 +304,11 @@ class OutputPage extends ContextSource {
         */
        private $mLinkHeader = [];
 
+       /**
+        * @var string The nonce for Content-Security-Policy
+        */
+       private $CSPNonce;
+
        /**
         * Constructor for OutputPage. This should not be called directly.
         * Instead a new RequestContext should be created and it will implicitly create
@@ -475,7 +480,7 @@ class OutputPage extends ContextSource {
                if ( is_null( $version ) ) {
                        $version = $this->getConfig()->get( 'StyleVersion' );
                }
-               $this->addScript( Html::linkedScript( wfAppendQuery( $path, $version ) ) );
+               $this->addScript( Html::linkedScript( wfAppendQuery( $path, $version ), $this->getCSPNonce() ) );
        }
 
        /**
@@ -485,7 +490,7 @@ class OutputPage extends ContextSource {
         * @param string $script JavaScript text, no script tags
         */
        public function addInlineScript( $script ) {
-               $this->mScripts .= Html::inlineScript( $script );
+               $this->mScripts .= Html::inlineScript( "\n$script\n", $this->getCSPNonce() ) . "\n";
        }
 
        /**
@@ -550,9 +555,7 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add one or more modules recognized by ResourceLoader. Modules added
-        * through this function will be loaded by ResourceLoader when the
-        * page loads.
+        * Load one or more ResourceLoader modules on this page.
         *
         * @param string|array $modules Module name (string) or array of module names
         */
@@ -561,7 +564,7 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Get the list of module JS to include on this page
+        * Get the list of script-only modules to load on this page.
         *
         * @param bool $filter
         * @param string|null $position Unused
@@ -574,10 +577,13 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add only JS of one or more modules recognized by ResourceLoader. Module
-        * scripts added through this function will be loaded by ResourceLoader when
-        * the page loads.
+        * Load the scripts of one or more ResourceLoader modules, on this page.
+        *
+        * This method exists purely to provide the legacy behaviour of loading
+        * a module's scripts in the global scope, and without dependency resolution.
+        * See <https://phabricator.wikimedia.org/T188689>.
         *
+        * @deprecated since 1.31 Use addModules() instead.
         * @param string|array $modules Module name (string) or array of module names
         */
        public function addModuleScripts( $modules ) {
@@ -585,7 +591,7 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Get the list of module CSS to include on this page
+        * Get the list of style-only modules to load on this page.
         *
         * @param bool $filter
         * @param string|null $position Unused
@@ -598,11 +604,11 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add only CSS of one or more modules recognized by ResourceLoader.
+        * Load the styles of one or more ResourceLoader modules on this page.
         *
-        * Module styles added through this function will be added using standard link CSS
-        * tags, rather than as a combined Javascript and CSS package. Thus, they will
-        * load when JavaScript is disabled (unless CSS also happens to be disabled).
+        * Module styles added through this function will be loaded as a stylesheet,
+        * using a standard `<link rel=stylesheet>` HTML tag, rather than as a combined
+        * Javascript and CSS package. Thus, they will even load when JavaScript is disabled.
         *
         * @param string|array $modules Module name (string) or array of module names
         */
@@ -2331,6 +2337,23 @@ class OutputPage extends ContextSource {
                }
        }
 
+       /**
+        * Transfer styles and JavaScript modules from skin.
+        *
+        * @param Skin $sk to load modules for
+        */
+       public function loadSkinModules( $sk ) {
+               foreach ( $sk->getDefaultModules() as $group => $modules ) {
+                       if ( $group === 'styles' ) {
+                               foreach ( $modules as $key => $moduleMembers ) {
+                                       $this->addModuleStyles( $moduleMembers );
+                               }
+                       } else {
+                               $this->addModules( $modules );
+                       }
+               }
+       }
+
        /**
         * Finally, all the text has been munged and accumulated into
         * the object, let's actually output it:
@@ -2415,18 +2438,18 @@ class OutputPage extends ContextSource {
                        $response->header( "X-Frame-Options: $frameOptions" );
                }
 
+               ContentSecurityPolicy::sendHeaders( $this );
+
                if ( $this->mArticleBodyOnly ) {
                        echo $this->mBodytext;
                } else {
-                       // Enable safe mode if requested
+                       // Enable safe mode if requested (T152169)
                        if ( $this->getRequest()->getBool( 'safemode' ) ) {
                                $this->disallowUserJs();
                        }
 
                        $sk = $this->getSkin();
-                       foreach ( $sk->getDefaultModules() as $group ) {
-                               $this->addModules( $group );
-                       }
+                       $this->loadSkinModules( $sk );
 
                        MWDebug::addModules( $this );
 
@@ -2838,6 +2861,17 @@ class OutputPage extends ContextSource {
 
                        $rlClient = new ResourceLoaderClientHtml( $context, [
                                'target' => $this->getTarget(),
+                               'nonce' => $this->getCSPNonce(),
+                               // When 'safemode', disallowUserJs(), or reduceAllowedModules() is used
+                               // to only restrict modules to ORIGIN_CORE (ie. disallow ORIGIN_USER), the list of
+                               // modules enqueud for loading on this page is filtered to just those.
+                               // However, to make sure we also apply the restriction to dynamic dependencies and
+                               // lazy-loaded modules at run-time on the client-side, pass 'safemode' down to the
+                               // StartupModule so that the client-side registry will not contain any restricted
+                               // modules either. (T152169, T185303)
+                               'safemode' => ( $this->getAllowedModules( ResourceLoaderModule::TYPE_COMBINED )
+                                       <= ResourceLoaderModule::ORIGIN_CORE_INDIVIDUAL
+                               ) ? '1' : null,
                        ] );
                        $rlClient->setConfig( $this->getJSVars() );
                        $rlClient->setModules( $this->getModules( /*filter*/ true ) );
@@ -2895,7 +2929,8 @@ class OutputPage extends ContextSource {
                                ResourceLoaderContext::newDummyContext(),
                                [ 'html5shiv' ],
                                ResourceLoaderModule::TYPE_SCRIPTS,
-                               [ 'sync' => true ]
+                               [ 'sync' => true ],
+                               $this->getCSPNonce()
                        ) .
                        '<![endif]-->';
 
@@ -2976,7 +3011,8 @@ class OutputPage extends ContextSource {
                        $this->getRlClientContext(),
                        $modules,
                        $only,
-                       $extraQuery
+                       $extraQuery,
+                       $this->getCSPNonce()
                );
        }
 
@@ -3009,7 +3045,8 @@ class OutputPage extends ContextSource {
                        $chunks[] = ResourceLoader::makeInlineScript(
                                ResourceLoader::makeConfigSetScript(
                                        [ 'wgPageParseReport' => $this->limitReportJSData ]
-                               )
+                               ),
+                               $this->getCSPNonce()
                        );
                }
 
@@ -3976,4 +4013,26 @@ class OutputPage extends ContextSource {
                        );
                }
        }
+
+       /**
+        * Get (and set if not yet set) the CSP nonce.
+        *
+        * This value needs to be included in any <script> tags on the
+        * page.
+        *
+        * @return string|bool Nonce or false to mean don't output nonce
+        * @since 1.32
+        */
+       public function getCSPNonce() {
+               if ( !ContentSecurityPolicy::isEnabled( $this->getConfig() ) ) {
+                       return false;
+               }
+               if ( $this->CSPNonce === null ) {
+                       // XXX It might be expensive to generate randomness
+                       // on every request, on windows.
+                       $rand = MWCryptRand::generate( 15 );
+                       $this->CSPNonce = base64_encode( $rand );
+               }
+               return $this->CSPNonce;
+       }
 }