r/PHPhelp • u/MyYoutubeIsKuroro • Sep 17 '21
Making PHP more secure
For example a blog website. Is there a way to delete a certain blog that won't need to put a hidden input field containing the id of that blog?
For example:
<form> <input type="hidden" value="<?= $blogId; ?>"> (This is the part I want to remove from my code) <button>Delete</button </form>
On some programming languages such as nodejs, there is no need to do that. Thats why Im asking here if there is also a way in PHP to not do that.
EDIT: Thank you to everybody that answered my question.
1
u/MyYoutubeIsKuroro Oct 08 '21
Update: I saw another way on how to do this. Instead of using forms and sending IDs, it depends on the URL and maybe session for some other cases.
5
Sep 17 '21
You've got to tell the server what blog post you want to delete, whether your server is running Node, PHP, or whatever.
There are multiple ways of doing that, which will work with both Node and PHP.
Like you've done here, you can send the ID as part of the request body
You can send a DELETE request to /blogs/{blogId}/ (i.e. where {blogId} is the ID. That's the "REST" approach.
You can send a POST request to /blog/{blogId}/ with an extra
_method=DELETEvalue, and interpret that. Many frameworks do this for you.You could use a slug instead of the ID to represent the blog post.
So this isn't really a PHP vs Node thing. Are you perhaps using a Node framework like Express, which is abstracting the problem?
4
u/HolyGonzo Sep 17 '21
On some programming languages such as nodejs, there is no need to do that...
Actually, there is ALWAYS a need to know the ID. If you don't see the ID in the page, then one of two things is happening. Either:
A: It's not a standard web page or client/server application. For example, a nodeJS application can run completely on the server, so when you are accessing it, variables are being managed separately from the form.
or
B: The data is simply encoded or encrypted or passed along in some other channel like a session variable or a cookie, or injected into the form at submission with Javascript. However, using a session or cookie is a very bad idea for doing this and can cause unexpected problems / errors later.
If you're working on a normal web site, and you're giving someone a form to be able to make some kind of changes to a record, then you NEED to have that record ID somewhere.
You can help prevent people from changing the ID by either encrypting the IDs and sensitive info with something like openssl_encrypt(), or you can add a simple hash to your form like this:
<input type="hidden" name="hash" value="<?= md5("YoutubeIsKuroro:".$blogId); ?>">And when someone submits the form, just re-calculate the same hash with the submitted blog ID. Something like this:
$hash_should_be = md5("YoutubeIsKuroro:".$_POST["blogID"]);
if($hash_should_be != $_POST["hash"])
{
echo "Illegal form manipulation detected!";
exit;
}However, NO MATTER WHAT, you should never only base your security on your form inputs. You should always have some kind of security model that verifies whether or not a user has the permissions to do something, like delete that blog entry. That way, if someone ever gets past the form security, your security model should catch and prevent the action.
1
u/dabenu Sep 18 '21
Lots of good answers already. But I'm also kind of missing:
why would you (not) want to do this in the first place?
what security benefits do you think to gain by not doing this?
1
u/MyYoutubeIsKuroro Sep 19 '21
As stated on my post the method I am using is having a hidden input field that contains the id of a blog post. By doing inspect element a user can try to manipulate the data inside that input. Im looking at different established PHP websites, and they are not doing the same method as I am doing. So I thought that there are better ways on doing that.
I also saw a demo of next.js in which the ID of a blog post is passed behind the scenes so there's no need to put a hidden input field that contains the ID.Maybe less ways of tampering the data? Although I know that even though a user figure out the ID of a blog post, they will still need to go through the validation whether they are allowed to do something with that ID.
A question that just popped up on my head. What is the better thing to do below:
Let the database's auto increment the ID
Use the uniqid function of PHP to be the ID
Use auto increment then concatenate the value uniqid will give
1
u/itemluminouswadison Sep 17 '21
How else can you know which blog to delete? You usually send along the id in the request
You could post a hash and lookup using that instead
Or post to itself since the blog id is already in the current page route
You could use sessions to persist the last viewed blog id but that's dangerous
1
u/p0llk4t Sep 17 '21
You could store a unique ID in a separate field for each blog post using the PHP uniqid() function. So when the post is initially created you use that function to generate a uniqid and then store that in the uniqid field (or whatever you name it), along with the other blog post data.
This unique ID that's generated cannot be used for crypto type stuff, but it's pretty much guaranteed that you won't have a collision. You can set the more_entropy flag to true to generate even a longer string of I'd recommend setting the properties for that unique ID field in whatever database you are using to only accept unique values so if there ever is a collision you can catch that. Likely won't ever happen for a normal blog situation.
Then you can use the unique ID field in all your public references.:
<input type="hidden" value="<?= $blogUniqueId; ?>">
There is virtually no chance that anyone can start messing with that uniqid string by swapping out characters and affect a record they are not supposed to access.
As mentioned by u/CyberJack77 you need to also do a check before deleting that the user has the right authorization to modify that record.
0
Sep 17 '21
Hi, I don't know the exact solution but you can try below approach. As per standard rules you should not expose ids in the forms so to prevent that you can do one thing encrypt the I'd in form and when you updating the values using SQL query before that decrypt ids . It is standard approach as per my knowledge. You can use different encrypt and decrypt techniques given in php documentation.
14
u/CyberJack77 Sep 17 '21
You don't need to delete it, just make sure the user doing the request is allowed to remove it.
In the backend you need to check if the user is loggedin, and if he has the right permissions to delete the post.
It might be that the user can only delete it's own posts. In that case check if the owner of the post is the same as the loggedin user. Or you can check if the user is an admin user.
In addition, you need to add some csrf protection to the form.
You always need to validate if someone is allowed to remove something, no matter the programming language. Maybe you are using application written in Node that does this for you.