🌐
Medium
medium.com › @mudassir.shabbir › understanding-javascript-vulnerabilities-with-examples-6eb9be9065e6
Understanding JavaScript Vulnerabilities with Examples | by mudassir shabbir | Medium
July 25, 2024 - DOM-based XSS is when the vulnerability is in the client-side code and the attack is executed in the user’s browser. ... Reflected XSS, also known as non-persistent XSS, occurs when the injected script is reflected off a web server, such as in an error message, search result, or any other response that includes some or all of the input sent to the server as part of the request. Here’s an in-depth example to illustrate how reflected XSS works and how to prevent it.
🌐
GitHub
github.com › veracode › example-javascript-vulnerable-methods
GitHub - veracode/example-javascript-vulnerable-methods: SourceClear’s example node project with vulnerable methods in third party libraries
git clone https://github.com/srcclr/example-javascript-vulnerable-methods.git cd example-javascript-vulnerable-methods npm install node index.js · The vulnerable method is called twice during the server startup, however another one needs to be trigged by issuing a request to the endpoint by running the following command in another terminal to trigger the code execution vulnerability in js-yaml:load
Starred by 4 users
Forked by 41 users
Languages   JavaScript
Discussions

Common JavaScript Vulnerabilities - Information Security Stack Exchange
Most other web vulnerabilities ... with JavaScript code per se. Any linter worth it's salt (JSLint, JSHint, ESLint) will warn you of potentially problematic code (e.g. eval). ... This thread over at Stack Overflow may have the answers you're looking for. ... js doesn't really have vulnerabilities (though other areas of the browser reachable by JS might, example flash) and ... More on security.stackexchange.com
🌐 security.stackexchange.com
March 28, 2018
javascript - JS code vulnerable? - Information Security Stack Exchange
When the user clicks on the redirectpg ... JavaScript code will be executed. This can lead to cookie theft and impersonating a legitimate user, and probably re-render the login form and steal the actual password of the user. Basically, the attacker is able to show the user anything they want within your website. ... Yes, this code seems to be vulnerable to an XSS ... More on security.stackexchange.com
🌐 security.stackexchange.com
March 7, 2014
Looking for Vulnerable Code Example Site
I got you. Even if this isn't the site you were on before, this has vulnerable examples and fixed examples for most vuln classes. Just pick a language https://rules.sonarsource.com/ More on reddit.com
🌐 r/cybersecurity
11
1
January 15, 2025
Collection of vulnerable code snippets (updated every friday)
This is gonna be picked up by GitHub Copilot 😂 More on reddit.com
🌐 r/netsec
11
214
November 18, 2022
🌐
University of Toronto
cs.toronto.edu › ~arnold › 427 › 18s › 427_18S › indepth › vulnerableJs › index.html
Vulnerable Javascript
Since our javascript code is now executable, we can steal user's cookies/session tokens, play with the DOM or redirect to a malicious website.
🌐
OWASP Foundation
owasp.org › www-project-web-security-testing-guide › v41 › 4-Web_Application_Security_Testing › 11-Client_Side_Testing › 02-Testing_for_JavaScript_Execution
Testing for JavaScript Execution
This implies that an attacker could inject JavaScript code simply by submitting the following query string: www.victim.com/?javascript:alert(1). For example, consider the following URL: http://www.domxss.com/domxss/01_Basics/04_eval.html
🌐
YouTube
youtube.com › watch
JavaScript Security Vulnerabilities Tutorial – With Code Examples - YouTube
Learn about 10 security vulnerabilities every JavaScript developer should know. First try to find the vulnerabilities in the different code examples. Then le...
Published   May 16, 2023
🌐
Codacy
blog.codacy.com › javascript-vulnerabilities
Common JavaScript Vulnerabilities and How to Avoid Them
2 weeks ago - Insecure Use of eval() refers to ... in JavaScript allows executing a string as JavaScript code. For example, eval("2 + 2") would return 4....
🌐
Invicti
invicti.com › blog › web-security › understanding-javascript-vulnerabilities
Understanding the most common JavaScript vulnerabilities
April 29, 2021 - In other words, the most common JavaScript vulnerabilities are all different types of cross-site scripting. There are the 3 main types of cross-site scripting: Reflected XSS: Malicious script code entered by the attacker (for instance, as a search query) is accepted by the server.
Find elsewhere
🌐
DEV Community
dev.to › orlandov14 › how-to-see-if-your-javascript-code-is-vulnerable-3g8k
How to see if your JavaScript code is vulnerable? - DEV Community
November 8, 2022 - One of the most immediate things you can do as an individual developer is to check if your source code is vulnerable and how to determine what piece of the information is critical, by using a good scanning tool. To scan your applications for vulnerable JavaScript code start by using a free SAST tools like GitHub CodeQL, Snyk, SonarQube, or CodeSec by Contrast that are simple to install and provide an immediate list of vulnerable source code that need updating at no cost.
🌐
SecureCoding
securecoding.com › home › blog › most common security vulnerabilities using javascript
Most Common Security Vulnerabilities Using JavaScript - SecureCoding
October 15, 2020 - This post dives into common JavaScript vulnerabilities, the risks they pose, and how developers can address these vulnerabilities to keep their web applications secure. According to OWASP, cross-site scripting (XSS) is one of the most widespread security risks in web applications. It occurs when an attacker injects malicious code ...
🌐
Security Boulevard
securityboulevard.com › home › cybersecurity › application security › the top ten javascript vulnerabilities and how to avoid them
The Top Ten JavaScript Vulnerabilities and How to Avoid Them - Security Boulevard
April 19, 2023 - One example is the Magecart attack on British Airways. In this attack, attackers were able to insert malicious code into a third-party JavaScript library used by the British Airways website to process payments.
🌐
Z Cybersecurity
zcybersecurity.com › home › top 9 javascript security vulnerabilities in 2026
9 Javascript Security Vulnerabilities 2024 | JavaScript Most Common Security Vulnerabilities Explained
September 30, 2025 - Here are some common JavaScript vulnerabilities and their technical aspects: ... XSS is a type of injection attack where an attacker injects malicious scripts into a legitimate website or web application. This occurs when user input is not properly sanitized or validated before being incorporated into the web page’s HTML or JavaScript code.
🌐
GitHub
github.com › snoopysecurity › Broken-Vulnerable-Code-Snippets
GitHub - snoopysecurity/Broken-Vulnerable-Code-Snippets: A small collection of vulnerable code snippets · GitHub
A small collection of vulnerable code snippets . Contribute to snoopysecurity/Broken-Vulnerable-Code-Snippets development by creating an account on GitHub.
Starred by 797 users
Forked by 794 users
Languages   PHP 34.4% | JavaScript 16.8% | C# 14.4% | C 12.5% | Python 11.0% | Java 6.2%
🌐
Medium
medium.com › @ghostlulzhacks › vulnerable-javascript-file-188b6287179
Vulnerable Javascript Files. Finding easy javascript CVEs | by ghostlulz | Medium
May 6, 2019 - Note the script also outputs the domain and vulnerable url, I had to cut it out of the picture because it wouldn't fit. But as you can see you get the same output as the chrome plugin but now you can mass scan hundreds of websites. My script can be downloaded below: ... 99% of the internet uses javascript in their website.
🌐
Snyk
snyk.io › learn › javascript-security
JavaScript Security | JavaScript Vulnerabilities | Snyk
June 23, 2022 - These can be prevented by scanning your code for vulnerabilities during development and educating your developers about security. In this article, we’ll take a look at the most common JavaScript vulnerabilities and how to prevent them through popular modern security approaches combined with ...
Top answer
1 of 2
2

