Registry / auth-security / libsast

libsast

JSON →
library3.1.6pypypi✓ verified 83d ago

libsast is a Python library providing generic Static Application Security Testing (SAST) capabilities, built upon Semgrep and regex patterns. It allows users to define custom rules and scan codebases for security vulnerabilities. The library is actively maintained with frequent patch and minor releases, with the current version being 3.1.6.

pip install libsast
INSTALL
IMPORT
SIG · LIBSAST
L
libsast
auth-securitypythonv3.1.6
Install
2.4s avg
Import
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
installs and imports cleanly · install 0.0s · import 0.000s · 24.3MB
glibc
py 3.103.940 runs
installs and imports cleanly · install 2.4s · import 0.000s · 25MB
23MB installed
● package 23MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Scanner
from libsast import Scanner
from libsast.core.scan import Scan
PatternMatcher
from libsast import PatternMatcher
SemanticGrep
from libsast import SemanticGrep

This quickstart demonstrates how to define a custom regex rule and use `libsast` to scan a temporary file. It initializes `Scan` with a target directory and a list of rules, then runs the scan and prints any found vulnerabilities. It also highlights the option to switch multiprocessing executors for compatibility.

import os import tempfile import shutil from libsast.core.scan import Scan from libsast.core.rule import Rule # Create a dummy directory and file for scanning temp_dir = tempfile.mkdtemp() temp_file_path = os.path.join(temp_dir, "test_code.py") try: with open(temp_file_path, "w") as f: f.write("password = 'mysecretpassword'\n") f.write("API_KEY = 'YOUR_API_KEY_HERE'\n") f.write("def my_func():\n print('Hello')\n") # Define a simple regex rule my_rule = Rule( rule_id="HARDCODED_SECRET", description="Detects hardcoded sensitive keywords like 'password' or 'API_KEY'", severity="high", patterns=[ {"regex": r"(password\s*=|API_KEY\s*=)", "confidence": "high", "message": "Hardcoded secret found."} ], metadata={ "cwe": "CWE-798", "owasp": "A07:2021-Identification and Authentication Failures" } ) # Initialize the scanner # In environments like AWS Lambda or Celery, consider `multiprocessing_executor="thread"` or "billiard" scanner = Scan( target=temp_dir, # Scan the entire directory rules=[my_rule], enable_default_rules=False, # Set to True to include libsast's built-in rules multiprocessing_executor="process" # Options: "process", "thread", "billiard" ) # Run the scan results = scanner.run() # Process results if results: print(f"Found {len(results)} vulnerabilities:") for result in results: print(f"- Rule ID: {result.rule_id}") print(f" File: {result.file_path}") print(f" Line: {result.line_number}") print(f" Match: '{result.match_string}'") if result.message: print(f" Message: {result.message}") else: print("No vulnerabilities found.") finally: # Clean up the temporary directory shutil.rmtree(temp_dir)
Debug
Known issues
gotchaMultiprocessing (ProcessPoolExecutor) can cause issues in serverless environments (e.g., AWS Lambda) or task queues (e.g., Celery, Django-Q).
fix
Initialize `Scan` with `multiprocessing_executor='thread'` or `multiprocessing_executor='billiard'` to use ThreadPoolExecutor or the billiard library, respectively. Example: `Scan(..., multiprocessing_executor='thread')`.
affects: >=3.1.2
gotchaThe `semgrep` dependency is now optional. If you use Semgrep-based rules without installing `semgrep`, you will encounter runtime errors.
fix
Ensure `semgrep` is installed if you intend to use Semgrep rules. Install with `pip install libsast[semgrep]` or `pip install semgrep` separately.
affects: >=3.1.4
breakingInternal APIs for `PatternMatcher` and `ChoiceMatcher` were updated.
fix
If you developed custom integrations or rules relying directly on `PatternMatcher` or `ChoiceMatcher` internals, you may need to review and update your code. Standard rule definitions via `libsast.core.rule.Rule` should remain compatible.
affects: 3.1.3
gotchalibsast requires Python 3.8 or newer, but is not compatible with Python 4.0 or higher.
fix
Ensure your project environment uses a Python version within the supported range (e.g., Python 3.8, 3.9, 3.10, 3.11, 3.12).
affects: <4.0, >=3.8
Upgrade
Version history
3.1.6latest on PyPI · released Nov 14, 2024
Audit
Dependencies
semgrepoptionalRequired for Semgrep-based rules. Optional if only using regex patterns.
Agent activity
19 hits · last 30 days
node
18
OpenAI (training)
1
Resources
libsast — pip install libsast · libregistry