Issues (7)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/NodeVisitor/TemplatePathResolver.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the puli/twig-puli-extension package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\TwigExtension\NodeVisitor;
13
14
use Puli\Repository\Api\ResourceRepository;
15
use Puli\TwigExtension\PuliExtension;
16
use Puli\UrlGenerator\Api\UrlGenerator;
17
use RuntimeException;
18
use Twig_Node;
19
use Twig_Node_Expression_Constant;
20
use Twig_Node_Expression_Function;
21
use Twig_Node_Import;
22
use Twig_Node_Include;
23
use Twig_Node_Module;
24
25
/**
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class TemplatePathResolver extends AbstractPathResolver
31
{
32
    /**
33
     * @var UrlGenerator
34
     */
35
    private $urlGenerator;
36
37
    /**
38
     * @var bool
39
     */
40
    private $checkPaths;
41
42 23
    public function __construct(ResourceRepository $repo, UrlGenerator $urlGenerator = null, $checkPaths = false)
43
    {
44 23
        parent::__construct($repo);
45
46 23
        $this->urlGenerator = $urlGenerator;
47 23
        $this->checkPaths = $checkPaths;
48 23
    }
49
50
    /**
51
     * Returns the priority for this visitor.
52
     *
53
     * Priority should be between -10 and 10 (0 is the default).
54
     *
55
     * @return int The priority level
56
     */
57 23
    public function getPriority()
58
    {
59 23
        return PuliExtension::RESOLVE_PATHS;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 19
    protected function processNode(Twig_Node $node)
66
    {
67 19
        if ($node instanceof Twig_Node_Module) {
68 19
            return $this->processModuleNode($node);
69
        }
70
71 19
        if ($node instanceof Twig_Node_Include) {
72 6
            return $this->processIncludeNode($node);
73
        }
74
75 19
        if ($node instanceof Twig_Node_Import) {
76 2
            return $this->processImportNode($node);
77
        }
78
79 19
        if ($node instanceof Twig_Node_Expression_Function && 'resource_url' === $node->getAttribute('name')) {
80 3
            return $this->processResourceUrlFunction($node);
81
        }
82
83 19
        return null;
84
    }
85
86 19
    private function processModuleNode(Twig_Node_Module $node)
87
    {
88
        // Resolve relative parent template paths to absolute paths
89 19
        $parentNode = $node->hasNode('parent') ? $node->getNode('parent') : null;
90 19
        $traitsNode = $node->getNode('traits');
91
92
        // If the template extends another template, resolve the path
93 19
        if ($parentNode instanceof Twig_Node_Expression_Constant) {
94 5
            $this->processConstantNode($parentNode, $this->checkPaths);
95
        }
96
97
        // Resolve paths of embedded templates
98 19
        foreach ($node->getAttribute('embedded_templates') as $embeddedNode) {
99 2
            $this->processEmbeddedTemplateNode($embeddedNode);
100
        }
101
102
        // Resolve paths of used templates
103 19
        foreach ($traitsNode as $traitNode) {
104 2
            $this->processTraitNode($traitNode);
105
        }
106
107 19
        return null;
108
    }
109
110 2
    private function processEmbeddedTemplateNode(Twig_Node_Module $embeddedNode)
111
    {
112 2
        $embedParent = $embeddedNode->hasNode('parent') ? $embeddedNode->getNode('parent') : null;
113
114
        // If the template extends another template, resolve the path
115 2
        if ($embedParent instanceof Twig_Node_Expression_Constant) {
116 2
            $this->processConstantNode($embedParent, $this->checkPaths);
117
        }
118 2
    }
119
120 2
    private function processTraitNode(Twig_Node $traitNode)
121
    {
122 2
        $usedTemplate = $traitNode->getNode('template');
123
124
        // If the template extends another template, resolve the path
125 2
        if ($usedTemplate instanceof Twig_Node_Expression_Constant) {
126 2
            $this->processConstantNode($usedTemplate, $this->checkPaths);
127
        }
128 2
    }
129
130 6 View Code Duplication
    private function processIncludeNode(Twig_Node_Include $node)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132 6
        $exprNode = $node->getNode('expr');
133
134 6
        if ($exprNode instanceof Twig_Node_Expression_Constant) {
135 6
            $this->processConstantNode($exprNode, $this->checkPaths);
136
        }
137
138 6
        return null;
139
    }
140
141 2 View Code Duplication
    private function processImportNode(Twig_Node_Import $node)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143 2
        $exprNode = $node->getNode('expr');
144
145 2
        if ($exprNode instanceof Twig_Node_Expression_Constant) {
146 2
            $this->processConstantNode($exprNode, $this->checkPaths);
147
        }
148
149 2
        return null;
150
    }
151
152 3
    protected function processResourceUrlFunction(Twig_Node $node)
153
    {
154 3
        if (!$this->urlGenerator) {
155
            throw new RuntimeException(
156
                'The resource_url() function is only available if puli/url-generator is installed and an instance of'.
157
                '"Puli\UrlGenerator\Api\UrlGenerator" is passed to the TemplatePathResolver.'
158
            );
159
        }
160
161 3
        $argsNode = $node->getNode('arguments');
162
163 3
        if (!$argsNode->hasNode(0)) {
164
            return null;
165
        }
166
167 3
        $exprNode = $argsNode->getNode(0);
168
169 3
        if (!$exprNode instanceof Twig_Node_Expression_Constant) {
170
            return null;
171
        }
172
173
        // The paths passed to the resource_url() function must always be Puli
174
        // paths, so disable path checking
175 3
        $this->processConstantNode($exprNode, false);
176
177
        // Optimize away function call
178 3
        $exprNode->setAttribute('value', $this->urlGenerator->generateUrl($exprNode->getAttribute('value')));
179
180 3
        return $exprNode;
181
    }
182
183 16
    private function processConstantNode(Twig_Node_Expression_Constant $node, $checkPath)
184
    {
185 16
        $node->setAttribute('value', $this->resolvePath($node->getAttribute('value'), $checkPath));
186 16
    }
187
}
188