As part of a bigger project that I am working on, I need to write a PHP script to convert a Base64 string into an image. The way I would like to go about passing the Base64 string into the script via a GET query, so...
example.com/base64ToPng.php?base64=thisIsTheBase64ForTheImage
I would then like to convert the Base64 that is passed in to a PNG image and display it as if the PHP file itself were an image. While this is not an issue from a development standpoint, I am concerned about the safety of doing this. Would I put myself in any danger since the Base64 is going to be created by the user? (Or more accurately, the Base64 is going to be created by a program based on the image the user provided it.) I am particularly worried about the user crafting a malicious image that could execute arbitrary code, but I do not know if this is a valid concern given that the image is only going to be recreated by the PHP script but not stored on the server.
<?php
if(!isset($_GET['x'])) {
die();
}
$image = base64_decode($_GET['x']);
header ('Content-type: image/png');
imagepng($image, NULL, 0);
?>
<img src="data:image/png;base64, <?php echo base64decode($userinput) ?>" />
you're gonna have problems with escaping the decoded output. One could give you the base64 of">/ <script> alert("xss");</script>
and it would trigger an XSS. The implementation could also choose to do the PNG decoding by piping the user input into a root-shell with likeexec("base64 -d $userinput > image.png"); read_echo_and_delete_file("image.png");
, with an obvious code exec vuln. Further questions can't be answered without the implementation.\x89PNG
, the filter could be bypassed easily with a fake header and then some other string.