KAHIBARO
Discord Login Register

15.1. Introduction to Web Security

Why Web Security Matters

When you build backend systems, you are handling other people’s data and money. A single security mistake can expose passwords, leak private messages, or allow attackers to control your servers.

Security is not something you “add at the end.” It is part of every decision you make, from how you design your database to how you handle a single HTTP request.

Some real world examples of what can go wrong:

All of these are common, and all of them were caused by simple coding or design mistakes.

Important statement: As a backend developer, you are responsible for protecting user data, not only for making features work.

In this chapter, you will get a high level map of common security concepts and typical risks, so that later chapters about specific attacks and defenses will make sense.

Core Security Concepts

Before you learn “how to defend,” you need a mental model of what you are defending and what “secure” means.

Confidentiality, Integrity, Availability (CIA)

Security is often summarized with three letters, CIA:

ConceptMeaningExample in a backend context
ConfidentialityOnly authorized people or systems can see the dataOthers cannot see my private messages
IntegrityData cannot be changed in an unauthorized wayNo one can change my order amount without my consent
AvailabilityThe service is up and can be used when neededThe API is not easily taken down by an attacker

Core rule: A secure system protects confidentiality, integrity, and availability of data and services.

A system can fail in any of these ways:

When you design and code, ask yourself: “Could this feature break confidentiality, integrity, or availability if misused?”

The Security Mindset

Backend beginners often think: “How do I make this work?” A security minded developer also asks: “How could someone make this behave in a way I did not intend?”

Some practical ways to think like this:

Example:

Assets, Threats, and Attackers

You cannot protect “everything” equally. You need to know what is valuable, who might attack it, and how.

What Are You Protecting? (Assets)

In backend systems, typical assets include:

Different assets have different sensitivity levels. Exposing an email list is bad. Exposing password hashes is worse. Exposing raw passwords or payment details is usually critical.

Who Might Attack You?

Attackers are not always “super hackers.” Many successful attacks are very simple.

Types of attackers:

You often do not know which kind of attacker you face, so you cannot rely on “obscurity” like “nobody will guess this URL.”

How Might They Attack? (Threats)

Common threat examples for a backend:

Later chapters will cover these threats in detail. For now, remember this pattern:

  1. You have an asset.
  2. There is a way to reach it (an endpoint, a database, a file system).
  3. An attacker misuses that path.

Every public interface in your backend can be a potential attack path.

Defense in Depth

No single protection is perfect. If you rely on just one check, one bug might be enough to compromise your system. The idea of defense in depth is to have multiple, independent protections.

Example for storing passwords:

  1. Use HTTPS so passwords are encrypted in transit.
  2. Use strong password hashing on the server.
  3. Apply rate limiting on login attempts.
  4. Monitor login failures for suspicious activity.

If one layer fails or is bypassed, other layers still provide some protection.

Another example, accessing a protected admin panel:

Key principle: Do not rely on a single security mechanism. Combine several layers, each reducing risk.

The Backend Trust Boundary

A trust boundary is a line between what you control and trust, and what you do not control.

For a typical web backend:

All data crossing into your backend from outside the boundary is untrusted.

This includes:

Do not treat data as safe just because it comes from “your frontend.” Attackers can ignore the frontend and send requests directly.

Example:

Your frontend has a disabled HTML input field role="user". You trust that all users are “user” and never “admin.” An attacker uses a tool like curl or Postman and sends "role": "admin" in the request body. If your backend code does not enforce role rules, you just gave them admin rights.

The trust boundary exists at the HTTP interface of your backend. Everything that crosses that line must be validated, sanitized, and authorized on the server.

Common Types of Vulnerabilities

You will later study specific attacks like SQL injection, XSS, and CSRF in their own chapters. Here we give a high level introduction so that you can recognize the categories.

Injection Attacks

An injection occurs when untrusted input is treated as part of a command or query sent to another system.

Some main types:

Example of unsafe Python code:

python
# Very unsafe example
user_id = request.query_params["user_id"]
sql = f"SELECT * FROM users WHERE id = {user_id};"
cursor.execute(sql)

If user_id is 1; DROP TABLE users;, the combined SQL might delete the table.

The safe approach uses parameterized queries, which will be covered in SQL and ORM chapters.

Authentication and Authorization Flaws

These problems occur when:

Typical examples:

You will see these issues deeply in the Authentication and Authorization sections.

Cross-Site Scripting (XSS) and Cross-Site Request Forgery (CSRF)

These are attacks related to the browser and the way it interacts with your backend:

As a backend developer, you prevent XSS by encoding output properly and validating input. You prevent CSRF by using proper tokens and same site cookies, which will be discussed later.

Insecure Direct Object References (IDOR)

In an IDOR vulnerability, the backend exposes a reference to an internal object, such as a numeric ID, and fails to check whether the current user is allowed to access it.

Example pattern:

An attacker logs in, sees that their invoice is id=10, then tries id=11, id=12, and so on. If your backend does not verify ownership, they can read other people’s invoices.

Security Misconfigurations

Not all vulnerabilities are in your code. Many are in the environment:

As a backend developer, you will be involved in configuration for web servers, containers, and cloud services. Misconfigurations often give attackers a very easy path.

Secure Development Principles

Security is easier if it is part of your usual way of writing code, not a special step you remember sometimes.

Principle of Least Privilege

Give users, services, and processes the minimum rights they need to do their job, and no more.

Examples:

If an attacker gains access to a limited account, the damage is limited.

Important rule: Always grant the smallest set of permissions that still allows a task to work.

Never Trust, Always Verify

Do not trust:

Server side you must:

Example:

A registration form validates email format in JavaScript. That is helpful for users, but the backend should still enforce valid email format. Attackers can bypass frontend validation easily.

Fail Securely

When something goes wrong, your system should fail in a safe way.

Examples:

A bad pattern is “if something goes wrong in security related code, just ignore the error and continue.” That often leads to open doors.

Do Not Roll Your Own Crypto or Security Schemes

Security is hard to get right. Small mistakes can completely break a protection.

Avoid:

Instead, rely on:

You still need to understand how to use them correctly, but you do not need to invent them.

Basics of Protecting Data in Transit and at Rest

Two basic ideas appear everywhere in web security: encryption “in transit” and encryption “at rest.”

Data in Transit

“Data in transit” is data moving between clients and servers, usually over HTTP.

To protect it:

With HTTP, any network observer can see and modify requests:

With HTTPS, data is encrypted between browser and server, which protects confidentiality and integrity.

You will learn details of HTTPS and TLS in a separate chapter. For now, remember that any production backend that handles user data must use HTTPS.

Data at Rest

“Data at rest” is data stored on disk:

You need different strategies:

Encryption is not a magic bullet. If an attacker can access your running application, they may still get decrypted data. However, encryption at rest protects you in case of lost disks, backup leaks, or offline attacks.

Logging, Monitoring, and Security

Security is not only about prevention. It is also about detection and response.

You should log:

But be careful:

Monitoring tools and alerts help you see patterns:

Later chapters on Logging and Monitoring will cover tools. For now, know that security without visibility is incomplete.

Security as an Ongoing Process

Security is never “done.” New vulnerabilities are discovered. New libraries have bugs. Requirements change.

Some continuous tasks for backend developers:

A useful habit is to ask for any new feature:

This mindset will help you apply all later security topics effectively throughout your backend projects.

Views: 6

Comments

Please login to add a comment.

Don't have an account? Register now!