r/PHPhelp • u/PewPaw-Grams • Sep 23 '19
How do you prevent XSS and Code Injection using PHP?
3
u/Wiwwil Sep 23 '19
https://johnmorrisonline.com/prevent-xss-attacks-escape-strings-in-php/
echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
// OR
// echo htmlentities($string, ENT_QUOTES, 'UTF-8');
Basically this
1
u/PewPaw-Grams Sep 23 '19
That easy? If that's the case, why is it that websites out there are so easy to hack and deface?
3
u/Wiwwil Sep 23 '19
It is easy to make a working website with PHP, even without knowledge. Easy to host as well. So beginners just write some code and boom that's on the web. It's fine in local and in the learning process, but they don't bother to learn any security related stuff. Old code most likely. Not using CMS or a framework. In the world there is PHP and PHP. Those easy to hack website are not called PHP for me.
5
2
Sep 23 '19
Use frameworks, because they already have some preventions which you can use...
2
u/PewPaw-Grams Sep 23 '19
Which frameworks to use? The reason why I'm asking is to learn how to prevent it by coding it myself
4
2
u/geosparks Sep 25 '19
Here is a video about XSS, what it does and how to prevent it if you are doing your own vanilla PHP.
https://youtu.be/CPeTGdRTYT4
1
5
u/ddrght12345 Sep 23 '19
Sanitization. Never trusting input. Ever.htmlspecialchars or htmlentities will get you close, but ultimately, it's still too context specific.
Code injection is a pretty broad subject. SQL injection, directory traversal/injection, object injection, etc all have different prevention measures.
Your best bet for "how" is to just read up and learn about the different ways to "do". Check out OWASP. They have tons of examples.
1
u/crazedizzled Sep 23 '19
Twig
-1
u/PewPaw-Grams Sep 23 '19
WHAT!?!?!? Are you mocking me?
3
u/crazedizzled Sep 23 '19
No? Twig is a templating library which basically eliminates XSS from being a concern. On top of that, use a good Content-Security-Policy and you're set.
1
u/barvid Sep 24 '19
This was a great example of why explanations are needed instead of one-word answers.
1
1
0
Sep 23 '19
[deleted]
5
u/ddrght12345 Sep 23 '19
I'm sorry... NEVER, EVER tell someone to not sanitize server side!
Ever.
Client side is fine and all. UI/UX, usability, immediate feedback, and what not. But you should ALWAYS sanitize server side. I own everything I'm sending you, and I can personally guarantee, if I'm targeting your site, I won't be using a (traditional) browser. Your client side code won't be executed. Even if I was using a normal browser, it's stupid trivial to stop it.
Geezes, PLEASE learn more about how HTTP requests work before recommending advice like this.
4
u/CyberJack77 Sep 23 '19
Some basic rules (when handling text data):
Never ever trust (user) input.
Prevent SQL injections by using tings like prepare statements and never try to place data inside a query manually.
Like said before: Sanitization.
But sanitize only in the correct places. Since you don't know what you will be doing with given data in the future, keep your stored data (in a database) the same as the original data (so don't sanitize). But when displaying the data, you always need to sanitize it.
Note: A XSS script can do no harm inside a database (be sure to prevent sql injections though), but it can do harm inside the users browser.
Note2: If you want to analyze a (potential) attack, the original source will help way more than a sanitized string.
When working with files, make sure they are stored outside the document root (which will prevent direct access) and always process uploaded images (which will remove any scripts hidden inside images).
There are a lot of other things that can cause problems (like path traversal), but most are explained at the already mentioned OWASP website.