JavaScript Security Basics Quiz
Test your knowledge with 40 questions on web security fundamentals—covering XSS types, prevention strategies, CSRF mechanics, and defense techniques like tokens, SameSite cookies, and Content Security Policy.
Question 1
Why is client-side security important even when the server is secure?
Question 2
What is the principle of “defense in depth” in web security?
Question 3
Which of the following is a common goal of client-side attacks?
Question 4
What role does the Same-Origin Policy (SOP) play in browser security?
Question 5
What is Cross-Site Scripting (XSS)?
Question 6
Why is XSS dangerous?
Question 7
What enables an XSS attack to succeed?
Question 8
Which built-in browser feature can help mitigate some XSS impacts?
Question 9
What characterizes a reflected XSS attack?
Question 10
How does stored (persistent) XSS differ from reflected XSS?
Question 11
What defines DOM-based XSS?
Question 12
Which code snippet shows a DOM-based XSS vulnerability?
// URL: https://example.com/#<script>alert(1)</script>
const hash = location.hash.substring(1);
document.body.innerHTML = hash;Question 13
In which type of XSS is the malicious payload never sent to the server?
Question 14
Which input source is commonly exploited in DOM-based XSS?
Question 15
What is the most effective way to prevent XSS when inserting user data into HTML?
Question 16
Why is input validation alone insufficient to prevent XSS?
Question 17
Which method is safe for inserting user text into the DOM?
const userInput = '<img src=x onerror=alert(1)>';
// Which is secure?
Question 18
What does this code do safely?
const div = document.createElement('div');
div.textContent = userInput;
container.appendChild(div);Question 19
What is the purpose of a Content Security Policy (CSP)?
Question 20
Which CSP directive helps prevent inline script execution?
Question 21
What is a safe alternative to using innerHTML with user data?
Question 22
Which library is commonly used to sanitize HTML and prevent XSS?
Question 23
What is Cross-Site Request Forgery (CSRF)?
Question 24
Why can CSRF succeed even on secure sites?
Question 25
Which HTTP methods are most commonly targeted in CSRF attacks?
Question 26
Can a CSRF attack read the response from the forged request?
Question 27
Which scenario is a classic CSRF example?
Question 28
What is the purpose of an anti-CSRF token?
Question 29
Where should an anti-CSRF token be included in a form?
Question 30
What does the SameSite cookie attribute do?
Question 31
Which SameSite value allows cookies on top-level navigations but blocks them in cross-site POSTs?
Question 32
What is the “double submit cookie” pattern?
Question 33
Which code snippet shows a secure way to include a CSRF token in a fetch request?
// Assume csrfToken is rendered in a meta tag or global JS var
fetch('/transfer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': csrfToken // from trusted source
},
body: JSON.stringify({ to: 'attacker', amount: 100 })
})Question 34
Why can’t an attacker read your CSRF token if it’s in a meta tag?
Question 35
What is a limitation of relying only on SameSite cookies for CSRF protection?
Question 36
How should CSRF tokens be generated?
Question 37
Which cookie attribute should accompany SameSite=None?
Question 38
What is wrong with this form?
<form action="/delete-account" method="POST">
<button type="submit">Delete</button>
</form>Question 39
Why is it unsafe to store a CSRF token in localStorage and send it in a header?
Question 40
Which combination provides strong CSRF protection?
