-
Notifications
You must be signed in to change notification settings - Fork 9.4k
magento/magento2#39720: Can't upload image for custom customer addres… #40426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.4-develop
Are you sure you want to change the base?
Changes from 2 commits
4320612
35cd5ee
4bceb81
5bb007a
9da1e39
d4d3667
500ceaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -194,8 +194,15 @@ public function extractValue(\Magento\Framework\App\RequestInterface $request) | |||||||||||||||||
| */ | ||||||||||||||||||
| protected function _validateByRules($value) | ||||||||||||||||||
| { | ||||||||||||||||||
| $label = $value['name']; | ||||||||||||||||||
| $label = $value['name'] ?? $value['file'] ?? ''; | ||||||||||||||||||
| $rules = $this->getAttribute()->getValidationRules(); | ||||||||||||||||||
|
|
||||||||||||||||||
| // For UI component uploads, get name from file path if not provided | ||||||||||||||||||
| if (empty($value['name']) && !empty($value['file'])) { | ||||||||||||||||||
| $value['name'] = basename($value['file']); | ||||||||||||||||||
| $label = $value['name']; | ||||||||||||||||||
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| $extension = $this->ioFile->getPathInfo($value['name'])['extension']; | ||||||||||||||||||
|
||||||||||||||||||
| $extension = $this->ioFile->getPathInfo($value['name'])['extension']; | |
| // Ensure we have a valid file name before attempting to get extension | |
| if (empty($value['name'])) { | |
| return [__('"%1" is not a valid file.', $label)]; | |
| } | |
| $pathInfo = $this->ioFile->getPathInfo($value['name']); | |
| $extension = $pathInfo['extension'] ?? ''; |
Outdated
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback to an empty string for filePath could bypass validation. If both tmp_name and file are empty/null, an empty string will be passed to _isUploadedFile, which may not properly handle this case. Consider adding explicit validation to ensure a valid file path exists before proceeding with file validation, or return an error if no valid file path is found.
| $filePath = $value['tmp_name'] ?? $value['file'] ?? ''; | |
| $filePath = $value['tmp_name'] ?? $value['file'] ?? null; | |
| if (empty($filePath)) { | |
| return [__('"%1" is not a valid file.', $label)]; | |
| } |
Outdated
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistency in how file size is retrieved between File.php and Image.php. In File.php (line 240), the code constructs a temporary file path as "FileProcessor::TMP_DIR . '/' . basename(value['file'])", while in Image.php (line 154), it uses "mediaEntityTmpReadDirectory->getAbsolutePath(ltrim(value['file'], '/'))" to construct the full path. This inconsistency could lead to different behaviors and makes the codebase harder to maintain. Consider using a consistent approach for constructing temporary file paths across both classes, preferably by extracting this logic into a shared helper method.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -136,17 +136,32 @@ public function __construct( | |||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| protected function _validateByRules($value) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| $label = $value['name']; | ||||||||||||||||||||||||||||||||||
| $label = $value['name'] ?? $value['file'] ?? ''; | ||||||||||||||||||||||||||||||||||
| $rules = $this->getAttribute()->getValidationRules(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // For UI component uploads, get name from file path if not provided | ||||||||||||||||||||||||||||||||||
| if (empty($value['name']) && !empty($value['file'])) { | ||||||||||||||||||||||||||||||||||
| $value['name'] = basename($value['file']); | ||||||||||||||||||||||||||||||||||
| $label = $value['name']; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Determine file path for validation | ||||||||||||||||||||||||||||||||||
| $filePath = $value['tmp_name'] ?? null; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // For UI component uploads, construct the full temporary file path | ||||||||||||||||||||||||||||||||||
| if (empty($filePath) && !empty($value['file'])) { | ||||||||||||||||||||||||||||||||||
| $tmpFileName = ltrim($value['file'], '/'); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| $tmpFileName = ltrim($value['file'], '/'); | |
| $tmpFileName = ltrim($value['file'], '/'); | |
| // Basic path validation to prevent traversal and suspicious paths | |
| $hasNullByte = strpos($tmpFileName, "\0") !== false; | |
| $hasTraversal = (bool)preg_match('#(^|/)\.\.(?:/|$)#', $tmpFileName); | |
| $isWindowsAbsolute = (bool)preg_match('#^[a-zA-Z]:[\\\\/]#', $tmpFileName); | |
| $startsWithBackslash = isset($tmpFileName[0]) && $tmpFileName[0] === '\\'; | |
| if ($hasNullByte || $hasTraversal || $isWindowsAbsolute || $startsWithBackslash) { | |
| return [__('"%1" is not a valid file.', $label)]; | |
| } |
Outdated
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new UI component upload functionality (handling of value['file']) lacks test coverage. While the repository has comprehensive unit tests for the Image class, there are no tests added to verify that the new code paths for UI component uploads work correctly. Consider adding unit tests that cover scenarios where value['file'] is provided instead of value['tmp_name'], including validation of the mediaEntityTmpReadDirectory path construction and file size retrieval.
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filePath variable could be null or an invalid path, but is passed directly to getimagesize without validation. If both tmp_name and file are empty/null, this could result in getimagesize receiving null or an empty string, potentially causing warnings or unexpected behavior. Consider adding explicit validation to ensure filePath is not null/empty before calling getimagesize, or handle the case more gracefully.
| // Ensure we have a valid file path before attempting to read image properties | |
| if (empty($filePath) || !is_string($filePath) || !file_exists($filePath)) { | |
| return [__('"%1" is not a valid file.', $label)]; | |
| } |
Outdated
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code doesn't handle the case where value['file'] might be empty after checking !empty, and ltrim could return an empty string if value['file'] is just '/'. This would result in getAbsolutePath being called with an empty string, which could lead to unexpected behavior. Consider adding validation to ensure tmpFileName is not empty before constructing the file path.
| $filePath = $this->mediaEntityTmpReadDirectory->getAbsolutePath($tmpFileName); | |
| } | |
| if ($tmpFileName !== '') { | |
| $filePath = $this->mediaEntityTmpReadDirectory->getAbsolutePath($tmpFileName); | |
| } | |
| } | |
| if (empty($filePath)) { | |
| return [__('"%1" is not a valid file.', $label)]; | |
| } |
Outdated
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filesize function is called directly without checking if it returned false. If the file doesn't exist or is inaccessible, filesize returns false, which would then be compared against maxFileSize. Consider checking the result of filesize before using it in the comparison or handling the false case explicitly.
| $size = filesize($filePath); | |
| $fileSize = filesize($filePath); | |
| if ($fileSize !== false) { | |
| $size = $fileSize; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new UI component upload functionality (handling of value['file']) lacks test coverage. While the repository has comprehensive unit tests for the File and Image classes, there are no tests added to verify that the new code paths for UI component uploads work correctly. Consider adding unit tests that cover scenarios where value['file'] is provided instead of value['tmp_name'], including edge cases like empty strings, path traversal attempts, and proper file size calculation.