Issues (2)

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/CSS.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
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.07.16 at 18:19
5
 */
6
namespace samsonphp\css;
7
8
use samson\core\ExternalModule;
9
use samsonphp\compressor\Compressor;
10
use samsonphp\event\Event;
11
use samsonphp\resource\exception\ResourceNotFound;
12
use samsonphp\resource\ResourceValidator;
13
use samsonphp\resource\Router;
14
use samsonphp\resource\ResourceManager;
15
16
/**
17
 * CSS assets handling class
18
 *
19
 * @author Vitaly Iegorov <[email protected]>
20
 * @package samsonphp\resource
21
 * TODO: Remove ResourceValidator as it is unnecessary
22
 */
23
class CSS extends ExternalModule
24
{
25
    /** @var string Module identifer */
26
    protected $id = 'resource_css';
27
28
    /** Pattern for matching CSS url */
29
    const P_URL = '/url\s*\(\s*(\'|\")?([^\)\s\'\"]+)(\'|\")?\s*\)/i';
30
31
    /** Event for firing before handling CSS resource */
32
    const E_BEFORE_HANDLER = 'samsonphp.css.before_handle';
33
34
    /** Event for firing after handling CSS resource */
35
    const E_AFTER_HANDLER = 'samsonphp.css.after_handle';
36
37
    /** @var string Path to current resource file */
38
    protected $currentResource;
39
40 7
    /** Module preparation stage handler */
41
    public function prepare(array $params = [])
42
    {
43 7
        // Subscribe for CSS handling
44
        Event::subscribe(Router::E_RESOURCE_COMPILE, [$this, 'compile']);
45 7
        
46
        Event::subscribe(Compressor::E_RESOURCE_COMPRESS, [$this, 'deCompile']);
47
48
        return parent::prepare($params);
0 ignored issues
show
The call to ExternalModule::prepare() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
    }
50
51
    /**
52
     * LESS resource compiler.
53
     *
54
     * @param string $resource  Resource full path
55 7
     * @param string $extension Resource extension
56
     * @param string $content   Compiled output resource content
57 7
     */
58 7
    public function compile($resource, $extension, &$content)
59
    {
60
        if (in_array($extension, [ResourceManager::T_CSS, ResourceManager::T_LESS, ResourceManager::T_SASS, ResourceManager::T_SCSS])) {
61 7
            $this->currentResource = $resource;
62
63
            // Fire event
64 7
            Event::fire(self::E_BEFORE_HANDLER, [&$content, $resource]);
65
66
            // Rewrite Urls
67 6
            $content = preg_replace_callback(self::P_URL, [$this, 'rewriteUrls'], $content);
68 6
69 6
            // Fire event
70
            Event::fire(self::E_AFTER_HANDLER, [&$content, $resource]);
71
        }
72
    }
73
    
74
    public function deCompile($extension, &$content)
75
    {
76
        if (in_array($extension, [ResourceManager::T_CSS, ResourceManager::T_LESS, ResourceManager::T_SASS, ResourceManager::T_SCSS])) {
77
            if (preg_match_all('/url\s*\(\s*(\'|\")*(?<url>[^\'\"\)]+)\s*(\'|\")*\)/i', $content, $matches)) {
78
                if (isset($matches['url'])) {
79 7
                    foreach ($matches['url'] as $url) {
0 ignored issues
show
The expression $matches['url'] of type string|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
                        if (preg_match('/' . STATIC_RESOURCE_HANDLER . '\/\?p=(((\/src\/|\/vendor\/samson[^\/]+\/)(?<module>[^\/]+)(?<path>.+))|((?<local>.+)))/ui', $url, $matches)) {
81
                            if (array_key_exists('local', $matches)) {
82 7
                                $module = 'local';
83
                                $path = $matches['local'];
84
                            } else {
85 7
                                $module = $matches['module'];
86 7
                                $path = $matches['path'];
87 7
                            }
88
                            // Always remove first public path /www/
89
                            $path = ltrim(str_replace(__SAMSON_PUBLIC_PATH, '', $path), '/');
90 7
                            // Replace url in file
91
                            $content = str_replace($url, url()->base() . ($module == 'local' ? '' : $module . '/www/') . $path, $content);
92 4
                        }
93
                    }
94
                }
95
            }
96 4
        }
97 4
    }
98 1
99
    /**
100
     * Callback for CSS url(...) rewriting.
101
     *
102 3
     * @param array $matches Regular expression matches collection
103
     *
104
     * @return string Rewritten url(..) with static resource handler url
105 3
     * @throws ResourceNotFound
106
     */
107
    public function rewriteUrls($matches)
108
    {
109
        // Store static resource path
110
        $url = $matches[2];
111
112
        // Validate url for restricted protocols and inline images
113
        $validation  = array_filter(['data/', 'data:', 'http:', 'https:'], function ($item) use ($url) {
114
            return strpos($url, $item) !== false;
115
        });
116 4
117
        // Ignore inline resources
118
        if (!count($validation)) {
119 4
            // Remove possible GET, HASH parameters from resource path
120 3
            $url = $this->getOnlyUrl($this->getOnlyUrl($url, '#'), '?');
121
122
            // Try to find resource and output full error
123 4
            try {
124
                $path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource));
125
            } catch (ResourceNotFound $e) {
126
                throw new ResourceNotFound('Cannot find resource "' . $url . '" in "' . $this->currentResource . '"');
127
            }
128
129
            // Build path to static resource handler
130
            return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")';
131
        }
132
133
        return $matches[0];
134
    }
135
136
    /**
137
     * Get only path or URL before marker.
138
     *
139
     * @param string $path   Full URL with possible unneeded data
140
     * @param string $marker Marker for separation
141
     *
142
     * @return string Filtered asset URL
143
     */
144
    protected function getOnlyUrl($path, $marker)
145
    {
146
        // Remove possible GET parameters from resource path
147
        if (($getStart = strpos($path, $marker)) !== false) {
148
            return substr($path, 0, $getStart);
149
        }
150
151
        return $path;
152
    }
153
}
154