开发者

jQuery AJAX add/edit/delete actions can be hacked?

开发者 https://www.devze.com 2023-01-31 03:29 出处:网络
Just wondering about a security issue. Right now I\'m using the following function to delete movies from my database:

Just wondering about a security issue. Right now I'm using the following function to delete movies from my database:

    function deleteVideo(video_id){
        function mycallbackform(v,m,f){
            if(v=="yes"){
                $.ajax({
                    type: "POST",
                    url: "delete.php?action=video",
                    data:   "video_id=" + video_id,
                    suc开发者_C百科cess: function(html){
                        if(html == "1"){
                            //$("#result").html(html);
                            $("#row_"+video_id).fadeOut("slow");
                            $("#result").show();
                            $("#result").html("<div class='notification success png_bg'> <div><?php echo $LANG_video_succesfull_delete; ?> </div></div>");
                            setTimeout(function(){ $('#result').fadeOut('slow'); }, 5000); 
                        }else{
                            $("#result").show();
                            $("#result").html(html);

                        }
                    }
                });
            }
        }
            $.prompt('Are you sure?',{ buttons: { Ok: 'yes', Cancel: 'no'}, callback: mycallbackform});
            }

At the back end the following code is executed:

/*** DELETE data ***/
    /*** prepare the SQL statement ***/
    $stmt = $dbh->prepare("DELETE FROM videos WHERE username=:username AND videos_id=:video_id");
    $stmt->bindParam(':username', $currUser);
    $stmt->bindParam(':video_id', $video_id);



    /*** execute the prepared statement ***/
    $stmt->execute();

The username is stored in a session in this case.

Is there any way user A will be able to delete user B data with this code?

I was thinkinig to add a query to check if the current user is the same user who added the video in the database. If not he can't delete the data. But is this necessary or is this code safe enough?

Thanks in advance.


You'd better store a unique user id in the session. What if there are two people with the same username?


Edit: If the username is unique, it is quite safe. It is not possible to change the value of a session variable working the client side, unless you've made a terrible mistake in your PHP code. But if you're sure the session variable is always set correctly, you don't have to worry.

0

精彩评论

暂无评论...
验证码 换一张
取 消