Skip to main content

Tlacacoca - Shared Foundation Library for TLS-Based Protocols

Python 3.10+ License: MIT Code style: Ruff

A protocol-agnostic foundation library providing shared components for building secure TLS-based network protocol implementations in Python. Tlacacoca (pronounced "tla-ka-KO-ka", from Nahuatl meaning "secure/safe") provides security, middleware, and logging utilities that can be shared across multiple protocol implementations.

Key Features

  • Security First - TLS context creation, TOFU certificate validation, certificate utilities
  • Middleware System - Rate limiting, IP access control, certificate authentication
  • PROXY Protocol - Opt-in v1/v2 support so servers behind TCP relays see real client IPs
  • Structured Logging - Privacy-preserving logging with IP hashing
  • Protocol Agnostic - Abstract interfaces that any TLS-based protocol can build upon
  • Modern Python - Full type hints, async/await support, and modern tooling with uv

Quick Start

Installation

# As a library
uv add tlacacoca

# From source (for development)
git clone https://github.com/alanbato/tlacacoca.git
cd tlacacoca && uv sync

Basic Usage

import ssl
from tlacacoca import (
    create_client_context,
    create_server_context,
    TOFUDatabase,
    RateLimiter,
    RateLimitConfig,
    AccessControl,
    AccessControlConfig,
    MiddlewareChain,
)

# Create TLS contexts
client_ctx = create_client_context(verify_mode=ssl.CERT_REQUIRED)
server_ctx = create_server_context("cert.pem", "key.pem")

# Set up TOFU certificate validation
async with TOFUDatabase(app_name="myapp") as tofu:
    # First connection - certificate is stored
    await tofu.verify_or_trust("example.com", 1965, cert_fingerprint)

    # Subsequent connections - certificate is verified
    await tofu.verify_or_trust("example.com", 1965, cert_fingerprint)

# Configure middleware chain
rate_config = RateLimitConfig(capacity=10, refill_rate=1.0)
access_config = AccessControlConfig(
    allow_list=["192.168.1.0/24"],
    default_allow=False
)

chain = MiddlewareChain([
    AccessControl(access_config),
    RateLimiter(rate_config),
])

# Process requests through middleware
result = await chain.process_request(
    request_url="protocol://example.com/path",
    client_ip="192.168.1.100"
)

if result.allowed:
    # Handle request
    pass
else:
    # Map denial_reason to protocol-specific response
    if result.denial_reason == "rate_limit":
        # e.g., Gemini: "44 SLOW DOWN\r\n"
        pass

Components

Security

Component Description
create_client_context() Create TLS context for client connections
create_server_context() Create TLS context for server connections
TOFUDatabase Trust-On-First-Use certificate validation
generate_self_signed_cert() Generate self-signed certificates
get_certificate_fingerprint() Get SHA-256 fingerprint of certificate
load_certificate() Load certificate from PEM file
create_permissive_server_context() PyOpenSSL context accepting self-signed client certs
TLSServerProtocol asyncio protocol for manual TLS via PyOpenSSL memory BIOs
TLSTransportWrapper Transport wrapper exposing peer certificate to inner protocol

Middleware

Component Description
MiddlewareChain Chain multiple middleware components
RateLimiter Token bucket rate limiting per IP
AccessControl IP-based allow/deny lists with CIDR support
CertificateAuth Client certificate authentication

Logging

Component Description
configure_logging() Configure structured logging
get_logger() Get a logger instance
hash_ip_processor() Privacy-preserving IP hashing

Protocol Implementations Using Tlacacoca

Tlacacoca is designed to be used by protocol-specific implementations:

  • nauyaca - Gemini protocol server & client
  • amatl - Scroll protocol implementation (planned)

Documentation

Middleware Return Types

Middleware components return MiddlewareResult with protocol-agnostic denial reasons:

from tlacacoca import MiddlewareResult, DenialReason

# Allowed request
result = MiddlewareResult(allowed=True)

# Denied request
result = MiddlewareResult(
    allowed=False,
    denial_reason=DenialReason.RATE_LIMIT,
    retry_after=30
)

Protocol implementations map these to their specific status codes:

Denial Reason Gemini Status Description
RATE_LIMIT 44 SLOW DOWN Rate limit exceeded
ACCESS_DENIED 53 PROXY REFUSED IP not allowed
CERT_REQUIRED 60 CLIENT CERT REQUIRED Need client certificate
CERT_NOT_AUTHORIZED 61 CERT NOT AUTHORIZED Certificate not in allowed list

Contributing

# Setup
git clone https://github.com/alanbato/tlacacoca.git
cd tlacacoca && uv sync

# Test
uv run pytest

# Lint & Type Check
uv run ruff check src/ tests/
uv run ty check src/

See CONTRIBUTING.md for guidelines.

License

MIT License - see LICENSE for details.

Resources


Status: Active development (pre-1.0). Core security and middleware features are stable.

Metadata

Release files for tlacacoca 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tlacacoca 0.3.0
File Size Uploaded Provenance Actions
tlacacoca-0.3.0.tar.gz 28.2 kB With provenance Details for tlacacoca-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tlacacoca 0.3.0
File Interpreter ABI Platform Actions
tlacacoca-0.3.0-py3-none-any.whl Python 3 none any Details for tlacacoca-0.3.0-py3-none-any.whl

Total release size: 62.8 kB

Release files / tlacacoca-0.3.0.tar.gz

Download URL tlacacoca-0.3.0.tar.gz
Size 28.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5f0b67b0cbc84c9fbbcd53eb799315cc740f036e69df3b4a9e11ebf9528b27ad
BLAKE2b-256 checksum
How to use checksums
a6debeb9aa72c3c1c84bebc73e7beb7134ff84c9047215f118b1e0ce016cee2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / tlacacoca-0.3.0-py3-none-any.whl

Download URL tlacacoca-0.3.0-py3-none-any.whl
Size 34.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3f4bff58d0502c51e58f288d2272f45a01284be9a28d755ce100f05b1d54b11
BLAKE2b-256 checksum
How to use checksums
00ffdaa2f11062fbf96ab860399104b9843c840c765701735f150dfa09e7d6e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Security for tlacacoca 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tlacacoca-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page