coffeehouse-common
Shared Python package for Coffee House ecosystem FastAPI apps:
coffeehouse_common.csrf—CSRFMiddlewaresubclass (form body + header + safe body replay)coffeehouse_common.security_middleware—SecurityHeadersMiddleware(raw ASGI)coffeehouse_common.logging_config—RequestIdMiddleware(raw ASGI),RequestIdFilter,configure_logging,get_request_idcoffeehouse_common.observability—init_sentry(app_slug)Sentry SDK bootstrap; readsSENTRY_DSNfrom env (no-op when unset), tags events withapp_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).
Report-table sorting
coffeehouse_common.report_sort (promoted from Grind after Stoply became the
second adopter) pairs with coffeehouse-ui's sortable_th macro:
parse_sort(request, allowed=..., default_sort=...) validates
?sort=/?dir= against a per-report allowlist; sort_data(rows, sort, dir)
sorts a materialized list[dict] in place (Nones last in both directions,
case-insensitive strings). Both are re-exported from the package root.
See CONVENTIONS.md in the coffeehouse-ecosystem repo for middleware order and CSRF patterns.
First-time publish (maintainers)
- Create an empty repo on GitHub:
rubencfh/coffeehouse-common - 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).
Metadata
Release files for coffeehouse-common 0.17.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coffeehouse_common-0.17.0.tar.gz | 57.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coffeehouse_common-0.17.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 101.5 kB
Release files / coffeehouse_common-0.17.0.tar.gz
| Download URL | coffeehouse_common-0.17.0.tar.gz |
|---|---|
| Size | 57.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88f6e8a347dfa3eee1814ec76bf45ee39a195f5dd81031602be9cf00c19f19fa
|
|
BLAKE2b-256 checksum How to use checksums |
3e2e0d3cc46a13e3e5026ceaecda5a45a1b0c465bb1fd9387e964a6409885a35
|
| 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 Aug 26, 2026.
Transparency logRelease files / coffeehouse_common-0.17.0-py3-none-any.whl
| Download URL | coffeehouse_common-0.17.0-py3-none-any.whl |
|---|---|
| Size | 43.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da491d8cdad7a28ee84b6cd3768a6c12f32ec16fc2b965dff70c1847f543721b
|
|
BLAKE2b-256 checksum How to use checksums |
bb62427b0a0bb3742222895da30ea02016831537994961f7736a4c943fbceeca
|
| 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 Aug 26, 2026.
Transparency log