Issues (48)

Security Analysis    not enabled

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.

Negotiation/FormatNegotiator.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
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Negotiation;
13
14
use FOS\RestBundle\Util\StopFormatListenerException;
15
use Negotiation\Accept;
16
use Negotiation\AcceptHeader;
17
use Negotiation\Negotiator as BaseNegotiator;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
/**
23
 * @author Ener-Getick <[email protected]>
24
 */
25
final class FormatNegotiator extends BaseNegotiator
26
{
27
    private $map = [];
28
    private $requestStack;
29
    private $mimeTypes;
30
31 25
    public function __construct(RequestStack $requestStack, array $mimeTypes = array())
32
    {
33 25
        $this->requestStack = $requestStack;
34 25
        $this->mimeTypes = $mimeTypes;
35 25
    }
36
37 23
    public function add(RequestMatcherInterface $requestMatcher, array $options = []): void
38
    {
39 23
        $this->map[] = [$requestMatcher, $options];
40 23
    }
41
42 21
    public function getBest($header, array $priorities = []): ?AcceptHeader
43
    {
44 21
        $request = $this->getRequest();
45 21
        $header = $header ?: $request->headers->get('Accept');
46
47 21
        foreach ($this->map as $elements) {
48
            // Check if the current RequestMatcherInterface matches the current request
49 19
            if (!$elements[0]->matches($request)) {
50 1
                continue;
51
            }
52 19
            $options = &$elements[1]; // Do not reallow memory for this variable
53
54 19
            if (!empty($options['stop'])) {
55 1
                throw new StopFormatListenerException('Stopped format listener');
56
            }
57 18
            if (empty($options['priorities']) && empty($priorities)) {
58 3
                if (!empty($options['fallback_format'])) {
59 3
                    return new Accept($request->getMimeType($options['fallback_format']));
60
                }
61
62 1
                continue;
63
            }
64
65 15
            if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) {
66 10
                $extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION);
67
68 10
                if (!empty($extension)) {
69
                    // $extensionHeader will now be either a non empty string or an empty string
70 5
                    $extensionHeader = $request->getMimeType($extension);
71
72 5
                    if ($extensionHeader) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extensionHeader of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73 2
                        $header = $extensionHeader.'; q='.$options['prefer_extension'].($header ? ','.$header : '');
74
                    }
75
                }
76
            }
77
78 15
            if ($header) {
79 14
                $mimeTypes = $this->normalizePriorities(
80 14
                    $request,
81 14
                    empty($priorities) ? $options['priorities'] : $priorities
82
                );
83
84 14
                $mimeType = parent::getBest($header, $mimeTypes);
85
86 14
                if (null !== $mimeType) {
87 13
                    return $mimeType;
88
                }
89
            }
90
91 2
            if (isset($options['fallback_format'])) {
92
                // if false === fallback_format then we fail here instead of considering more rules
93 2
                if (false === $options['fallback_format']) {
94
                    return null;
95
                }
96
97
                // stop looking at rules since we have a fallback defined
98 2
                return new Accept($request->getMimeType($options['fallback_format']));
99
            }
100
        }
101
102 4
        return null;
103
    }
104
105 14
    private function sanitize(array $values): array
106
    {
107
        return array_map(function ($value) {
108 14
            return preg_replace('/\s+/', '', strtolower($value));
109 14
        }, $values);
110
    }
111
112
    /**
113
     * @param string[] $priorities
114
     *
115
     * @return string[] formatted priorities
116
     */
117 14
    private function normalizePriorities(Request $request, array $priorities): array
0 ignored issues
show
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119 14
        $priorities = $this->sanitize($priorities);
120
121 14
        $mimeTypes = array();
122 14
        foreach ($priorities as $priority) {
123 14
            if (strpos($priority, '/')) {
124 4
                $mimeTypes[] = $priority;
125
126 4
                continue;
127
            }
128
129 11
            $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority));
130
131 11
            if (isset($this->mimeTypes[$priority])) {
132 3
                foreach ($this->mimeTypes[$priority] as $mimeType) {
133 3
                    $mimeTypes[] = $mimeType;
134
                }
135
            }
136
        }
137
138 14
        return $mimeTypes;
139
    }
140
141 21
    private function getRequest(): Request
142
    {
143 21
        $request = $this->requestStack->getCurrentRequest();
144 21
        if (null === $request) {
145
            throw new \RuntimeException('There is no current request.');
146
        }
147
148 21
        return $request;
149
    }
150
}
151