Completed
Push — master ( c9dc8f...993e26 )
by Josh
16:11
created

DisallowUnsafeDynamicURL::checkElementNode()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.8666
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 4.3731
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateChecks;
9
10
use DOMAttr;
11
use DOMElement;
12
use DOMText;
13
use s9e\TextFormatter\Configurator\Helpers\TemplateHelper;
14
use s9e\TextFormatter\Configurator\Items\Attribute;
15
use s9e\TextFormatter\Configurator\Items\Tag;
16
17
/**
18
* This primary use of this check is to ensure that dynamic content cannot be used to create
19
* javascript: links
20
*/
21
class DisallowUnsafeDynamicURL extends AbstractDynamicContentCheck
22
{
23
	/**
24
	* @var string Regexp used to exclude nodes that start with a hardcoded scheme part, a hardcoded
25
	*             local part, or a fragment
26
	*/
27
	protected $exceptionRegexp = '(^(?:(?!data|\\w*script)\\w+:|[^:]*/|#))i';
28
29
	/**
30
	* {@inheritdoc}
31
	*/
32 16
	protected function getNodes(DOMElement $template)
33
	{
34 16
		return TemplateHelper::getURLNodes($template->ownerDocument);
35
	}
36
37
	/**
38
	* {@inheritdoc}
39
	*/
40 8
	protected function isSafe(Attribute $attribute)
41
	{
42 8
		return $attribute->isSafeAsURL();
43
	}
44
45
	/**
46
	* {@inheritdoc}
47
	*/
48 8
	protected function checkAttributeNode(DOMAttr $attribute, Tag $tag)
49
	{
50
		// Ignore this attribute if its scheme is hardcoded or it starts with //
51 8
		if (!preg_match($this->exceptionRegexp, $attribute->value))
52
		{
53 8
			parent::checkAttributeNode($attribute, $tag);
54
		}
55
	}
56
57
	/**
58
	* {@inheritdoc}
59
	*/
60 6
	protected function checkElementNode(DOMElement $element, Tag $tag)
61
	{
62
		// Ignore this element if its scheme is hardcoded or it starts with //
63 6
		if ($element->firstChild
64 6
		 && $element->firstChild instanceof DOMText
65 6
		 && preg_match($this->exceptionRegexp, $element->firstChild->textContent))
66
		{
67
			return;
68
		}
69
70 6
		parent::checkElementNode($element, $tag);
71
	}
72
}