Issues (35)

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.

Response/ResponseProcessor.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
 * This file is part of the FirebaseCloudMessagingBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\FirebaseCloudMessagingBundle\Response;
14
15
use Fresh\FirebaseCloudMessagingBundle\Exception\AuthenticationException;
16
use Fresh\FirebaseCloudMessagingBundle\Exception\ExceptionInterface;
17
use Fresh\FirebaseCloudMessagingBundle\Exception\InternalServerErrorException;
18
use Fresh\FirebaseCloudMessagingBundle\Exception\InvalidJsonException;
19
use Fresh\FirebaseCloudMessagingBundle\Exception\UnsupportedResponseException;
20
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Target\TokenTargetInterface;
21
use Fresh\FirebaseCloudMessagingBundle\Message\Type\AbstractMessage;
22
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\CanonicalTokenMessageResult;
23
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\CanonicalTokenMessageResultCollection;
24
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\FailedMessageResultCollection;
25
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\SuccessfulMessageResultCollection;
26
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\FailedMessageResult;
27
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\SuccessfulMessageResult;
28
use Psr\Http\Message\ResponseInterface;
29
use Symfony\Component\HttpFoundation\Response;
30
31
/**
32
 * Class ResponseProcessor.
33
 *
34
 * @author Artem Henvald <[email protected]>
35
 */
36
class ResponseProcessor
37
{
38
    private array $jsonContentTypes = [
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
39
        'application/json',
40
        'application/json; charset=UTF-8',
41
    ];
42
43
    private AbstractMessage $message;
44
45
    /**
46
     * @param AbstractMessage   $message
47
     * @param ResponseInterface $response
48
     *
49
     * @throws ExceptionInterface
50
     *
51
     * @return FirebaseResponseInterface
52
     */
53
    public function processResponse(AbstractMessage $message, ResponseInterface $response): FirebaseResponseInterface
54
    {
55
        $this->message = $message;
56
57
        if (Response::HTTP_OK === $response->getStatusCode()) {
58
            $result = $this->processHttpOkResponse($response);
59
        } elseif (Response::HTTP_BAD_REQUEST === $response->getStatusCode()) {
60
            throw new InvalidJsonException();
61
        } elseif (Response::HTTP_UNAUTHORIZED === $response->getStatusCode()) {
62
            throw new AuthenticationException();
63
        } elseif (Response::HTTP_INTERNAL_SERVER_ERROR === $response->getStatusCode()) {
64
            throw new InternalServerErrorException();
65
        } else {
66
            throw new UnsupportedResponseException();
67
        }
68
69
        return $result;
70
    }
71
72
    /**
73
     * @param ResponseInterface $response
74
     *
75
     * @return FirebaseResponseInterface
76
     */
77
    private function processHttpOkResponse(ResponseInterface $response)
78
    {
79
        $body = $this->getBodyAsArray($response);
80
81
        if (isset($body['error'])) {
82
            $response = $this->processHttpOkResponseWithError($body);
83
        } else {
84
            $response = $this->processHttpOkResponseWithoutError($body);
85
        }
86
87
        return $response;
88
    }
89
90
    /**
91
     * @param array $body
92
     *
93
     * @return MulticastMessageResponse
94
     */
95
    private function processHttpOkResponseWithoutError(array $body): MulticastMessageResponse
96
    {
97
        $successfulMessageResults = new SuccessfulMessageResultCollection();
98
        $failedMessageResults = new FailedMessageResultCollection();
99
        $canonicalTokenMessageResults = new CanonicalTokenMessageResultCollection();
100
101
        if ($this->message->getTarget() instanceof TokenTargetInterface) {
102
            $numberOfSequentialSentTokens = $this->message->getTarget()->getNumberOfSequentialSentTokens();
103
104
            if (isset($body['results']) && \count($body['results']) !== $numberOfSequentialSentTokens) {
105
                throw new \Exception('Mismatch number of sent tokens and results');
106
            }
107
108
            for ($i = 0; $i < $numberOfSequentialSentTokens; ++$i) {
109
                $currentToken = $this->message->getTarget()->getSequentialSentTokens()[$i];
110
                $currentResult = $body['results'][$i];
111
112
                if (isset($currentResult['error'])) {
113
                    $messageResult = (new FailedMessageResult())
114
                        ->setError($currentResult['error']);
115
116
                    $failedMessageResults[] = $messageResult;
117
                } elseif (isset($currentResult['registration_id'])) {
118
                    $messageResult = (new CanonicalTokenMessageResult())
119
                        ->setMessageId($currentResult['message_id'])
120
                        ->setCanonicalToken($currentResult['registration_id']);
121
122
                    $canonicalTokenMessageResults[] = $messageResult;
123
                } else {
124
                    $messageResult = (new SuccessfulMessageResult())->setMessageId($currentResult['message_id']);
125
                    $successfulMessageResults[] = $messageResult;
126
                }
127
128
                $messageResult->setToken($currentToken);
129
            }
130
        }
131
132
        return (new MulticastMessageResponse())
133
            ->setMulticastId($body['multicast_id'])
134
            ->setSuccessfulMessageResults($successfulMessageResults)
135
            ->setFailedMessageResults($failedMessageResults)
136
            ->setCanonicalTokenMessageResults($canonicalTokenMessageResults);
137
    }
138
139
    /**
140
     * @param array $body
141
     */
142
    private function processHttpOkResponseWithError(array $body)
143
    {
144
        // @todo finish it
145
    }
146
147
    /**
148
     * @param ResponseInterface $response
149
     *
150
     * @throws \InvalidArgumentException
151
     *
152
     * @return array
153
     */
154
    private function getBodyAsArray(ResponseInterface $response): array
155
    {
156
        if ($this->responseContentTypeIsJson($response)) {
157
            $response->getBody()->rewind();
158
            $body = null;
159
160
            if ($response->getBody()->getSize() > 0) {
161
                $body = $response->getBody()->getContents();
162
            }
163
164
            return \json_decode($body, true);
165
        }
166
167
        throw new \InvalidArgumentException('Response from Firebase Cloud Messaging is not a JSON');
168
    }
169
170
    /**
171
     * @param ResponseInterface $response
172
     *
173
     * @return bool
174
     */
175
    private function responseContentTypeIsJson(ResponseInterface $response): bool
176
    {
177
        return $response->hasHeader('Content-Type')
178
               && \in_array($response->getHeader('Content-Type')[0], $this->jsonContentTypes, true);
179
    }
180
}
181