<!DOCTYPE html>
<html>
<body>
<form action="search.php" method="get">
<input type="text" name="search">
<input type="submit" value="Search">
</form>
<?php
$searchTerm = $_GET['search'] ?? '';
echo "<p>You searched for: $searchTerm</p>";
?>
</body>
</html>
An attacker could craft a malicious URL like http://example.com/search.php?search=<script>alert('XSS')</script>
and trick a user into clicking it.
// Server - side code to save comment (pseudo - code)
function saveComment(comment) {
// Save comment to database without sanitization
database.insert('comments', { text: comment });
}
If an attacker submits a comment like <script>stealCookies()</script>
, all users who view the comment will execute the malicious script.
DOMPurify
library.const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
function sanitizeInput(input) {
return DOMPurify.sanitize(input);
}
function encodeHTML(input) {
return input.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
CSRF attacks occur when a malicious website tricks a user’s browser into making an unwanted request to a different website where the user is authenticated. For example, if a user is logged into their online banking account and visits a malicious website, the malicious site could have a hidden form that makes a transfer request to the banking site.
<!DOCTYPE html>
<html>
<body>
<form action="https://bank.example.com/transfer" method="post">
<input type="hidden" name="amount" value="1000">
<input type="hidden" name="to" value="attacker">
<input type="submit" value="Submit">
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
// Server - side code to generate CSRF token (pseudo - code)
function generateCSRFToken() {
return crypto.randomBytes(16).toString('hex');
}
// Include token in form
function addCSRFTokenToForm() {
const token = generateCSRFToken();
const form = document.createElement('form');
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'csrf_token';
input.value = token;
form.appendChild(input);
document.body.appendChild(form);
}
On the server - side, verify the token with each request.
In a Node.js application that interacts with a SQL database, if user input is not properly sanitized in SQL queries, it can lead to SQL injection.
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'test'
});
function getUser(username) {
const query = `SELECT * FROM users WHERE username = '${username}'`;
connection.query(query, (error, results) => {
if (error) throw error;
console.log(results);
});
}
An attacker could pass a username like ' OR '1'='1
to bypass authentication.
function getUser(username) {
const query = 'SELECT * FROM users WHERE username = ?';
connection.query(query, [username], (error, results) => {
if (error) throw error;
console.log(results);
});
}
npm update
JavaScript security is a critical aspect of web development. By understanding common vulnerabilities such as XSS, CSRF, injection attacks, and the risks associated with third - party libraries, developers can take proactive steps to secure their applications. Implementing proper input sanitization, output encoding, using CSRF tokens, prepared statements, and following best practices for third - party libraries are essential for building secure JavaScript - based web applications.