Full disclosure: the following guide was written by the research team of Checkmarx, creator of CxSAST (static code analysis tool), where I am employed.

Here is a JavaScript Secure Coding Practices guide:

  • GitHub - https://github.com/Checkmarx/JS-SCP
  • GitBooks - https://checkmarx.gitbooks.io/js-scp/

Since JavaScript is used these days both in front and back ends of applications, a plethora of vulnerabilities and issues are relevant and important to know while developing in JavaScript:

  • Cross Site Scripting (XSS) - Reflected, Stored and DOM based.
  • SQL and NoSQL injections
  • Authentication and password management issues
  • Session management issues
  • Access control issues
  • Unsafe use of cryptography - Hashing, Encryption, Decryption and Random number generators
  • Error Handling and Logging issues

To name a few... :)

2 of 2
1

I wrote an article about JavaScript vulnerabilities, so I believe I can help here:

Most security vulnerabilities in javascript come as a result of end-user interaction.

Malicious users can input query strings into forms to access or contaminate protected data.

It is, therefore, a responsibility on engineering teams to add a validation middleware on user inputs.

Listed below are the most common JavaScript vulnerabilities:

  • Cross-Site Scripting (XSS)
  • SQL injection (SQLi)
  • Open-source vulnerabilities

Cross-Site Scripting (XSS)

