Skip to content

Commit

Permalink
Sanitise filenames for error pages
Browse files Browse the repository at this point in the history
fixes #299
  • Loading branch information
wernerkrauss committed Sep 7, 2017
1 parent adfa725 commit 2da5828
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
9 changes: 6 additions & 3 deletions code/extensions/ErrorPageSubsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Subsites\Extensions;

use SilverStripe\Assets\FileNameFilter;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataExtension;
Expand Down Expand Up @@ -52,11 +53,13 @@ public function updateErrorFilename(&$name, &$statusCode)
&& $locale
&& $locale != Translatable::default_locale()
) {
$filepath = $static_filepath . "/error-{$statusCode}-{$locale}{$subdomainPart}.html";
$fileName = "error-{$statusCode}-{$locale}{$subdomainPart}.html";
} else {
$filepath = $static_filepath . "/error-{$statusCode}{$subdomainPart}.html";
$fileName= "error-{$statusCode}{$subdomainPart}.html";
}

$name = $filepath;
$fileName = FileNameFilter::create()->filter($fileName);

$name = implode('/', [$static_filepath, $fileName]);
}
}
1 change: 1 addition & 0 deletions tests/php/FileSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testSubsitesFolderDropdown()
'Test 3',
'Test Non-SSL',
'Test SSL',
'Test Vagrant VM on port 8080'
], array_values($source));
}
}
25 changes: 19 additions & 6 deletions tests/php/SiteTreeSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace SilverStripe\Subsites\Tests;

use Page;
use SilverStripe\Assets\FileNameFilter;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\ErrorPage\ErrorPage;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\ErrorPage\ErrorPage;
use SilverStripe\Forms\FieldList;
use SilverStripe\Security\Member;
use SilverStripe\SiteConfig\SiteConfig;
Expand Down Expand Up @@ -80,15 +81,27 @@ public function testBasicSanity()
$this->assertTrue(is_array(singleton(SiteTreeSubsites::class)->extraStatics()));
}

public function testErrorPageLocations()
public function errorPageLocationsProvider()
{
$subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1');
return [
['domaintest1', '/error-500-one.example.org.html'],
['domaintestVagrant', '/error-500-localhost8080.html']
];
}

Subsite::changeSubsite($subsite1->ID);
/**
* @dataProvider errorPageLocationsProvider
*/
public function testErrorPageLocations($subsiteFixtureName, $expectedFilename)
{
$static_path = Config::inst()->get(ErrorPage::class, 'static_filepath');

$subsite = $this->objFromFixture(Subsite::class, $subsiteFixtureName);
$expected_path = $static_path . $expectedFilename;

Subsite::changeSubsite($subsite->ID);
$path = TestErrorPage::get_error_filename_spy(500);

$static_path = Config::inst()->get(ErrorPage::class, 'static_filepath');
$expected_path = $static_path . '/error-500-' . $subsite1->domain() . '.html';
$this->assertEquals($expected_path, $path);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/php/SubsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ public function testAllSites()
['Title' => 'Test 2'],
['Title' => 'Test 3'],
['Title' => 'Test Non-SSL'],
['Title' => 'Test SSL']
['Title' => 'Test SSL'],
['Title' => 'Test Vagrant VM on port 8080']
], $subsites, 'Lists all subsites');
}

Expand Down Expand Up @@ -396,7 +397,8 @@ public function testAccessibleSites()
'Test 2',
'Test 3',
'Test Non-SSL',
'Test SSL'
'Test SSL',
'Test Vagrant VM on port 8080'
], array_values($adminSiteTitles));

$member2Sites = Subsite::accessible_sites(
Expand Down
7 changes: 7 additions & 0 deletions tests/php/SubsiteTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ SilverStripe\Subsites\Model\Subsite:
Title: 'Test SSL'
domaintest5:
Title: 'Test Non-SSL'
domaintestVagrant:
Title: 'Test Vagrant VM on port 8080'
SilverStripe\Subsites\Model\SubsiteDomain:
subsite1:
SubsiteID: =>SilverStripe\Subsites\Model\Subsite.subsite1
Expand Down Expand Up @@ -61,6 +63,11 @@ SilverStripe\Subsites\Model\SubsiteDomain:
Domain: www.tertiary.com
Protocol: http
IsPrimary: 1
dtVagrant:
SubsiteID: =>SilverStripe\Subsites\Model\Subsite.domaintestVagrant
Domain: localhost:8080
Protocol: http
IsPrimary: 1
Page:
mainSubsitePage:
Title: 'MainSubsitePage'
Expand Down

0 comments on commit 2da5828

Please sign in to comment.