[!!!][SECURITY] Fix link spoofing in prefixLocalAnchors 22/35222/2
authorHelmut Hummel <helmut.hummel@typo3.org>
Wed, 10 Dec 2014 10:07:53 +0000 (11:07 +0100)
committerOliver Hader <oliver.hader@typo3.org>
Wed, 10 Dec 2014 10:07:54 +0000 (11:07 +0100)
Specially crafted request could lead to anchors prefixed
with URLs to domains controlled by the attacker on the
domain root page (home page). No other pages are affected!

Fix this by prefixing the anchors with a canonical URL
to the current request. This could lead to the situation
that the prefix does not match the current REQUEST_URI
which leads to a page reload instead of just "jumping" to the page section.

Additionally this change assures that REQUEST_URI always starts
with a slash, which mitigates similar attack vectors when using
getIndpEnv('REQUEST_URI')

To mitigate the impact of this breaking change, the REQUEST_URI
is used for anchor prefix if a backend user is logged in,
to not disturb the preview functionality of the home page.

In case prefixLocalAnchors is used in the HTML parser configuration
with prefixLocalAnchors = 2, always the canonical URL is used as prefix.

This change does *not* fix, that arbitrary (non functional) GET parameters
will be included in the generated prefix URL. To fix this it is recommended
to use absRefPrefix instead of baseUrl and prefixLocalAnchors.

Resolves: #62723
Releases: 4.5, 6.2, master
Security-Commit: 16003fd71982a9da3fde04c7cc298425d8b539dc
Security-Bulletin: TYPO3-CORE-SA-2014-003
Change-Id: I120f7a0fa32e48644c88d54d65863a6ac96acf4c
Reviewed-on: http://review.typo3.org/35222
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
t3lib/class.t3lib_div.php
t3lib/class.t3lib_parsehtml.php
typo3/sysext/cms/tslib/class.tslib_content.php
typo3/sysext/cms/tslib/class.tslib_fe.php

index 55966d6..2fc6813 100644 (file)
@@ -4013,7 +4013,7 @@ final class t3lib_div {
                                        $retVal = '/' . ltrim(self::getIndpEnv('SCRIPT_NAME'), '/') .
                                                        ($_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '');
                                } else {
-                                       $retVal = $_SERVER['REQUEST_URI'];
+                                       $retVal = '/' . ltrim($_SERVER['REQUEST_URI'], '/');
                                }
                                        // add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
                                if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
index a1fe56d..f001c26 100644 (file)
@@ -868,11 +868,14 @@ class t3lib_parsehtml {
                                                                                        }
                                                                                        if ($params['prefixLocalAnchors']) {
                                                                                                if (substr($tagAttrib[0][$attr], 0, 1) == '#') {
-                                                                                                       $prefix = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
-                                                                                                       $tagAttrib[0][$attr] = $prefix . $tagAttrib[0][$attr];
-                                                                                                       if ($params['prefixLocalAnchors'] == 2 && t3lib_div::isFirstPartOfStr($prefix, t3lib_div::getIndpEnv('TYPO3_SITE_URL'))) {
-                                                                                                               $tagAttrib[0][$attr] = substr($tagAttrib[0][$attr], strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL')));
+                                                                                                       if ($params['prefixLocalAnchors'] == 2) {
+                                                                                                               /** @var tslib_cObj $contentObjectRenderer */
+                                                                                                               $contentObjectRenderer = t3lib_div::makeInstance('tslib_cObj');
+                                                                                                               $prefix = $contentObjectRenderer->getUrlToCurrentLocation();
+                                                                                                       } else {
+                                                                                                               $prefix = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
                                                                                                        }
+                                                                                                       $tagAttrib[0][$attr] = $prefix . $tagAttrib[0][$attr];
                                                                                                }
                                                                                        }
                                                                                        if ($params['prefixRelPathWith']) {
index 7e36ec3..78d174a 100644 (file)
@@ -6110,6 +6110,29 @@ class tslib_cObj {
        }
 
        /**
+        * Returns the canonical URL to the current "location", which include the current page ID and type
+        * and optionally the query string
+        *
+        * @param bool $addQueryString Whether additional GET arguments in the query string should be included or not
+        * @return string
+        */
+       public function getUrlToCurrentLocation($addQueryString = TRUE) {
+               $conf = array();
+               $conf['parameter'] = $GLOBALS['TSFE']->id . ',' . $GLOBALS['TSFE']->type;
+               if ($addQueryString) {
+                       $conf['addQueryString'] = '1';
+                       $linkVars = implode(',', array_keys(t3lib_div::explodeUrl2Array($GLOBALS['TSFE']->linkVars)));
+                       $conf['addQueryString.'] = array(
+                               'method' => 'GET',
+                               'exclude' => 'id,type,cHash' . ($linkVars ? ',' . $linkVars : '')
+                       );
+                       $conf['useCacheHash'] = t3lib_div::_GET('cHash') ? '1' : '0';
+               }
+
+               return $this->typoLink_URL($conf);
+       }
+
+       /**
         * Returns the URL of a "typolink" create from the input parameter string, url-parameters and target
         *
         * @param string $params Link parameter; eg. "123" for page id, "kasperYYYY@typo3.com" for email address, "http://...." for URL, "fileadmin/blabla.txt" for file.
index daf9a18..38c0809 100644 (file)
@@ -4404,7 +4404,14 @@ if (version == "n3") {
         * @return      void            Works directly on $this->content
         */
        function prefixLocalAnchorsWithScript() {
-               $scriptPath = $GLOBALS['TSFE']->absRefPrefix . substr(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'),strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL')));
+               if (!$this->beUserLogin) {
+                       $scriptPath = $this->cObj->getUrlToCurrentLocation();
+               } else {
+                       // To break less existing sites, we allow the REQUEST_URI to be used for the prefix
+                       $scriptPath = t3lib_div::getIndpEnv('REQUEST_URI');
+                       // Disable the cache so that these URI will not be the ones to be cached
+                       $this->disableCache();
+               }
                $originalContent = $this->content;
                $this->content = preg_replace('/(<(?:a|area).*?href=")(#[^"]*")/i', '${1}' . htmlspecialchars($scriptPath) . '${2}', $originalContent);
                        // There was an error in the call to preg_replace, so keep the original content (behavior prior to PHP 5.2)