One of the most common attacks involving web applications and JavaScript are Cross-site Scripting vulnerabilities which are included in the OWASP Top 10 web application security risks.

An XSS attack is a flaw in web applications which allow malicious users to execute JavaScript code on a website hence its popularity because almost every website requires JavaScript turned on in users browsers.

Cross-Site Scripting is primarily of two types namely: - Client XSS - Server XSS

Client XSS

This type of vulnerability occurs when untrusted data supplied from a remote location (mostly APIs) is used to update the DOM with an unsafe JavaScript call. As an example, the following snippet below gets weather information from an untrusted API to display current weather information.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>DEMO XSS</title>
</head>
<body>
<p>Today's weather information reads: </br>
<div id="temp"></div>
</p>
<script type="text/javascript">
let untrusted_ajax_res = document.write(location.replace("https://www.w3schools.com"));
document.getElementById("temp").innerHTML=untrusted_ajax_res;
</script>
</body>
</html>
The snippet above shows how malicious code from an untrusted API can be injected into the DOM.
To prevent client xss attacks, developers can install secure xss filters which can be used to sanitise inputs gotten from untrusted domains/locations. A snippet for a web use-case can be seen below:
<!DOCTYPE html>
...
<script src="dist/xss-filters.min.js"></script>
<script>
let untrusted_ajax_res = document.write(location.replace("https://www.w3schools.com"));
document.getElementById("temp").innerHTML=xssFilters.inHTMLData(untrusted_ajax_res);
</script>

-The xss-filters package must only be applied to UTF-8 encoded documents. HTML5 mode is also required.

Server XSS This type of vulnerability occurs when untrusted data is included as user input and sent into the backend to be stored on the database. This data is used to compute a response to be sent back to the user which contains malicious JavaScript code when sent back to the user.

An example of a server XSS attack on an express based node server can be seen in the snippet below:

...
app.get('/search', (req, res) => {
const results = db.search(req.query.product);
if (results.length === 0) {
return res.send('<p>No results found for "' + req.query.product + '"</p>');
}
...
});

The snippet above shows the definition of a products search route for http://www.domain.com. An XSS attack can be performed by passing javascript code as a query for the server like so:

https://www.domain.com/search?product=<script>alert(XSS:Gat Ya!!)</script>

Since there wouldn’t be any record like

<script>alert(XSS:Gat Ya!!)</script>

on the database, the server’s response will be:

<p>No results found for "<script>alert(XSS:Gat Ya!!)</script> </p>

After the server’s response is received and No results found for is displayed, the script is then injected into the DOM to give the output shown below:

Even though this doesn’t look very severe as this script doesn’t pose any serious threat of sort. Attackers, however, can save malicious code as “comments” in blogs/social media sites. These so-called comments, when served to other users, is then injected into the DOM and performs the wishes of the attacker. To fix this vulnerability we can install the xss-filter package using npm like so:

$ npm i xss-filters --save

After installing, we can now filter the user’s input in our server’s response like so:

...
var xssFilters = require('xss-filters');
app.get('/search', (req, res) => {
var productParam = req.query.product;
const results = db.search(req.query.product);
if (results.length === 0) {
res.send('<p>No result found for "' + xssFilters.inHTMLData(productParam) + '"</p>');
}
});
...
  • Using the xss-filters packages comes with certain warnings that can be found on its installation page

SQL Injection

SQL Injection is a type of attack which makes it possible to execute SQL statements to control a database behind a web application. An SQL Injection attack can be seen in the code snippet below:

...
const db = require('./db');
app.get('/products', (req, res) => {
db.query('SELECT * FROM products WHERE id = ' + req.query.id);
.then((product) => {
...
res.send(product);
})
});

There are two things wrong with the code snippet above:

  1. The user’s input which is not to be trusted is directly concatenated with the query string.
  2. The database query is built via string concatenation.

    db.query('SELECT * FROM products WHERE id = $1', req.query.id);

In the snippet above, the database client will pass in the req.query.id as the first argument. Although the MySQL package for node doesn’t support parameterised queries, you can escape users input (i.e remove all special characters ) before it’s concatenated with the query string like so:

