Issues (8)

Security Analysis    not enabled

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/HandlebarsEnvironment.php (1 issue)

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
 * @author @jenschude <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle;
8
9
10
use JaySDe\HandlebarsBundle\Cache\Filesystem;
11
use JaySDe\HandlebarsBundle\Error\LoaderException;
12
use JaySDe\HandlebarsBundle\Loader\FilesystemLoader;
13
use LightnCandy\LightnCandy;
14
use Symfony\Component\Config\Resource\FileResource;
15
16
class HandlebarsEnvironment
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $options;
22
23
    /**
24
     * @var Filesystem
25
     */
26
    protected $cache;
27
28
    /**
29
     * @var FilesystemLoader
30
     */
31
    protected $loader;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $autoReload;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $debug;
42
43
    /**
44
     * @var HandlebarsProfileExtension
45
     */
46
    private $profiler;
47
48
    /**
49
     * @var HandlebarsHelperServiceInterface
50
     */
51
    private $helper;
52
53
    /**
54
     * @var \ArrayObject
55
     */
56
    private $partials;
57
58 14
    public function __construct(
59
        FilesystemLoader $loader,
60
        HandlebarsHelperServiceInterface $helper,
61
        $options = [],
62
        Filesystem $cache,
63
        HandlebarsProfileExtension $profiler
64
    )
65
    {
66 14
        $this->loader = $loader;
67 14
        $this->partials = $partials = new \ArrayObject();
68 14
        $this->helper = $helper;
69
70 14
        $this->options = array_merge([
71 14
            'auto_reload' => null,
72
            'debug' => true,
73 14
            'helpers' => $this->helper->getHelperMethods(),
74 14
            'partialresolver' => function($cx, $name) use ($loader, &$partials) {
75 4
                $extension = $this->getTemplateExtension($loader, $name);
76 3
                if ($extension === false) {
77
                    return null;
78
                }
79 3
                $partials[] = new FileResource($loader->getCacheKey($name.$extension));
80 3
                return $loader->getSource($name.$extension);
81 14
            },
82 14
        ], $options);
83 14
        $this->debug = (bool) $this->options['debug'];
84 14
        $this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload'];
85 14
        $this->cache = $cache;
86 14
        $this->profiler = $profiler;
87 14
    }
88
89 4
    private function getTemplateExtension(FilesystemLoader $loader, $name) {
90 4
        if ($loader->exists($name.'.handlebars')) {
91 3
            return '.handlebars';
92
        }
93 3
        if ($loader->exists($name.'.hbs')) {
94 3
            return '.hbs';
95
        }
96
        return false;
97
    }
98
99 9
    public function compile($name)
100
    {
101 9
        $source = $this->getLoader()->getSource($name);
102 9
        $cacheKey = $this->getCacheFilename($name);
103
104 9
        $phpStr = '';
105
        try {
106 9
            $this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]);
107 9
            $phpStr = LightnCandy::compile($source, $this->options);
108 1
        } catch (\Exception $e) {
109 1
            throw new LoaderException($e->getMessage());
110
        }
111 8
        $this->cache->write($cacheKey, '<?php // '.$name.PHP_EOL.$phpStr, $this->partials->getArrayCopy());
112
113 8
        return $phpStr;
114
    }
115
116 4
    public function render($name, array $context = [])
117
    {
118 4
        $renderer = $this->loadTemplate($name);
119
120 4
        $templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name);
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Profiler_Profile has been deprecated with message: since Twig 2.7, use "Twig\Profiler\Profile" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
121 4
        $this->profiler->enter($templateProfile);
122 4
        $html = $renderer($context, ['helpers' => $this->helper->getHelpers()]);
123 4
        $this->profiler->leave($templateProfile);
124
125 4
        return $html;
126
    }
127
128 14
    public function getCacheFilename($name)
129
    {
130 14
        $key = $this->cache->generateKey($name);
131
132 14
        return !$key ? false : $key;
133
    }
134
135 9
    public function getLoader()
136
    {
137 9
        return $this->loader;
138
    }
139
140
    /**
141
     * @param $templateName
142
     * @return callable
143
     */
144 9
    public function loadTemplate($templateName)
145
    {
146 9
        $name = (string) $templateName;
147 9
        $cacheKey = $this->getCacheFilename($name);
148
149 9
        if (!file_exists($cacheKey) || $this->isAutoReload() && !$this->cache->isFresh($cacheKey)) {
150 8
            $this->compile($name);
151
        }
152
153 9
        return $this->cache->load($cacheKey);
154
    }
155
156
    /**
157
     * Checks if the auto_reload option is enabled.
158
     *
159
     * @return bool true if auto_reload is enabled, false otherwise
160
     */
161 1
    public function isAutoReload()
162
    {
163 1
        return $this->autoReload;
164
    }
165
}
166