Skip to main content

Python module to validate a CAP message against the OASIS CAP 1.2 schema and verify the XML signature.

Project description

The CAP Validator

The Python module for ensuring the validity of CAP alerts.

License Badge Super-Linter Unit-Tests Publish-To-PyPI

Features

  • Schema Validation: Ensure your CAP XML file follows the CAP v1.2 standard.
  • Digital Signature Validation: Verify that the CAP XML file comes from a legitimate source and has not been tampered with.

Note: Currently, only certificates issued by a trused certificate authority (CA) are supported. This means signatures with a self-signed certificate will not pass the validation.

Getting Started

1. Installation

pip install capvalidator

2A. Using the API

We can perform a total validation of the CAP XML file using validate_xml(cap, strict).

  • cap: The CAP alert XML byte string.
  • strict: Whether or not signature validation is enforced. Defaults to True.
from capvalidator import validate_xml

# Read the CAP XML file as a byte string
with open(<cap-file-directory>, "rb") as f:
    cap = f.read()

# Perform the validation
result = validate_xml(cap, strict=True)

# Check the result
passed = result.passed
msg = result.msg

if not passed:
    # Logic for handling invalid CAP file

# Logic for handling valid CAP file

Or, alternatively, we can perform a more refined validation using check_schema(cap) and/or check_signature(cap):

from capvalidator import check_schema, check_signature

# Read the CAP XML file as a byte string
with open(<cap-file-directory>, "rb") as f:
    cap = f.read()

# Validate the schema
schema_result = check_schema(cap)

# Check the results
passed = schema_result.passed
msg = schema_result.msg

if not passed:
    # Logic for handling invalid CAP file

# Validate the signature
signature_result = check_signature(cap)

# Check the results
passed = signature_result.passed
msg = signature_result.msg

if not passed:
    # Logic for handling invalid CAP file

# Logic for handling valid CAP file

There is also a date extractor get_dates(cap) which you may find useful:

from capvalidator import get_dates

# Read the CAP XML file as a byte string
with open(<cap-file-directory>, "rb") as f:
    cap = f.read()

dts = get_dates(cap)

sent_date = dts.sent
effective_date = dts.effective
onset_date = dts.onset
expiry_date = dts.expiry

2B. Using the CLI

We can perform a total validation of the CAP XML file:

capvalidator validate <cap-file-directory>

By default this includes schema and signature validation.

To manually enable/disable enforcement of a valid XML signature, we can use the --strict or --no-strict arguments respectively:

capvalidator validate --strict <cap_file-directory> 
capvalidator validate --no-strict <cap_file-directory> 

Or, alternatively, for more refined validations we can use the --type argument:

capvalidator validate --type schema <cap-file-directory>
capvalidator validate --type signature <cap-file-directory>

Bugs and Issues

All bugs, enhancements and issues are managed on GitHub.

Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

capvalidator-0.1.0.dev3.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

capvalidator-0.1.0.dev3-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file capvalidator-0.1.0.dev3.tar.gz.

File metadata

  • Download URL: capvalidator-0.1.0.dev3.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for capvalidator-0.1.0.dev3.tar.gz
Algorithm Hash digest
SHA256 82d408ddf17acc920d62dd9cbe05370382e75b9150ed9e6145f533981319d641
MD5 7072d6995366d0b0e6d52aa4f0832643
BLAKE2b-256 c124f2847b35fc82f630f399cc8fd08f6955586b6ae495af4c96eb46b15fb19a

See more details on using hashes here.

File details

Details for the file capvalidator-0.1.0.dev3-py3-none-any.whl.

File metadata

File hashes

Hashes for capvalidator-0.1.0.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 d47e3d09a5fd8cf2a263b6c98cd392498b5a086cf1ba812306d0488ea1be7176
MD5 09f31a703c77094e706619929de48239
BLAKE2b-256 6a29c87ccad1e34ae61b7c02e6d0b7bae64f84448501059b182323239b2d7369

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page