Issues (6)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/FileFactory.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage;
18
19
use Phauthentic\Infrastructure\Storage\Exception\FileDoesNotExistException;
20
use Phauthentic\Infrastructure\Storage\Exception\FileNotReadableException;
21
use Phauthentic\Infrastructure\Storage\Utility\MimeType;
22
use Phauthentic\Infrastructure\Storage\Utility\PathInfo;
23
use Phauthentic\Infrastructure\Storage\Utility\StreamWrapper;
24
use Psr\Http\Message\UploadedFileInterface;
25
use RuntimeException;
26
27
/**
28
 * File Factory
29
 */
30
class FileFactory implements FileFactoryInterface
31
{
32
    /**
33
     * @inheritDoc
34
     */
35
    public static function fromUploadedFile(
36
        UploadedFileInterface $uploadedFile,
37
        string $storage
38
    ): FileInterface {
39
        static::checkUploadedFile($uploadedFile);
40
41
        $file = File::create(
42
            (string)$uploadedFile->getClientFilename(),
43
            (int)$uploadedFile->getSize(),
44
            (string)$uploadedFile->getClientMediaType(),
45
            $storage
46
        );
47
48
        return $file->withResource(
49
            StreamWrapper::getResource($uploadedFile->getStream())
50
        );
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public static function fromDisk(string $path, string $storage): FileInterface
57
    {
58
        static::checkFile($path);
59
60
        $info = PathInfo::for($path);
61
        $filesize = filesize($path);
62
        if ($filesize === false) {
63
            throw new RuntimeException(sprintf('Failed to get file size for: %s', $path));
64
        }
65
66
        $extension = $info->extension();
67
        $mimeType = $extension !== null ? MimeType::byExtension($extension) : 'application/octet-stream';
68
        if ($mimeType === null) {
0 ignored issues
show
The condition $mimeType === null is always false.
Loading history...
69
            $mimeType = 'application/octet-stream';
70
        }
71
72
        $file = File::create(
73
            $info->basename(),
74
            $filesize,
75
            $mimeType,
76
            $storage,
77
        );
78
79
        $resource = fopen($path, 'rb');
80
81
        return $file->withResource($resource);
82
    }
83
84
    /**
85
     * Checks if the uploaded file is a valid upload
86
     *
87
     * @param \Psr\Http\Message\UploadedFileInterface $uploadedFile Uploaded File
88
     * @return void
89
     */
90
    protected static function checkUploadedFile(UploadedFileInterface $uploadedFile): void
91
    {
92
        if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
93
            throw new RuntimeException(sprintf(
94
                'Can\'t create storage object from upload with error code: %d',
95
                $uploadedFile->getError()
96
            ));
97
        }
98
    }
99
100
    /**
101
     * @param string $path Path
102
     * @return void
103
     */
104
    protected static function checkFile(string $path): void
105
    {
106
        if (!file_exists($path)) {
107
            throw FileDoesNotExistException::filename($path);
108
        }
109
110
        if (!is_readable($path)) {
111
            throw FileNotReadableException::filename($path);
112
        }
113
    }
114
}
115