Skip to content

Commit 7fa8fee

Browse files
Copilotswissspidy
andcommitted
Add cross-platform security and improve constant clarity
- Add Windows system directory protection (C:\Windows, C:\Program Files) - Use case-insensitive path comparison for Windows compatibility - Separate MAX_IMAGE_SIZE_BYTES constant for better clarity - Improve documentation for size constants with detailed explanation Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent e9034cb commit 7fa8fee

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

src/AI_Command.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@
3131
class AI_Command extends WP_CLI_Command {
3232

3333
/**
34-
* Maximum size for base64-encoded image data (50MB binary = ~67MB base64).
34+
* Maximum binary image size in bytes (50MB).
3535
*/
36-
const MAX_IMAGE_SIZE_BASE64 = 70000000; // 50 * 1024 * 1024 * 4 / 3 rounded up
36+
const MAX_IMAGE_SIZE_BYTES = 52428800; // 50 * 1024 * 1024
37+
38+
/**
39+
* Maximum size for base64-encoded image data.
40+
* Base64 encoding increases size by ~33%, so 50MB binary = ~67MB base64.
41+
* Using 70MB as safe upper bound.
42+
*/
43+
const MAX_IMAGE_SIZE_BASE64 = 70000000;
3744

3845
/**
3946
* Generates AI content.
@@ -239,9 +246,24 @@ private function generate_image( $builder, $assoc_args ) {
239246
$safe_output_path = $real_parent_dir . DIRECTORY_SEPARATOR . basename( $output_path );
240247

241248
// Prevent writing to sensitive system directories
242-
$forbidden_paths = array( '/etc', '/bin', '/usr/bin', '/sbin', '/usr/sbin', '/boot', '/sys', '/proc' );
249+
$forbidden_paths = array(
250+
// Unix/Linux system directories
251+
'/etc',
252+
'/bin',
253+
'/usr/bin',
254+
'/sbin',
255+
'/usr/sbin',
256+
'/boot',
257+
'/sys',
258+
'/proc',
259+
// Windows system directories (case-insensitive)
260+
'C:\\Windows',
261+
'C:\\Program Files',
262+
'C:\\Program Files (x86)',
263+
);
243264
foreach ( $forbidden_paths as $forbidden ) {
244-
if ( 0 === strpos( $real_parent_dir, $forbidden ) ) {
265+
// Case-insensitive comparison for Windows paths
266+
if ( 0 === stripos( $real_parent_dir, $forbidden ) ) {
245267
WP_CLI::error( 'Cannot write to system directory: ' . $safe_output_path );
246268
}
247269
}

0 commit comments

Comments
 (0)