Public source file

backend/scripts/payload_revalidation_poc_demo.py

Documentation home
160 lines5,019 bytesread-only generated view
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Demonstrate verifier-side payload revalidation behavior.

Usage:
    python3 backend/scripts/payload_revalidation_poc_demo.py
"""

from __future__ import annotations

import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[2]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from backend.app.services.payload_revalidation_poc import (  # noqa: E402
    match_payload_to_verified_domains,
)


CASES = [
    {
        "label": "Exact match",
        "payload": "https://acme.example/pay",
        "verified_domains": ["acme.example"],
        "allow_subdomains": False,
    },
    {
        "label": "WWW normalization",
        "payload": "https://www.acme.example/menu",
        "verified_domains": ["acme.example"],
        "allow_subdomains": False,
    },
    {
        "label": "Subdomain blocked by exact-only policy",
        "payload": "https://login.acme.example/sign-in",
        "verified_domains": ["acme.example"],
        "allow_subdomains": False,
    },
    {
        "label": "Subdomain allowed by policy",
        "payload": "https://login.acme.example/sign-in",
        "verified_domains": ["acme.example"],
        "allow_subdomains": True,
    },
    {
        "label": "Phishing mismatch",
        "payload": "https://evil.example/redirect?target=acme.example",
        "verified_domains": ["acme.example"],
        "allow_subdomains": False,
    },
    {
        "label": "Credential removed after issuance",
        "payload": "https://acme.example/pay",
        "verified_domains": [],
        "allow_subdomains": False,
    },
]


STATE_CHANGE_SCENARIOS = [
    {
        "label": "Issuer rotates verified payment host after issuance",
        "payload": "https://acme.example/pay",
        "steps": [
            {
                "state_label": "Initial issuer state",
                "verified_domains": ["acme.example"],
                "allow_subdomains": False,
            },
            {
                "state_label": "Issuer state after host rotation",
                "verified_domains": ["pay.acme.example"],
                "allow_subdomains": False,
            },
            {
                "state_label": "Issuer state after restoring original host",
                "verified_domains": ["acme.example"],
                "allow_subdomains": False,
            },
        ],
    },
    {
        "label": "Issuer tightens subdomain policy after issuance",
        "payload": "https://login.acme.example/sign-in",
        "steps": [
            {
                "state_label": "Initial issuer state with subdomains allowed",
                "verified_domains": ["acme.example"],
                "allow_subdomains": True,
            },
            {
                "state_label": "Issuer state after subdomain policy tightened",
                "verified_domains": ["acme.example"],
                "allow_subdomains": False,
            },
        ],
    },
    {
        "label": "Issuer removes credential record and later re-approves it",
        "payload": "https://merchant.acme.example/redeem",
        "steps": [
            {
                "state_label": "Initial issuer state",
                "verified_domains": ["acme.example"],
                "allow_subdomains": True,
            },
            {
                "state_label": "Issuer state after record removal",
                "verified_domains": [],
                "allow_subdomains": True,
            },
            {
                "state_label": "Issuer state after re-approval",
                "verified_domains": ["acme.example"],
                "allow_subdomains": True,
            },
        ],
    },
]


def main() -> None:
    print("Payload Revalidation PoC")
    print("========================")
    for case in CASES:
        decision = match_payload_to_verified_domains(
            case["payload"],
            case["verified_domains"],
            allow_subdomains=case["allow_subdomains"],
        )
        print(f"{case['label']}: {'ALLOW' if decision.allowed else 'BLOCK'}")
        print(f"  payload: {case['payload']}")
        print(f"  reason: {decision.reason}")

    print()
    print("Issuer-state change scenarios")
    print("=============================")
    for scenario in STATE_CHANGE_SCENARIOS:
        print(scenario["label"])
        print(f"  payload: {scenario['payload']}")
        for step in scenario["steps"]:
            decision = match_payload_to_verified_domains(
                scenario["payload"],
                step["verified_domains"],
                allow_subdomains=step["allow_subdomains"],
            )
            print(
                f"  {step['state_label']}:"
                f" {'ALLOW' if decision.allowed else 'BLOCK'}"
            )
            print(f"    verified_domains: {step['verified_domains']}")
            print(f"    allow_subdomains: {step['allow_subdomains']}")
            print(f"    reason: {decision.reason}")


if __name__ == "__main__":
    main()