Welcome Guest, Not a member yet? Register   Sign In
Stress testing and can't track error messages
#1

(This post was last modified: 01-19-2022, 03:42 PM by BilltheCat.)

I wrote a little avatar upload method with Cropper.js, which works great, but I'm trying to stress test it, so I set the permissions on the original image that the upload is overwriting to 444. I can see it fail in the log perfectly, but what's going on with the rest of it I can't tell.

Cropper takes the cropped image portion and turns it into a PNG base64 string, so I had to (I think) write a custom validation to check that it's a good image.  Here's everything I'm doing.

Controller Method:
PHP Code:
public function upload()
    {
        $validated $this->validate(['image' => 'required|base64imagestring']);
        $msg '';
        if ($validated) {
            $avatar $this->request->getPost('image');

            $image_array_1 explode(";"$avatar);
            $image_array_2 explode(","$image_array_1[1]);
            $data base64_decode($image_array_2[1]);

            $image_name WRITEPATH 'uploads/avatars/' $this->data['user']->customerid '.png';
            if (!file_put_contents($image_name$data)) {
                $msg lang('validation.b64ImageSave');
            }
        } else {
            $msg $this->validator->listErrors();
        }
        echo $msg;
    


Custom Validation Rule:
PHP Code:
namespace App\Validation;

class 
CustomRules
{

    //Rule is to validate base64 image strings can be converted into a real image    
    public function base64imagestring(string $strstring &$error null): bool
    
{
        $b64 explode(";base64,"$str);

        $data base64_decode($b64[1]);

        $im imagecreatefromstring($data);
        if ($im !== false) {
            imagepng($im'testimage.png');
            if (getimagesize('testimage.png')) {
                unlink('testimage.png');
                return TRUE;
            }
        $error lang('validation.invalidB64ImageString');
        return false;
        }
    }


Language File Validation:
PHP Code:
return [

    'invalidB64ImageString'              => 'The image is not a valid base64 string',
    'b64ImageSave'                      => 'The image could not be saved'  
]; 



Log Messages:
Quote:CRITICAL - 2022-01-19 16:15:56 --> file_put_contents(/home/mysite/CI4path/writable/uploads/avatars/145208.png): failed to open stream: Permission denied
#0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'file_put_conten...', '/home/mysite/CI4pa...', 132, Array)
#1 /home/mysite/CI4path/app/Controllers/User/Avatar.php(132): file_put_contents('/home/mysite/CI4pa...', '\x89PNG\r\n\x1A\n\x00\x00\x00\rIHD...')
#2 /home/mysite/CI4path/vendor/codeigniter4/framework/system/CodeIgniter.php(825): App\Controllers\User\Avatar->upload()
#3 /home/mysite/CI4path/vendor/codeigniter4/framework/system/CodeIgniter.php(412): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\User\Avatar))
#4 /home/mysite/CI4path/vendor/codeigniter4/framework/system/CodeIgniter.php(320): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#5 /home/mysite/public_html/index.php(36): CodeIgniter\CodeIgniter->run()
#6 {main}
CRITICAL - 2022-01-19 16:15:56 --> Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Malformed UTF-8 characters, possibly incorrectly encoded". in /home/mysite/CI4path/vendor/codeigniter4/framework/system/Format/JSONFormatter.php:41
Stack trace:
#0 /home/mysite/CI4path/vendor/codeigniter4/framework/system/Format/JSONFormatter.php(41): CodeIgniter\Format\Exceptions\FormatException::forInvalidJSON('Malformed UTF-8...')
#1 /home/mysite/CI4path/vendor/codeigniter4/framework/system/API/ResponseTrait.php(341): CodeIgniter\Format\JSONFormatter->format(Array)
#2 /home/mysite/CI4path/vendor/codeigniter4/framework/system/API/ResponseTrait.php(99): CodeIgniter\Debug\Exceptions->format(Array)
#3 /home/mysite/CI4path/vendor/codeigniter4/framework/system/Debug/Exceptions.php(115): CodeIgniter\Debug\Exceptions->respond(Array, 500)
#4 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException))
#5 {main}
  thrown
#0 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler()
#1 {main}
Reply
#2

> I'm trying to stress test it, so I set the permissions on the original image that the upload is overwriting to 444.

I don't get what you say. What did you do exactly?
The permission 444 means read only.
Reply
#3

Hi @kenjis,
I'm trying to make it fail in testing, so that I can add logic to account for possible real-world issues. By setting the read only permission, I'm testing if the file could not be saved to disk.
This testing is not the issue I'm asking about though, it's the rest of the log file detail that I can't account for.

Code:
Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Malformed UTF-8 characters, possibly incorrectly encoded". in /home/mysite/CI4path/vendor/codeigniter4/framework/system/Format/JSONFormatter.php:41
Where did this come from? I'm not even using any json in my code.
Reply
#4

As you see, it comes from Debug/Exceptions.php -> API/ResponseTrait.php -> Format/JSONFormatter.php.
It is because of the first error.

#0 /home/mysite/CI4path/vendor/codeigniter4/framework/system/Format/JSONFormatter.php(41): CodeIgniter\Format\Exceptions\FormatException::forInvalidJSON('Malformed UTF-8...')
#1 /home/mysite/CI4path/vendor/codeigniter4/framework/system/API/ResponseTrait.php(341): CodeIgniter\Format\JSONFormatter->format(Array)
#2 /home/mysite/CI4path/vendor/codeigniter4/framework/system/API/ResponseTrait.php(99): CodeIgniter\Debug\Exceptions->format(Array)
#3 /home/mysite/CI4path/vendor/codeigniter4/framework/system/Debug/Exceptions.php(115): CodeIgniter\Debug\Exceptions->respond(Array, 500)
#4 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException))
Reply




Theme © iAndrew 2016 - Forum software by © MyBB