Issues (41)

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

Labels
Severity

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
namespace Mpociot\ApiDoc\Tools;
4
5
use Illuminate\Routing\Route;
6
use League\Flysystem\Adapter\Local;
7
use League\Flysystem\Filesystem;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\VarExporter\VarExporter;
11
12
class Utils
13
{
14
    public static function getFullUrl(Route $route, array $urlParameters = []): string
15
    {
16
        $uri = $route->uri();
17
18
        return self::replaceUrlParameterPlaceholdersWithValues($uri, $urlParameters);
19
    }
20
21
    /**
22
     * @param array|Route $routeOrAction
23
     *
24
     * @return array|null
25
     */
26
    public static function getRouteClassAndMethodNames($routeOrAction)
27
    {
28
        $action = $routeOrAction instanceof Route ? $routeOrAction->getAction() : $routeOrAction;
0 ignored issues
show
The class Illuminate\Routing\Route does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
29
30
        if ($action['uses'] !== null) {
31
            if (is_array($action['uses'])) {
32
                return $action['uses'];
33
            } elseif (is_string($action['uses'])) {
34
                return explode('@', $action['uses']);
35
            }
36
        }
37
        if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
38
            return [
39
                0 => $action[0],
40
                1 => $action[1],
41
            ];
42
        }
43
    }
44
45
    /**
46
     * Transform parameters in URLs into real values (/users/{user} -> /users/2).
47
     * Uses @urlParam values specified by caller, otherwise just uses '1'.
48
     *
49
     * @param string $uri
50
     * @param array $urlParameters Dictionary of url params and example values
51
     *
52
     * @return mixed
53
     */
54
    public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters)
55
    {
56
        $matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths);
57
        if (!$matches) {
58
            return $uri;
59
        }
60
61
        foreach ($parameterPaths[0] as $parameterPath) {
62
            $key = trim($parameterPath, '{?}');
63
            if (isset($urlParameters[$key])) {
64
                $example = $urlParameters[$key];
65
                $uri = str_replace($parameterPath, $example, $uri);
66
            }
67
        }
68
        // Remove unbound optional parameters with nothing
69
        $uri = preg_replace('#{([^/]+\?)}#', '', $uri);
70
        // Replace any unbound non-optional parameters with '1'
71
        $uri = preg_replace('#{([^/]+)}#', '1', $uri);
72
73
        return $uri;
74
    }
75
76
    public static function dumpException(\Exception $e)
77
    {
78
        if (class_exists(\NunoMaduro\Collision\Handler::class)) {
79
            $output = new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE);
80
            $handler = new \NunoMaduro\Collision\Handler(new \NunoMaduro\Collision\Writer($output));
81
            $handler->setInspector(new \Whoops\Exception\Inspector($e));
82
            $handler->setException($e);
83
            $handler->handle();
84
        } else {
85
            dump($e);
86
            echo "You can get better exception output by installing the library \nunomaduro/collision (PHP 7.1+ only).\n";
87
        }
88
    }
89
90
    public static function deleteDirectoryAndContents($dir)
91
    {
92
        $dir = ltrim($dir, '/');
93
        $adapter = new Local(realpath(__DIR__ . '/../../'));
94
        $fs = new Filesystem($adapter);
95
        $fs->deleteDir($dir);
96
    }
97
98
    /**
99
     * @param mixed $value
100
     * @param int $indentationLevel
101
     *
102
     * @return string
103
     * @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
104
     *
105
     */
106
    public static function printPhpValue($value, int $indentationLevel = 0): string
107
    {
108
        $output = VarExporter::export($value);
109
        // Padding with x spaces so they align
110
        $split = explode("\n", $output);
111
        $result = '';
112
        $padWith = str_repeat(' ', $indentationLevel);
113
        foreach ($split as $index => $line) {
114
            $result .= ($index == 0 ? '' : "\n$padWith") . $line;
115
        }
116
117
        return $result;
118
    }
119
120
    public static function printQueryParamsAsString(array $cleanQueryParams): string
121
    {
122
        $qs = '';
123
        foreach ($cleanQueryParams as $parameter => $value) {
124
            $paramName = urlencode($parameter);
125
126
            if (!is_array($value)) {
127
                $qs .= "$paramName=" . urlencode($value) . "&";
128
            } else {
129
                if (array_keys($value)[0] === 0) {
130
                    // List query param (eg filter[]=haha should become "filter[]": "haha")
131
                    $qs .= "$paramName" . '[]=' . urlencode($value[0]) . '&';
132
                } else {
133
                    // Hash query param (eg filter[name]=john should become "filter[name]": "john")
134
                    foreach ($value as $item => $itemValue) {
135
                        $qs .= "$paramName" . '[' . urlencode($item) . ']=' . urlencode($itemValue) . '&';
136
                    }
137
                }
138
            }
139
        }
140
141
        return rtrim($qs, '&');
142
    }
143
144
    public static function printQueryParamsAsKeyValue(
145
        array $cleanQueryParams,
146
        string $quote = "\"",
147
        string $delimiter = ":",
148
        int $spacesIndentation = 4,
149
        string $braces = "{}",
150
        int $closingBraceIndentation = 0
151
    ): string {
152
        $output = "{$braces[0]}\n";
153
        foreach ($cleanQueryParams as $parameter => $value) {
154
            if (!is_array($value)) {
155
                $output .= str_repeat(" ", $spacesIndentation);
156
                $output .= "$quote$parameter$quote$delimiter $quote$value$quote,\n";
157
            } else {
158
                if (array_keys($value)[0] === 0) {
159
                    // List query param (eg filter[]=haha should become "filter[]": "haha")
160
                    $output .= str_repeat(" ", $spacesIndentation);
161
                    $output .= "$quote$parameter" . "[]$quote$delimiter $quote$value[0]$quote,\n";
162
                } else {
163
                    // Hash query param (eg filter[name]=john should become "filter[name]": "john")
164
                    foreach ($value as $item => $itemValue) {
165
                        $output .= str_repeat(" ", $spacesIndentation);
166
                        $output .= "$quote$parameter" . "[$item]$quote$delimiter $quote$itemValue$quote,\n";
167
                    }
168
                }
169
            }
170
        }
171
172
        return $output . str_repeat(" ", $closingBraceIndentation) . "{$braces[1]}";
173
    }
174
}
175