Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Safely check for is_site_url before parsing a shortcode #2007

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion code/Model/SiteTreeLinkTracking_Parser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace SilverStripe\CMS\Model;

use DOMElement;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\Parsers\HTMLValue;
Expand Down Expand Up @@ -33,12 +34,16 @@ public function process(HTMLValue $htmlValue)
return $results;
}

/** @var DOMElement $link */
foreach ($links as $link) {
if (!$link->hasAttribute('href')) {
continue;
}

$href = Director::makeRelative($link->getAttribute('href'));
$href = $link->getAttribute('href');
if (Director::is_site_url($href)) {
$href = Director::makeRelative($href);
}

// Definitely broken links.
if ($href == '' || $href[0] == '/') {
Expand Down
18 changes: 18 additions & 0 deletions tests/php/Model/SiteTreeLinkTrackingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

use SilverStripe\CMS\Model\SiteTreeLinkTracking_Parser;
use SilverStripe\Assets\File;
use SilverStripe\Control\Director;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\View\Parsers\HTMLValue;
use Page;

class SiteTreeLinkTrackingTest extends SapphireTest
{
protected function setUp()
{
parent::setUp();
Director::config()->set('alternate_base_url', 'http://www.mysite.com/');
}

protected function isBroken($content)
{
Expand All @@ -25,12 +31,24 @@ protected function isBroken($content)

public function testParser()
{
// Shortcodes
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]">link</a>'));
$this->assertTrue($this->isBroken('<a href="[sitetree_link,id=123]#no-such-anchor">link</a>'));
$this->assertTrue($this->isBroken('<a href="[file_link,id=123]">link</a>'));

// Relative urls
$this->assertTrue($this->isBroken('<a href="">link</a>'));
$this->assertTrue($this->isBroken('<a href="/">link</a>'));

// Non-shortcodes, assume non-broken without due reason
$this->assertFalse($this->isBroken('<a href="/some-page">link</a>'));
$this->assertFalse($this->isBroken('<a href="some-page">link</a>'));

// Absolute urls
$this->assertFalse($this->isBroken('<a href="http://www.mysite.com/some-page">link</a>'));
$this->assertFalse($this->isBroken('<a href="http://www.google.com/some-page">link</a>'));

// Anchors
$this->assertFalse($this->isBroken('<a name="anchor">anchor</a>'));
$this->assertFalse($this->isBroken('<a id="anchor">anchor</a>'));
$this->assertTrue($this->isBroken('<a href="##anchor">anchor</a>'));
Expand Down