Security & Performance

Web Security Fundamentals

20 min Lesson 1 of 35

Web Security Fundamentals

Web security is the practice of protecting websites, web applications, and web services from various cyber threats. In this lesson, we'll explore the foundational principles that guide modern web security practices.

The CIA Triad

The CIA triad is the cornerstone of information security, consisting of three core principles:

  • Confidentiality: Ensuring that information is accessible only to authorized individuals. Examples include encryption, access controls, and authentication mechanisms.
  • Integrity: Maintaining the accuracy and trustworthiness of data throughout its lifecycle. This prevents unauthorized modification, deletion, or creation of data.
  • Availability: Ensuring that authorized users have reliable and timely access to information and resources. This includes protection against DDoS attacks and ensuring system uptime.
Note: All security measures should support at least one aspect of the CIA triad. If a security control doesn't enhance confidentiality, integrity, or availability, its value should be questioned.

The Threat Landscape

Modern web applications face numerous threats from various actors:

  • Script Kiddies: Inexperienced attackers using automated tools and pre-written exploits
  • Hacktivists: Politically or socially motivated attackers
  • Organized Crime: Professional criminals seeking financial gain
  • Nation-State Actors: Government-sponsored groups with advanced capabilities
  • Insider Threats: Malicious or negligent employees with legitimate access
Common attack vectors:
- Phishing and social engineering
- Malware and ransomware
- Zero-day exploits
- Supply chain attacks
- API vulnerabilities
- Misconfigured cloud services

OWASP Top 10 Overview

The Open Web Application Security Project (OWASP) maintains a list of the most critical web application security risks. The OWASP Top 10 (2021) includes:

  1. Broken Access Control: Improper enforcement of user permissions
  2. Cryptographic Failures: Weak encryption or exposed sensitive data
  3. Injection: SQL, NoSQL, OS command, and LDAP injection attacks
  4. Insecure Design: Missing or ineffective security controls in design phase
  5. Security Misconfiguration: Improperly configured security settings
  6. Vulnerable and Outdated Components: Using libraries with known vulnerabilities
  7. Identification and Authentication Failures: Weak session management or authentication
  8. Software and Data Integrity Failures: Insecure CI/CD pipelines or unsigned updates
  9. Security Logging and Monitoring Failures: Insufficient logging and alerting
  10. Server-Side Request Forgery (SSRF): Abusing server functionality to access internal resources
Tip: Familiarize yourself with the full OWASP Top 10 documentation at owasp.org. Each vulnerability includes detailed explanations, examples, and prevention strategies.

Security Mindset

Developing a security mindset is essential for building secure applications. Key principles include:

  • Trust Nothing: Validate all inputs, even from trusted sources
  • Fail Securely: Ensure that failures don't expose sensitive information or grant unintended access
  • Think Like an Attacker: Consider how features could be abused or exploited
  • Security by Design: Integrate security from the initial design phase, not as an afterthought
  • Minimize Attack Surface: Reduce the number of potential entry points for attackers
Example of security mindset in code review:
<!-- Insecure -->
<img src="{{userInput}}">

<!-- Secure thinking: What if userInput contains JavaScript? -->
<img src="{{sanitize(userInput)}}" onerror="this.style.display='none'">

Defense in Depth

Defense in depth is a layered security strategy that implements multiple security controls at different levels:

  • Network Layer: Firewalls, VPNs, network segmentation
  • Host Layer: Antivirus, host-based firewalls, patch management
  • Application Layer: Input validation, authentication, authorization
  • Data Layer: Encryption at rest and in transit, data masking
  • Physical Layer: Access controls, surveillance, environmental controls
Note: If one layer fails, other layers continue to provide protection. This redundancy is crucial for robust security posture.
Defense in depth example for user authentication:
1. HTTPS for encrypted transmission
2. Rate limiting on login endpoint
3. Strong password requirements
4. Password hashing with bcrypt
5. Multi-factor authentication
6. Account lockout after failed attempts
7. Session timeout and secure cookies
8. Audit logging of authentication events

Principle of Least Privilege

The principle of least privilege states that users, processes, and systems should have only the minimum access rights necessary to perform their functions:

  • User Accounts: Grant only required permissions, avoid unnecessary admin rights
  • Database Access: Application should use accounts with limited privileges
  • API Keys: Scope tokens to specific resources and operations
  • Service Accounts: Limit permissions to specific services and resources
Warning: Running applications or services with excessive privileges (e.g., root or administrator accounts) significantly increases the impact of a successful attack. If compromised, attackers inherit those elevated privileges.
Example: Database user with least privilege
-- Instead of granting all privileges
GRANT ALL PRIVILEGES ON database.* TO 'app_user'@'localhost';

-- Grant only necessary permissions
GRANT SELECT, INSERT, UPDATE ON database.users TO 'app_user'@'localhost';
GRANT SELECT ON database.products TO 'app_user'@'localhost';

Security is a Process, Not a Product

Effective security requires ongoing effort and continuous improvement:

  • Regular Updates: Keep all software, libraries, and dependencies current
  • Security Testing: Conduct regular penetration tests and vulnerability assessments
  • Incident Response: Have a plan for detecting, responding to, and recovering from security incidents
  • Security Training: Educate developers and users about security best practices
  • Threat Intelligence: Stay informed about emerging threats and vulnerabilities
Exercise: Review a web application you've worked on or use regularly. For each component (frontend, backend, database, APIs), identify:
1. What CIA triad principles it needs to protect
2. What threats it might face
3. What layers of defense are in place
4. Whether it follows the principle of least privilege

Document at least 3 security improvements you would recommend.

Summary

Web security is built on fundamental principles including the CIA triad, defense in depth, and least privilege. Understanding the threat landscape and the OWASP Top 10 provides context for why specific security measures are necessary. Developing a security mindset means thinking proactively about threats and building security into every layer of your application from the start.

Next Steps: In the following lessons, we'll dive deep into specific vulnerabilities from the OWASP Top 10, starting with Cross-Site Scripting (XSS), and learn practical techniques to prevent them.