Skip to main content

Web Security & Best Practices

Web Security & Best Practices

How do you secure your home? By installing locks, only giving your house keys to your family members, making sure the fence is high enough, installing CCTVs, and probably a lot more that I don’t know about.


But what about your web application? Is your app secure? How do you even know if it’s secured? How can a person hack an application?


In this post I will be sharing about web application security and the best practices. There are a few common exploits a hacker can use against you.


Common Exploits


SQL Injection

Let's start with something basic that you may have heard about. SQL injection prey on applications where a user can affect an SQL query directly. Say you have a table list with a search on top, and you developed this on vanilla PHP with no framework and no input sanitization. Chances are, a user can input an SQL script into the search bar and have direct access to your database. Try this and see:


; DROP TABLE users;


If the users table is deleted. Bad news, your application is prone to this basic attack. If your database is safe, then congrats. 


SQL injection is highly uncommon nowadays due to it being as old as the internet and frameworks blocking this exploit by default.

Cross Site Scripting (XSS)

Think of SQL injection, but instead of SQL it's JavaScript. Same principle, just different language. Try typing a simple JS script into one of your site’s inputs to see if it’s susceptible to this attack. Then save it to your database.


<script>

    window.alert('if you see this, you’re cooked lol');

</script>


If that alert pops up, you absolutely should be worried. This attack may seem silly, but on the wrong hands can be deadly. Especially when combined with the next exploit.

Cross Site Request Forgery (CSRF)

Request forgery basically means an attacker is wrongly using an actual user’s session to log into your system. They can craft a JS that steals the cookies or caches of your site’s user, then send it to themselves to use for malicious reasons.

Broken Access Control (BAC)

This is the most common exploit nowadays. An application gets too large too quickly, and nobody cares to test all access for all pages. To test this, try to directly access a page that you don’t have access to. Examples include opening reports without logging in, trying to access an admin-only page with a normal user account, randomly replacing numbers at the end of a URL to access data that isn't yours. 


Best Practices

We now know the problems, so what the heck is the solution? It’s actually simpler than you might think.


Ditch Vanilla, Go Framework

This alone will solve 99% of all vulnerabilities, provided you know how to use the framework’s security libraries. 

Input Sanitization

Never assume a user’s input as safe. Always handle them like it’s contaminated. Validate on both frontend and backend. Sanitize it by removing any HTML, JS or SQL code. 

Validate File Extensions

Never trust a file’s extensions. Would you plug a stranger’s usb stick into your computer? Why would you trust a stranger’s file? A ‘.pdf.php’ file might contain malicious code that can run on your server if you didn’t properly check its extensions extensively. 

Even More Access Checking

Example:

- Check if user is logged in

- Check if the user has appropriate access to the module. Eg: admin/hr/finance

- Check if user have access for requested data

- Check if user have access to write/delete data


In conclusion, being a bit paranoid pays off and frameworks exists for a reason. That is all.

Comments

Popular posts from this blog

Turning AIs Into Cavemen Is Actually A Good Idea

    Token limits have been the number 1 problem of vibecoding. Aside from high usage, vague prompting and inefficient context management are two key causes that contribute to this problem. Tokens are counted from two things, the user’s input and agent’s output. You can have the best prompt in the world, but if your AI puke a Shakespearean essay for each response, your token will reach its limit faster. Similarly, if your prompt is too long, your token usage will also skyrocket. Prompting Prompting seems easy enough, but the reality is, if you want quality output, the input must be of similar quality. Here are some tips you can try to achieve a better, token-efficient prompt. Stick to English language only Certain languages may use higher tokens. Switching between languages might not be a good idea. Sure you can control your input prompt, but the output may use more tokens. Extremely short input Do not use ‘please’ or ‘thank you’. Get rid of filler words. Straight to the point....

🗑️ Clear storage Mac OS

  🗑️ Clear storage Mac OS 1: Clear system cache: Go to Finder > Go > Go to Folder, then type in "~/Library/Caches" and hit enter. Select all the folders inside the Caches folder and delete them. 2: Clear system logs: Go to Finder > Go > Go to Folder, then type in "/var/log" and hit enter. Select all the files inside the Log folder and delete them. 3: Remove unused language files: Go to Finder > Go > Go to Folder, then type in "/Library/Languages" and hit enter. Delete all the language folders you don't need. 4: Uninstall unused apps: Go to the Applications folder and delete the apps you don't use. 5: Clean up system files: Use a system cleaning tool like CleanMyMac X to scan and remove unnecessary system files. 6: If you have npm installed, clear the caches once in a while with ‘sudo npm cache clean --force’ 7: If you have ionic projects, open the ‘.angular’ folder and delete the ‘cache’ folder inside it.

Git Revert is Easier Than You Might Think

    Its surprisingly easy and not as complicated as some people might say. You just need to know which one is the 'bad' commit, then revert to one commit before the 'bad' one. In this example, person 'M' accidentally pulled from the dev branch instead of main. So to revert this, just follow these steps: Find the 'bad' commit In this case it's git message have something like 'pulled dev into branch...'. This is the bad commit, do not copy the git hash of this commit. Instead, get the last 'good' commit. Copy the commit hash of the one before the 'bad' one. Paste it somewhere safe, we will get back to this later. Do not revert right now.   Backup your commits after the 'bad' one After pulling from dev, 'M' pushed a few more commits afterwards. If we've already reverted, finding the committed changes would be a bit harder. So now is the best chance to copy all the commit hashes for cherry-picking later.    Rev...