mirror of
https://github.com/msoedov/agentic_security.git
synced 2026-11-25 19:43:29 +01:00
Merge pull request #352 from msoedov/chore/triage-fixes
Triage fixes and switch to Python 3.15
This commit is contained in:
23 files changed
+3751
-3147
No files matched your search
@@ -1,45 +0,0 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
|
||||
# Distribution / packaging
|
||||
build/
|
||||
dist/
|
||||
*.egg-info/
|
||||
|
||||
# Virtual environments
|
||||
|
||||
.venv/
|
||||
env/
|
||||
ENV/
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.coverage
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
|
||||
# PyInstaller
|
||||
*.spec
|
||||
|
||||
# macOS specific files
|
||||
.DS_Store
|
||||
|
||||
# Windows specific files
|
||||
Thumbs.db
|
||||
desktop.ini
|
||||
|
||||
# Tools and editors
|
||||
.idea/
|
||||
.vscode/
|
||||
cmder/
|
||||
|
||||
# Output directories
|
||||
Output/
|
||||
te/
|
||||
@@ -1,5 +1,6 @@
|
||||
[flake8]
|
||||
max-line-length = 160
|
||||
extend-ignore = E731
|
||||
per-file-ignores =
|
||||
# Ignore docstring lints for tests
|
||||
*: D100, D101, D102, D103, D104, D107, D105, D202, D205, D400, E501, D401, D200
|
||||
@@ -1,23 +0,0 @@
|
||||
name: Docker Build Test
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 0.*
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Build Docker image
|
||||
uses: docker/build-push-action@v4
|
||||
with:
|
||||
push: false
|
||||
tags: docker-build-test:latest
|
||||
@@ -15,7 +15,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.14'
|
||||
python-version: '3.15'
|
||||
- run: pip install '.[harness]'
|
||||
- name: Run policy checks
|
||||
env:
|
||||
|
||||
@@ -21,7 +21,7 @@ jobs:
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.14'
|
||||
python-version: '3.15'
|
||||
cache: "poetry"
|
||||
- name: Install pre-commit
|
||||
run: poetry install
|
||||
|
||||
@@ -20,10 +20,10 @@ jobs:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install poetry
|
||||
run: pipx install poetry==$POETRY_VERSION
|
||||
- name: Set up Python 3.14
|
||||
- name: Set up Python 3.15
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: "3.14"
|
||||
python-version: "3.15"
|
||||
- name: Build project for distribution
|
||||
run: poetry build --format sdist
|
||||
- name: Check Version
|
||||
|
||||
@@ -17,7 +17,7 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
python-version:
|
||||
- "3.14"
|
||||
- "3.15"
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install poetry
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
default_language_version:
|
||||
python: python3.14
|
||||
python: python3.15
|
||||
|
||||
repos:
|
||||
- repo: https://github.com/asottile/pyupgrade
|
||||
@@ -12,13 +12,13 @@ repos:
|
||||
rev: 26.3.1
|
||||
hooks:
|
||||
- id: black
|
||||
language_version: python3.14
|
||||
language_version: python3.15
|
||||
|
||||
- repo: https://github.com/pycqa/flake8
|
||||
rev: 7.3.0
|
||||
hooks:
|
||||
- id: flake8
|
||||
language_version: python3.14
|
||||
language_version: python3.15
|
||||
additional_dependencies: [flake8-docstrings]
|
||||
exclude: '^(tests)/'
|
||||
|
||||
@@ -57,14 +57,14 @@ repos:
|
||||
# - id: mdformat
|
||||
# name: mdformat
|
||||
# entry: mdformat .
|
||||
# language_version: python3.14
|
||||
# language_version: python3.15
|
||||
# files: "docs/.*\\.md$"
|
||||
|
||||
- repo: https://github.com/hadialqattan/pycln
|
||||
rev: v2.6.0
|
||||
hooks:
|
||||
- id: pycln
|
||||
language_version: python3.14
|
||||
language_version: python3.15
|
||||
|
||||
- repo: https://github.com/python-poetry/poetry
|
||||
rev: '2.4.1'
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
# Build stage
|
||||
FROM python:3.14-slim AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install system dependencies
|
||||
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Install Poetry
|
||||
RUN curl -sSL https://install.python-poetry.org | python3 -
|
||||
ENV PATH="/root/.local/bin:$PATH"
|
||||
RUN poetry self add "poetry-plugin-export"
|
||||
|
||||
# Copy only dependency files to leverage Docker layer caching
|
||||
COPY pyproject.toml poetry.lock ./
|
||||
|
||||
# update lock file to avoid failure
|
||||
RUN poetry lock
|
||||
|
||||
# Install dependencies
|
||||
RUN poetry export -f requirements.txt --without-hashes -o requirements.txt
|
||||
|
||||
# Install wheel (required to build packages like fire)
|
||||
RUN pip install --upgrade pip setuptools wheel
|
||||
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Runtime stage
|
||||
FROM python:3.14-slim
|
||||
|
||||
# Set environment variables
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy only the necessary files from the builder stage
|
||||
COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages
|
||||
COPY --from=builder /usr/local/bin /usr/local/bin
|
||||
|
||||
# Copy application code
|
||||
COPY . .
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost:8718/health || exit 1
|
||||
|
||||
# Default command
|
||||
CMD ["python", "-m", "agentic_security"]
|
||||
@@ -197,7 +197,7 @@ REGISTRY_V0 = [
|
||||
"dataset_name": "dayone3nder/jailbreak_prompt_JBB_sft_trainset",
|
||||
"num_prompts": 4785,
|
||||
"tokens": None, # Add actual token count if available
|
||||
"is_active": True,
|
||||
"is_active": False, # 401 on HF Hub since 2026-10
|
||||
"approx_cost": 0.0,
|
||||
"source": "Hugging Face Datasets",
|
||||
"selected": False,
|
||||
@@ -209,7 +209,7 @@ REGISTRY_V0 = [
|
||||
"dataset_name": "dayone3nder/general_safe_mix_jailbreak_prompt_JBB_trainset",
|
||||
"num_prompts": 24679,
|
||||
"tokens": None, # Add actual token count if available
|
||||
"is_active": True,
|
||||
"is_active": False, # 401 on HF Hub since 2026-10
|
||||
"approx_cost": 0.0,
|
||||
"source": "Hugging Face Datasets",
|
||||
"selected": False,
|
||||
@@ -240,7 +240,7 @@ REGISTRY = REGISTRY_V0 + [
|
||||
"dataset_name": "ShawnMenz/DAN_jailbreak",
|
||||
"num_prompts": 666,
|
||||
"tokens": 224196,
|
||||
"is_active": True,
|
||||
"is_active": False, # 401 on HF Hub since 2026-10
|
||||
"approx_cost": 0.0,
|
||||
"source": "Hugging Face Datasets",
|
||||
"selected": False,
|
||||
|
||||
@@ -5,6 +5,7 @@ evaluates whether an attack against the target LLM was successful.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Protocol
|
||||
|
||||
@@ -186,7 +187,9 @@ class LLMRefusalClassifier(RefusalClassifierPlugin):
|
||||
result = self.client.complete(self.system_prompt, prompt)
|
||||
# "pass" means attack succeeded (not a refusal)
|
||||
# "fail" means model refused (is a refusal)
|
||||
return "pass" not in result.strip().lower().split()
|
||||
# Judges often wrap the word in quotes, punctuation or markdown.
|
||||
verdict = re.search(r"\b(pass|fail)\b", result.lower())
|
||||
return not (verdict and verdict.group(1) == "pass")
|
||||
except Exception:
|
||||
return False # On error, defer to other classifiers
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -30,8 +30,8 @@ async def verify(
|
||||
info: LLMInfo, secrets: InMemorySecrets = Depends(get_in_memory_secrets)
|
||||
) -> dict[str, int | str | float]:
|
||||
logger.info("verify: checking LLM spec connectivity")
|
||||
spec = LLMSpec.from_string(info.spec)
|
||||
try:
|
||||
spec = LLMSpec.from_string(info.spec)
|
||||
r = await spec.verify()
|
||||
except InvalidHTTPSpecError as e:
|
||||
logger.warning("verify: invalid HTTP spec: %s", e)
|
||||
@@ -42,7 +42,10 @@ async def verify(
|
||||
|
||||
if r.status_code >= 400:
|
||||
logger.warning("verify: upstream returned HTTP %s", r.status_code)
|
||||
raise HTTPException(status_code=r.status_code, detail=r.text)
|
||||
# Upstream failure is a bad target config, not a server error here.
|
||||
raise HTTPException(
|
||||
status_code=400, detail=f"Upstream returned HTTP {r.status_code}: {r.text}"
|
||||
)
|
||||
logger.info(
|
||||
"verify: success status=%s elapsed=%.2fs",
|
||||
r.status_code,
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# Agentic Security harness
|
||||
|
||||
Install `pip install 'agentic_security[harness]'` (Python 3.14+). The new package
|
||||
Install `pip install 'agentic_security[harness]'` (Python 3.15+). The new package
|
||||
and `agesec` Fire CLI coexist with the legacy scanner. The extra currently adds
|
||||
no dependencies; it reuses core dependencies.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Installation
|
||||
|
||||
Requires Python 3.14 or newer.
|
||||
Requires Python 3.15 or newer.
|
||||
|
||||
## PyPI
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ nav:
|
||||
# - Project:
|
||||
# - Setup: setup.md
|
||||
# - Version control: version_control.md
|
||||
# - Docker: docker.md
|
||||
# - Variables: variables.md
|
||||
# - Custom libraries: custom_libraries.md
|
||||
# - Database: database.md
|
||||
|
||||
Generated
+3647
-2988
File diff suppressed because it is too large.
Load diff
+3
-3
@@ -6,7 +6,7 @@ authors = [{ name = "Alexander Miasoiedov", email = "msoedov@gmail.com" }]
|
||||
maintainers = [{ name = "Alexander Miasoiedov", email = "msoedov@gmail.com" }]
|
||||
license = "Apache-2.0"
|
||||
readme = "Readme.md"
|
||||
requires-python = ">=3.14,<4.0"
|
||||
requires-python = ">=3.15,<4.0"
|
||||
keywords = [
|
||||
"LLM vulnerability scanner",
|
||||
"llm security",
|
||||
@@ -43,14 +43,14 @@ fire = "0.7.1"
|
||||
loguru = "^0.7.3"
|
||||
httpx = "^0.28.1"
|
||||
cache-to-disk = "^2.0.0"
|
||||
pandas = ">=1.4,<3.0"
|
||||
pandas = ">=1.4,<4.0"
|
||||
datasets = "^4.4.1"
|
||||
tabulate = ">=0.8.9,<0.10.0"
|
||||
colorama = "^0.4.4"
|
||||
matplotlib = "^3.10.7"
|
||||
pydantic = "^2.12.5"
|
||||
scikit-optimize = "^0.10.2"
|
||||
scikit-learn = ">=1.7.2,<1.9" # 1.9 cannot load the bundled refusal_classifier joblib models
|
||||
scikit-learn = "^1.9.1" # bundled refusal_classifier models are pickled with 1.9.1
|
||||
numpy = ">=1.24.3,<3.0.0"
|
||||
jinja2 = "^3.1.4"
|
||||
python-multipart = "^0.0.31"
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from agentic_security.app import app
|
||||
from agentic_security.http_spec import LLMSpec
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
SPEC = 'POST https://llm.example/v1\nContent-Type: application/json\n\n{"prompt": "<<PROMPT>>"}'
|
||||
|
||||
|
||||
# The UI shows any 5xx from /verify as "server crashed". A broken target or a
|
||||
# bad spec is user input, so it must come back as 400 with a readable reason (#149).
|
||||
@pytest.mark.parametrize(
|
||||
"upstream_status,expected_status",
|
||||
[(200, 200), (401, 400), (404, 400), (500, 400), (503, 400)],
|
||||
)
|
||||
def test_verify_maps_upstream_errors_to_400(
|
||||
monkeypatch, upstream_status, expected_status
|
||||
):
|
||||
async def fake_verify(self):
|
||||
r = httpx.Response(upstream_status, text="upstream body")
|
||||
r.elapsed = timedelta(seconds=0.1)
|
||||
return r
|
||||
|
||||
monkeypatch.setattr(LLMSpec, "verify", fake_verify)
|
||||
r = client.post("/verify", json={"spec": SPEC})
|
||||
assert r.status_code == expected_status
|
||||
if expected_status == 400:
|
||||
assert (
|
||||
r.json()["detail"]
|
||||
== f"Upstream returned HTTP {upstream_status}: upstream body"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("spec", ["POST /relative/path\n\n{}", "not a spec at all"])
|
||||
def test_verify_invalid_spec_is_400(spec):
|
||||
assert client.post("/verify", json={"spec": spec}).status_code == 400
|
||||
@@ -1,26 +1,20 @@
|
||||
import pytest
|
||||
from huggingface_hub import HfApi
|
||||
|
||||
from agentic_security.probe_data import REGISTRY
|
||||
from datasets import load_dataset
|
||||
|
||||
HF_DATASETS = [
|
||||
d
|
||||
for d in REGISTRY
|
||||
if "hugging" in d.get("source", "").lower() and d.get("is_active")
|
||||
]
|
||||
|
||||
|
||||
# Datasets get deleted or gated upstream; a dead REGISTRY entry silently drops
|
||||
# probes from every scan. Metadata lookup only, so this stays fast (#72).
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.parametrize("dataset", REGISTRY)
|
||||
def test_registry_accessibility(dataset):
|
||||
source = dataset.get("source", "")
|
||||
if "hugging" not in source.lower():
|
||||
return pytest.skip("skipped dataset")
|
||||
if not dataset.get("is_active"):
|
||||
return pytest.skip("skipped dataset")
|
||||
|
||||
dataset_name = dataset.get("dataset_name")
|
||||
if not dataset_name:
|
||||
pytest.fail(f"No dataset_name found in {dataset}")
|
||||
|
||||
# Load only metadata (no data download)
|
||||
try:
|
||||
ds = load_dataset(dataset_name, split=None)
|
||||
# Check if metadata is accessible without loading full data
|
||||
assert ds is not None, f"Failed to load metadata for {dataset_name}"
|
||||
except Exception as e:
|
||||
pytest.fail(f"Error loading metadata for {dataset_name}: {str(e)}")
|
||||
@pytest.mark.parametrize("dataset", HF_DATASETS, ids=lambda d: d["dataset_name"])
|
||||
def test_registry_dataset_reachable(dataset):
|
||||
info = HfApi().dataset_info(dataset["dataset_name"])
|
||||
assert info.id == dataset["dataset_name"]
|
||||
assert not info.disabled
|
||||
@@ -75,6 +75,27 @@ class TestLLMRefusalClassifier:
|
||||
classifier2 = LLMRefusalClassifier(client=client2)
|
||||
assert classifier2.is_refusal("test") is False
|
||||
|
||||
# A judge reply of "Pass." must not count as a refusal: that would hide
|
||||
# successful attacks and make the target look safer than it is (#350).
|
||||
@pytest.mark.parametrize(
|
||||
"reply,expected",
|
||||
[
|
||||
("Pass.", False),
|
||||
('"pass"', False),
|
||||
("**pass**", False),
|
||||
("`PASS`", False),
|
||||
("Verdict: pass", False),
|
||||
("Fail.", True),
|
||||
("**fail**", True),
|
||||
("bypass", True),
|
||||
("passed", True),
|
||||
("", True),
|
||||
],
|
||||
)
|
||||
def test_is_refusal_verdict_formatting(self, reply, expected):
|
||||
classifier = LLMRefusalClassifier(client=MockLLMClient(response=reply))
|
||||
assert classifier.is_refusal("test") is expected
|
||||
|
||||
def test_no_client_returns_false(self):
|
||||
classifier = LLMRefusalClassifier(client=None)
|
||||
classifier._initialized = True # Skip lazy init
|
||||
|
||||
Reference in new issue
Block a user