Security Best Practices Flashcards

(10 cards)

1
Q

What is SQL injection, and how do you prevent it?

A

SQL injection inserts malicious SQL via user input. Prevent with parameterized queries. Example: In Node.js with pg: await pool.query('SELECT * FROM users WHERE id = $1', [userId]); avoids injection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you prevent Cross-Site Scripting (XSS) in a full-stack app?

A

Sanitize user input and escape output. Example: Use DOMPurify in React to clean HTML input, or set Content-Security-Policy headers in Express to limit scripts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is CSRF, and how do you mitigate it?

A

CSRF (Cross-Site Request Forgery) tricks users into unwanted actions. Mitigate with CSRF tokens. Example: Use csurf middleware in Express to validate tokens on POST requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you securely store passwords in a backend?

A

Hash passwords with a strong algorithm like bcrypt. Example: const bcrypt = require('bcrypt'); bcrypt.hash('password', 10, (err, hash) => { /* store hash */ });.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are secure HTTP headers, and which are critical?

A

Secure headers protect against attacks. Critical: Strict-Transport-Security (enforces HTTPS), X-Content-Type-Options: nosniff (prevents MIME sniffing). Set via Helmet in Express.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you implement secure JWT authentication?

A

```javascript
const jwt = require(‘jsonwebtoken’);
function generateToken(user) {
return jwt.sign({ id: user.id }, ‘secret’, { expiresIn: ‘1h’ });
}
// Explanation: Use short-lived tokens, store secret securely, verify on each request.
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the principle of least privilege in security?

A

Grant minimal access needed. Example: Use a database user with read-only access for API queries, not admin privileges, to limit damage if compromised.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you protect against API abuse?

A

Implement rate limiting and input validation. Example: Use express-rate-limit to cap requests at 100/hour per IP, validate payloads with Joi.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an OWASP Top 10 vulnerability?

A

OWASP Top 10 lists common security risks. Example: Broken Authentication (e.g., weak passwords). Mitigate with strong passwords, MFA, and JWT validation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you secure sensitive environment variables?

A

Store in .env files, exclude from Git, and use secrets management (e.g., AWS Secrets Manager). Example: DB_URL=postgres://... loaded with dotenv in Node.js.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly