Issues (6)

Security Analysis    no request data  

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

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/Symfony/SymfonyMessageSerializer.php (4 issues)

1
<?php
2
3
/**
4
 * Messages serializer implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\MessageSerializer\Symfony;
14
15
use ServiceBus\MessageSerializer\Exceptions\DecodeMessageFailed;
16
use ServiceBus\MessageSerializer\Exceptions\DenormalizeFailed;
17
use ServiceBus\MessageSerializer\Exceptions\EncodeMessageFailed;
18
use ServiceBus\MessageSerializer\Exceptions\NormalizationFailed;
19
use ServiceBus\MessageSerializer\MessageDecoder;
20
use ServiceBus\MessageSerializer\MessageEncoder;
21
use ServiceBus\MessageSerializer\Symfony\Extensions\EmptyDataNormalizer;
22
use ServiceBus\MessageSerializer\Symfony\Extensions\PropertyNameConverter;
23
use ServiceBus\MessageSerializer\Symfony\Extensions\PropertyNormalizerWrapper;
24
use ServiceBus\MessageSerializer\Symfony\Extractor\CombinedExtractor;
25
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
26
use Symfony\Component\Serializer as SymfonySerializer;
27
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
28
use function ServiceBus\Common\jsonDecode;
0 ignored issues
show
The function ServiceBus\Common\jsonDecode was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
29
use function ServiceBus\Common\jsonEncode;
0 ignored issues
show
The function ServiceBus\Common\jsonEncode was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
31
/**
32
 *
33
 */
34
final class SymfonyMessageSerializer implements MessageEncoder, MessageDecoder
35
{
36
    /**
37
     * Symfony normalizer\denormalizer.
38
     *
39
     * @var SymfonySerializer\Serializer
40
     */
41
    private $normalizer;
42
43
    /**
44
     * @param SymfonySerializer\Normalizer\DenormalizerInterface[]|SymfonySerializer\Normalizer\NormalizerInterface[] $normalizers
45
     */
46 15
    public function __construct(array $normalizers = [])
47
    {
48 15
        $extractor = \PHP_VERSION_ID >= 70400 ? new CombinedExtractor() : new PhpDocExtractor();
49
50 15
        $defaultNormalizers = [
51 15
            new DateTimeNormalizer(['datetime_format' => 'c']),
52 15
            new SymfonySerializer\Normalizer\ArrayDenormalizer(),
53 15
            new PropertyNormalizerWrapper(null, new PropertyNameConverter(), $extractor),
54 15
            new EmptyDataNormalizer(),
55
        ];
56
57
        /** @psalm-var array<array-key, (\Symfony\Component\Serializer\Normalizer\NormalizerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface)> $normalizers */
58 15
        $normalizers = \array_merge($normalizers, $defaultNormalizers);
59
60 15
        $this->normalizer = new SymfonySerializer\Serializer($normalizers);
61 15
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 9
    public function encode(object $message): string
67
    {
68
        try
69
        {
70 9
            $data = ['message' => $this->normalize($message), 'namespace' => \get_class($message)];
71
72 9
            return jsonEncode($data);
0 ignored issues
show
The function jsonEncode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            return /** @scrutinizer ignore-call */ jsonEncode($data);
Loading history...
73
        }
74 1
        catch (\Throwable $throwable)
75
        {
76 1
            throw new EncodeMessageFailed(
77 1
                \sprintf('Message serialization failed: %s', $throwable->getMessage()),
78 1
                (int) $throwable->getCode(),
79
                $throwable
80
            );
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 13
    public function decode(string $serializedMessage): object
88
    {
89
        try
90
        {
91 13
            $data = jsonDecode($serializedMessage);
0 ignored issues
show
The function jsonDecode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
            $data = /** @scrutinizer ignore-call */ jsonDecode($serializedMessage);
Loading history...
92
93 13
            self::validateUnserializedData($data);
94
95
            /** @var object $object */
96 8
            $object = $this->denormalize($data['message'], $data['namespace']);
97
98 8
            return $object;
99
        }
100 5
        catch (\Throwable $throwable)
101
        {
102 5
            throw new DecodeMessageFailed(
103 5
                \sprintf('Message deserialization failed: %s', $throwable->getMessage()),
104 5
                (int) $throwable->getCode(),
105
                $throwable
106
            );
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 9
    public function denormalize(array $payload, string $class): object
114
    {
115
        try
116
        {
117
            /** @var object $object */
118 9
            $object = $this->normalizer->denormalize(
119 9
                $payload,
120
                $class
121
            );
122
123 8
            return $object;
124
        }
125 1
        catch (\Throwable $throwable)
126
        {
127 1
            throw new DenormalizeFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 9
    public function normalize(object $message): array
135
    {
136
        try
137
        {
138 9
            $data = $this->normalizer->normalize($message);
139
140 9
            if (\is_array($data))
141
            {
142
                /** @psalm-var array<string, mixed> $data */
143
144 9
                return $data;
145
            }
146
147
            // @codeCoverageIgnoreStart
148
            throw new \UnexpectedValueException(
149
                \sprintf(
150
                    'The normalization was to return the array. Type "%s" was obtained when object "%s" was normalized',
151
                    \gettype($data),
152
                    \get_class($message)
153
                )
154
            );
155
            // @codeCoverageIgnoreEnd
156
        }
157
        catch (\Throwable $throwable)
158
        {
159
            throw new NormalizationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
160
        }
161
    }
162
163
    /**
164
     * @psalm-assert array{message:array<string, string|int|float|array|null>, namespace:class-string} $data
165
     *
166
     * @throws \UnexpectedValueException
167
     */
168 13
    private static function validateUnserializedData(array $data): void
169
    {
170
        /** Let's check if there are mandatory fields */
171
        if (
172 13
            isset($data['namespace']) === false ||
173 13
            isset($data['message']) === false
174
        ) {
175 2
            throw new \UnexpectedValueException(
176 2
                'The serialized data must contains a "namespace" field (indicates the message class) and "message" (indicates the message parameters)'
177
            );
178
        }
179
180 11
        if (\is_array($data['message']) === false)
181
        {
182 1
            throw new \UnexpectedValueException('"message" field from serialized data should be an array');
183
        }
184
185 10
        if (\is_string($data['namespace']) === false)
186
        {
187 1
            throw new \UnexpectedValueException('"namespace" field from serialized data should be a string');
188
        }
189
190
        /** Let's check if the specified class exists. */
191 9
        if ($data['namespace'] === '' || \class_exists($data['namespace']) === false)
192
        {
193 1
            throw new \UnexpectedValueException(
194 1
                \sprintf('Class "%s" not found', $data['namespace'])
195
            );
196
        }
197 8
    }
198
}
199