我有一个API,可以接受base64编码的图像数据,并且需要对数据进行解码,保存图像文件,然后从该图像创建缩略图。
我担心如果在尝试创建缩略图之前未正确验证POST负载的内容,便可能会执行恶意代码。
到目前为止,我的基本工作流程如下。是否有足够的验证让我不必担心安全性?我想我担心有人编码不好的东西,然后调用下面的图像功能之一,互联网就会爆炸。
<?php
$decodedImage = base64_decode($_POST["canvas"]);
if ($decodedImage === false) {
// Error out
}
$imageSizeValidation = getimagesizefromstring($decodedImage);
if ($imageSizeValidation[0] < 1 || $imageSizeValidation[1] < 1 || !$imageSizeValidation['mime']) {
// Error out
}
$tempFilePath = "/tmp/" . microtime(true) . "-canvas-web.jpg";
file_put_contents($tempFilePath, $decodedImage);
$originalWidth = $imageSizeValidation[0];
$originalHeight = $imageSizeValidation[1];
$newWidth = 49;
$newHeight = 49;
$scaleWidth = $newWidth / $originalWidth;
$scaleHeight = $newHeight / $originalHeight;
$scale = min($scaleWidth, $scaleHeight);
$width = (int)($originalWidth * $scale);
$height = (int)($originalHeight * $scale);
$xpos …
Run Code Online (Sandbox Code Playgroud)