CodeQL documentation

Incomplete URL substring sanitization

ID: py/incomplete-url-substring-sanitization
Kind: problem
Severity: warning
Precision: high
   - correctness
   - security
   - external/cwe/cwe-20
Query suites:
   - python-code-scanning.qls
   - python-security-extended.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Sanitizing untrusted URLs is a common technique for preventing attacks such as request forgeries and malicious redirections. Usually, this is done by checking that the host of a URL is in a set of allowed hosts.

However, treating the URL as a string and checking if one of the allowed hosts is a substring of the URL is very prone to errors. Malicious URLs can bypass such security checks by embedding one of the allowed hosts in an unexpected location.

Even if the substring check is not used in a security-critical context, the incomplete check may still cause undesirable behaviors when the check succeeds accidentally.


Parse a URL before performing a check on its host value, and ensure that the check handles arbitrary subdomain sequences correctly.


The following example code checks that a URL redirection will reach the domain.

from flask import Flask, request, redirect
from urllib.parse import urlparse

app = Flask(__name__)

# Not safe, as "" would be accepted

def unsafe1(request):
    target = request.args.get('target', '')
    if "" in target:
        return redirect(target)

# Not safe, as "" would be accepted

def unsafe2(request):
    target = request.args.get('target', '')
    if target.endswith(""):
        return redirect(target)

#Simplest and safest approach is to use an allowlist

def safe1(request):
    allowlist = [
    target = request.args.get('target', '')
    if target in allowlist:
        return redirect(target)

#More complex example allowing sub-domains.

def safe2(request):
    target = request.args.get('target', '')
    host = urlparse(target).hostname
    #Note the '.' preceding
    if host and host.endswith(""):
        return redirect(target)

The first two examples show unsafe checks that are easily bypassed. In unsafe1 the attacker can simply add anywhere in the url. For example,

In unsafe2 the attacker must use a hostname ending in, but that is easy to do. For example,

The second two examples show safe checks. In safe1, an allowlist is used. Although fairly inflexible, this is easy to get right and is most likely to be safe.

In safe2, urlparse is used to parse the URL, then the hostname is checked to make sure it ends with