Issues (15)

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.

lib/Service/FileService.php (4 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
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING file.
8
 * 
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Service;
13
14
use OCA\Ocr\Db\FileMapper;
15
use OCA\Ocr\Db\File;
16
use OCP\ILogger;
17
use OCA\Ocr\Db\ShareMapper;
18
use OCP\IL10N;
19
use OCA\Ocr\Constants\OcrConstants;
20
use OCA\Ocr\Util\FileUtil;
21
22
23
/**
24
 * Class FileService
25
 * 
26
 * @package OCA\Ocr\Service
27
 */
28
class FileService {
29
30
    /**
31
     *
32
     * @var ILogger
33
     */
34
    private $logger;
35
36
    /**
37
     *
38
     * @var FileMapper
39
     */
40
    private $fileMapper;
41
42
    /**
43
     *
44
     * @var ShareMapper
45
     */
46
    private $shareMapper;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    private $userId;
53
54
    /**
55
     *
56
     * @var IL10N
57
     */
58
    private $l10n;
59
60
    /**
61
     *
62
     * @var FileUtil
63
     */
64
    private $fileUtil;
65
66 23
    public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper, 
67
            FileUtil $fileUtil) {
68 23
        $this->l10n = $l10n;
69 23
        $this->logger = $logger;
70 23
        $this->userId = $userId;
71 23
        $this->fileMapper = $fileMapper;
72 23
        $this->shareMapper = $shareMapper;
73 23
        $this->fileUtil = $fileUtil;
74 23
    }
75
76
    /**
77
     * Checks if shared with the process initiator
78
     * 
79
     * @param File $fileInfo            
80
     * @return boolean
81
     */
82 2
    public function checkSharedWithInitiator($fileInfo) {
83 2
        $owner = str_replace('home::', '', $fileInfo->getStoragename());
84 2
        if ($this->userId === $owner) {
85
            // user is owner (no shared file)
86 1
            return false;
87
        } else {
88
            // user is not owner (shared file)
89 1
            return true;
90
        }
91
    }
92
93
    /**
94
     * Builds the target name.
95
     * 
96
     * @param File $fileInfo            
97
     * @param boolean $shared            
98
     * @param boolean $replace            
99
     * @return string
100
     */
101 8
    public function buildTarget($fileInfo, $shared, $replace) {
102 8
        if ($shared) {
103 4
            $target = $this->buildTargetForShared($fileInfo, $replace);
104
        } else {
105 4
            $target = $this->buildTargetNotForShared($fileInfo, $replace);
106
        }
107 8
        return $target;
108
    }
109
110
    /**
111
     * Builds the source name.
112
     * 
113
     * @param File $fileInfo            
114
     * @param boolean $shared            
115
     * @return string
116
     */
117 2
    public function buildSource($fileInfo, $shared) {
118 2
        $source = $fileInfo->getPath();
119 2
        if ($shared) {
120 1
            $source = str_replace('home::', '', $fileInfo->getStoragename()) . '/' . $source;
121
        } else {
122 1
            $source = $this->userId . '/' . $source;
123
        }
124 2
        return $source;
125
    }
126
127
    /**
128
     * Returns the fileInfo for each file in files and checks
129
     * if it has a allowed MIME type and some other conditions.
130
     * 
131
     * @param array $files            
132
     * @return File[]
133
     * @throws NotFoundException
134
     */
135 4
    public function buildFileInfo($files) {
136 4
        $fileArray = array();
137 4
        foreach ($files as $file) {
138
            // Check if anything is missing and file type is correct
139 4
            if (!empty($file['id'])) {
140 3
                $fileInfo = $this->fileMapper->find($file['id']);
141 3
                $this->checkMimeType($fileInfo);
142 2
                array_push($fileArray, $fileInfo);
143
            } else {
144 3
                throw new NotFoundException($this->l10n->t('Wrong parameter.'));
145
            }
146
        }
147 1
        return $fileArray;
148
    }
149
150
    /**
151
     * Determines the correct type for the ocr process worker.
152
     * 
153
     * @param File $fileInfo            
154
     * @return integer
155
     */
156 2
    public function getCorrectType($fileInfo) {
157 2
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
158 1
            return OcrConstants::OCRmyPDF;
159
        } else {
160 1
            return OcrConstants::TESSERACT;
161
        }
162
    }