const query = 'SELECT * FROM products WHERE id = ' + connection.escape(req.query.id);
Top answer
1 of 2
3

Yes, it's vulnerable to XSS. A simple exploit would be something like this:

example.com/index.php?rpath=javascript:alert(document.cookie)

The rpath parameter will be grabbed by your getUrlVars() function and be assigned to redirectPath. The element redirectpg will have its href attribute changed to the XSS code. When the user clicks on the redirectpg element (presumably an anchor <a>), the malicious JavaScript code will be executed.

This can lead to cookie theft and impersonating a legitimate user, and probably re-render the login form and steal the actual password of the user. Basically, the attacker is able to show the user anything they want within your website.

2 of 2
3

Yes, this code seems to be vulnerable to an XSS injection.

What does this code do?

It checks the URL the user typed for GET-arguments. GET-arguments are everything behind a question-mark and are typical a bunch of key/value pairs with an equal-sign between them separated by &.

In this example URL

 http://example.com/somesite.html?somevar=42&rpath=someothersite.html

the argument there are two such variables. somevar = 42 and rpath = someothersite.html

Your script will then search for a specific link on your website, take the value of rpath, and replace the target of the link with it. In this example, that link would then be changed to point to someothersite.html.

How could this be abused?

A link on a website can not just point to another website. It is also possible to have links which start with javascript: followed by javascript code. When a user clicks on this link, that code would get executed in the context of the currently loaded website.

That means an attacker could send the user a link like this:

http://example.com/somesite.html?rpath=javascript:alert('hello world')

When the user would click on that link and then on the redirect-link on your website, that javascript code would get execute.

Why is this a problem?

The current website might contain sensitive information. A javascript executed in the context of that website can grab that information and send it to a server the attacker controls.

The script also has access to any cookies and the users localstorage. When you use cookies to track user sessions, an attacker could have the script send the users cookie to his server. This might allow to hijack the session of the user and take over their account.

Depending on what your website actually does, there could also be a lot of other ways an attacker could cause all kinds of mischief by tricking users into executing javascript code.

How could this be done better?

Putting unfiltered user-data into a HTML document is always dangerous. In this case you might think that it might be sufficient to just reject any arguments containing javascript:, but the exploit shown above is only the most obvious one. There are likely much more subtile ones. Trying to filter anything that might be evil seems to be beyond the programming skill of the author of above code snippet, so I would advise against trying it. Instead I would recommend them to take a much safer approach. Instead of just putting any string in the website the user provides, have a limited set of possible strings to insert and choose one depending on the argument provided:

switch(redirectPath) {
    case 'home':
        document.getElementById('redirectpg').href = 'home.php';
        break;
    case 'about':
        document.getElementById('redirectpg').href = 'about.php';
        break;
    case 'contact':
        document.getElementById('redirectpg').href = 'contact.php';
        break;
    default:
        document.getElementById('redirectpg').href =  'index.php';
        break;
}
🌐
GitHub
github.com › clarkio › vulnerable-app
GitHub - clarkio/vulnerable-app: [DEPRECATED] A sample web application using Node.js, Express and Angular that is vulnerable to common security vulnerabilities.
November 20, 2019 - [DEPRECATED] A sample web application using Node.js, Express and Angular that is vulnerable to common security vulnerabilities. - clarkio/vulnerable-app
Starred by 85 users
Forked by 56 users
Languages   JavaScript 60.2% | CSS 28.7% | HTML 11.1% | JavaScript 60.2% | CSS 28.7% | HTML 11.1%
🌐
freeCodeCamp
freecodecamp.org › news › how-to-secure-javascript-applications
How to Secure JavaScript Applications: Common Vulnerabilities and Best Practices
October 24, 2024 - The Segway Magecart attack is a well-known example of digital skimming. The attackers were able to steal users' information by injecting a script that manipulated the code running in the web browser, allowing them to access the data entered ...
🌐
Qualys
blog.qualys.com › vulnerabilities-threat-research › 2023 › 01 › 16 › detection-of-vulnerabilities-in-javascript-libraries
Detecting Vulnerabilities in JavaScript Libraries: jQuery & Bootstrap | Qualys
February 24, 2026 - Below is an example to highlight detection of latest CVE vulnerabilities on an application running outdated Moment.js JavaScript library using its dedicated QID 151025: