Security in JavaScript
1. Cross Site Scripting (XSS Attack)
This type of attack allows an attacker to inject malicious scripts into web pages viewed by other users.
Example: Consider you have an anchor tag that sets the value of href somewhere in your code based on query parameter.
?redirect=https://www.youtube.com/
should take you to youtube when a redirect link is clicked.
But a hacker can have any other JavaScript code in the query parameter instead
?redirect=javascript:document.getElementsByTagName(%27button%27)[0].click()
Issue: https://stackblitz.com/edit/js-r6efjx
Fix: https://stackblitz.com/edit/js-bwcj8v
Here, the hacker is trying to click the "payment" button when a "redirect" link is clicked
To resolve this, make sure the URL is first validated to confirm that it contains "https" or it does not contain "javascript:"
2. CSRF Attack
Consider a simple web app that listens to GET requests. It is going to be listening to an endpoint "/api/data"and fetches the "url" query parameter and makes a call to that path (/api/data?url="countries")
This attack is targeted on an "authenticated" user who is using a website so they can now make API calls with the token stored in the browser session.
Consider we have few public facing APIs: /api/data?url="countries" , /api/data?url="states"
But other private API: /api/data?url="users" (which should not be made by un-authenticated users)
Now, because the user is authenticated, hacker can now add a link as an advertisement that will make a request to "users" API.
<a href="http://bank.com/transfer.do?acct=MARIA&amount=100000">View my Pictures!</a>
3. Timing Attack
consider the below code:
Here a secret token is matched to check if the token is correct or not using "==="
The " = = = " will compare every character
like comparing abcd = = = pqr will immediately give false
comparing abcd = = = abcx will take some time and then give false
So the hacker will know that part of their string is already correct and keep trying with brute force.
You can avoid this using crypto package
Comments
Post a Comment