SQL Injection is a security vulnerability where attackers insert malicious SQL code into input fields to manipulate the database and access or modify data without permission.
Click to reveal answer
beginner
How can parameterized queries help prevent SQL Injection?
Parameterized queries separate SQL code from data inputs, so user inputs are treated only as data, not executable code, preventing attackers from injecting harmful SQL commands.
Click to reveal answer
beginner
What is Cross-Site Scripting (XSS)?
XSS is a security flaw where attackers inject malicious scripts into web pages viewed by other users, allowing theft of information or control over user actions.
Click to reveal answer
intermediate
Name two common methods to prevent XSS attacks.
1. Escaping user inputs before displaying them on web pages.
2. Using Content Security Policy (CSP) headers to restrict script execution.
Click to reveal answer
beginner
Why is input validation important in preventing SQL Injection and XSS?
Input validation ensures only expected and safe data is accepted, reducing the chance of malicious code entering the system and causing harm.
Click to reveal answer
Which technique is most effective in preventing SQL Injection?
AUsing parameterized queries
BEncrypting the database
CUsing complex passwords
DDisabling user input
✗ Incorrect
Parameterized queries keep user input separate from SQL commands, stopping injection attacks.
What does XSS stand for?
AExtra Secure Script
BCross Server Security
CCross System Sync
DCross-Site Scripting
✗ Incorrect
XSS means Cross-Site Scripting, a type of web security vulnerability.
Which of these helps prevent XSS attacks?
AEscaping user inputs
BUsing SQL queries
CDisabling HTTPS
DIncreasing server RAM
✗ Incorrect
Escaping user inputs stops malicious scripts from running in browsers.
What is a Content Security Policy (CSP)?
AA password policy
BA database encryption method
CA browser security feature to control resources loaded
DA network firewall
✗ Incorrect
CSP helps prevent XSS by restricting which scripts can run on a web page.
Why should user inputs be validated?
ATo speed up the server
BTo ensure data is safe and expected
CTo increase database size
DTo allow all inputs
✗ Incorrect
Validating inputs helps block harmful or unexpected data that could cause security issues.
Explain how parameterized queries prevent SQL Injection attacks.
Think about how the database understands commands versus data.
You got /3 concepts.
Describe two ways to prevent Cross-Site Scripting (XSS) in web applications.
Consider both input handling and browser security settings.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the best way to prevent SQL injection attacks in a web application?
easy
A. Escape HTML tags in user input
B. Use parameterized queries or prepared statements
C. Disable JavaScript in the browser
D. Use client-side validation only
Solution
Step 1: Understand SQL injection cause
SQL injection happens when user input is directly included in SQL queries without safe handling.
Step 2: Identify prevention method
Parameterized queries separate code from data, preventing attackers from injecting SQL commands.
Final Answer:
Use parameterized queries or prepared statements -> Option B
But users report that some scripts still run. What is the likely cause?
medium
A. The replace method does not work on strings
B. The function should remove all HTML tags instead of replacing
C. The code should use double quotes instead of single quotes
D. Only replacing < and > is insufficient; other characters like quotes must be escaped
Solution
Step 1: Analyze current escaping
The code replaces only < and > but leaves quotes and other characters unescaped.
Step 2: Understand XSS vectors
Attackers can use quotes or other entities to break out of attributes or inject scripts.
Final Answer:
Only replacing < and > is insufficient; other characters like quotes must be escaped -> Option D
Quick Check:
Partial escaping = incomplete XSS prevention [OK]
Hint: Escape all special HTML chars, not just < and > [OK]
Common Mistakes:
Thinking replacing < and > is enough
Believing string replace fails silently
Ignoring quotes and ampersands in input
5. You are designing a web app that stores user comments and displays them safely. Which combination of techniques best prevents both SQL injection and XSS attacks?
hard
A. Use parameterized queries for database and escape HTML output with a Content Security Policy (CSP)
B. Sanitize user input by removing all special characters before storing
C. Use client-side JavaScript to validate input and disable scripts in browser settings
D. Store raw input and rely on HTTPS to secure data transmission
Solution
Step 1: Prevent SQL injection
Use parameterized queries to safely handle user input in database commands.
Step 2: Prevent XSS attacks
Escape HTML output when rendering user content and enforce a Content Security Policy to restrict script execution.
Final Answer:
Use parameterized queries for database and escape HTML output with a Content Security Policy (CSP) -> Option A