Repository navigation
feat(sensor): Python runtime probe and secfoo instrument (#55) - #99
Merged
Merged
Conversation
In-process probe that records which watched vulnerable functions run, as #53 events: sys.monitoring PY_START on watched code objects (3.12+) or a setprofile fallback (3.10/3.11), import-time symbol resolution, app-code coverage, load inventory, per-process sessions for forked and spawned workers, and file/unix/tcp/npipe/https transports. Inert unless SECFOO_SENSOR=1, never reads locals or arguments, and fails open. secfoo instrument wraps any command via a temporary sitecustomize. Includes an overhead benchmark. Closes #55 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| import subprocess # nosec B404 -- runs the user's own command, as given | ||
| import tempfile | ||
|
|
||
| import secfoo |
| if conn is not None: | ||
| try: | ||
| conn.close() | ||
| except OSError: |
|
|
||
| import pytest | ||
|
|
||
| import secfoo |
Leaderboard update
karthickmk2001 now has 51 points (rank #1, Silver). 49 more to reach Gold. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #55
What does this change, and why?
The in-process Python probe (
secfoo.sensor). It records which watched vulnerable functions actually run in a live app and writes #53 events. Docs are indocs/REFERENCE.md→ Python probe.Approach (as the issue describes, in
src/secfoo/sensor/probe.py):sys.monitoringwith its own tool id.PY_STARTis set locally on the watched functions' code objects, so unwatched code costs nothing.PY_STARTthat returnsDISABLEafter each function's first event, forapp_functions_executed.sys.setprofile+threading.setprofilefallback filtered to watched code (forced withSECFOO_SENSOR_BACKEND=profile). CI exercises it on py3.10.register_at_fork. Spawned ormultiprocessingworkers start their own probe throughsitecustomize, and flush throughmultiprocessing.util.Finalize, because workers leave viaos._exitand skipatexit.{pid}inSECFOO_EVENTS_OUTgives one file per process. Otherwise each flush is a singleO_APPENDwrite, so processes can share a file.SECFOO_FLUSH_INTERVAL(default 10 s) and at exit. That's safe to resend because Runtime evidence format, storage andsecfoo runtime ingest#53 replaces totals.loadlines for imported distributions.secfoo instrumentinjects a temporarysitecustomize.SECFOO_SENSOR_ADDRsupportsfile://,unix://,tcp://(loopback only),npipe://andhttps://direct mode (the Runtime ingest API: sensors use your Secfoo API key #66 API, with a 200 ms cap). Stream transports send the Probe ↔ sensor protocol and conformance kit (one contract for every runtime) #73hellofirst.route: unknown(shown as Executed).Must not (all tested):
f_locals, arguments or return values. A test asserts that the argumentpassword=hunter2never appears in the events.SECFOO_SENSOR_ADDR, and withoutSECFOO_SENSOR=1it installs nothing.Overhead benchmark (
python benchmarks/sensor_overhead.py --requests 200000, CPython 3.14, Windows, a ~7 µs request-like handler; each mode in a fresh interpreter):sys.monitoring: ~0 cost for unwatched code. About 1.4 µs per call of a watched function, even when it's called on every request. That's well inside Tracking: Runtime reachability for SCA (in-app probe + live sensor) #51's 1 ms p99 budget.setprofilefallback: about 10× slower, as the issue anticipates, because it fires on every call. The docs say to prefer 3.12+ in production.The #73 conformance kit (#73 says this probe is its reference) comes in the next PR, which builds on these transports.
Testing
pytest testspasses. 1204 passed locally. The only failures are the 4 existing Windows-only POSIX permission and symlink tests that fail onmaintoo; Linux CI is unaffected.ruff check .adds no new findings.tests/test_sensor/test_python_probe.py(21 tests, real subprocesses, both backends):secfoo runtime ingest#53SECFOO_SENSOR{pid}fileshello, and loopback being enforced🤖 Generated with Claude Code