"""Local allow-list that disagrees with the path the app will open.

The strings are literals in this file. Nothing is sent to a network.
"""

from urllib.parse import unquote
import posixpath


def app_path(raw: str) -> str:
    return posixpath.normpath(unquote(raw))


def gate_on_raw(raw: str) -> bool:
    return raw.startswith("/public")


def gate_on_app_path(raw: str) -> bool:
    return app_path(raw).startswith("/public")


def main() -> None:
    samples = [
        "/public/note",
        "/admin",
        "/public/../admin",
        "/public/%2e%2e/admin",
    ]
    print("raw app_path raw_allow normalized_allow")
    for raw in samples:
        opened = app_path(raw)
        print(f"{raw} {opened} {gate_on_raw(raw)} {gate_on_app_path(raw)}")
    # A client-supplied override header is not the request path.
    request_path = "/public/note"
    claimed = "/admin"
    print(f"header_override_if_trusted={claimed}")
    print(f"header_override_if_ignored={request_path}")


if __name__ == "__main__":
    main()
