FIX Multibyte URL routing

Was double url encoding (once in database value, then again in request)
Fixes https://github.com/silverstripe/silverstripe-framework/issues/8723
This commit is contained in:
Ingo Schommer 2019-01-14 09:17:06 +13:00
parent 287f17b848
commit c61ba88d82
3 changed files with 49 additions and 2 deletions

View File

@ -25,6 +25,7 @@ use SilverStripe\Security\Security;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\View\Parsers\URLSegmentFilter;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer; use SilverStripe\View\SSViewer;
use Translatable; use Translatable;
@ -195,11 +196,16 @@ class ContentController extends Controller
if (class_exists('Translatable')) { if (class_exists('Translatable')) {
Translatable::disable_locale_filter(); Translatable::disable_locale_filter();
} }
$filter = URLSegmentFilter::create();
// look for a page with this URLSegment // look for a page with this URLSegment
$child = SiteTree::get()->filter([ $child = SiteTree::get()->filter([
'ParentID' => $this->ID, 'ParentID' => $this->ID,
'URLSegment' => rawurlencode($action), // url encode unless it's multibyte (already pre-encoded in the database)
'URLSegment' => $filter->getAllowMultibyte() ? $action : rawurlencode($action),
])->first(); ])->first();
if (class_exists('Translatable')) { if (class_exists('Translatable')) {
Translatable::enable_locale_filter(); Translatable::enable_locale_filter();
} }

View File

@ -16,6 +16,7 @@ use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Debug; use SilverStripe\Dev\Debug;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\View\Parsers\URLSegmentFilter;
use Translatable; use Translatable;
/** /**
@ -127,8 +128,14 @@ class ModelAsController extends Controller implements NestedController
Translatable::disable_locale_filter(); Translatable::disable_locale_filter();
} }
// url encode unless it's multibyte (already pre-encoded in the database)
$filter = URLSegmentFilter::create();
if (!$filter->getAllowMultibyte()) {
$URLSegment = rawurlencode($URLSegment);
}
// Select child page // Select child page
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); $conditions = array('"SiteTree"."URLSegment"' => $URLSegment);
if (SiteTree::config()->get('nested_urls')) { if (SiteTree::config()->get('nested_urls')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0); $conditions[] = array('"SiteTree"."ParentID"' => 0);
} }

View File

@ -11,6 +11,7 @@ use SilverStripe\Control\Director;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use Page; use Page;
use SilverStripe\View\Parsers\URLSegmentFilter;
class ModelAsControllerTest extends FunctionalTest class ModelAsControllerTest extends FunctionalTest
{ {
@ -321,4 +322,37 @@ class ModelAsControllerTest extends FunctionalTest
'The page should not be found since its parent has not been published, in this case http://<yousitename>/root/sub-root or http://<yousitename>/sub-root' 'The page should not be found since its parent has not been published, in this case http://<yousitename>/root/sub-root or http://<yousitename>/sub-root'
); );
} }
public function testAllowMultibyte()
{
Config::modify()->set(URLSegmentFilter::class, 'default_allow_multibyte', true);
$parent = new Page();
$parent->Title = 'Multibyte test';
$parent->URLSegment = 'بلاگ';
$parent->write();
$parent->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$child = new Page();
$child->Title = 'Multibyte test';
$child->URLSegment = 'فضة';
$child->ParentID = $parent->ID;
$child->write();
$child->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
// Emulate browser behaviour around multibyte URL encodings
$response = $this->get(rawurlencode('بلاگ'));
$this->assertEquals(
$response->getStatusCode(),
200,
'Routes toplevel paths'
);
$response = $this->get(join('/', [rawurlencode('بلاگ'), rawurlencode('فضة')]));
$this->assertEquals(
$response->getStatusCode(),
200,
'Routes nested paths'
);
}
} }