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/FileAttachment.php (1 issue)

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\Header\ContentDisposition;
7
use Genkgo\Mail\Header\ContentTransferEncoding;
8
use Genkgo\Mail\Header\ContentType;
9
use Genkgo\Mail\Header\HeaderValue;
10
use Genkgo\Mail\HeaderInterface;
11
use Genkgo\Mail\Stream\Base64EncodedStream;
12
use Genkgo\Mail\StreamInterface;
13
14
final class FileAttachment implements PartInterface
15
{
16
    /**
17
     * @var PartInterface
18
     */
19
    private $decoratedPart;
20
21
    /**
22
     * @param string $filename
23
     * @param ContentType $contentType
24
     * @param string $attachmentName
25
     */
26 21
    public function __construct(string $filename, ContentType $contentType, string $attachmentName = '')
27
    {
28 21
        if (!\file_exists($filename) || !\is_file($filename)) {
29 2
            throw new \InvalidArgumentException('Attachment does not exists');
30
        }
31
32 19
        if ($attachmentName === '') {
33 19
            $attachmentName = \basename($filename);
34
        }
35
36 19
        $resource = \fopen($filename, 'r');
37 19
        if ($resource === false) {
38
            throw new \UnexpectedValueException('Cannot open file ' . $filename . ' for reading');
39
        }
40
41 19
        $this->decoratedPart = (new GenericPart())
42 19
            ->withBody(new Base64EncodedStream($resource))
43 19
            ->withHeader($contentType)
44 19
            ->withHeader(new ContentTransferEncoding('base64'))
45 19
            ->withHeader(ContentDisposition::newAttachment($attachmentName));
46 19
    }
47
48
    /**
49
     * @param string $filename
50
     * @param string $attachmentName
51
     * @return FileAttachment
52
     */
53 1
    public static function fromUnknownFileType(string $filename, string $attachmentName = ''): FileAttachment
54
    {
55 1
        $fileInfo = new \finfo(FILEINFO_MIME);
56 1
        $mime = $fileInfo->file($filename);
57 1
        if ($mime === false) {
58
            return new self(
59
                $filename,
60
                new ContentType('application/octet-stream'),
61
                $attachmentName
62
            );
63
        }
64
65 1
        $headerValue = HeaderValue::fromString($mime);
66
67
        try {
68 1
            $charset = $headerValue->getParameter('charset')->getValue();
69 1
            if ($charset === 'binary') {
70
                $contentType = new ContentType($headerValue->getRaw());
71
            } else {
72 1
                $contentType = new ContentType($headerValue->getRaw(), $charset);
73
            }
74
        } catch (\UnexpectedValueException $e) {
75
            $contentType = new ContentType($headerValue->getRaw());
76
        }
77
78 1
        return new self(
79 1
            $filename,
80
            $contentType,
81
            $attachmentName
82
        );
83
    }
84
85
    /**
86
     * @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...
87
     */
88 2
    public function getHeaders(): iterable
89
    {
90 2
        return $this->decoratedPart->getHeaders();
91
    }
92
93
    /**
94
     * @param string $name
95
     * @return bool
96
     */
97 8
    public function hasHeader(string $name): bool
98
    {
99 8
        return $this->decoratedPart->hasHeader($name);
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return HeaderInterface
105
     */
106 9
    public function getHeader(string $name): HeaderInterface
107
    {
108 9
        return $this->decoratedPart->getHeader($name);
109
    }
110
111
    /**
112
     * @param HeaderInterface $header
113
     * @return PartInterface
114
     */
115 4
    public function withHeader(HeaderInterface $header): PartInterface
116
    {
117 4
        if (\strtolower((string)$header->getName()) === 'content-disposition') {
118 2
            throw new \InvalidArgumentException('Cannot modify content disposition for file attachment');
119
        }
120
121 2
        $clone = clone $this;
122 2
        $clone->decoratedPart = $this->decoratedPart->withHeader($header);
123 2
        return $clone;
124
    }
125
126
    /**
127
     * @param string $name
128
     * @return PartInterface
129
     */
130 4
    public function withoutHeader(string $name): PartInterface
131
    {
132 4
        if (\strtolower($name) === 'content-disposition') {
133 2
            throw new \InvalidArgumentException('Cannot modify content disposition for file attachment');
134
        }
135
136 2
        $clone = clone $this;
137 2
        $clone->decoratedPart = $this->decoratedPart->withoutHeader($name);
138 2
        return $clone;
139
    }
140
141
    /**
142
     * @param StreamInterface $body
143
     * @return PartInterface
144
     */
145 2
    public function withBody(StreamInterface $body): PartInterface
146
    {
147 2
        throw new \RuntimeException('Cannot modify body of FileAttachment');
148
    }
149
150
    /**
151
     * @return StreamInterface
152
     */
153 8
    public function getBody(): StreamInterface
154
    {
155 8
        return $this->decoratedPart->getBody();
156
    }
157
}
158