1

I'm using this with html2canvas.js to generate and save images from HTML.

I use url params to make this work - eg: website.com/?price=10&name=xxx

All ok untill here - the script works fine - images are saved in /cart/ dir

<?php
$image = $_POST['image'];
$username = $_POST['username'];
$front_class = $_POST['front_plass'];
$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image));
$date = date('d-M-Y-h-i-a', time());
$curdir = getcwd();
$cartDir = $curdir ."/cart";
$userDir = $cartDir.'/'.$username;
if (!file_exists($userDir)) {
    mkdir($cartDir.'/'.$username, 0777);
}
$name = $front_class."-front-".$date.".png";
$full_path = $userDir.'/'.$name;
$name1 = 'cart/'.$username.'/'.$name;
function ImageFillAlpha($image, $color) {
    imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $color);
}
function imageCreateCorners($sourceImageFile, $name, $radius) {
...
}
file_put_contents($full_path, $decoded);
imageCreateCorners($full_path, $name, 25);
echo '<img src="'.$name1.'" alt="front" id="front_img" />'; 
?>

And the js

 html2canvas($('#front'), {
        "logging": true,
        //"proxy":"html2canvasproxy.php",
        "onrendered": function(canvas){
               var dataURL = canvas.toDataURL("image/png");
               $.post('image_front.php',{
                    image: dataURL,
                    username: username,
                    front_class: frontClass
               },function(data){
                    $('.imageHolder_front').html(data);
               });
        }
});

The problem is that someone hacked me twice yesterday thought this and I need to protect the $_POST or the params can be the problem?

Any help here please? I'm not really good with backend development - more with frontend.

Thanks.

Adrian
  • 491
  • 6
  • 23
  • 8
    You don't validate your data. The first rule of programming is to assume your users are malicious. (The second is to assume they're stupid). – John Conde Nov 05 '15 at 13:40
  • @JohnConde I can't validate **username** because this can use any type of charaters. – Adrian Nov 05 '15 at 13:42
  • You should also have some CSRF (Cross-site request forgery)-protection, because I'm guessing you're building the URL with the params from a form? – M. Eriksson Nov 05 '15 at 13:43
  • the URL is automatically generated by js script - on click the url gets the params – Adrian Nov 05 '15 at 13:45
  • 2
    @Adrian You *can* validate the username is a real one. – John Conde Nov 05 '15 at 13:46
  • 2
    why are some fixed on the use of a database? this question has no code to support it, nor is there any mention of it. – Funk Forty Niner Nov 05 '15 at 13:51
  • I don't use a database for this and is not a duplicate question – Adrian Nov 05 '15 at 13:55
  • @Adrian I know that which is why it's a "possible" dupe. That link talks about databases, sure. However it does talk about functions that you can use in conjunction with. I took that link from one of the answers given here in your question http://stackoverflow.com/a/33546140/ – Funk Forty Niner Nov 05 '15 at 13:56
  • Have a look through this http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php – Funk Forty Niner Nov 05 '15 at 14:00
  • @Adrian you say it's not a duplicate, yet you accepted the answer that contained that link. I've closed your question as a duplicate. – Funk Forty Niner Nov 05 '15 at 14:08

4 Answers4

0

Sanitize your user input. In general: Don't ever ever ever trust user input!

I would recommend the very good writeup from @Charles in the first answer of this question: What are the best PHP input sanitizing functions?

pmayer
  • 341
  • 2
  • 13
0

You made a couple of big mistakes.

First, validate your POST data as @JohnConde said, don't use them directly in your code, ever.

Second, don't create directory with 777 permission on your server, since everybody will be able to write into it and hack you that way.

cakan
  • 2,099
  • 5
  • 32
  • 42
0

You cannot "protect" parameters. Your server is a box which receives arbitrary HTTP requests and returns HTTP response. Realise this: anybody can send any arbitrary HTTP request to your server at any time containing any data they wish. You do not control what somebody sends you. The only thing you control is what you do with this data. Expect this data to not conform to your expectations. In fact, expect it to be malicious. Validate it instead of assuming it conforms to any particular format. Never blindly use user provided data in something like SQL queries or in constructing file paths without escaping/binding/validating/confirming the data, or you might be building strings you didn't expect to.

This is the one fundamental truth of all programming. You need to write your applications from the ground up with this in mind. There is no easy fix, there's only diligence.

deceze
  • 510,633
  • 85
  • 743
  • 889
-2

Hackers can hack even if you are not using url parameters.

It has to be done in the backend. Before interacting with database you have check whether the parameters are what you are expecting. For example you should not allow single quotes in your params, this will actually allow hackers to add some more queries to your query.

Use mysqli prepared statements

Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
  • 1
    'Not allow single quotes'...?! So, nobody ever can send single quotes over the interweb? I call shenanigans. – deceze Nov 05 '15 at 13:49
  • Prepared statements only protect you from bad things which can happen to your database. What about Cross-Site-Scripting attacks? I've already pointed out where to geht good info about this in my answer. – pmayer Nov 05 '15 at 13:54
  • for example select * from users where username='shiva' and password='password'; To select * from users where username='shiva' and password='1'or 1=1 or 1=1. if allow this strng in the password field they can easily login to your system. – Sugumar Venkatesan Nov 05 '15 at 13:55
  • I don't use a database here. I will try to filter the username - user ID can only include letters, numbers, full stops, asterisks, underscores, or dashes. – Adrian Nov 05 '15 at 13:56
  • Hello Adrian even if you protect from front end there tools like proxy which can be used modify you values and send it to the database. so you must sanitize in the bankend. – Sugumar Venkatesan Nov 05 '15 at 14:01