Web Security / Broken Authentication and Session Management

Preventing unvalidated redirects and forwards

In this tutorial, we will explore the topic of unvalidated redirects and forwards. We will understand what they are, why they are dangerous, and how to protect your web applicatio…

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Application functions related to authentication and session management are often implemented incorrectly, allowing attackers to compromise passwords, keys, or session tokens.

1. Introduction

In this tutorial, we will delve into the concept of unvalidated redirects and forwards, which is a common vulnerability in web applications. A web application might be manipulated into redirecting or forwarding a user to an unexpected, potentially malicious URL. By the end of this tutorial, you will understand the risks associated with these vulnerabilities and how to prevent them in your web applications.

What you'll learn:
- What unvalidated redirects and forwards are
- The potential risks they pose
- How to prevent unvalidated redirects and forwards in your web applications

Prerequisites:
- Basic understanding of web development
- Familiarity with a server-side language such as PHP, JavaScript (Node.js), Python, or Java.

2. Step-by-Step Guide

Unvalidated redirects and forwards occur when a web application accepts untrusted input that could cause the web application to redirect the request to a URL contained within untrusted input.

Example:

// PHP code vulnerable to unvalidated redirects
header("Location: " . $_GET['url']);

In the above example, an attacker could provide a malicious URL as a parameter, causing users to be redirected to a harmful site.

Best Practice:

To prevent unvalidated redirects and forwards, avoid using user input to determine the destination of redirects and forwards. If it's necessary, ensure that the supplied value is valid, and is authorized for the user.

3. Code Examples

Example 1: Ensuring only valid URLs are used in redirects (PHP)

$allowed_urls = ['https://yourwebsite.com/page1', 'https://yourwebsite.com/page2'];

if(in_array($_GET['url'], $allowed_urls)){
    header("Location: " . $_GET['url']);
} else {
    // Redirect to a default page
    header("Location: https://yourwebsite.com/error");
}

Example 2: Preventing unvalidated forwards in Java

// Java code vulnerable to unvalidated forwards
request.getRequestDispatcher("/" + request.getParameter("page")).forward(request, response);

// Java code safe from unvalidated forwards
String page = request.getParameter("page");
if (page.equals("home") || page.equals("about") || page.equals("contact")) {
    request.getRequestDispatcher("/" + page).forward(request, response);
} else {
    response.sendRedirect("error.jsp");
}

4. Summary

In this tutorial, we covered what unvalidated redirects and forwards are, why they are a security risk, and how to mitigate these risks. The best way to prevent unvalidated redirects and forwards is to avoid using user input to determine the destination. If user input is necessary, ensure the supplied value is valid and authorized for the user.

5. Practice Exercises

Exercise 1: Given an array of valid URLs, write a function in PHP that only redirects to these URLs. If an invalid URL is provided, the function should redirect to a default error page.

Exercise 2: In Java, modify the provided code that uses request.getRequestDispatcher() to forward the request to a new page based on user input. Ensure that the new page is one of "home", "about", or "contact". If an invalid page is provided, redirect the user to an error page.

Solutions:

Please refer to the code examples provided in section 3 for the solutions to these exercises.

Keep practicing to further solidify your understanding of preventing unvalidated redirects and forwards.

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

Favicon Generator

Create favicons from images.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

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