validation.py: validate one selected key per submission (section 19)
This commit is contained in:
1 parent
303c903228
commit
589ede0e91
1 file changed
+145
-68
+145
-68
@@ -1,10 +1,18 @@
|
|||||||
"""Validation of incoming health check payloads."""
|
"""Validation of incoming health check payloads.
|
||||||
|
|
||||||
|
One submission records one selected key only, as decided in section 19 of the
|
||||||
|
requirements. The next check date is derived on the server and never taken from
|
||||||
|
the client.
|
||||||
|
"""
|
||||||
|
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
GENERAL_STATUSES = ("OK", "PROBLEM", "IKKJE_VURDERT")
|
GENERAL_STATUSES = ("OK", "PROBLEM", "IKKJE_VURDERT")
|
||||||
SECURITY_STATUSES = ("TRYGG", "PATCHA", "SÅRBAR", "KOMPROMITTERT")
|
VERIFICATION_STATUSES = ("OK", "PROBLEM")
|
||||||
PRODUCTS = ("SeedSigner", "Blockstream Jade", "Nunchuk", "Sparrow Wallet")
|
SECURITY_STATUSES = ("SECURE", "PATCHED", "VULNERABLE", "COMPROMISED")
|
||||||
|
WALLET_SOFTWARE = ("Sparrow Wallet", "Nunchuk")
|
||||||
|
KEY_PRODUCTS = {1: "SeedSigner", 2: "Blockstream Jade", 3: "Nunchuk"}
|
||||||
|
CLOUD_KEY_NUMBER = 3
|
||||||
MAX_VERSION_LENGTH = 100
|
MAX_VERSION_LENGTH = 100
|
||||||
MAX_PATH_LENGTH = 200
|
MAX_PATH_LENGTH = 200
|
||||||
MAX_NOTES_LENGTH = 2000
|
MAX_NOTES_LENGTH = 2000
|
||||||
@@ -15,30 +23,130 @@ class ValidationError(ValueError):
|
|||||||
"""Raised when a payload does not match the expected health check shape."""
|
"""Raised when a payload does not match the expected health check shape."""
|
||||||
|
|
||||||
|
|
||||||
def validate_health_check(payload, key_ids):
|
def next_check_due(checked_at):
|
||||||
"""Return a normalized health check ready for storage."""
|
"""Return the check date one calendar year later, clamping 29 February."""
|
||||||
|
checked = date.fromisoformat(checked_at)
|
||||||
|
try:
|
||||||
|
return checked.replace(year=checked.year + 1).isoformat()
|
||||||
|
except ValueError:
|
||||||
|
return checked.replace(year=checked.year + 1, day=28).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def validate_health_check(payload, keys):
|
||||||
|
"""Return a normalized single-key health check ready for storage."""
|
||||||
if not isinstance(payload, dict):
|
if not isinstance(payload, dict):
|
||||||
raise ValidationError("payload must be a JSON object")
|
raise ValidationError("payload must be a JSON object")
|
||||||
|
|
||||||
|
entry = payload.get("key_check")
|
||||||
|
if not isinstance(entry, dict):
|
||||||
|
raise ValidationError("key_check must be an object")
|
||||||
|
|
||||||
|
key = _select_key(entry.get("key_id"), keys)
|
||||||
|
checked_at = _date(payload.get("checked_at"), "checked_at")
|
||||||
|
wallet_software = _wallet_software(payload.get("wallet_software"), key)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"checked_at": _date(payload.get("checked_at"), "checked_at"),
|
"checked_at": checked_at,
|
||||||
"next_check_due": _date(payload.get("next_check_due"), "next_check_due"),
|
"next_check_due": next_check_due(checked_at),
|
||||||
|
"wallet_software": wallet_software,
|
||||||
|
"software_version": _text(
|
||||||
|
payload.get("software_version"), "software_version", MAX_VERSION_LENGTH
|
||||||
|
),
|
||||||
"software_compatibility_status": _status(
|
"software_compatibility_status": _status(
|
||||||
payload.get("software_compatibility_status"),
|
payload.get("software_compatibility_status"),
|
||||||
"software_compatibility_status",
|
"software_compatibility_status",
|
||||||
GENERAL_STATUSES,
|
GENERAL_STATUSES,
|
||||||
),
|
),
|
||||||
"sparrow_version": _text(payload.get("sparrow_version"), "sparrow_version", MAX_VERSION_LENGTH),
|
"software_comments": _text(
|
||||||
"nunchuk_version": _text(payload.get("nunchuk_version"), "nunchuk_version", MAX_VERSION_LENGTH),
|
payload.get("software_comments"), "software_comments", MAX_NOTES_LENGTH
|
||||||
"software_comments": _text(payload.get("software_comments"), "software_comments", MAX_NOTES_LENGTH),
|
),
|
||||||
"contact_info_verified": _flag(payload.get("contact_info_verified"), "contact_info_verified"),
|
"contact_info_verified": _flag(
|
||||||
|
payload.get("contact_info_verified"), "contact_info_verified"
|
||||||
|
),
|
||||||
"comments": _text(payload.get("comments"), "comments", MAX_COMMENTS_LENGTH),
|
"comments": _text(payload.get("comments"), "comments", MAX_COMMENTS_LENGTH),
|
||||||
"key_checks": _key_checks(payload.get("key_checks"), key_ids),
|
"key_check": _key_check(entry, key),
|
||||||
"tool_checks": _tool_checks(payload.get("tool_checks")),
|
"tool_checks": _tool_checks(
|
||||||
"ai_security_checks": _ai_security_checks(payload.get("ai_security_checks")),
|
payload.get("tool_checks"), _expected_products(key, wallet_software)
|
||||||
|
),
|
||||||
|
"ai_security_checks": _ai_security_checks(
|
||||||
|
payload.get("ai_security_checks"), _expected_providers(key)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _select_key(key_id, keys):
|
||||||
|
"""Return the configured key profile with the given id."""
|
||||||
|
if isinstance(key_id, bool) or not isinstance(key_id, int):
|
||||||
|
raise ValidationError("key_id must be a configured key")
|
||||||
|
for key in keys:
|
||||||
|
if key["id"] == key_id:
|
||||||
|
return key
|
||||||
|
raise ValidationError("key_id is not a configured key")
|
||||||
|
|
||||||
|
|
||||||
|
def _wallet_software(value, key):
|
||||||
|
"""Return the selected wallet software, fixed to Nunchuk for the cloud key."""
|
||||||
|
if value not in WALLET_SOFTWARE:
|
||||||
|
raise ValidationError("wallet_software must be Sparrow Wallet or Nunchuk")
|
||||||
|
if key["key_number"] == CLOUD_KEY_NUMBER and value != "Nunchuk":
|
||||||
|
raise ValidationError("Nunchuk is the only wallet software for the cloud key")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _expected_products(key, wallet_software):
|
||||||
|
"""Return the products to record BIP327/SLIP39 for, without duplicates."""
|
||||||
|
return tuple(dict.fromkeys((KEY_PRODUCTS[key["key_number"]], wallet_software)))
|
||||||
|
|
||||||
|
|
||||||
|
def _expected_providers(key):
|
||||||
|
"""Return the security status providers relevant to the selected key."""
|
||||||
|
if key["key_number"] == CLOUD_KEY_NUMBER:
|
||||||
|
return ("Nunchuk",)
|
||||||
|
return (KEY_PRODUCTS[key["key_number"]], "Sparrow Wallet", "Nunchuk")
|
||||||
|
|
||||||
|
|
||||||
|
def _key_check(entry, key):
|
||||||
|
"""Return the normalized check for the selected key."""
|
||||||
|
available = _flag(entry.get("available"), "available")
|
||||||
|
check = {
|
||||||
|
"key_id": key["id"],
|
||||||
|
"available": available,
|
||||||
|
"hardware_works": None,
|
||||||
|
"firmware_version": None,
|
||||||
|
"seed_backup_verified": None,
|
||||||
|
"cloud_health_status": None,
|
||||||
|
"cloud_provider_operational": None,
|
||||||
|
"address_path_verified": None,
|
||||||
|
"address_path": None,
|
||||||
|
"comments": _text(entry.get("comments"), "comments", MAX_NOTES_LENGTH),
|
||||||
|
}
|
||||||
|
if available == 0:
|
||||||
|
# The remaining checks could not be performed, so nothing is recorded.
|
||||||
|
return check
|
||||||
|
|
||||||
|
required = available == 1
|
||||||
|
if key["type"] == "hardware":
|
||||||
|
check["hardware_works"] = _flag(entry.get("hardware_works"), "hardware_works")
|
||||||
|
check["firmware_version"] = _text(
|
||||||
|
entry.get("firmware_version"), "firmware_version", MAX_VERSION_LENGTH
|
||||||
|
)
|
||||||
|
check["seed_backup_verified"] = _verification(
|
||||||
|
entry.get("seed_backup_verified"), "seed_backup_verified", required
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
check["cloud_health_status"] = _optional_status(
|
||||||
|
entry.get("cloud_health_status"), "cloud_health_status"
|
||||||
|
)
|
||||||
|
check["cloud_provider_operational"] = _flag(
|
||||||
|
entry.get("cloud_provider_operational"), "cloud_provider_operational"
|
||||||
|
)
|
||||||
|
check["address_path_verified"] = _verification(
|
||||||
|
entry.get("address_path_verified"), "address_path_verified", required
|
||||||
|
)
|
||||||
|
check["address_path"] = _text(entry.get("address_path"), "address_path", MAX_PATH_LENGTH)
|
||||||
|
return check
|
||||||
|
|
||||||
|
|
||||||
def _date(value, field):
|
def _date(value, field):
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
raise ValidationError(f"{field} must be an ISO date")
|
raise ValidationError(f"{field} must be an ISO date")
|
||||||
@@ -49,12 +157,6 @@ def _date(value, field):
|
|||||||
return parsed.isoformat()
|
return parsed.isoformat()
|
||||||
|
|
||||||
|
|
||||||
def _optional_date(value, field):
|
|
||||||
if value is None or value == "":
|
|
||||||
return None
|
|
||||||
return _date(value, field)
|
|
||||||
|
|
||||||
|
|
||||||
def _status(value, field, allowed):
|
def _status(value, field, allowed):
|
||||||
if value not in allowed:
|
if value not in allowed:
|
||||||
raise ValidationError(f"{field} is not an allowed status")
|
raise ValidationError(f"{field} is not an allowed status")
|
||||||
@@ -67,6 +169,15 @@ def _optional_status(value, field):
|
|||||||
return _status(value, field, GENERAL_STATUSES)
|
return _status(value, field, GENERAL_STATUSES)
|
||||||
|
|
||||||
|
|
||||||
|
def _verification(value, field, required):
|
||||||
|
"""Return OK/PROBLEM, requiring a value when the key was available."""
|
||||||
|
if value is None or value == "":
|
||||||
|
if required:
|
||||||
|
raise ValidationError(f"{field} is required when the key is available")
|
||||||
|
return None
|
||||||
|
return _status(value, field, VERIFICATION_STATUSES)
|
||||||
|
|
||||||
|
|
||||||
def _flag(value, field):
|
def _flag(value, field):
|
||||||
if value is None or value == "":
|
if value is None or value == "":
|
||||||
return None
|
return None
|
||||||
@@ -89,52 +200,19 @@ def _text(value, field, max_length):
|
|||||||
return text or None
|
return text or None
|
||||||
|
|
||||||
|
|
||||||
def _key_checks(entries, key_ids):
|
def _tool_checks(entries, expected):
|
||||||
if not isinstance(entries, list):
|
"""Return the BIP327/SLIP39 rows for the key product and selected software."""
|
||||||
raise ValidationError("key_checks must be a list")
|
|
||||||
expected = set(key_ids)
|
|
||||||
seen = set()
|
|
||||||
normalized = []
|
|
||||||
for entry in entries:
|
|
||||||
if not isinstance(entry, dict):
|
|
||||||
raise ValidationError("each key check must be an object")
|
|
||||||
key_id = entry.get("key_id")
|
|
||||||
if not isinstance(key_id, int) or key_id not in expected:
|
|
||||||
raise ValidationError("key_id is not a configured key")
|
|
||||||
if key_id in seen:
|
|
||||||
raise ValidationError("each key may only be checked once")
|
|
||||||
seen.add(key_id)
|
|
||||||
normalized.append({
|
|
||||||
"key_id": key_id,
|
|
||||||
"available": _flag(entry.get("available"), "available"),
|
|
||||||
"hardware_works": _flag(entry.get("hardware_works"), "hardware_works"),
|
|
||||||
"firmware_version": _text(entry.get("firmware_version"), "firmware_version", MAX_VERSION_LENGTH),
|
|
||||||
"seed_backup_status": _optional_status(entry.get("seed_backup_status"), "seed_backup_status"),
|
|
||||||
"seed_backup_verified": _flag(entry.get("seed_backup_verified"), "seed_backup_verified"),
|
|
||||||
"cloud_health_status": _optional_status(entry.get("cloud_health_status"), "cloud_health_status"),
|
|
||||||
"cloud_provider_operational": _flag(entry.get("cloud_provider_operational"), "cloud_provider_operational"),
|
|
||||||
"address_verified": _flag(entry.get("address_verified"), "address_verified"),
|
|
||||||
"derivation_height": _text(entry.get("derivation_height"), "derivation_height", MAX_PATH_LENGTH),
|
|
||||||
"address_path_verified": _flag(entry.get("address_path_verified"), "address_path_verified"),
|
|
||||||
"address_path": _text(entry.get("address_path"), "address_path", MAX_PATH_LENGTH),
|
|
||||||
"comments": _text(entry.get("comments"), "comments", MAX_NOTES_LENGTH),
|
|
||||||
})
|
|
||||||
if seen != expected:
|
|
||||||
raise ValidationError("every configured key must be checked")
|
|
||||||
return normalized
|
|
||||||
|
|
||||||
|
|
||||||
def _tool_checks(entries):
|
|
||||||
if not isinstance(entries, list):
|
if not isinstance(entries, list):
|
||||||
raise ValidationError("tool_checks must be a list")
|
raise ValidationError("tool_checks must be a list")
|
||||||
|
allowed = set(expected)
|
||||||
seen = set()
|
seen = set()
|
||||||
normalized = []
|
normalized = []
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if not isinstance(entry, dict):
|
if not isinstance(entry, dict):
|
||||||
raise ValidationError("each tool check must be an object")
|
raise ValidationError("each tool check must be an object")
|
||||||
product = entry.get("product")
|
product = entry.get("product")
|
||||||
if product not in PRODUCTS:
|
if product not in allowed:
|
||||||
raise ValidationError("tool check product is not supported")
|
raise ValidationError("tool check product is not relevant for this submission")
|
||||||
if product in seen:
|
if product in seen:
|
||||||
raise ValidationError("each product may only be checked once")
|
raise ValidationError("each product may only be checked once")
|
||||||
seen.add(product)
|
seen.add(product)
|
||||||
@@ -142,26 +220,25 @@ def _tool_checks(entries):
|
|||||||
"product": product,
|
"product": product,
|
||||||
"bip327_status": _status(entry.get("bip327_status"), "bip327_status", GENERAL_STATUSES),
|
"bip327_status": _status(entry.get("bip327_status"), "bip327_status", GENERAL_STATUSES),
|
||||||
"slip39_status": _status(entry.get("slip39_status"), "slip39_status", GENERAL_STATUSES),
|
"slip39_status": _status(entry.get("slip39_status"), "slip39_status", GENERAL_STATUSES),
|
||||||
"version": _text(entry.get("version"), "version", MAX_VERSION_LENGTH),
|
|
||||||
"checked_at": _optional_date(entry.get("checked_at"), "checked_at"),
|
|
||||||
"notes": _text(entry.get("notes"), "notes", MAX_NOTES_LENGTH),
|
|
||||||
})
|
})
|
||||||
if seen != set(PRODUCTS):
|
if seen != allowed:
|
||||||
raise ValidationError("every supported product must be checked")
|
raise ValidationError("every relevant product must be checked")
|
||||||
return normalized
|
return normalized
|
||||||
|
|
||||||
|
|
||||||
def _ai_security_checks(entries):
|
def _ai_security_checks(entries, expected):
|
||||||
|
"""Return the security status rows relevant to the selected key."""
|
||||||
if not isinstance(entries, list):
|
if not isinstance(entries, list):
|
||||||
raise ValidationError("ai_security_checks must be a list")
|
raise ValidationError("ai_security_checks must be a list")
|
||||||
|
allowed = set(expected)
|
||||||
seen = set()
|
seen = set()
|
||||||
normalized = []
|
normalized = []
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if not isinstance(entry, dict):
|
if not isinstance(entry, dict):
|
||||||
raise ValidationError("each security check must be an object")
|
raise ValidationError("each security check must be an object")
|
||||||
provider = entry.get("provider")
|
provider = entry.get("provider")
|
||||||
if provider not in PRODUCTS:
|
if provider not in allowed:
|
||||||
raise ValidationError("security check provider is not supported")
|
raise ValidationError("security check provider is not relevant for this submission")
|
||||||
if provider in seen:
|
if provider in seen:
|
||||||
raise ValidationError("each provider may only be checked once")
|
raise ValidationError("each provider may only be checked once")
|
||||||
seen.add(provider)
|
seen.add(provider)
|
||||||
@@ -170,6 +247,6 @@ def _ai_security_checks(entries):
|
|||||||
"status": _status(entry.get("status"), "status", SECURITY_STATUSES),
|
"status": _status(entry.get("status"), "status", SECURITY_STATUSES),
|
||||||
"comments": _text(entry.get("comments"), "comments", MAX_NOTES_LENGTH),
|
"comments": _text(entry.get("comments"), "comments", MAX_NOTES_LENGTH),
|
||||||
})
|
})
|
||||||
if seen != set(PRODUCTS):
|
if seen != allowed:
|
||||||
raise ValidationError("every provider must have a security status")
|
raise ValidationError("every relevant provider must have a security status")
|
||||||
return normalized
|
return normalized
|
||||||
Reference in new issue
Block a user