|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CRUDlex package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CRUDlex; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* An implementation of the {@see FileProcessorInterface} simply using the |
|
20
|
|
|
* file system. |
|
21
|
|
|
*/ |
|
22
|
|
|
class SimpleFilesystemFileProcessor implements FileProcessorInterface { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Holds the base path where all files will be stored into subfolders. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $basePath; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructs a file system path for the given parameters for storing the |
|
31
|
|
|
* file of the file field. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $entityName |
|
34
|
|
|
* the entity name |
|
35
|
|
|
* @param Entity $entity |
|
36
|
|
|
* the entity |
|
37
|
|
|
* @param string $field |
|
38
|
|
|
* the file field in the entity |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
* the constructed path for storing the file of the file field |
|
42
|
|
|
*/ |
|
43
|
4 |
|
protected function getPath($entityName, Entity $entity, $field) { |
|
44
|
4 |
|
return $this->basePath.$entity->getDefinition()->getField($field, 'path').'/'.$entityName.'/'.$entity->get('id').'/'.$field; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $basePath |
|
51
|
|
|
* the base path where all files will be stored into subfolders |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function __construct($basePath = '') { |
|
54
|
5 |
|
$this->basePath = $basePath; |
|
55
|
5 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function createFile(Request $request, Entity $entity, $entityName, $field) { |
|
61
|
4 |
|
$file = $request->files->get($field); |
|
62
|
4 |
|
if ($file) { |
|
63
|
4 |
|
$targetPath = $this->getPath($entityName, $entity, $field); |
|
64
|
4 |
|
if (!file_exists($targetPath)) { |
|
65
|
4 |
|
mkdir($targetPath, 0777, true); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
4 |
|
$file->move($targetPath, $file->getClientOriginalName()); |
|
68
|
|
|
} |
|
69
|
4 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
* For now, this implementation is defensive and doesn't delete ever. |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function updateFile(Request $request, Entity $entity, $entityName, $field) { |
|
76
|
|
|
// We could first delete the old file, but for now, we are defensive and don't delete ever. |
|
77
|
1 |
|
$this->createFile($request, $entity, $entityName, $field); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
* For now, this implementation is defensive and doesn't delete ever. |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function deleteFile(Entity $entity, $entityName, $field) { |
|
85
|
|
|
// For now, we are defensive and don't delete ever. |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function renderFile(Entity $entity, $entityName, $field) { |
|
92
|
1 |
|
$targetPath = $this->getPath($entityName, $entity, $field); |
|
93
|
1 |
|
$fileName = $entity->get($field); |
|
94
|
1 |
|
$file = $targetPath.'/'.$fileName; |
|
95
|
1 |
|
$response = new Response(''); |
|
96
|
1 |
|
$mimeTypes = new MimeTypes(); |
|
97
|
1 |
|
$mimeType = $mimeTypes->getMimeType($file); |
|
98
|
1 |
|
$size = filesize($file); |
|
99
|
1 |
|
if ($fileName && file_exists($file)) { |
|
100
|
1 |
|
$streamedFileResponse = new StreamedFileResponse(); |
|
101
|
1 |
|
$response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, [ |
|
102
|
1 |
|
'Content-Type' => $mimeType, |
|
103
|
1 |
|
'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
|
104
|
1 |
|
'Content-length' => $size |
|
105
|
|
|
]); |
|
106
|
1 |
|
$response->send(); |
|
107
|
|
|
} |
|
108
|
1 |
|
return $response; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getFile(Entity $entity, $entityName, $field) { |
|
116
|
|
|
$targetPath = $this->getPath($entityName, $entity, $field); |
|
117
|
|
|
$fileName = $entity->get($field); |
|
118
|
|
|
$file = $targetPath.'/'.$fileName; |
|
119
|
|
|
|
|
120
|
|
|
return fopen($file, 'r'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
$targetPathcan contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.8 paths for user data to reach this point
$_FILES,and$_FILESis passed to Request::createRequestFromFactory() in Request.php on line 317$_FILES,and$_FILESis passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$filesis passed to Request::__construct()in vendor/Request.php on line 2018
$filesis passed to Request::initialize()in vendor/Request.php on line 258
$filesis passed to FileBag::__construct()in vendor/Request.php on line 280
$parametersis passed to FileBag::replace()in vendor/FileBag.php on line 33
$filesis passed to FileBag::add()in vendor/FileBag.php on line 42
$fileis assignedin vendor/FileBag.php on line 62
$fileis passed to FileBag::set()in vendor/FileBag.php on line 63
$valueis passed to FileBag::convertFileInformation()in vendor/FileBag.php on line 54
$fileis assignedin vendor/FileBag.php on line 80
$file['name']is passed to UploadedFile::__construct()in vendor/FileBag.php on line 89
in vendor/File/File.php on line 130
in vendor/File/UploadedFile.php on line 90
in vendor/File/UploadedFile.php on line 109
$file->getClientOriginalName()is passed to Entity::set()in src/CRUDlex/Entity.php on line 146
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
$_SERVER,and$serveris assigned in Request.php on line 307$_SERVER,and$serveris assignedin vendor/Request.php on line 307
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 310HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 310
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 313HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 313
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 383$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 383
$serveris assignedin vendor/Request.php on line 431
$serveris assignedin vendor/Request.php on line 432
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 434
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 43
$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 282
$valuesis assignedin vendor/HeaderBag.php on line 31
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 143
in vendor/HeaderBag.php on line 146
in vendor/HeaderBag.php on line 67
$headersis assignedin vendor/HeaderBag.php on line 115
$requestUriis assignedin vendor/Request.php on line 1788
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1819
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 282
$valuesis assignedin vendor/HeaderBag.php on line 31
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 143
in vendor/HeaderBag.php on line 146
in vendor/HeaderBag.php on line 67
$headersis assignedin vendor/HeaderBag.php on line 115
$requestUriis assignedin vendor/Request.php on line 1788
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1819
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$arrayis assignedin src/CRUDlex/Entity.php on line 155
$arrayis passed through array_map()in src/CRUDlex/Entity.php on line 159
$manyis assignedin src/CRUDlex/Entity.php on line 157
$manyis passed to Entity::set()in src/CRUDlex/Entity.php on line 160
in src/CRUDlex/Entity.php on line 77
$valueis assignedin src/CRUDlex/Entity.php on line 119
in src/CRUDlex/SimpleFilesystemFileProcessor.php on line 44
$targetPathis assignedin src/CRUDlex/SimpleFilesystemFileProcessor.php on line 63
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: