Web Security / Cross-Site Scripting (XSS)

Working with Content Security Policy

In this tutorial, we will focus on Content Security Policy (CSP), a powerful tool to prevent XSS attacks. You'll learn how to implement CSP in your HTML headers to specify trusted…

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

A type of security vulnerability typically found in web applications that enables attackers to inject client-side scripts into web pages viewed by other users.

Working with Content Security Policy

Introduction

This tutorial aims to provide a comprehensive understanding of Content Security Policy (CSP), a critical security feature used to mitigate Cross-Site Scripting (XSS) attacks. By the end of this guide, you will have a practical understanding of implementing CSP in your HTML headers to specify trusted sources of content.

What Will You Learn

  • The basics of Content Security Policy (CSP)
  • How to implement CSP using HTML headers
  • Best practices for using CSP

Prerequisites

  • Basic understanding of HTML
  • Familiarity with HTTP headers

Step-by-Step Guide

CSP is an added layer of security that helps to detect and mitigate certain types of attacks, including XSS and data injection attacks. It is primarily implemented through a set of HTTP headers that allow you to specify which domains are considered safe sources of scripts, styles, images, or other resources.

Set Up CSP

To set up CSP on your website, you need to send the Content-Security-Policy HTTP header from your server. For example, to allow content from the same origin and allow scripts from trusted APIs, your HTTP header would look like this:

Content-Security-Policy: default-src 'self'; script-src 'self' api.example.com;

In this example, 'self' refers to the current domain.

CSP Directives

CSP provides a wide range of directives, like script-src, style-src, img-src, etc., that control various resource types. For instance, script-src is used to restrict where scripts can be loaded from.

Code Examples

Example 1: Basic Content Security Policy

Content-Security-Policy: default-src 'self';

This policy only allows resources from the site's own origin. This is a good default policy to prevent content loading from outside sources.

Example 2: Allowing Specific Domains

Content-Security-Policy: default-src 'self'; img-src 'self' https://images.example.com;

This policy allows images to be loaded from the site's origin and the specified domain.

Example 3: Allowing Inline Scripts

Content-Security-Policy: script-src 'self' 'unsafe-inline';

This policy allows scripts from the same origin and inline scripts. However, allowing 'unsafe-inline' can increase the risk of XSS attacks.

Summary

In this tutorial, we covered the basics of CSP, how to implement it using HTML headers, and best practices. The next step is to explore more complex CSP directives and how they can further enhance your website's security.

Practice Exercises

  1. Implement a CSP that only allows scripts and styles from the same origin.
  2. Extend the above policy to allow images from https://images.example.com.
  3. Modify the policy to allow inline scripts.

Solutions

  1. Content-Security-Policy: script-src 'self'; style-src 'self';
  2. Content-Security-Policy: script-src 'self'; style-src 'self'; img-src 'self' https://images.example.com;
  3. Content-Security-Policy: script-src 'self' 'unsafe-inline'; style-src 'self'; img-src 'self' https://images.example.com;

Remember, unsafe-inline can expose your site to XSS attacks. Always review your CSP policies to ensure they provide the right balance between functionality and security.

Additional Resources

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help