163
164
    /**
165
     * Returns a not existing file name for pdf or image processing.
166
     * 
167
     * @param File $fileInfo            
168
     * @param boolean $replace            
169
     * @return string
170
     */
171 4
    private function buildTargetForShared(File $fileInfo, $replace) {
172 4
        $share = $this->shareMapper->find($fileInfo->getFileid(), $this->userId, 
173 4
                str_replace('home::', '', $fileInfo->getStoragename()));
174
        // get rid of the .png or .pdf and so on
175
        // '/thedom.png' => '/thedom' || '/Test/thedom.png' => '/Test/thedom'
176 4
        $fileName = substr($share->getFileTarget(), 0, (strrpos($share->getFileTarget(), '.')));
177
        // remove everything in front of and including of the first appearance of a slash from behind
178
        // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom'
179 4
        $fileName = substr(strrchr($fileName, "/"), 1);
180
        // eliminate the file name from the path
181
        // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test'
182 4
        $filePath = dirname($share->getFileTarget());
183
        // replace the first slash
184 4
        $pos = strpos($filePath, '/');
185 4
        if ($pos !== false) {
186
            // '/' => '' || '/Test/' => 'Test'
187 4
            $filePath = substr_replace($filePath, '', $pos, strlen('/'));
188
        }
189 4 View Code Duplication
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
            // PDFs:
191 3
            if ($replace) {
192 2
                if($filePath === '/') {
193
                    $filePath = '';
194
                }
195 2
                return $filePath . '/'. $fileName . '.pdf';
196
            } else {
197 1
                return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
198
            }
199
        } else {
200
            // IMAGES:
201 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
202
        }
203
    }
204
205
    /**
206
     * Returns a not existing file name for PDF or image processing.
207
     * 
208
     * @param File $fileInfo            
209
     * @param boolean $replace            
210
     * @return string
211
     */
212 4
    private function buildTargetNotForShared(File $fileInfo, $replace) {
213
        // get rid of the .png or .pdf and so on
214
        // 'thedom.png' => 'thedom'
215 4
        $fileName = substr($fileInfo->getName(), 0, (strrpos($fileInfo->getName(), '.')));
0 ignored issues
show
Consider using $fileInfo->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
216
        // eliminate the file name from the path
217
        // 'files/Test/thedom.png' => 'files/Test/' || 'files/thedom.png' => 'files/'
218 4
        $filePath = str_replace($fileInfo->getName(), '', $fileInfo->getPath());
0 ignored issues
show
Consider using $fileInfo->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
219
        // and get the path on top of the files/ dir
220
        // 'files/Test/' => '/Test/' || 'files/' => '/'
221 4
        $filePath = str_replace('files', '', $filePath);
222
        // remove the last slash
223
        // '/Test/' => '/Test' || '/' => ''
224 4
        $filePath = substr_replace($filePath, '', strrpos($filePath, '/'), strlen('/'));
225
        // replace the first slash
226 4
        $pos = strpos($filePath, '/');
227 4
        if ($pos !== false) {
228
            // '/Test' => '// 'Test' || '/' => ''
229 3
            $filePath = substr_replace($filePath, '', $pos, strlen('/'));
230
        }
231 4 View Code Duplication
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
            // PDFs:
233 3
            if ($replace) {
234 2
                if($filePath === '/') {
235
                    $filePath = '';
236
                }
237 2
                return $filePath . '/' . $fileName . '.pdf';
238
            } else {
239 1
                return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
240
            }
241
        } else {
242
            // IMAGES:
243 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
244
        }
245
    }
246
247
    /**
248
     * Checks a MIME type for a specifically given FileInfo.
249
     * 
250
     * @param File $fileInfo            
251
     */
252 3
    private function checkMimeType(File $fileInfo) {
253 3
        if (!$fileInfo || !in_array($fileInfo->getMimetype(), OcrConstants::ALLOWED_MIME_TYPES)) {
254 1
            $this->logger->debug('Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.', ['app' => OcrConstants::APP_NAME]);
255 1
            throw new NotFoundException($this->l10n->t('Wrong MIME type.'));
256
        }
257
    }
258
}