Issues (1)

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/Client.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
namespace xmarcos\Carbon;
3
4
use Exception;
5
use ErrorException;
6
use InvalidArgumentException;
7
8
class Client
9
{
10
    protected $stream;
11
    protected $namespace;
12
    protected $throw_exceptions;
13
14
    /**
15
     * Creates an instance of the Carbon Client
16
     *
17
     * @param resource $stream A php stream that knows how to talk to Carbon.
18
     */
19
    public function __construct($stream)
20
    {
21
        if (!is_resource($stream)) {
22
            throw new InvalidArgumentException('Stream must be a resource.');
23
        }
24
25
        $this->stream = $stream;
26
        $this->throwExceptions(false);
27
    }
28
29
    /**
30
     * Controls whether failed calls to Carbon will throw an Exception.
31
     *
32
     * @see send()
33
     *
34
     * @param boolean $throw
35
     *
36
     * @return self
37
     */
38
    public function throwExceptions($throw = true)
39
    {
40
        $this->throw_exceptions = (bool) $throw;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Sets the namespace used to prepend metric's paths
47
     *
48
     * @param string $namespace
49
     *
50
     * @return self
51
     */
52
    public function setNamespace($namespace)
53
    {
54
        $this->namespace = $this->sanitizePath($namespace);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Returns the current namespace.
61
     *
62
     * @return string
63
     */
64
    public function getNamespace()
65
    {
66
        return $this->namespace;
67
    }
68
69
    /**
70
     * Sends a metric to Carbon.
71
     *
72
     * @see http://graphite.readthedocs.org/en/latest/feeding-carbon.html
73
     *
74
     * @param string    $path      Metric Path
75
     * @param int|float $value     Metric Value
76
     * @param int|null  $timestamp Metric Timestamp
77
     *
78
     * @throws ErrorException If $this->throw_exceptions is true
79
     * @return bool
80
     */
81
    public function send($path, $value, $timestamp = null)
82
    {
83
        $result    = false;
84
        $exception = null;
85
86
        set_error_handler(function ($code, $message, $file = null, $line = 0) {
87
            throw new ErrorException($message, $code, null, $file, $line);
88
        });
89
90
        try {
91
            if (!is_string($path) || empty($path)) {
92
                throw new InvalidArgumentException('$path must be a non-empty string');
93
            }
94
95
            if (!is_numeric($value)) {
96
                throw new InvalidArgumentException(
97
                    sprintf('$value must be of type int|float, %s given.', gettype($value))
98
                );
99
            }
100
101
            $value     = (float) $value;
102
            $timestamp = is_numeric($timestamp) ? (int) $timestamp : time();
103
            $full_path = $this->sanitizePath(
104
                sprintf('%s.%s', $this->getNamespace(), $path)
105
            );
106
107
            $data   = sprintf("%s %f %d\n", $full_path, $value, $timestamp);
108
            $sent   = fwrite($this->stream, $data);
109
            $result = is_int($sent) && $sent === strlen($data);
110
        } catch (Exception $e) {
111
            $exception = $e;
112
        }
113
        restore_error_handler();
114
115
        if (!empty($exception) && $this->throw_exceptions) {
116
            throw $exception;
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * Sanitizes a path string
124
     *
125
     * Carbon stores metrics using dot delimited paths
126
     * {@link http://graphite.readthedocs.org/en/latest/feeding-carbon.html}
127
     *
128
     * Replaces:
129
     * - whitespace with undercores
130
     * - consecutive dots with a single dot.
131
     *
132
     * Removes:
133
     * - the wildcard character (used by graphite)
134
     * - leading and trailing dots
135
     *
136
     * @param string $path the path string to sanitize
0 ignored issues
show
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
137
     *
138
     * @return string The sanitized path string or an empty one.
139
     */
140
    public function sanitizePath($string)
141
    {
142
        if (!is_string($string) || empty($string)) {
143
            return '';
144
        }
145
146
        $replace = [
147
            '/\s+/'    => '_',
148
            '/\*{1,}/' => '',
149
            '/\.{2,}/' => '.',
150
            '/^\./'    => '',
151
            '/\.$/'    => '',
152
        ];
153
154
        return preg_replace(
155
            array_keys($replace),
156
            array_values($replace),
157
            trim($string)
158
        );
159
    }
160
161
    /**
162
     * Closes the stream when the object is destructed
163
     */
164
    public function __destruct()
165
    {
166
        if (is_resource($this->stream)) {
167
            fclose($this->stream);
168
        }
169
    }
170
}
171