Issues (8)

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/ApplicationResponse.php (8 issues)

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
declare(strict_types=1);
3
4
namespace Selami;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Zend\Config\Config;
9
use Selami\View\ViewInterface;
10
use Selami\Router\Router;
11
use Selami\Stdlib\CaseConverter;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\Response\JsonResponse;
14
use Zend\Diactoros\Response\HtmlResponse;
15
use Zend\Diactoros\Response\TextResponse;
16
use Zend\Diactoros\Response\RedirectResponse;
17
use Zend\Diactoros\Response\EmptyResponse;
18
use Zend\Diactoros\Response\XmlResponse;
19
use Zend\Diactoros\Stream;
20
21
class ApplicationResponse
22
{
23
    private $controllerClass;
24
    private $controllerResponse;
25
    private $config;
26
    private $view;
27
    private $headers;
28
29
    public function __construct(
30
        ServerRequestInterface $request,
31
        string $controllerClass,
32
        ControllerResponse $controllerResponse,
33
        Config $config,
34
        ViewInterface $view
35
    ) {
36
        $this->controllerClass = $controllerClass;
37
        $this->controllerResponse = $controllerResponse;
38
        $this->config = $config;
39
        $this->headers = isset( $config->get('app')['default_headers']) ?
40
            $config->get('app')->get('default_headers')->toArray() : [];
41
        $this->view = $view;
42
        $this->view->addGlobal('Request', $request);
43
        $this->view->addGlobal(
44
            'QueryParameters',
45
            array_merge($request->getQueryParams(), $request->getParsedBody())
46
        );
47
    }
48
49
    public function getResponseHeaders() : array
50
    {
51
        return array_merge($this->headers, $this->controllerResponse->getHeaders());
52
    }
53
54
    public function returnResponse() : ResponseInterface
55
    {
56
        switch ($this->controllerResponse->getReturnType()) {
57
            case Router::HTML:
58
                return new HtmlResponse(
59
                    $this->renderResponse(),
60
                    $this->controllerResponse->getStatusCode(),
61
                    $this->getResponseHeaders()
62
                );
63
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
            case Router::JSON:
65
                return new JsonResponse(
66
                    $this->controllerResponse->getData(),
67
                    $this->controllerResponse->getStatusCode(),
68
                    $this->getResponseHeaders(),
69
                    JsonResponse::DEFAULT_JSON_FLAGS | JSON_PARTIAL_OUTPUT_ON_ERROR
70
                );
71
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
            case Router::TEXT:
73
                return new TextResponse(
74
                    $this->renderResponse(),
75
                    $this->controllerResponse->getStatusCode(),
76
                    $this->getResponseHeaders()
77
                );
78
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
            case Router::XML:
80
                return new XmlResponse(
81
                    $this->renderResponse(),
82
                    $this->controllerResponse->getStatusCode(),
83
                    $this->getResponseHeaders()
84
                );
85
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
86
            case Router::DOWNLOAD:
87
                $metaData = $this->controllerResponse->getMetaData();
88
                /**
89
                 * @var $stream Stream
90
                 */
91
                $stream = $metaData['stream'];
92
                return new Response(
93
                    $stream,
94
                    $this->controllerResponse->getStatusCode(),
95
                    $this->getResponseHeaders()
96
                );
97
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
98
            case Router::REDIRECT:
99
                return new RedirectResponse(
100
                    $this->controllerResponse->getMetaData()['uri'],
101
                    $this->controllerResponse->getStatusCode(),
102
                    $this->getResponseHeaders()
103
                );
104
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
105
            case Router::CUSTOM:
106
                return new HtmlResponse(
107
                    $this->renderResponse(),
108
                    $this->controllerResponse->getStatusCode(),
109
                    $this->getResponseHeaders()
110
                );
111
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
112
            case Router::EMPTY:
113
                return new EmptyResponse(
114
                    $this->controllerResponse->getStatusCode(),
115
                    $this->getResponseHeaders()
116
                );
117
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
118
        }
119
    }
120
121
    private function renderResponse() : string
122
    {
123
        $paths = explode("\\", $this->controllerClass);
124
        $templateFile = array_pop($paths);
125
        $templateFolder = array_pop($paths);
126
        $template = CaseConverter::toSnakeCase($templateFolder)
127
            . '/' . CaseConverter::toSnakeCase($templateFile);
128
        $layout = $this->controllerResponse->getMetaData()['layout'] ?? $template;
129
        $templatePath = $layout. '.' . $this->config->view->get('template_file_extension');
130
131
        $this->checkTemplateFile($templatePath, 'Method\'s', $this->controllerClass);
132
        return $this->view->render($templatePath, $this->controllerResponse->getData());
133
    }
134
135
    private function checkTemplateFile($template, $type, $controller) : void
136
    {
137
        if (!file_exists($this->config->view->get('templates_path') .'/'. $template)) {
138
            $message  = sprintf(
139
                '%s  template file not found! %s  needs a main template file at: %s',
140
                $type,
141
                $controller,
142
                $this->config['app_dir'] .'/'. $template
143
            );
144
            throw new \DomainException($message);
145
        }
146
    }
147
148
    public function notFound(int $status, int $returnType, string $message) : ResponseInterface
149
    {
150
        if ($returnType === Router::JSON) {
151
            return new JsonResponse(['status' => $status, 'message' => $message], $status);
152
        }
153
        $notFoundTemplate = '_layouts/404.twig';
154
        $content = $this->view->render(
155
            $notFoundTemplate,
156
            ['message' => $message, 'status' => $status]
157
        );
158
        return new HtmlResponse($content, $status);
159
    }
160
}
161