-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsImage.php
More file actions
283 lines (247 loc) · 7.65 KB
/
IsImage.php
File metadata and controls
283 lines (247 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Neuron\Validation;
/**
* Validates image data, supporting base64 encoded images with optional MIME type and size constraints.
*/
class IsImage extends Base
{
private array $allowedMimeTypes;
private ?int $maxSize;
private bool $checkImageData;
private bool $allowSvg;
/**
* @param array $allowedMimeTypes List of allowed MIME types (e.g., ['image/jpeg', 'image/png'])
* @param int|null $maxSize Maximum file size in bytes (null for no limit)
* @param bool $checkImageData Whether to validate the actual image data (requires decoding)
* @param bool $allowSvg Whether to allow SVG images (default: false for security - SVG can contain scripts)
*/
public function __construct(
array $allowedMimeTypes = [ 'image/jpeg', 'image/png', 'image/gif', 'image/webp' ],
?int $maxSize = null,
bool $checkImageData = true,
bool $allowSvg = false
)
{
parent::__construct();
$this->allowedMimeTypes = $allowedMimeTypes;
$this->maxSize = $maxSize;
$this->checkImageData = $checkImageData;
$this->allowSvg = $allowSvg;
// If SVG is explicitly allowed AND we have MIME type restrictions, add SVG to allowed list
// Don't add if allowedMimeTypes is empty (meaning allow all types)
if( $this->allowSvg && !empty( $this->allowedMimeTypes ) && !in_array( 'image/svg+xml', $this->allowedMimeTypes, true ) )
{
$this->allowedMimeTypes[] = 'image/svg+xml';
}
}
/**
* @param mixed $value
* @return bool
*/
protected function validate( mixed $value ) : bool
{
if( !is_string( $value ) )
{
return false;
}
// Empty string is not a valid image
if( $value === '' )
{
return false;
}
// Check if it's a data URI
if( strpos( $value, 'data:' ) === 0 )
{
return $this->validateDataUri( $value );
}
// Otherwise, treat it as base64 encoded image data
return $this->validateBase64Image( $value );
}
/**
* Validates a data URI formatted image.
*
* @param string $dataUri
* @return bool
*/
private function validateDataUri( string $dataUri ) : bool
{
// Parse data URI: data:[<mediatype>][;base64],<data>
// Use 's' modifier to allow . to match newlines in base64 data
$pattern = '/^data:([a-zA-Z0-9][a-zA-Z0-9\/+\-]*);base64,(.+)$/s';
if( !preg_match( $pattern, $dataUri, $matches ) )
{
return false;
}
$mimeType = strtolower( $matches[1] ); // Normalize to lowercase per RFC 2045
$base64Data = $matches[2];
// Check MIME type (case-insensitive per RFC 2045)
if( !empty( $this->allowedMimeTypes ) && !$this->isMimeTypeAllowed( $mimeType ) )
{
return false;
}
// Validate base64 data
return $this->validateBase64Image( $base64Data );
}
/**
* Validates base64 encoded image data.
*
* @param string $base64Data
* @return bool
*/
private function validateBase64Image( string $base64Data ) : bool
{
// Remove any whitespace
$cleanData = preg_replace( '/\s+/', '', $base64Data );
// Attempt to decode
$decoded = base64_decode( $cleanData, true );
if( $decoded === false )
{
return false;
}
// Check size constraint
if( $this->maxSize !== null && strlen( $decoded ) > $this->maxSize )
{
return false;
}
// Always validate image data to check MIME type restrictions
// Even when checkImageData is false, we need to detect the type for MIME validation
// but we can skip the more expensive image content validation
if( !empty( $this->allowedMimeTypes ) )
{
// Detect the image type from signatures
$detectedType = $this->detectImageType( $decoded );
// Check for SVG if:
// 1. allowSvg is true (general SVG support enabled), OR
// 2. 'image/svg+xml' is explicitly in allowedMimeTypes
$svgExplicitlyAllowed = $this->isMimeTypeAllowed( 'image/svg+xml' );
if( $detectedType === null && ( $this->allowSvg || $svgExplicitlyAllowed ) )
{
$detectedType = $this->detectSvg( $decoded );
}
// If we couldn't detect a type, it's not a valid image
if( $detectedType === null )
{
return false;
}
// Check if detected type is allowed
if( !$this->isMimeTypeAllowed( $detectedType ) )
{
return false;
}
}
elseif( $this->checkImageData )
{
// No MIME restrictions but need to check if it's a recognizable image
return $this->isRecognizableImage( $decoded );
}
return true;
}
/**
* Detects image type from binary data signatures.
*
* @param string $imageData
* @return string|null Returns MIME type if detected, null otherwise
*/
private function detectImageType( string $imageData ) : ?string
{
// Check for common image file signatures (magic numbers)
$signatures = [
// JPEG
"\xFF\xD8\xFF" => 'image/jpeg',
// PNG
"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" => 'image/png',
// GIF
"GIF87a" => 'image/gif',
"GIF89a" => 'image/gif',
// WebP
"RIFF" => 'image/webp', // Note: WebP also needs WEBP at offset 8
];
$dataStart = substr( $imageData, 0, 20 );
// Check for image signatures
foreach( $signatures as $signature => $mimeType )
{
if( strpos( $dataStart, $signature ) === 0 )
{
// Special handling for WebP
if( $signature === 'RIFF' && substr( $imageData, 8, 4 ) !== 'WEBP' )
{
continue;
}
return $mimeType;
}
}
return null;
}
/**
* Checks if the decoded data is a recognizable image format.
* This method only validates that the data contains valid image signatures,
* without checking MIME type restrictions (caller's responsibility).
*
* @param string $imageData
* @return bool
*/
private function isRecognizableImage( string $imageData ) : bool
{
// Use the extracted method to detect image type
$detectedType = $this->detectImageType( $imageData );
// Check for SVG if:
// 1. allowSvg is true (general SVG support enabled), OR
// 2. 'image/svg+xml' is explicitly in allowedMimeTypes (if any restrictions exist)
$svgExplicitlyAllowed = !empty( $this->allowedMimeTypes ) && $this->isMimeTypeAllowed( 'image/svg+xml' );
if( $detectedType === null && ( $this->allowSvg || $svgExplicitlyAllowed ) )
{
$detectedType = $this->detectSvg( $imageData );
}
// Return true if we recognized any valid image format
return $detectedType !== null;
}
/**
* Checks if a MIME type is in the allowed list (case-insensitive per RFC 2045).
*
* @param string $mimeType
* @return bool
*/
private function isMimeTypeAllowed( string $mimeType ) : bool
{
$normalizedMimeType = strtolower( $mimeType );
foreach( $this->allowedMimeTypes as $allowed )
{
if( strtolower( $allowed ) === $normalizedMimeType )
{
return true;
}
}
return false;
}
/**
* Detects if the data is a valid SVG image.
* SVG detection is more permissive but requires explicit opt-in for security.
*
* @param string $imageData
* @return string|null Returns 'image/svg+xml' if valid SVG, null otherwise
*/
private function detectSvg( string $imageData ) : ?string
{
// Only check first 1KB for performance
$dataToCheck = substr( $imageData, 0, 1024 );
// Remove UTF-8 BOM if present (exact 3-byte sequence)
if( substr( $dataToCheck, 0, 3 ) === "\xEF\xBB\xBF" )
{
$dataToCheck = substr( $dataToCheck, 3 );
}
// Case-insensitive check for <svg tag with proper namespace
// Must find an actual SVG element with namespace declaration
// This regex ensures xmlns is an attribute, not just text content
if( preg_match( '/<svg\b[^>]*xmlns\s*=\s*["\']http:\/\/www\.w3\.org\/2000\/svg["\'][^>]*>/i', $dataToCheck ) )
{
// Valid SVG with proper namespace declaration
return 'image/svg+xml';
}
// Also check for xmlns:svg pattern (less common but valid)
if( preg_match( '/<svg\b[^>]*xmlns:svg\s*=\s*["\']http:\/\/www\.w3\.org\/2000\/svg["\'][^>]*>/i', $dataToCheck ) )
{
return 'image/svg+xml';
}
return null;
}
}