Skip to main content

Shared CSRF, security headers, logging, observability, flash messages, role-cleanup, and Auth client for Coffee House FastAPI apps

Project description

coffeehouse-common

Shared Python package for Coffee House ecosystem FastAPI apps:

  • coffeehouse_common.csrfCSRFMiddleware subclass (form body + header + safe body replay)
  • coffeehouse_common.security_middlewareSecurityHeadersMiddleware (raw ASGI)
  • coffeehouse_common.logging_configRequestIdMiddleware (raw ASGI), RequestIdFilter, configure_logging, get_request_id
  • coffeehouse_common.observabilityinit_sentry(app_slug) Sentry SDK bootstrap; reads SENTRY_DSN from env (no-op when unset), tags events with app_slug, drops 4xx HTTP/CSRF noise, keeps 5xx

v0.3.0+ implements security headers and request ID middleware as raw ASGI (not BaseHTTPMiddleware) so streaming responses are not fully buffered. v0.3.1 sets X-Frame-Options: SAMEORIGIN (same-origin iframes allowed; third-party framing blocked). Logs include a per-request correlation id when configure_logging() + RequestIdMiddleware are used.

Install

From GitHub (pinned tag):

pip install "coffeehouse-common @ git+https://github.com/rubencfh/coffeehouse-common.git@v0.9.0"

Local editable install:

pip install -e ../coffeehouse-common

Development

Set up the repo for local development + testing:

git clone https://github.com/rubencfh/coffeehouse-common.git
cd coffeehouse-common
pip install -e ".[testing]"
pip install -r requirements-dev.txt
pre-commit install

Run the full test suite (with coverage, fails under 70%):

pytest

Run the pre-commit hooks against all files (format, lint, secret scan, syntax check):

pre-commit run --all-files

Individual checks:

ruff format --check .          # formatting
ruff check .                   # linting
mypy coffeehouse_common        # type-checking (lenient baseline)
bandit -c pyproject.toml -r coffeehouse_common coffeehouse_testing  # SAST
pip-audit -r requirements-dev.txt                                    # SCA

CI runs the same checks on every push / PR via .github/workflows/ci.yml. See coffeehouse-ecosystem/docs/pipeline.md for the broader pipeline.

Shared test fixtures

The coffeehouse_testing subpackage ships with this repo and provides shared pytest fixtures that consumer apps import in their own tests/conftest.py:

from coffeehouse_testing.jwt_factory import jwt_factory
from coffeehouse_testing.client import authed_client
from coffeehouse_testing.db import postgres_container, clean_db  # noqa: F401
from coffeehouse_testing.auth_stub import make_auth_stub, build_roles_manifest

All heavy dependencies (PyJWT, testcontainers, starlette) are loaded lazily — import the modules you need in tests and the import errors only fire if the underlying package is not installed.

Usage

from coffeehouse_common.csrf import CSRFMiddleware
from coffeehouse_common.logging_config import RequestIdMiddleware, configure_logging
from coffeehouse_common.observability import init_sentry
from coffeehouse_common.security_middleware import SecurityHeadersMiddleware

configure_logging()  # once at startup; attaches RequestIdFilter for %(request_id)s
init_sentry(app_slug="auth")  # no-op if SENTRY_DSN is unset

init_sentry reads SENTRY_DSN, RAILWAY_ENVIRONMENT_NAME (or ENVIRONMENT), and RAILWAY_GIT_COMMIT_SHA from the environment. Errors-only sample rate by default (traces_sample_rate=0.0). 401/403/404/405 responses, 4xx HTTPExceptions, and CSRFErrors are dropped client-side so they don't burn the Sentry quota. 5xx HTTPExceptions and any other exception class are kept. Pass extra_ignored_status_codes= / extra_ignored_exceptions= to extend the filter per app.

Explicit dependency: itsdangerous (CSRF token signing).

See CONVENTIONS.md in the coffeehouse-ecosystem repo for middleware order and CSRF patterns.

First-time publish (maintainers)

  1. Create an empty repo on GitHub: rubencfh/coffeehouse-common
  2. From this directory:
git remote add origin https://github.com/rubencfh/coffeehouse-common.git
git push -u origin master
git push origin v0.9.0

Until the repo and tag exist, app Docker builds that pip install from GitHub will fail. For local dev, use editable install (above).

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

coffeehouse_common-0.15.1.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coffeehouse_common-0.15.1-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file coffeehouse_common-0.15.1.tar.gz.

File metadata

  • Download URL: coffeehouse_common-0.15.1.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for coffeehouse_common-0.15.1.tar.gz
Algorithm Hash digest
SHA256 be0860baed442b3d4a2ea9f785064fab7547370023105e79cca824cedd21be86
MD5 386b40a75e93cf2c532b6a5524233874
BLAKE2b-256 a0706a4224ddb0837fda73896632b6ccfc60b61f1c44becc2f4175cd72fec423

See more details on using hashes here.

Provenance

The following attestation bundles were made for coffeehouse_common-0.15.1.tar.gz:

Publisher: release.yml on coffeehouse-tools/coffeehouse-common

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file coffeehouse_common-0.15.1-py3-none-any.whl.

File metadata

File hashes

Hashes for coffeehouse_common-0.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4eb983cf2d5a1ed5062ab0165f0334ba4d286c9f8a0713aee751ceea1610c636
MD5 3a5a87395f0d3d8f39e598a00a8ce2e8
BLAKE2b-256 bb32bc4c847af3ba74cc81bb6fde72587d1dfe4e52f71937bb72b5b4842c29a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for coffeehouse_common-0.15.1-py3-none-any.whl:

Publisher: release.yml on coffeehouse-tools/coffeehouse-common

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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