Passed
Push — master ( 94f99b...a15ece )
by Tim
02:03
created

XMLSecurityDSig::getXPathObj()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace SimpleSAML\XMLSecurity;
4
5
use DOMXPath;
6
7
/**
8
 * xmlseclibs.php
9
 *
10
 * Copyright (c) 2007-2017, Robert Richards <[email protected]>.
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions
15
 * are met:
16
 *
17
 *   * Redistributions of source code must retain the above copyright
18
 *     notice, this list of conditions and the following disclaimer.
19
 *
20
 *   * Redistributions in binary form must reproduce the above copyright
21
 *     notice, this list of conditions and the following disclaimer in
22
 *     the documentation and/or other materials provided with the
23
 *     distribution.
24
 *
25
 *   * Neither the name of Robert Richards nor the names of his
26
 *     contributors may be used to endorse or promote products derived
27
 *     from this software without specific prior written permission.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40
 * POSSIBILITY OF SUCH DAMAGE.
41
 *
42
 * @copyright 2007-2017 Robert Richards <[email protected]>
43
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
44
 */
45
46
class XMLSecurityDSig extends \RobRichards\XMLSecLibs\XMLSecurityDSig
47
{
48
    /** @var string */
49
    private $prefix = '';
0 ignored issues
show
introduced by
The private property $prefix is not used, and could be removed.
Loading history...
50
51
52
    /**
53
     * @return bool
54
     * @throws Exception
55
     */
56
    public function validateReference()
57
    {
58
        $sigNode = $this->sigNode;
59
        $docElem = $sigNode->ownerDocument->documentElement;
60
61
        // enveloped signature, remove it
62
        if (!$docElem->isSameNode($sigNode)) {
0 ignored issues
show
Bug introduced by
It seems like $sigNode can also be of type null; however, parameter $otherNode of DOMNode::isSameNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        if (!$docElem->isSameNode(/** @scrutinizer ignore-type */ $sigNode)) {
Loading history...
63
            if ($sigNode->parentNode !== null) {
64
                $sigNode->parentNode->removeChild($sigNode);
0 ignored issues
show
Bug introduced by
It seems like $sigNode can also be of type null; however, parameter $child of DOMNode::removeChild() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                $sigNode->parentNode->removeChild(/** @scrutinizer ignore-type */ $sigNode);
Loading history...
65
            }
66
        }
67
        $xpath = $this->getXPathObj();
68
        $query = "./secdsig:SignedInfo[1]/secdsig:Reference";
69
        $nodeset = $xpath->query($query, $sigNode);
70
        if ($nodeset->length < 1) {
71
            throw new Exception("Reference nodes not found");
72
        }
73
74
        /* Initialize/reset the list of validated nodes. */
75
        $this->validatedNodes = [];
0 ignored issues
show
Bug introduced by
The property validatedNodes is declared private in RobRichards\XMLSecLibs\XMLSecurityDSig and cannot be accessed from this context.
Loading history...
76
77
        foreach ($nodeset as $refNode) {
78
            if (!$this->processRefNode($refNode)) {
79
                /* Clear the list of validated nodes. */
80
                $this->validatedNodes = null;
81
                throw new Exception("Reference validation failed");
82
            }
83
        }
84
        return true;
85
    }
86
87
88
    /**
89
     * Returns the XPathObj or null if xPathCtx is set and sigNode is empty.
90
     *
91
     * @return DOMXPath|null
92
     */
93
    private function getXPathObj()
94
    {
95
        if (empty($this->xPathCtx) && ! empty($this->sigNode)) {
0 ignored issues
show
Bug introduced by
The property xPathCtx is declared private in RobRichards\XMLSecLibs\XMLSecurityDSig and cannot be accessed from this context.
Loading history...
96
            $xpath = new DOMXPath($this->sigNode->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $this->sigNode->ownerDocument can also be of type null; however, parameter $document of DOMXPath::__construct() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            $xpath = new DOMXPath(/** @scrutinizer ignore-type */ $this->sigNode->ownerDocument);
Loading history...
97
            $xpath->registerNamespace('secdsig', self::XMLDSIGNS);
98
            $this->xPathCtx = $xpath;
99
        }
100
        return $this->xPathCtx;
101
    }
102
103
104
    /**
105
     * Reset the XPathObj to null
106
     */
107
    private function resetXPathObj()
0 ignored issues
show
Unused Code introduced by
The method resetXPathObj() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
108
    {
109
        $this->xPathCtx = null;
0 ignored issues
show
Bug introduced by
The property xPathCtx is declared private in RobRichards\XMLSecLibs\XMLSecurityDSig and cannot be accessed from this context.
Loading history...
110
    }
111
}
112