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/Mime/MultiPart.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\Mime;
5
6
use Genkgo\Mail\GenericMessage;
7
use Genkgo\Mail\Header\ContentType;
8
use Genkgo\Mail\Header\ParsedHeader;
9
use Genkgo\Mail\Header\HeaderValueParameter;
10
use Genkgo\Mail\HeaderInterface;
11
use Genkgo\Mail\MessageInterface;
12
use Genkgo\Mail\Stream\EmptyStream;
13
use Genkgo\Mail\Stream\LineIterator;
14
use Genkgo\Mail\StreamInterface;
15
16
final class MultiPart implements MultiPartInterface
17
{
18
    /**
19
     * @var PartInterface
20
     */
21
    private $decoratedPart;
22
23
    /**
24
     * @var Boundary
25
     */
26
    private $boundary;
27
28
    /**
29
     * @var array|PartInterface[]
30
     */
31
    private $parts = [];
32
33
    /**
34
     * @param Boundary $boundary
35
     * @param ContentType $contentType
36
     */
37 30
    public function __construct(Boundary $boundary, ContentType $contentType)
38
    {
39 30
        $this->boundary = $boundary;
40
41 30
        if (\substr((string)$contentType->getValue(), 0, 10) !== 'multipart/') {
42 1
            throw new \InvalidArgumentException('Content type must be of type multipart/type');
43
        }
44
45 29
        $this->decoratedPart = (new GenericPart())
46 29
            ->withHeader(
47 29
                new ParsedHeader(
48 29
                    $contentType->getName(),
49 29
                    $contentType->getValue()
50 29
                        ->withParameter(
51 29
                            new HeaderValueParameter(
52 29
                                'boundary',
53 29
                                (string)$boundary
54
                            )
55
                        )
56
                )
57
            );
58 29
    }
59
60
    /**
61
     * @return iterable<HeaderInterface>
0 ignored issues
show
The doc-type 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...
62
     */
63 17
    public function getHeaders(): iterable
64
    {
65 17
        return $this->decoratedPart->getHeaders();
66
    }
67
68
    /**
69
     * @param string $name
70
     * @return bool
71
     */
72 3
    public function hasHeader(string $name): bool
73
    {
74 3
        return $this->decoratedPart->hasHeader($name);
75
    }
76
77
    /**
78
     * @param string $name
79
     * @return HeaderInterface
80
     */
81 20
    public function getHeader(string $name): HeaderInterface
82
    {
83 20
        return $this->decoratedPart->getHeader($name);
84
    }
85
86
    /**
87
     * @param HeaderInterface $header
88
     * @return PartInterface
89
     */
90 1
    public function withHeader(HeaderInterface $header): PartInterface
91
    {
92 1
        throw new \RuntimeException('Cannot modify headers of MultiPart');
93
    }
94
95
    /**
96
     * @param string $name
97
     * @return PartInterface
98
     */
99 1
    public function withoutHeader(string $name): PartInterface
100
    {
101 1
        throw new \RuntimeException('Cannot modify headers of MultiPart');
102
    }
103
104
    /**
105
     * @param StreamInterface $body
106
     * @return PartInterface
107
     */
108 1
    public function withBody(StreamInterface $body): PartInterface
109
    {
110 1
        throw new \RuntimeException('Cannot modify body of MultiPart');
111
    }
112
113
    /**
114
     * @return StreamInterface
115
     */
116 16
    public function getBody(): StreamInterface
117
    {
118 16
        return new EmptyStream();
119
    }
120
121
    /**
122
     * @return Boundary
123
     */
124 18
    public function getBoundary(): Boundary
125
    {
126 18
        return $this->boundary;
127
    }
128
129
    /**
130
     * @param PartInterface $part
131
     * @return MultiPartInterface
132
     */
133 17
    public function withPart(PartInterface $part): MultiPartInterface
134
    {
135 17
        $clone = clone $this;
136 17
        $clone->parts[] = $part;
137 17
        return $clone;
138
    }
139
140
    /**
141
     * @param iterable|PartInterface[] $parts
142
     * @return MultiPartInterface
143
     */
144 7
    public function withParts(iterable $parts): MultiPartInterface
145
    {
146 7
        $clone = clone $this;
147
148 7
        foreach ($parts as $part) {
149 7
            $clone->parts[] = $part;
150
        }
151
152 7
        return $clone;
153
    }
154
155
    /**
156
     * @return iterable|PartInterface[]
157
     */
158 24
    public function getParts(): iterable
159
    {
160 24
        return $this->parts;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->parts; (array) is incompatible with the return type declared by the interface Genkgo\Mail\Mime\MultiPartInterface::getParts of type Genkgo\Mail\Mime\iterabl...il\Mime\PartInterface[].

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...
161
    }
162
163
    /**
164
     * @param MessageInterface $message
165
     * @return MultiPart
166
     */
167 22
    public static function fromMessage(MessageInterface $message): self
168
    {
169 22
        foreach ($message->getHeader('Content-Type') as $header) {
170 22
            $contentType = $header->getValue()->getRaw();
171 22
            if (\substr($contentType, 0, 10) !== 'multipart/') {
172 22
                throw new \InvalidArgumentException(
173 22
                    \sprintf(
174 22
                        'Message is not a multipart/alternative message, but %s',
175 22
                        $contentType
176
                    )
177
                );
178
            }
179
180
            try {
181 18
                $boundary = new Boundary($header->getValue()->getParameter('boundary')->getValue());
182
            } catch (\UnexpectedValueException $e) {
183
                throw new \InvalidArgumentException('Message does not contain a boundary');
184
            }
185
186 18
            $part = new self(
187 18
                $boundary,
188 18
                new ContentType($header->getValue()->getRaw())
189
            );
190
191 18
            $content = '';
192 18
            $preamble = true;
193 18
            foreach (new LineIterator($message->getBody()) as $line) {
194 18
                if ($boundary->isOpening($line) && $preamble) {
195 18
                    $content = '';
196 18
                    $preamble = false;
197 18
                    continue;
198
                }
199
200 18
                if ($boundary->isClosing($line) || $boundary->isOpening($line)) {
201 18
                    $message = GenericMessage::fromString(\rtrim($content));
202
203
                    try {
204 18
                        $part->parts[] = MultiPart::fromMessage($message);
205 18
                    } catch (\InvalidArgumentException $e) {
206 18
                        $part->parts[] = GenericPart::fromMessage($message);
207
                    }
208
209 18
                    $content = '';
210
                }
211
212 18
                if ($boundary->isOpening($line)) {
213 18
                    continue;
214
                }
215
216 18
                if ($boundary->isClosing($line)) {
217 18
                    break;
218
                }
219
220 18
                $content .= $line . "\r\n";
221
            }
222
223 18
            return $part;
224
        }
225
226
        throw new \InvalidArgumentException('Message is not a multipart/alternative message');
227
    }
228
}
229