Issues (74)

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/GenericMessage.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
declare(strict_types=1);
3
4
namespace Genkgo\Mail;
5
6
use Genkgo\Mail\Header\HeaderLine;
7
use Genkgo\Mail\Header\MimeVersion;
8
use Genkgo\Mail\Stream\EmptyStream;
9
use Genkgo\Mail\Stream\MessageStream;
10
use Genkgo\Mail\Stream\StringStream;
11
12
final class GenericMessage implements MessageInterface
13
{
14
    /**
15
     * @var array<string, array<int, HeaderInterface>>
16
     */
17
    private $headers = [
18
        'return-path' => [],
19
        'received' => [],
20
        'dkim-signature' => [],
21
        'domainkey-signature' => [],
22
        'sender' => [],
23
        'message-id' => [],
24
        'date' => [],
25
        'subject' => [],
26
        'from' => [],
27
        'reply-to' => [],
28
        'to' => [],
29
        'cc' => [],
30
        'bcc' => [],
31
        'mime-version' => [],
32
        'content-type' => [],
33
        'content-transfer-encoding' => [],
34
    ];
35
36
    /**
37
     * @var StreamInterface
38
     */
39
    private $body;
40
    
41 132
    public function __construct()
42
    {
43 132
        $this->body = new EmptyStream();
44 132
        $this->headers['mime-version'] = [new MimeVersion()];
45 132
    }
46
47
    /**
48
     * @return iterable<iterable<HeaderInterface>>
0 ignored issues
show
The doc-type iterable<iterable<HeaderInterface>> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
49
     */
50 76
    public function getHeaders(): iterable
51
    {
52 76
        return $this->headers;
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return bool
58
     */
59 34
    public function hasHeader(string $name): bool
60
    {
61 34
        $name = \strtolower($name);
62 34
        return isset($this->headers[$name]) && $this->headers[$name];
63
    }
64
65
    /**
66
     * @param string $name
67
     * @return HeaderInterface[]|iterable
68
     */
69 61
    public function getHeader(string $name): iterable
70
    {
71 61
        $name = \strtolower($name);
72
73 61
        if (!isset($this->headers[$name])) {
74 2
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Genkgo\Mail\MessageInterface::getHeader of type Genkgo\Mail\iterable|Genkgo\Mail\HeaderInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
        }
76
77 61
        return $this->headers[$name];
78
    }
79
80
    /**
81
     * @param HeaderInterface $header
82
     * @return MessageInterface
83
     */
84 115
    public function withHeader(HeaderInterface $header): MessageInterface
85
    {
86 115
        $clone = clone $this;
87 115
        $clone->headers[\strtolower((string)$header->getName())] = [$header];
88 115
        return $clone;
89
    }
90
91
    /**
92
     * @param HeaderInterface $header
93
     * @return MessageInterface
94
     */
95 3
    public function withAddedHeader(HeaderInterface $header): MessageInterface
96
    {
97 3
        $clone = clone $this;
98 3
        $clone->headers[\strtolower((string)$header->getName())][] = $header;
99 3
        return $clone;
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return MessageInterface
105
     */
106 5
    public function withoutHeader(string $name): MessageInterface
107
    {
108 5
        $clone = clone $this;
109 5
        unset($clone->headers[(string)\strtolower($name)]);
110 5
        return $clone;
111
    }
112
113
    /**
114
     * @param StreamInterface $body
115
     * @return MessageInterface
116
     */
117 93
    public function withBody(StreamInterface $body): MessageInterface
118
    {
119 93
        $clone = clone $this;
120 93
        $clone->body = $body;
121 93
        return $clone;
122
    }
123
124
    /**
125
     * @return StreamInterface
126
     */
127 78
    public function getBody(): StreamInterface
128
    {
129 78
        return $this->body;
130
    }
131
132
    /**
133
     * @return string
134
     */
135 58
    public function __toString(): string
136
    {
137 58
        return (string) (new MessageStream($this));
138
    }
139
140
    /**
141
     * @param string $messageString
142
     * @return MessageInterface
143
     */
144 48
    public static function fromString(string $messageString): MessageInterface
145
    {
146 48
        $message = new self();
147
148 48
        $lines = \preg_split('/\r\n/', $messageString);
149 48
        if ($lines === false) {
150
            throw new \UnexpectedValueException('Cannot parse from string, cannot split lines');
151
        }
152
153 48
        for ($n = 0, $length = \count($lines); $n < $length; $n++) {
154 48
            $line = $lines[$n];
155
156 48
            if ($line === '') {
157 47
                $message = $message->withBody(
158 47
                    new StringStream(
159 47
                        \implode(
160 47
                            "\r\n",
161 47
                            \array_slice($lines, $n + 1)
162
                        )
163
                    )
164
                );
165 47
                break;
166
            }
167
168 48
            while (isset($lines[$n + 1]) && $lines[$n + 1] !== '' && \trim($lines[$n + 1][0]) === '') {
169 2
                $line .= ' ' . \ltrim($lines[$n + 1]);
170 2
                $n++;
171
            }
172
173 48
            $message = $message->withHeader(HeaderLine::fromString($line)->getHeader());
174
        }
175
176 47
        return $message;
177
    }
178
}
179