-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5434 from open-sausages/pulls/4.0/fix-htmleditorf…
…ield-whitelist [ss-2015-027] FIX HtmlEditorField_Toolbar#viewfile not whitelisting URLs
- Loading branch information
Showing
3 changed files
with
220 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
class HtmlEditorFieldToolbarTest_Toolbar extends HtmlEditorField_Toolbar { | ||
public function viewfile_getLocalFileByID($id) { | ||
return parent::viewfile_getLocalFileByID($id); | ||
} | ||
|
||
public function viewfile_getRemoteFileByURL($fileUrl) { | ||
return parent::viewfile_getRemoteFileByURL($fileUrl); | ||
} | ||
} | ||
|
||
class HtmlEditorFieldToolbarTest extends SapphireTest { | ||
|
||
protected static $fixture_file = 'HtmlEditorFieldToolbarTest.yml'; | ||
|
||
/** | ||
* @return HtmlEditorFieldToolbarTest_Toolbar | ||
*/ | ||
protected function getToolbar() { | ||
return new HtmlEditorFieldToolbarTest_Toolbar(null, '/'); | ||
} | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
Config::inst()->update('HtmlEditorField_Toolbar', 'fileurl_scheme_whitelist', array('http')); | ||
Config::inst()->update('HtmlEditorField_Toolbar', 'fileurl_domain_whitelist', array('example.com')); | ||
|
||
// Filesystem mock | ||
AssetStoreTest_SpyStore::activate(__CLASS__); | ||
|
||
// Load up files | ||
/** @var File $file1 */ | ||
$file1 = $this->objFromFixture('File', 'example_file'); | ||
$file1->setFromString(str_repeat('x', 1000), $file1->Name); | ||
$file1->write(); | ||
|
||
/** @var Image $image1 */ | ||
$image1 = $this->objFromFixture('Image', 'example_image'); | ||
$image1->setFromLocalFile( | ||
__DIR__ . '/images/HTMLEditorFieldTest-example.jpg', | ||
'folder/subfolder/HTMLEditorFieldTest_example.jpg' | ||
); | ||
$image1->write(); | ||
} | ||
|
||
public function testValidLocalReference() { | ||
/** @var File $exampleFile */ | ||
$exampleFile = $this->objFromFixture('File', 'example_file'); | ||
$expectedUrl = $exampleFile->AbsoluteLink(); | ||
Config::inst()->update('HtmlEditorField_Toolbar', 'fileurl_domain_whitelist', array( | ||
'example.com', | ||
strtolower(parse_url($expectedUrl, PHP_URL_HOST)) | ||
)); | ||
|
||
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL($exampleFile->AbsoluteLink()); | ||
$this->assertEquals($expectedUrl, $url); | ||
} | ||
|
||
public function testValidScheme() { | ||
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf'); | ||
$this->assertEquals($url, 'http://example.com/test.pdf'); | ||
} | ||
|
||
/** @expectedException SS_HTTPResponse_Exception */ | ||
public function testInvalidScheme() { | ||
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('nosuchscheme://example.com/test.pdf'); | ||
} | ||
|
||
public function testValidDomain() { | ||
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://example.com/test.pdf'); | ||
$this->assertEquals($url, 'http://example.com/test.pdf'); | ||
} | ||
|
||
/** @expectedException SS_HTTPResponse_Exception */ | ||
public function testInvalidDomain() { | ||
list($file, $url) = $this->getToolbar()->viewfile_getRemoteFileByURL('http://evil.com/test.pdf'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Folder: | ||
folder1: | ||
Name: folder | ||
Title: folder | ||
folder2: | ||
Name: subfolder | ||
Title: subfolder | ||
Parent: =>Folder.folder1 | ||
|
||
File: | ||
example_file: | ||
Name: example.pdf | ||
Parent: =>Folder.folder2 | ||
|
||
Image: | ||
example_image: | ||
Name: HTMLEditorFieldTest_example.jpg | ||
Parent: =>Folder.folder2 |