scan-gateway 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scan_gateway-0.1.1/.github/workflows/release-please.yml +61 -0
- scan_gateway-0.1.1/.gitignore +11 -0
- scan_gateway-0.1.1/CHANGELOG.md +34 -0
- scan_gateway-0.1.1/CONTRIBUTING.md +115 -0
- scan_gateway-0.1.1/LICENSE +21 -0
- scan_gateway-0.1.1/PKG-INFO +176 -0
- scan_gateway-0.1.1/README.md +158 -0
- scan_gateway-0.1.1/logo.png +0 -0
- scan_gateway-0.1.1/pyproject.toml +36 -0
- scan_gateway-0.1.1/src/scan_gateway/__init__.py +11 -0
- scan_gateway-0.1.1/src/scan_gateway/discovery.py +40 -0
- scan_gateway-0.1.1/src/scan_gateway/exceptions.py +58 -0
- scan_gateway-0.1.1/src/scan_gateway/gateway.py +78 -0
- scan_gateway-0.1.1/src/scan_gateway/protocol.py +30 -0
- scan_gateway-0.1.1/src/scan_gateway/scanners/__init__.py +0 -0
- scan_gateway-0.1.1/src/scan_gateway/scanners/clamav.py +99 -0
- scan_gateway-0.1.1/src/scan_gateway/scanners/local_heuristic.py +59 -0
- scan_gateway-0.1.1/src/scan_gateway/scanners/rules/suspicious_pdf.yar +30 -0
- scan_gateway-0.1.1/tests/__init__.py +0 -0
- scan_gateway-0.1.1/tests/conftest.py +8 -0
- scan_gateway-0.1.1/tests/fixtures/pdfs/clean.pdf +13 -0
- scan_gateway-0.1.1/tests/fixtures/pdfs/eicar.pdf +9 -0
- scan_gateway-0.1.1/tests/fixtures/pdfs/js_openaction.pdf +10 -0
- scan_gateway-0.1.1/tests/fixtures/pdfs/launch_action.pdf +10 -0
- scan_gateway-0.1.1/tests/fixtures/plugins/fake_scanner_plugin/__init__.py +12 -0
- scan_gateway-0.1.1/tests/fixtures/plugins/pyproject.toml +15 -0
- scan_gateway-0.1.1/tests/integration/__init__.py +0 -0
- scan_gateway-0.1.1/tests/integration/test_clamav_live.py +42 -0
- scan_gateway-0.1.1/tests/unit/__init__.py +0 -0
- scan_gateway-0.1.1/tests/unit/test_clamav_scanner.py +106 -0
- scan_gateway-0.1.1/tests/unit/test_discovery.py +73 -0
- scan_gateway-0.1.1/tests/unit/test_gateway.py +119 -0
- scan_gateway-0.1.1/tests/unit/test_local_heuristic_scanner.py +37 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
name: release-please
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: write
|
|
10
|
+
pull-requests: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
release-please:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
outputs:
|
|
16
|
+
release_created: ${{ steps.release.outputs.release_created }}
|
|
17
|
+
steps:
|
|
18
|
+
- uses: googleapis/release-please-action@v4
|
|
19
|
+
id: release
|
|
20
|
+
with:
|
|
21
|
+
release-type: python
|
|
22
|
+
|
|
23
|
+
# A Release created by release-please-action uses the default
|
|
24
|
+
# GITHUB_TOKEN, and GitHub deliberately doesn't let a GITHUB_TOKEN-
|
|
25
|
+
# triggered event fire *other* workflows (anti-recursion protection) --
|
|
26
|
+
# so `on: release: types: [published]` in a separate workflow never
|
|
27
|
+
# fires. Publishing as a second job in this same workflow, gated on
|
|
28
|
+
# release-please's own output, is the officially recommended pattern
|
|
29
|
+
# for exactly this case.
|
|
30
|
+
publish:
|
|
31
|
+
needs: release-please
|
|
32
|
+
if: ${{ needs.release-please.outputs.release_created == 'true' }}
|
|
33
|
+
runs-on: ubuntu-latest
|
|
34
|
+
environment: pypi
|
|
35
|
+
permissions:
|
|
36
|
+
id-token: write
|
|
37
|
+
steps:
|
|
38
|
+
- name: Checkout code
|
|
39
|
+
uses: actions/checkout@v4
|
|
40
|
+
|
|
41
|
+
- name: Set up Python 3.13
|
|
42
|
+
uses: actions/setup-python@v5
|
|
43
|
+
with:
|
|
44
|
+
python-version: '3.13'
|
|
45
|
+
|
|
46
|
+
- name: Install dependencies
|
|
47
|
+
run: |
|
|
48
|
+
python -m pip install --upgrade pip
|
|
49
|
+
pip install build
|
|
50
|
+
|
|
51
|
+
- name: Build a binary wheel and a source tarball
|
|
52
|
+
run: >-
|
|
53
|
+
python3 -m
|
|
54
|
+
build
|
|
55
|
+
--sdist
|
|
56
|
+
--wheel
|
|
57
|
+
--outdir dist/
|
|
58
|
+
.
|
|
59
|
+
|
|
60
|
+
- name: Publish distribution 📦 to PyPI
|
|
61
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.1](https://github.com/sanfx/scan-gateway/compare/v0.1.0...v0.1.1) (2026-11-15)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **ci:** run PyPI publish in the same workflow as release-please ([20970be](https://github.com/sanfx/scan-gateway/commit/20970bee348cb00cc96cf22335696212365733a6))
|
|
9
|
+
* **ci:** run PyPI publish in the same workflow as release-please ([c8f8d61](https://github.com/sanfx/scan-gateway/commit/c8f8d614fbd8671023df95966e3e1cedfa7757d0))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Documentation
|
|
13
|
+
|
|
14
|
+
* add Returns/Args/Raises sections to every docstring ([5f6174b](https://github.com/sanfx/scan-gateway/commit/5f6174bb80fa999921d0925709f52db382cdf0b0))
|
|
15
|
+
* add Returns/Args/Raises sections to every docstring ([29c79d4](https://github.com/sanfx/scan-gateway/commit/29c79d4d93581b681079b03e38d0b0ba6bc7c8d7))
|
|
16
|
+
|
|
17
|
+
## 0.1.0 (2026-11-15)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* initial scan-gateway package ([85b0264](https://github.com/sanfx/scan-gateway/commit/85b02648570b325f95418a8af900be5c210d4327))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Documentation
|
|
26
|
+
|
|
27
|
+
* add logo to README, add CONTRIBUTING.md ([b3da727](https://github.com/sanfx/scan-gateway/commit/b3da727458d0ade38bf22520ffcf90af2f9c57c0))
|
|
28
|
+
* call out ruff + Conventional Commits requirement explicitly ([7c07ea2](https://github.com/sanfx/scan-gateway/commit/7c07ea22276b41ff6f528cf9ca12c515522a1768))
|
|
29
|
+
|
|
30
|
+
## Changelog
|
|
31
|
+
|
|
32
|
+
All notable changes to this project will be documented in this file.
|
|
33
|
+
|
|
34
|
+
This file is maintained automatically by [release-please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org/).
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Contributing to scan-gateway
|
|
2
|
+
|
|
3
|
+
Thanks for considering a contribution. This file covers *working on
|
|
4
|
+
scan-gateway itself* (the core package: `ScanGateway`, the built-in
|
|
5
|
+
scanners, discovery). If you just want to **write a scanner plugin**, you
|
|
6
|
+
don't need any of this — see the [README's "Writing a scanner
|
|
7
|
+
plugin"](README.md#writing-a-scanner-plugin) section instead; a plugin is
|
|
8
|
+
its own separate package and doesn't touch this repo at all.
|
|
9
|
+
|
|
10
|
+
## Requirements for every PR
|
|
11
|
+
|
|
12
|
+
- **Lint/format with [ruff](https://docs.astral.sh/ruff/)** -- `ruff
|
|
13
|
+
check .` and `ruff format .` must both pass with no changes left
|
|
14
|
+
uncommitted. This is the one linter/formatter for the whole repo; don't
|
|
15
|
+
introduce another.
|
|
16
|
+
- **Use [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)**
|
|
17
|
+
for every commit message (or at minimum the PR title, since PRs are
|
|
18
|
+
squash-merged) -- see "Commit messages" below for why this isn't just a
|
|
19
|
+
style preference here.
|
|
20
|
+
|
|
21
|
+
## Dev setup
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
git clone https://github.com/sanfx/scan-gateway.git
|
|
25
|
+
cd scan-gateway
|
|
26
|
+
python3.11 -m venv .venv
|
|
27
|
+
source .venv/bin/activate
|
|
28
|
+
pip install -e ".[dev]"
|
|
29
|
+
pip install -e tests/fixtures/plugins # enables the real-plugin discovery test
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Running checks
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
ruff check . # lint
|
|
36
|
+
ruff format . # auto-format (run before committing)
|
|
37
|
+
pytest # full suite -- the one real-clamd test auto-skips
|
|
38
|
+
# unless SCAN_GATEWAY_TEST_CLAMD_HOST is set
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
All three must pass before opening a PR. CI runs the same checks.
|
|
42
|
+
|
|
43
|
+
## Commit messages: Conventional Commits are required
|
|
44
|
+
|
|
45
|
+
Releases are fully automated by [release-please](https://github.com/googleapis/release-please),
|
|
46
|
+
which reads commit prefixes on `main` to decide the next version and
|
|
47
|
+
generate `CHANGELOG.md`. Every commit (or at minimum, every squashed PR
|
|
48
|
+
title) must follow the [Conventional Commits spec](https://www.conventionalcommits.org/en/v1.0.0/),
|
|
49
|
+
using one of:
|
|
50
|
+
|
|
51
|
+
| Prefix | Effect |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `fix: ...` | patch release (0.1.0 → 0.1.1) |
|
|
54
|
+
| `feat: ...` | minor release (0.1.0 → 0.2.0) |
|
|
55
|
+
| `feat!: ...` or a `BREAKING CHANGE:` footer | major release (0.1.0 → 1.0.0) |
|
|
56
|
+
| `chore:`, `docs:`, `test:`, `refactor:`, `ci:` | no release triggered on their own |
|
|
57
|
+
|
|
58
|
+
A PR with a non-conforming title/commits will still merge, but won't
|
|
59
|
+
produce the version bump you might expect -- match the prefix to what
|
|
60
|
+
actually changed.
|
|
61
|
+
|
|
62
|
+
## What belongs in core vs. a scanner module
|
|
63
|
+
|
|
64
|
+
- **`protocol.py`, `exceptions.py`, `gateway.py`, `discovery.py`** -- the
|
|
65
|
+
orchestration mechanism itself. Changes here affect every scanner,
|
|
66
|
+
built-in or third-party, so they need the strongest justification and
|
|
67
|
+
the most test coverage. In particular:
|
|
68
|
+
- The `"scan_gateway.scanners"` entry-point group name
|
|
69
|
+
(`discovery.py`) is a stable public contract -- don't rename it
|
|
70
|
+
without a major-version bump and a clear migration note.
|
|
71
|
+
- `LocalHeuristicScanner` being non-pluggable is a deliberate security
|
|
72
|
+
decision (see the README's "What ships built-in, and why" section),
|
|
73
|
+
not an oversight -- a PR trying to make it swappable via entry points
|
|
74
|
+
needs to address that reasoning directly, not route around it.
|
|
75
|
+
- **`scanners/clamav.py`** -- the one bundled real scanner. Bug fixes and
|
|
76
|
+
protocol-correctness improvements are welcome; it should stay a thin,
|
|
77
|
+
dependency-free (stdlib-only) INSTREAM client, not grow ClamAV features
|
|
78
|
+
unrelated to the scan/verdict path this package cares about.
|
|
79
|
+
- **`scanners/local_heuristic.py` + `scanners/rules/suspicious_pdf.yar`**
|
|
80
|
+
-- the built-in fallback. Because it's not swappable, a change here
|
|
81
|
+
affects every consumer that doesn't configure its own scanner. New
|
|
82
|
+
YARA rules or keyword heuristics should be conservative (prefer missing
|
|
83
|
+
a detection over false-flagging a legitimate file) -- this layer exists
|
|
84
|
+
specifically because it runs even when nothing else is configured, so a
|
|
85
|
+
false positive here has no second opinion to fall back on.
|
|
86
|
+
- **A genuinely new built-in scanner** (not ClamAV, not the heuristic
|
|
87
|
+
fallback) is a bigger discussion than a typical PR -- open an issue
|
|
88
|
+
first. The expectation is that most new scanners should be third-party
|
|
89
|
+
plugins, not additions to this repo.
|
|
90
|
+
|
|
91
|
+
## Tests
|
|
92
|
+
|
|
93
|
+
- Unit tests (`tests/unit/`) should never need a real network service.
|
|
94
|
+
`ClamAVScanner` is tested via fake `asyncio.open_connection` doubles
|
|
95
|
+
(see `test_clamav_scanner.py`); `discover_scanner_plugins()` is tested
|
|
96
|
+
both via a monkeypatched `entry_points()` and the real, installed
|
|
97
|
+
`tests/fixtures/plugins` package (proves genuine distribution-based
|
|
98
|
+
discovery, not just the mocked path).
|
|
99
|
+
- `tests/integration/test_clamav_live.py` is the one exception -- it
|
|
100
|
+
needs a real `clamd` and is skip-gated on `SCAN_GATEWAY_TEST_CLAMD_HOST`
|
|
101
|
+
being set. Don't make more tests depend on a real service without the
|
|
102
|
+
same explicit opt-in gating.
|
|
103
|
+
- New behavior needs a new test in the same file/style as its neighbors,
|
|
104
|
+
not a separate ad hoc script.
|
|
105
|
+
|
|
106
|
+
## Pull requests
|
|
107
|
+
|
|
108
|
+
1. One logical change per PR -- a scanner fix and a discovery-mechanism
|
|
109
|
+
change are two PRs, not one.
|
|
110
|
+
2. Update the README if you change anything a plugin author or a
|
|
111
|
+
consumer of `ScanGateway` would notice (new constructor arg, changed
|
|
112
|
+
exception behavior, etc).
|
|
113
|
+
3. CI (lint + tests) must pass before merge.
|
|
114
|
+
4. Use a Conventional Commits–formatted PR title -- that's what
|
|
115
|
+
release-please actually reads when the PR is squash-merged.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sanfx
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: scan-gateway
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Pluggable malware-scan orchestrator: tries configured scanner plugins, falls back to a built-in local heuristic check, decides accept/reject
|
|
5
|
+
Project-URL: Homepage, https://github.com/sanfx/scan-gateway
|
|
6
|
+
Project-URL: Repository, https://github.com/sanfx/scan-gateway
|
|
7
|
+
Author: sanfx
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Requires-Dist: yara-python>=4.5
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
14
|
+
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
|
|
15
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
16
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
<img src="logo.png" align="right" width="140" alt="scan-gateway logo" />
|
|
20
|
+
|
|
21
|
+
# scan-gateway
|
|
22
|
+
|
|
23
|
+
A pluggable malware-scan orchestrator for file uploads: tries configured
|
|
24
|
+
scanner plugins (in order), falls back to a built-in local heuristic check
|
|
25
|
+
when none can complete a scan, and raises a typed exception when something
|
|
26
|
+
is flagged. It doesn't scan anything itself -- it's a gateway in front of
|
|
27
|
+
whichever scanners you plug in.
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
from scan_gateway import ScanGateway, ThreatDetectedError
|
|
33
|
+
from scan_gateway.scanners.clamav import ClamAVScanner
|
|
34
|
+
|
|
35
|
+
gateway = ScanGateway(scanners=[ClamAVScanner(host="clamav.internal")])
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
await gateway.scan(Path("upload.pdf"))
|
|
39
|
+
except ThreatDetectedError as exc:
|
|
40
|
+
print(f"{exc.scanner_name} flagged this: {exc.verdict}")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If `ClamAVScanner` can't be reached, `ScanGateway` falls back to its
|
|
44
|
+
built-in `LocalHeuristicScanner` (YARA + a couple of PDF-structural
|
|
45
|
+
keyword checks) automatically -- the caller never needs to know which
|
|
46
|
+
layer actually scanned the file.
|
|
47
|
+
|
|
48
|
+
## Writing a scanner plugin
|
|
49
|
+
|
|
50
|
+
This is the whole point of the package, so it comes first.
|
|
51
|
+
|
|
52
|
+
1. Add `scan-gateway` as a dependency (optional at runtime if your plugin
|
|
53
|
+
only relies on structural typing, see below).
|
|
54
|
+
2. Implement a class with:
|
|
55
|
+
- `name: str` -- used in log lines and in exception messages so it's
|
|
56
|
+
obvious which plugin did what.
|
|
57
|
+
- `async def scan(self, path: Path) -> None` -- return `None` when
|
|
58
|
+
clean. Raise `scan_gateway.ThreatDetectedError(self.name, verdict)`
|
|
59
|
+
when the file is flagged. Raise
|
|
60
|
+
`scan_gateway.ScannerUnavailableError(self.name, reason)` when the
|
|
61
|
+
scan itself couldn't be completed for operational reasons
|
|
62
|
+
(connection refused, timeout, malformed response, etc) -- never for
|
|
63
|
+
"clean", and never confuse the two: `ScanGateway` treats
|
|
64
|
+
`ScannerUnavailableError` as "try the next scanner", and
|
|
65
|
+
`ThreatDetectedError` as "stop, this file is rejected".
|
|
66
|
+
|
|
67
|
+
Because `Scanner` is a `typing.Protocol`, your class doesn't need to
|
|
68
|
+
import or subclass anything from `scan_gateway` at all -- structural
|
|
69
|
+
typing means "has a `.name` and a matching `.scan()`" is enough.
|
|
70
|
+
|
|
71
|
+
3. Decide how your plugin gets configured:
|
|
72
|
+
- **Auto-discovered** (zero code change on the consuming app's side):
|
|
73
|
+
your class needs a zero-argument constructor, reading whatever
|
|
74
|
+
config it needs from its own environment variables. Register it in
|
|
75
|
+
*your own* `pyproject.toml`:
|
|
76
|
+
```toml
|
|
77
|
+
[project.entry-points."scan_gateway.scanners"]
|
|
78
|
+
my_scanner = "my_scanner_pkg.plugin:MyScanner"
|
|
79
|
+
```
|
|
80
|
+
Once both packages are installed, `ScanGateway(discover_plugins=True)`
|
|
81
|
+
(the default) finds and instantiates it automatically.
|
|
82
|
+
- **Explicitly registered**: if your plugin needs config only known at
|
|
83
|
+
runtime (hosts, API keys, secrets) that can't reasonably live in an
|
|
84
|
+
environment variable your class reads unprompted, document that
|
|
85
|
+
consuming apps should import and instantiate it themselves, then pass
|
|
86
|
+
the instance to `ScanGateway(scanners=[...])`. This is exactly how
|
|
87
|
+
the bundled `ClamAVScanner` works (see below) -- it is **not**
|
|
88
|
+
auto-discovered, on purpose.
|
|
89
|
+
|
|
90
|
+
Nothing stops you from supporting both: a constructor with defaults
|
|
91
|
+
that fall back to environment variables, auto-discovered for the common
|
|
92
|
+
case, still explicitly constructible when a caller wants to pass
|
|
93
|
+
config directly.
|
|
94
|
+
|
|
95
|
+
4. `pip install` your plugin package alongside `scan-gateway`. That's it --
|
|
96
|
+
no code change required in `scan-gateway` itself.
|
|
97
|
+
|
|
98
|
+
The `"scan_gateway.scanners"` entry-point group name is a stable public
|
|
99
|
+
contract. It will not change in a way that breaks existing plugins without
|
|
100
|
+
a major version bump.
|
|
101
|
+
|
|
102
|
+
## What ships built-in, and why
|
|
103
|
+
|
|
104
|
+
| Scanner | Built-in? | Auto-discovered? |
|
|
105
|
+
|---|---|---|
|
|
106
|
+
| `scanners.clamav.ClamAVScanner` | Yes | No -- needs runtime host/port, same constraint any plugin needing real secrets has |
|
|
107
|
+
| `scanners.local_heuristic.LocalHeuristicScanner` | Yes, and not swappable via entry points | N/A -- it's the fallback `ScanGateway` always has, not a plugin slot |
|
|
108
|
+
|
|
109
|
+
**ClamAV** ships in the box because it's the obvious, free, well-known
|
|
110
|
+
default -- but it's wired the same way any third-party plugin needing
|
|
111
|
+
secrets would be: explicit construction with its own host/port/timeout,
|
|
112
|
+
passed into `ScanGateway(scanners=[...])`. It deliberately does *not*
|
|
113
|
+
register itself via the entry-point mechanism, since that mechanism exists
|
|
114
|
+
to solve "code `scan-gateway` doesn't know about at import time" -- and
|
|
115
|
+
`scan-gateway` does know about its own bundled `ClamAVScanner`.
|
|
116
|
+
|
|
117
|
+
**The local heuristic fallback is intentionally not pluggable.** It's the
|
|
118
|
+
one check every caller is guaranteed to get when nothing else could run --
|
|
119
|
+
letting it be swapped via a `pip install` would mean one bad or malicious
|
|
120
|
+
plugin package could silently disable the only guaranteed scan for anyone
|
|
121
|
+
who installs it. If you genuinely need to replace it (e.g. in tests), use
|
|
122
|
+
the explicit, auditable `ScanGateway(fallback=my_scanner)` constructor
|
|
123
|
+
override -- a code-level decision, not something a package index can
|
|
124
|
+
change out from under a deployment.
|
|
125
|
+
|
|
126
|
+
## Exceptions
|
|
127
|
+
|
|
128
|
+
| Exception | Meaning | Who raises it |
|
|
129
|
+
|---|---|---|
|
|
130
|
+
| `ScanGatewayError` | Base class for everything this package raises | -- |
|
|
131
|
+
| `ThreatDetectedError` | A scanner positively flagged the file. Carries `.scanner_name` and `.verdict` | Any `Scanner` |
|
|
132
|
+
| `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 |
|
|
133
|
+
|
|
134
|
+
## Contributing
|
|
135
|
+
|
|
136
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for dev setup, commit conventions
|
|
137
|
+
(required for automated releases), and what belongs in core vs. a scanner
|
|
138
|
+
module.
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
python3.11 -m venv .venv
|
|
144
|
+
source .venv/bin/activate
|
|
145
|
+
pip install -e ".[dev]"
|
|
146
|
+
pip install -e tests/fixtures/plugins # enables the real-plugin discovery test
|
|
147
|
+
ruff check .
|
|
148
|
+
pytest
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The one integration test that needs a real `clamd` instance
|
|
152
|
+
(`tests/integration/test_clamav_live.py`) is skipped unless
|
|
153
|
+
`SCAN_GATEWAY_TEST_CLAMD_HOST` is set.
|
|
154
|
+
|
|
155
|
+
## Releasing
|
|
156
|
+
|
|
157
|
+
Releases are fully automated from [Conventional Commits](https://www.conventionalcommits.org/)
|
|
158
|
+
on `main`:
|
|
159
|
+
|
|
160
|
+
1. [release-please](https://github.com/googleapis/release-please) watches
|
|
161
|
+
every push to `main`, computes the next version from commit prefixes
|
|
162
|
+
(`feat:` → minor, `fix:` → patch, `BREAKING CHANGE:`/`!` → major), and
|
|
163
|
+
keeps a standing "release PR" with the version bump and generated
|
|
164
|
+
`CHANGELOG.md` up to date.
|
|
165
|
+
2. Merging that release PR creates a GitHub Release + tag, which sets
|
|
166
|
+
`release-please`'s own `release_created` output.
|
|
167
|
+
3. A second job in the same `.github/workflows/release-please.yml`,
|
|
168
|
+
gated on that output, builds the sdist/wheel and publishes to PyPI via
|
|
169
|
+
[Trusted Publishing](https://docs.pypi.org/trusted-publishers/) (OIDC)
|
|
170
|
+
-- no stored API token. (Publishing has to live in the *same* workflow
|
|
171
|
+
as release-please itself, not a separate workflow triggered by `on:
|
|
172
|
+
release:` -- a Release created via the default `GITHUB_TOKEN` doesn't
|
|
173
|
+
fire other workflows, by GitHub's own anti-recursion design.)
|
|
174
|
+
|
|
175
|
+
This means every commit to `main` should use a Conventional Commits prefix
|
|
176
|
+
for version bumps to compute correctly.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<img src="logo.png" align="right" width="140" alt="scan-gateway logo" />
|
|
2
|
+
|
|
3
|
+
# scan-gateway
|
|
4
|
+
|
|
5
|
+
A pluggable malware-scan orchestrator for file uploads: tries configured
|
|
6
|
+
scanner plugins (in order), falls back to a built-in local heuristic check
|
|
7
|
+
when none can complete a scan, and raises a typed exception when something
|
|
8
|
+
is flagged. It doesn't scan anything itself -- it's a gateway in front of
|
|
9
|
+
whichever scanners you plug in.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from scan_gateway import ScanGateway, ThreatDetectedError
|
|
15
|
+
from scan_gateway.scanners.clamav import ClamAVScanner
|
|
16
|
+
|
|
17
|
+
gateway = ScanGateway(scanners=[ClamAVScanner(host="clamav.internal")])
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
await gateway.scan(Path("upload.pdf"))
|
|
21
|
+
except ThreatDetectedError as exc:
|
|
22
|
+
print(f"{exc.scanner_name} flagged this: {exc.verdict}")
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
If `ClamAVScanner` can't be reached, `ScanGateway` falls back to its
|
|
26
|
+
built-in `LocalHeuristicScanner` (YARA + a couple of PDF-structural
|
|
27
|
+
keyword checks) automatically -- the caller never needs to know which
|
|
28
|
+
layer actually scanned the file.
|
|
29
|
+
|
|
30
|
+
## Writing a scanner plugin
|
|
31
|
+
|
|
32
|
+
This is the whole point of the package, so it comes first.
|
|
33
|
+
|
|
34
|
+
1. Add `scan-gateway` as a dependency (optional at runtime if your plugin
|
|
35
|
+
only relies on structural typing, see below).
|
|
36
|
+
2. Implement a class with:
|
|
37
|
+
- `name: str` -- used in log lines and in exception messages so it's
|
|
38
|
+
obvious which plugin did what.
|
|
39
|
+
- `async def scan(self, path: Path) -> None` -- return `None` when
|
|
40
|
+
clean. Raise `scan_gateway.ThreatDetectedError(self.name, verdict)`
|
|
41
|
+
when the file is flagged. Raise
|
|
42
|
+
`scan_gateway.ScannerUnavailableError(self.name, reason)` when the
|
|
43
|
+
scan itself couldn't be completed for operational reasons
|
|
44
|
+
(connection refused, timeout, malformed response, etc) -- never for
|
|
45
|
+
"clean", and never confuse the two: `ScanGateway` treats
|
|
46
|
+
`ScannerUnavailableError` as "try the next scanner", and
|
|
47
|
+
`ThreatDetectedError` as "stop, this file is rejected".
|
|
48
|
+
|
|
49
|
+
Because `Scanner` is a `typing.Protocol`, your class doesn't need to
|
|
50
|
+
import or subclass anything from `scan_gateway` at all -- structural
|
|
51
|
+
typing means "has a `.name` and a matching `.scan()`" is enough.
|
|
52
|
+
|
|
53
|
+
3. Decide how your plugin gets configured:
|
|
54
|
+
- **Auto-discovered** (zero code change on the consuming app's side):
|
|
55
|
+
your class needs a zero-argument constructor, reading whatever
|
|
56
|
+
config it needs from its own environment variables. Register it in
|
|
57
|
+
*your own* `pyproject.toml`:
|
|
58
|
+
```toml
|
|
59
|
+
[project.entry-points."scan_gateway.scanners"]
|
|
60
|
+
my_scanner = "my_scanner_pkg.plugin:MyScanner"
|
|
61
|
+
```
|
|
62
|
+
Once both packages are installed, `ScanGateway(discover_plugins=True)`
|
|
63
|
+
(the default) finds and instantiates it automatically.
|
|
64
|
+
- **Explicitly registered**: if your plugin needs config only known at
|
|
65
|
+
runtime (hosts, API keys, secrets) that can't reasonably live in an
|
|
66
|
+
environment variable your class reads unprompted, document that
|
|
67
|
+
consuming apps should import and instantiate it themselves, then pass
|
|
68
|
+
the instance to `ScanGateway(scanners=[...])`. This is exactly how
|
|
69
|
+
the bundled `ClamAVScanner` works (see below) -- it is **not**
|
|
70
|
+
auto-discovered, on purpose.
|
|
71
|
+
|
|
72
|
+
Nothing stops you from supporting both: a constructor with defaults
|
|
73
|
+
that fall back to environment variables, auto-discovered for the common
|
|
74
|
+
case, still explicitly constructible when a caller wants to pass
|
|
75
|
+
config directly.
|
|
76
|
+
|
|
77
|
+
4. `pip install` your plugin package alongside `scan-gateway`. That's it --
|
|
78
|
+
no code change required in `scan-gateway` itself.
|
|
79
|
+
|
|
80
|
+
The `"scan_gateway.scanners"` entry-point group name is a stable public
|
|
81
|
+
contract. It will not change in a way that breaks existing plugins without
|
|
82
|
+
a major version bump.
|
|
83
|
+
|
|
84
|
+
## What ships built-in, and why
|
|
85
|
+
|
|
86
|
+
| Scanner | Built-in? | Auto-discovered? |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `scanners.clamav.ClamAVScanner` | Yes | No -- needs runtime host/port, same constraint any plugin needing real secrets has |
|
|
89
|
+
| `scanners.local_heuristic.LocalHeuristicScanner` | Yes, and not swappable via entry points | N/A -- it's the fallback `ScanGateway` always has, not a plugin slot |
|
|
90
|
+
|
|
91
|
+
**ClamAV** ships in the box because it's the obvious, free, well-known
|
|
92
|
+
default -- but it's wired the same way any third-party plugin needing
|
|
93
|
+
secrets would be: explicit construction with its own host/port/timeout,
|
|
94
|
+
passed into `ScanGateway(scanners=[...])`. It deliberately does *not*
|
|
95
|
+
register itself via the entry-point mechanism, since that mechanism exists
|
|
96
|
+
to solve "code `scan-gateway` doesn't know about at import time" -- and
|
|
97
|
+
`scan-gateway` does know about its own bundled `ClamAVScanner`.
|
|
98
|
+
|
|
99
|
+
**The local heuristic fallback is intentionally not pluggable.** It's the
|
|
100
|
+
one check every caller is guaranteed to get when nothing else could run --
|
|
101
|
+
letting it be swapped via a `pip install` would mean one bad or malicious
|
|
102
|
+
plugin package could silently disable the only guaranteed scan for anyone
|
|
103
|
+
who installs it. If you genuinely need to replace it (e.g. in tests), use
|
|
104
|
+
the explicit, auditable `ScanGateway(fallback=my_scanner)` constructor
|
|
105
|
+
override -- a code-level decision, not something a package index can
|
|
106
|
+
change out from under a deployment.
|
|
107
|
+
|
|
108
|
+
## Exceptions
|
|
109
|
+
|
|
110
|
+
| Exception | Meaning | Who raises it |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `ScanGatewayError` | Base class for everything this package raises | -- |
|
|
113
|
+
| `ThreatDetectedError` | A scanner positively flagged the file. Carries `.scanner_name` and `.verdict` | Any `Scanner` |
|
|
114
|
+
| `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 |
|
|
115
|
+
|
|
116
|
+
## Contributing
|
|
117
|
+
|
|
118
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for dev setup, commit conventions
|
|
119
|
+
(required for automated releases), and what belongs in core vs. a scanner
|
|
120
|
+
module.
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python3.11 -m venv .venv
|
|
126
|
+
source .venv/bin/activate
|
|
127
|
+
pip install -e ".[dev]"
|
|
128
|
+
pip install -e tests/fixtures/plugins # enables the real-plugin discovery test
|
|
129
|
+
ruff check .
|
|
130
|
+
pytest
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The one integration test that needs a real `clamd` instance
|
|
134
|
+
(`tests/integration/test_clamav_live.py`) is skipped unless
|
|
135
|
+
`SCAN_GATEWAY_TEST_CLAMD_HOST` is set.
|
|
136
|
+
|
|
137
|
+
## Releasing
|
|
138
|
+
|
|
139
|
+
Releases are fully automated from [Conventional Commits](https://www.conventionalcommits.org/)
|
|
140
|
+
on `main`:
|
|
141
|
+
|
|
142
|
+
1. [release-please](https://github.com/googleapis/release-please) watches
|
|
143
|
+
every push to `main`, computes the next version from commit prefixes
|
|
144
|
+
(`feat:` → minor, `fix:` → patch, `BREAKING CHANGE:`/`!` → major), and
|
|
145
|
+
keeps a standing "release PR" with the version bump and generated
|
|
146
|
+
`CHANGELOG.md` up to date.
|
|
147
|
+
2. Merging that release PR creates a GitHub Release + tag, which sets
|
|
148
|
+
`release-please`'s own `release_created` output.
|
|
149
|
+
3. A second job in the same `.github/workflows/release-please.yml`,
|
|
150
|
+
gated on that output, builds the sdist/wheel and publishes to PyPI via
|
|
151
|
+
[Trusted Publishing](https://docs.pypi.org/trusted-publishers/) (OIDC)
|
|
152
|
+
-- no stored API token. (Publishing has to live in the *same* workflow
|
|
153
|
+
as release-please itself, not a separate workflow triggered by `on:
|
|
154
|
+
release:` -- a Release created via the default `GITHUB_TOKEN` doesn't
|
|
155
|
+
fire other workflows, by GitHub's own anti-recursion design.)
|
|
156
|
+
|
|
157
|
+
This means every commit to `main` should use a Conventional Commits prefix
|
|
158
|
+
for version bumps to compute correctly.
|
|
Binary file
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "scan-gateway"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "Pluggable malware-scan orchestrator: tries configured scanner plugins, falls back to a built-in local heuristic check, decides accept/reject"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = { text = "MIT" }
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
authors = [{ name = "sanfx" }]
|
|
9
|
+
dependencies = [
|
|
10
|
+
"yara-python>=4.5",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[project.urls]
|
|
14
|
+
Homepage = "https://github.com/sanfx/scan-gateway"
|
|
15
|
+
Repository = "https://github.com/sanfx/scan-gateway"
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = [
|
|
19
|
+
"pytest>=8.0",
|
|
20
|
+
"pytest-asyncio>=0.23",
|
|
21
|
+
"pytest-cov>=5.0",
|
|
22
|
+
"ruff>=0.4",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["hatchling"]
|
|
27
|
+
build-backend = "hatchling.build"
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
asyncio_mode = "auto"
|
|
31
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
32
|
+
testpaths = ["tests"]
|
|
33
|
+
|
|
34
|
+
[tool.ruff]
|
|
35
|
+
line-length = 100
|
|
36
|
+
target-version = "py311"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from scan_gateway.exceptions import ScanGatewayError, ScannerUnavailableError, ThreatDetectedError
|
|
2
|
+
from scan_gateway.gateway import ScanGateway
|
|
3
|
+
from scan_gateway.protocol import Scanner
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"ScanGateway",
|
|
7
|
+
"ScanGatewayError",
|
|
8
|
+
"Scanner",
|
|
9
|
+
"ScannerUnavailableError",
|
|
10
|
+
"ThreatDetectedError",
|
|
11
|
+
]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""importlib.metadata-based discovery for third-party scan-gateway plugins."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from importlib.metadata import entry_points
|
|
5
|
+
|
|
6
|
+
from scan_gateway.protocol import Scanner
|
|
7
|
+
|
|
8
|
+
ENTRY_POINT_GROUP = "scan_gateway.scanners"
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def discover_scanner_plugins() -> list[Scanner]:
|
|
14
|
+
"""Load every Scanner registered under the "scan_gateway.scanners" entry-point group.
|
|
15
|
+
|
|
16
|
+
Each entry point must resolve to a zero-argument callable (a class with
|
|
17
|
+
no required constructor args, or a factory function) returning a
|
|
18
|
+
Scanner instance -- any config it needs (host, API key, etc) is the
|
|
19
|
+
plugin's own responsibility to read, e.g. from its own environment
|
|
20
|
+
variables.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
list[Scanner]: One instance per successfully-loaded entry point, in
|
|
24
|
+
whatever order importlib.metadata returns them (not guaranteed
|
|
25
|
+
stable). An entry point that fails to import or instantiate is
|
|
26
|
+
logged and skipped, not fatal -- one broken third-party plugin
|
|
27
|
+
shouldn't take down every scan.
|
|
28
|
+
"""
|
|
29
|
+
discovered: list[Scanner] = []
|
|
30
|
+
for ep in entry_points(group=ENTRY_POINT_GROUP):
|
|
31
|
+
try:
|
|
32
|
+
factory = ep.load()
|
|
33
|
+
scanner = factory()
|
|
34
|
+
except Exception:
|
|
35
|
+
logger.exception(
|
|
36
|
+
"scan-gateway plugin %r (%s) failed to load, skipping", ep.name, ep.value
|
|
37
|
+
)
|
|
38
|
+
continue
|
|
39
|
+
discovered.append(scanner)
|
|
40
|
+
return discovered
|