Issues (4)

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/FileVault.php (3 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
3
namespace SoareCostin\FileVault;
4
5
use Illuminate\Support\Facades\Storage;
6
use Illuminate\Support\Str;
7
8
class FileVault
9
{
10
    /**
11
     * The storage disk.
12
     *
13
     * @var string
14
     */
15
    protected $disk;
16
17
    /**
18
     * The encryption key.
19
     *
20
     * @var string
21
     */
22
    protected $key;
23
24
    /**
25
     * The algorithm used for encryption.
26
     *
27
     * @var string
28
     */
29
    protected $cipher;
30
31
    /**
32
     * The storage adapter.
33
     *
34
     * @var string
35
     */
36
    protected $adapter;
37
38
    public function __construct()
39
    {
40
        $this->disk = config('file-vault.disk');
41
        $this->key = config('file-vault.key');
42
        $this->cipher = config('file-vault.cipher');
43
    }
44
45
    /**
46
     * Set the disk where the files are located.
47
     *
48
     * @param  string  $disk
49
     * @return $this
50
     */
51
    public function disk($disk)
52
    {
53
        $this->disk = $disk;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Set the encryption key.
60
     *
61
     * @param  string  $key
62
     * @return $this
63
     */
64
    public function key($key)
65
    {
66
        $this->key = $key;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Create a new encryption key for the given cipher.
73
     *
74
     * @return string
75
     */
76
    public static function generateKey()
77
    {
78
        return random_bytes(config('file-vault.cipher') === 'AES-128-CBC' ? 16 : 32);
79
    }
80
81
    /**
82
     * Encrypt the passed file and saves the result in a new file with ".enc" as suffix.
83
     *
84
     * @param string $sourceFile Path to file that should be encrypted, relative to the storage disk specified
85
     * @param string $destFile   File name where the encryped file should be written to, relative to the storage disk specified
86
     * @return $this
87
     */
88
    public function encrypt($sourceFile, $destFile = null, $deleteSource = true)
89
    {
90
        $this->registerServices();
91
92
        if (is_null($destFile)) {
93
            $destFile = "{$sourceFile}.enc";
94
        }
95
96
        $sourcePath = $this->getFilePath($sourceFile);
97
        $destPath = $this->getFilePath($destFile);
98
99
        // Create a new encrypter instance
100
        $encrypter = new FileEncrypter($this->key, $this->cipher);
101
102
        // If encryption is successful, delete the source file
103
        if ($encrypter->encrypt($sourcePath, $destPath) && $deleteSource) {
104
            Storage::disk($this->disk)->delete($sourceFile);
105
        }
106
107
        return $this;
108
    }
109
110
    public function encryptCopy($sourceFile, $destFile = null)
111
    {
112
        return self::encrypt($sourceFile, $destFile, false);
113
    }
114
115
    /**
116
     * Dencrypt the passed file and saves the result in a new file, removing the
117
     * last 4 characters from file name.
118
     *
119
     * @param string $sourceFile Path to file that should be decrypted
120
     * @param string $destFile   File name where the decryped file should be written to.
121
     * @return $this
122
     */
123
    public function decrypt($sourceFile, $destFile = null, $deleteSource = true)
124
    {
125
        $this->registerServices();
126
127
        if (is_null($destFile)) {
128
            $destFile = Str::endsWith($sourceFile, '.enc')
129
                        ? Str::replaceLast('.enc', '', $sourceFile)
130
                        : $sourceFile.'.dec';
131
        }
132
133
        $sourcePath = $this->getFilePath($sourceFile);
134
        $destPath = $this->getFilePath($destFile);
135
136
        // Create a new encrypter instance
137
        $encrypter = new FileEncrypter($this->key, $this->cipher);
138
139
        // If decryption is successful, delete the source file
140
        if ($encrypter->decrypt($sourcePath, $destPath) && $deleteSource) {
141
            Storage::disk($this->disk)->delete($sourceFile);
142
        }
143
144
        return $this;
145
    }
146
147
    public function decryptCopy($sourceFile, $destFile = null)
148
    {
149
        return self::decrypt($sourceFile, $destFile, false);
150
    }
151
152
    public function streamDecrypt($sourceFile)
153
    {
154
        $this->registerServices();
155
156
        $sourcePath = $this->getFilePath($sourceFile);
157
158
        // Create a new encrypter instance
159
        $encrypter = new FileEncrypter($this->key, $this->cipher);
160
161
        return $encrypter->decrypt($sourcePath, 'php://output');
162
    }
163
164
    protected function getFilePath($file)
165
    {
166
        if ($this->isS3File()) {
167
            return "s3://{$this->adapter->getBucket()}/{$file}";
0 ignored issues
show
The method getBucket cannot be called on $this->adapter (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
168
        }
169
170
        return Storage::disk($this->disk)->path($file);
171
    }
172
173
    protected function isS3File()
174
    {
175
        return $this->disk == 's3';
176
    }
177
178
    protected function setAdapter()
179
    {
180
        if ($this->adapter) {
181
            return;
182
        }
183
184
        $this->adapter = Storage::disk($this->disk)->getAdapter();
0 ignored issues
show
The method getAdapter() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
    }
186
187
    protected function registerServices()
188
    {
189
        $this->setAdapter();
190
191
        if ($this->isS3File()) {
192
            $client = $this->adapter->getClient();
0 ignored issues
show
The method getClient cannot be called on $this->adapter (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
193
            $client->registerStreamWrapper();
194
        }
195
    }
196
}
197