Passed
Push — master ( f4b1e7...c9beea )
by Tim
02:06
created

XMLSecurityDSig::validateReference()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

60
        if (!$docElem->isSameNode(/** @scrutinizer ignore-type */ $sigNode)) {
Loading history...
61
            if ($sigNode->parentNode !== null) {
62
                $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

62
                $sigNode->parentNode->removeChild(/** @scrutinizer ignore-type */ $sigNode);
Loading history...
63
            }
64
        }
65
        $xpath = $this->getXPathObj();
66
        $query = "./secdsig:SignedInfo[1]/secdsig:Reference";
67
        $nodeset = $xpath->query($query, $sigNode);
68
        if ($nodeset->length < 1) {
69
            throw new Exception("Reference nodes not found");
70
        }
71
72
        /* Initialize/reset the list of validated nodes. */
73
        $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...
74
75
        foreach ($nodeset as $refNode) {
76
            if (!$this->processRefNode($refNode)) {
77
                /* Clear the list of validated nodes. */
78
                $this->validatedNodes = null;
79
                throw new Exception("Reference validation failed");
80
            }
81
        }
82
        return true;
83
    }
84
85
86
    /**
87
     * Returns the XPathObj or null if xPathCtx is set and sigNode is empty.
88
     *
89
     * @return DOMXPath|null
90
     */
91
    private function getXPathObj()
92
    {
93
        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...
94
            $xpath = new DOMXPath($this->sigNode->ownerDocument);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\DOMXPath was not found. Did you mean DOMXPath? If so, make sure to prefix the type with \.
Loading history...
95
            $xpath->registerNamespace('secdsig', self::XMLDSIGNS);
96
            $this->xPathCtx = $xpath;
97
        }
98
        return $this->xPathCtx;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xPathCtx also could return the type DOMXPath which is incompatible with the documented return type SimpleSAML\XMLSecurity\DOMXPath|null.
Loading history...
99
    }
100
}
101