Public source file

backend/app/services/payload_revalidation_poc.py

Documentation home
250 lines7,753 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
PoC support for verification-time payload revalidation.

This module normalizes the payload destination and compares it against the
issuer's currently verified domains using deterministic rules.
"""

from __future__ import annotations

import json
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import parse_qsl, urlparse

from backend.app.services.governance_fixture_store import DEFAULT_FIXTURE_DIR


@dataclass
class NormalizedDestination:
    raw_payload: str
    host: str
    scheme: str | None
    port: int | None
    path: str
    query_keys: list[str]


@dataclass
class MatchDecision:
    allowed: bool
    matched_rule: str | None
    reason: str


def _normalize_host(host: str) -> str:
    normalized = host.strip().lower().rstrip(".")
    if normalized.startswith("www."):
        normalized = normalized[4:]
    return normalized


def normalize_payload_destination(payload: str) -> NormalizedDestination:
    payload = payload.strip()
    if not payload:
        raise ValueError("Payload is empty")

    if payload.startswith(("http://", "https://")):
        parsed = urlparse(payload)
    else:
        parsed = urlparse(f"https://{payload}")

    if not parsed.hostname:
        raise ValueError(f"Could not extract a host from payload: {payload}")

    try:
        port = parsed.port
    except ValueError as exc:
        raise ValueError(f"Invalid destination URL: {exc}") from exc

    return NormalizedDestination(
        raw_payload=payload,
        host=_normalize_host(parsed.hostname),
        scheme=parsed.scheme or None,
        port=port,
        path=parsed.path or "/",
        query_keys=list(dict.fromkeys(key for key, _ in parse_qsl(parsed.query))),
    )


def _is_same_or_subdomain(candidate: str, verified: str) -> bool:
    return candidate == verified or candidate.endswith(f".{verified}")


def _load_destination_policy(fixture_dir: Path = DEFAULT_FIXTURE_DIR) -> dict[str, object] | None:
    path = fixture_dir / "destination-policy.json"
    if not path.exists():
        return None
    payload = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(payload, dict):
        return None
    return payload


def _string_list(value: object) -> list[str]:
    if not isinstance(value, list):
        return []
    return [item for item in value if isinstance(item, str) and item.strip()]


def _url_without_query(value: str) -> str | None:
    parsed = urlparse(value.strip())
    if parsed.scheme not in {"http", "https"} or not parsed.hostname:
        return None
    port = f":{parsed.port}" if parsed.port else ""
    path = parsed.path or "/"
    return f"{parsed.scheme}://{parsed.hostname.lower()}{port}{path}"


def _rule_allows_destination_host(
    rule: dict[str, object],
    host: str,
) -> bool:
    expected_final_url = rule.get("expected_final_url")
    if isinstance(expected_final_url, str) and expected_final_url.strip():
        expected_host = urlparse(expected_final_url.strip()).hostname
        return expected_host is not None and host == _normalize_host(expected_host)

    allow_subdomains = rule.get("allow_subdomains") is True
    for allowed_host in _string_list(rule.get("allowed_hosts")):
        normalized = _normalize_host(allowed_host)
        if host == normalized or (
            allow_subdomains and _is_same_or_subdomain(host, normalized)
        ):
            return True
    return False


def _policy_path_reason(
    destination: NormalizedDestination,
    rule: dict[str, object],
) -> str | None:
    path_prefixes = _string_list(rule.get("path_prefixes"))
    if path_prefixes and not any(
        destination.path.startswith(prefix) for prefix in path_prefixes
    ):
        return (
            f"Destination path '{destination.path}' is outside issuer-approved "
            f"path prefixes: {path_prefixes}"
        )

    query_policy = rule.get("query_policy")
    if query_policy == "none" and destination.query_keys:
        return "Destination query string is not approved for this issuer policy"

    allowed_query_keys = _string_list(rule.get("allowed_query_keys"))
    if allowed_query_keys and any(
        key.split("=", 1)[0] not in allowed_query_keys for key in destination.query_keys
    ):
        return (
            "Destination query string contains keys outside issuer-approved "
            f"query keys: {allowed_query_keys}"
        )

    return None


def _match_destination_policy(
    destination: NormalizedDestination,
) -> MatchDecision | None:
    policy = _load_destination_policy()
    if policy is None:
        return None

    redirect_policy = policy.get("redirect_policy")
    if isinstance(redirect_policy, dict):
        resolver_urls = {
            _url_without_query(item)
            for item in _string_list(redirect_policy.get("resolver_urls"))
        }
        resolver_urls.discard(None)
        if _url_without_query(destination.raw_payload) in resolver_urls:
            return MatchDecision(
                allowed=True,
                matched_rule=destination.host,
                reason="Approved resolver URL",
            )

    raw_rules = policy.get("approved_destinations")
    if not isinstance(raw_rules, list):
        return None

    matched_rules = [
        rule
        for rule in raw_rules
        if isinstance(rule, dict) and _rule_allows_destination_host(rule, destination.host)
    ]
    if not matched_rules:
        return None

    first_reason: str | None = None
    for rule in matched_rules:
        reason = _policy_path_reason(destination, rule)
        if reason is None:
            return MatchDecision(
                allowed=True,
                matched_rule=destination.host,
                reason="Destination matches issuer-approved destination policy",
            )
        first_reason = first_reason or reason

    return MatchDecision(
        allowed=False,
        matched_rule=None,
        reason=first_reason or "Destination is outside issuer-approved destination policy",
    )


def match_payload_to_verified_domains(
    payload: str,
    verified_domains: list[str],
    *,
    allow_subdomains: bool = False,
) -> MatchDecision:
    try:
        destination = normalize_payload_destination(payload)
    except ValueError as exc:
        return MatchDecision(
            allowed=False,
            matched_rule=None,
            reason=str(exc),
        )

    normalized_rules = [_normalize_host(rule) for rule in verified_domains if rule.strip()]

    if not normalized_rules:
        return MatchDecision(
            allowed=False,
            matched_rule=None,
            reason="No currently verified domains are available for this issuer",
        )

    for rule in normalized_rules:
        if destination.host == rule:
            policy_decision = _match_destination_policy(destination)
            if policy_decision is not None:
                return policy_decision
            return MatchDecision(
                allowed=True,
                matched_rule=rule,
                reason="Exact host match",
            )
        if allow_subdomains and _is_same_or_subdomain(destination.host, rule):
            policy_decision = _match_destination_policy(destination)
            if policy_decision is not None:
                return policy_decision
            return MatchDecision(
                allowed=True,
                matched_rule=rule,
                reason="Subdomain match under verified parent domain",
            )

    return MatchDecision(
        allowed=False,
        matched_rule=None,
        reason=(
            f"Destination host '{destination.host}' does not match current "
            f"verified domains: {normalized_rules}"
        ),
    )