scan-gateway
A pluggable malware-scan orchestrator for file uploads: tries configured scanner plugins (in order), falls back to a built-in local heuristic check when none can complete a scan, and raises a typed exception when something is flagged. It doesn't scan anything itself -- it's a gateway in front of whichever scanners you plug in.
from pathlib import Path
from scan_gateway import ScanGateway, ThreatDetectedError
from scan_gateway.scanners.clamav import ClamAVScanner
gateway = ScanGateway(scanners=[ClamAVScanner(host="clamav.internal")])
try:
await gateway.scan(Path("upload.pdf"))
except ThreatDetectedError as exc:
print(f"{exc.scanner_name} flagged this: {exc.verdict}")
If ClamAVScanner can't be reached, ScanGateway falls back to its
built-in LocalHeuristicScanner (YARA + a couple of PDF-structural
keyword checks) automatically -- the caller never needs to know which
layer actually scanned the file.
Writing a scanner plugin
This is the whole point of the package, so it comes first.
-
Add
scan-gatewayas a dependency (optional at runtime if your plugin only relies on structural typing, see below). -
Implement a class with:
name: str-- used in log lines and in exception messages so it's obvious which plugin did what.async def scan(self, path: Path) -> None-- returnNonewhen clean. Raisescan_gateway.ThreatDetectedError(self.name, verdict)when the file is flagged. Raisescan_gateway.ScannerUnavailableError(self.name, reason)when the scan itself couldn't be completed for operational reasons (connection refused, timeout, malformed response, etc) -- never for "clean", and never confuse the two:ScanGatewaytreatsScannerUnavailableErroras "try the next scanner", andThreatDetectedErroras "stop, this file is rejected".
Because
Scanneris atyping.Protocol, your class doesn't need to import or subclass anything fromscan_gatewayat all -- structural typing means "has a.nameand a matching.scan()" is enough. -
Decide how your plugin gets configured:
- Auto-discovered (zero code change on the consuming app's side):
your class needs a zero-argument constructor, reading whatever
config it needs from its own environment variables. Register it in
your own
pyproject.toml:[project.entry-points."scan_gateway.scanners"] my_scanner = "my_scanner_pkg.plugin:MyScanner"
Once both packages are installed,ScanGateway(discover_plugins=True)(the default) finds and instantiates it automatically. - Explicitly registered: if your plugin needs config only known at
runtime (hosts, API keys, secrets) that can't reasonably live in an
environment variable your class reads unprompted, document that
consuming apps should import and instantiate it themselves, then pass
the instance to
ScanGateway(scanners=[...]). This is exactly how the bundledClamAVScannerworks (see below) -- it is not auto-discovered, on purpose.
Nothing stops you from supporting both: a constructor with defaults that fall back to environment variables, auto-discovered for the common case, still explicitly constructible when a caller wants to pass config directly.
- Auto-discovered (zero code change on the consuming app's side):
your class needs a zero-argument constructor, reading whatever
config it needs from its own environment variables. Register it in
your own
-
pip installyour plugin package alongsidescan-gateway. That's it -- no code change required inscan-gatewayitself.
The "scan_gateway.scanners" entry-point group name is a stable public
contract. It will not change in a way that breaks existing plugins without
a major version bump.
What ships built-in, and why
| Scanner | Built-in? | Auto-discovered? |
|---|---|---|
scanners.clamav.ClamAVScanner |
Yes | No -- needs runtime host/port, same constraint any plugin needing real secrets has |
scanners.local_heuristic.LocalHeuristicScanner |
Yes, and not swappable via entry points | N/A -- it's the fallback ScanGateway always has, not a plugin slot |
ClamAV ships in the box because it's the obvious, free, well-known
default -- but it's wired the same way any third-party plugin needing
secrets would be: explicit construction with its own host/port/timeout,
passed into ScanGateway(scanners=[...]). It deliberately does not
register itself via the entry-point mechanism, since that mechanism exists
to solve "code scan-gateway doesn't know about at import time" -- and
scan-gateway does know about its own bundled ClamAVScanner.
The local heuristic fallback is intentionally not pluggable. It's the
one check every caller is guaranteed to get when nothing else could run --
letting it be swapped via a pip install would mean one bad or malicious
plugin package could silently disable the only guaranteed scan for anyone
who installs it. If you genuinely need to replace it (e.g. in tests), use
the explicit, auditable ScanGateway(fallback=my_scanner) constructor
override -- a code-level decision, not something a package index can
change out from under a deployment.
Exceptions
| Exception | Meaning | Who raises it |
|---|---|---|
ScanGatewayError |
Base class for everything this package raises | -- |
ThreatDetectedError |
A scanner positively flagged the file. Carries .scanner_name and .verdict |
Any Scanner |
ScannerUnavailableError |
A scanner couldn't complete the scan for operational reasons. Carries .scanner_name and .reason |
Any Scanner; caught internally by ScanGateway, not expected to reach your code directly |
Contributing
See CONTRIBUTING.md for dev setup, commit conventions (required for automated releases), and what belongs in core vs. a scanner module.
Development
python3.11 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pip install -e tests/fixtures/plugins # enables the real-plugin discovery test
ruff check .
pytest
The one integration test that needs a real clamd instance
(tests/integration/test_clamav_live.py) is skipped unless
SCAN_GATEWAY_TEST_CLAMD_HOST is set.
Releasing
Releases are fully automated from Conventional Commits
on main:
- release-please watches
every push to
main, computes the next version from commit prefixes (feat:→ minor,fix:→ patch,BREAKING CHANGE:/!→ major), and keeps a standing "release PR" with the version bump and generatedCHANGELOG.mdup to date. - Merging that release PR creates a GitHub Release + tag, which sets
release-please's ownrelease_createdoutput. - A second job in the same
.github/workflows/release-please.yml, gated on that output, builds the sdist/wheel and publishes to PyPI via Trusted Publishing (OIDC) -- no stored API token. (Publishing has to live in the same workflow as release-please itself, not a separate workflow triggered byon: release:-- a Release created via the defaultGITHUB_TOKENdoesn't fire other workflows, by GitHub's own anti-recursion design.)
This means every commit to main should use a Conventional Commits prefix
for version bumps to compute correctly.
Metadata
Release files for scan-gateway 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scan_gateway-0.1.1.tar.gz | 1.7 MB | With provenance | Details for scan_gateway-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scan_gateway-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for scan_gateway-0.1.1-py3-none-any.whl |
Total release size: 1.7 MB
Release files / scan_gateway-0.1.1.tar.gz
| Download URL | scan_gateway-0.1.1.tar.gz |
|---|---|
| Size | 1.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a8f2962403be03735809b3d933e330d76a2b857516e3ac981eed22646a2b7f3
|
|
BLAKE2b-256 checksum How to use checksums |
ba02eedc1f3ae321e10ab024fabf0fc6d9fe61c37a119ba565c6ab0e2e50b1c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 15, 2026.
Transparency logRelease files / scan_gateway-0.1.1-py3-none-any.whl
| Download URL | scan_gateway-0.1.1-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf6f3736c3aa302e7c9d02f7c5e9d19431264e192a2d168a68f2c2f7b13ff5e3
|
|
BLAKE2b-256 checksum How to use checksums |
04e3ace4858b6adb51e82c400517a0f1bffe9c05f94c7a49b75b9ac08c771993
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 15, 2026.
Transparency logSecurity for scan-gateway 0.1.1
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
Both files in this release have been uploaded with provenance metadata.
Attestation for scan_gateway-0.1.1.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 Nov 15, 2026.
Transparency logA 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.