WhakerPy 2.4

https://github.com/brigitte-bigi/WhakerPy/

Module whakerpy.httpd

Class BlackListPath

Description

Manager of a list of URL paths to be rejected.

An entry is a folder name, a file name or an extension, written the way it appears in the path, for example: - /wp-login.php - /.git/ - .php

A match can be: - exact match - parent-path match - extension match

Example: If '/admin' is in the blacklist, then '/admin', '/admin/' and '/admin/page.html' are considered blacklisted. If '.php' is in the blacklist, then '/index.php' and '/wp/admin.php' are too.

Constructor

__init__

Create an empty blacklist.

View Source
def __init__(self) -> None:
    """Create an empty blacklist."""
    self._blacklist = set()

Public functions

match

Override. Return True if an entry starts or ends the given path.

The middle of a path is never tested: an entry rejects the path it names, and nothing else.

Parameters
  • value: (str) A URL path to test, without its query string.
Returns
  • (bool) True if blacklisted, False otherwise.
View Source
def match(self, value: str) -> bool:
    """Override. Return True if an entry starts or ends the given path.

        The middle of a path is never tested: an entry rejects the path it
        names, and nothing else.

        :param value: (str) A URL path to test, without its query string.
        :return: (bool) True if blacklisted, False otherwise.

        """
    if type(value) is not str:
        raise TypeError('Value must be a string.')
    for entry in self._blacklist:
        if value.startswith(entry) is True:
            return True
        if value.endswith(entry) is True:
            return True
    return False