Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 110 additions & 9 deletions src/secfoo/agents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import fnmatch
import os
import subprocess # nosec B404 -- fixed git argv, no shell
import time
from pathlib import Path
from typing import TypedDict
Expand Down Expand Up @@ -50,6 +51,10 @@ class _State(TypedDict):
workdir: str
model: str
timeout: int
prev_commit: str | None # Memory Bank: SHA of the last scanned commit; None = full scan.
# Memory Bank: paths an incremental scan covered (files sent + files
# deleted); None = full scan. The runner only closes findings in these.
scan_scope: list[str] | None
messages: list[dict]
report: str
attempts: int
Expand All @@ -63,42 +68,132 @@ def _matches(name: str, patterns: list[str]) -> bool:
return any(fnmatch.fnmatch(name, p) for p in patterns)


def collect_files(workdir: Path) -> str:
"""Return every readable text file under `workdir` as one labelled string."""
def _git(workdir: Path, *args: str, timeout: int = 30) -> subprocess.CompletedProcess[str]:
return subprocess.run( # nosec B603 B607 -- fixed git argv, no shell
["git", "-C", str(workdir), *args], capture_output=True, text=True, timeout=timeout, check=False
)


def _ensure_commit_available(workdir: Path, commit: str) -> bool:
"""GitHub targets are depth-1 clones, so the previous scan's commit is
usually missing. Fetch only that commit (all a diff against it needs)
rather than unshallowing the whole history on every rescan."""
if _git(workdir, "cat-file", "-e", f"{commit}^{{commit}}").returncode == 0:
return True
_git(workdir, "fetch", "--depth=1", "origin", commit, timeout=60)
return _git(workdir, "cat-file", "-e", f"{commit}^{{commit}}").returncode == 0


def _git_changed_files(workdir: Path, prev_commit: str) -> tuple[list[str], list[str]] | None:
"""Return (changed_paths, deleted_paths), relative to `workdir`, for
everything that differs between prev_commit and the files on disk now:
commits since prev_commit, uncommitted edits, and untracked files.

Renames come back as a delete of the old path plus a new path
(--no-renames), so findings recorded against the old path are covered.

None means the diff couldn't be computed (git missing, commit not
reachable); the caller then falls back to a full scan.
"""
try:
if not _ensure_commit_available(workdir, prev_commit):
return None
# `git diff <commit>` (no `..HEAD`) compares against the working
# tree, so staged and unstaged edits are included, not just commits.
diff = _git(workdir, "diff", "--name-status", "--no-renames", "--relative", "-z", prev_commit)
untracked = _git(workdir, "ls-files", "--others", "--exclude-standard", "-z")
except (OSError, subprocess.SubprocessError):
return None
if diff.returncode != 0 or untracked.returncode != 0:
return None
changed: set[str] = set()
deleted: set[str] = set()
tokens = diff.stdout.split("\0")
for status, path in zip(tokens[0::2], tokens[1::2]):
if path:
(deleted if status.startswith("D") else changed).add(path)
changed.update(path for path in untracked.stdout.split("\0") if path)
return sorted(changed), sorted(deleted)


def _read_files(workdir: Path, *, changed_only: list[str] | None = None) -> tuple[str, list[str]]:
"""collect_files, plus the relative paths that actually made it into the
text (after exclusions, size limits and binary detection)."""
allowed = set(changed_only) if changed_only is not None else None
chunks: list[str] = []
included: list[str] = []
total = 0
for root, dirs, files in os.walk(workdir):
dirs[:] = sorted(d for d in dirs if not _matches(d, DIR_PATTERNS))
for name in sorted(files):
path = Path(root) / name
rel = path.relative_to(workdir).as_posix()
if allowed is not None and rel not in allowed:
continue # incremental scan: unchanged file
if _matches(name, FILE_PATTERNS) or path.stat().st_size > MAX_FILE_BYTES:
continue
try:
text = path.read_text(encoding="utf-8")
except (UnicodeDecodeError, OSError):
continue # binary file (image, font, ...)
chunk = f"\n===== FILE: {path.relative_to(workdir).as_posix()} =====\n{text}\n"
chunk = f"\n===== FILE: {rel} =====\n{text}\n"
total += len(chunk)
if total > MAX_TOTAL_CHARS:
raise ValueError(
f"Target is too large for the api agent (over {MAX_TOTAL_CHARS:,} characters). "
"Use --exclude to narrow it, or use a CLI agent."
)
chunks.append(chunk)
return "".join(chunks)
included.append(rel)
return "".join(chunks), included


def collect_files(workdir: Path, *, changed_only: list[str] | None = None) -> str:
"""Return readable text files under `workdir` as one labelled string.
If `changed_only` is given, only those paths are included (incremental scan)."""
return _read_files(workdir, changed_only=changed_only)[0]


def build_graph(litellm, StateGraph, START, END):
"""The assessment workflow. Takes the libraries as arguments so tests can
run the real graph against a fake LiteLLM."""

def collect(state: _State) -> dict:
code = collect_files(Path(state["workdir"]))
workdir = Path(state["workdir"])
prev_commit = state["prev_commit"]
scan_scope: list[str] | None = None
scope_note = ""
code = ""
if prev_commit:
diff = _git_changed_files(workdir, prev_commit)
if diff is not None:
changed, deleted = diff
code, included = _read_files(workdir, changed_only=changed)
# Go incremental only if at least one changed file is actually
# scannable. A commit touching only images, lock files or
# excluded directories would otherwise send the model no
# source at all -- and an empty report would read as "every
# open finding is fixed".
if included:
scan_scope = sorted(set(included) | set(deleted))
scope_note = (
f"Note: this is an incremental scan. Only files changed since "
f"commit {prev_commit[:8]} are included below.\n\n"
)
if deleted:
scope_note += (
"## Deleted files\nThe following files were deleted since the last scan "
"and are no longer present:\n" + "\n".join(f"- {f}" for f in deleted) + "\n\n"
)
if scan_scope is None:
# First scan, nothing scannable changed, or git couldn't diff: full scan.
code = collect_files(workdir)
content = (
f"{state['prompt']}\n\n## Source files\nYou cannot open files. "
f"{state['prompt']}\n\n{scope_note}"
f"## Source files\nYou cannot open files. "
f"The complete target source is included below.\n{code}"
)
return {"messages": [{"role": "user", "content": content}]}
return {"messages": [{"role": "user", "content": content}], "scan_scope": scan_scope}

def assess(state: _State) -> dict:
response = litellm.completion(
Expand Down Expand Up @@ -149,7 +244,7 @@ def is_available(self) -> bool:
def build_command(self, prompt: str, *, workdir: Path) -> list[str]:
return [] # required by the base class; there is no program to launch

def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> AgentResult:
def run(self, prompt: str, *, workdir: Path, timeout: int | None = None, prev_commit: str | None = None) -> AgentResult: # Memory Bank: prev_commit triggers incremental diff scan.
started = time.monotonic()
_strip_key_whitespace()
if not self.is_available():
Expand All @@ -170,6 +265,8 @@ def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> Agen
"workdir": str(workdir),
"model": os.environ.get("SECFOO_API_MODEL", DEFAULT_MODEL),
"timeout": timeout or self.default_timeout_seconds,
"prev_commit": prev_commit, # Memory Bank: None = full scan, SHA = incremental diff.
"scan_scope": None,
"messages": [],
"report": "",
"attempts": 0,
Expand All @@ -186,7 +283,9 @@ def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> Agen
cost_usd=None if final["unpriced"] else final["cost_usd"],
)
report = final["report"]
return self._result("success", started, stdout=report, report=report, usage=usage)
return self._result(
"success", started, stdout=report, report=report, usage=usage, scan_scope=final.get("scan_scope")
)

def _result(
self,
Expand All @@ -197,6 +296,7 @@ def _result(
stderr: str = "",
report: str = "",
usage: Usage | None = None,
scan_scope: list[str] | None = None,
) -> AgentResult:
usage = usage or Usage()
return AgentResult(
Expand All @@ -211,6 +311,7 @@ def _result(
input_tokens=usage.input_tokens,
output_tokens=usage.output_tokens,
cost_usd=usage.cost_usd,
scan_scope=scan_scope,
)


Expand Down
7 changes: 6 additions & 1 deletion src/secfoo/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class AgentResult:
input_tokens: int | None = None
output_tokens: int | None = None
cost_usd: float | None = None
# Memory Bank: for an incremental scan, the paths it covered (files sent
# to the model plus files deleted since the last scan). None means a
# full scan. The runner only closes findings located in these paths.
scan_scope: list[str] | None = None


@dataclass(frozen=True)
Expand Down Expand Up @@ -112,6 +116,7 @@ def detect_failure(self, stdout: str, stderr: str) -> str | None:
its "no findings" report would read as a clean bill of health).
Default: trust the exit code."""
return None

def extract_usage_from_stderr(self, stderr: str) -> Usage:
"""Pull token counts / cost out of raw stderr. Default: unknown.

Expand All @@ -135,7 +140,7 @@ def _collect_usage(self, stdout: str, stderr: str) -> Usage:
usage = self.extract_usage_from_stderr(stderr)
return usage

def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> AgentResult:
def run(self, prompt: str, *, workdir: Path, timeout: int | None = None, prev_commit: str | None = None) -> AgentResult: # Memory Bank: prev_commit passed through; CLI adapters ignore it.
if not self.is_available():
return AgentResult(
agent=self.name,
Expand Down
28 changes: 26 additions & 2 deletions src/secfoo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import typer
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.prompt import Prompt
from rich.prompt import Confirm, Prompt
from rich.table import Table

from secfoo import cloud as cloud_module
Expand All @@ -23,7 +23,7 @@
from secfoo.cloud import CloudConfig, CloudError
from secfoo.config import CLOUD_CONFIG_PATH, attachment_dir
from secfoo.cost import format_cost, format_tokens, sum_optional
from secfoo.runner import _project_display_name, execute_runs
from secfoo.runner import _project_display_name, already_scanned_skills, execute_runs
from secfoo.settings import CONFIG_PATH, ConfigError, load_config
from secfoo.skills.loader import load_all_skills
from secfoo.skills.renderer import merge_excludes as _merge_excludes
Expand Down Expand Up @@ -271,6 +271,12 @@ def run(
help="Don't give the agent the secfoo-memory pattern tools for this run, "
"even if `secfoo memory signup/login` has configured a key.",
),
rescan: bool = typer.Option(
False,
"--rescan",
help="Scan even when this exact commit was already scanned for a skill. Without it, "
"a run on a terminal asks first; a non-interactive run (CI, --json) always scans.",
),
) -> None:
"""Run one or more security skills against a target using the chosen agent.

Expand Down Expand Up @@ -322,6 +328,24 @@ def run(
# config's list, so naming one path can't silently drop the others.
exclude_paths = _merge_excludes(file_defaults.exclude, exclude)

# Memory Bank: on a terminal, ask before repeating a scan of a commit that
# was already scanned. Only here, never in execute_runs(), which the
# dashboard and CI also call with no terminal to answer.
if not rescan and _is_interactive() and not json_output:
try:
head, repeats = already_scanned_skills(skill_ids=[s.value for s in skill], target=target)
except TargetResolutionError:
head, repeats = None, [] # execute_runs reports the bad target below
if head and repeats and not Confirm.ask(
f"Commit {head[:8]} was already scanned for {', '.join(repeats)}. Scan again?",
default=False,
console=console,
):
skill = [s for s in skill if s.value not in repeats]
if not skill:
console.print("Nothing new to scan. Use --rescan to scan this commit again.")
raise typer.Exit(code=0)

task_ids: dict[str, int] = {}
# In --json mode nothing but the final document may reach stdout, so
# the spinner is disabled and human-facing notes go to stderr.
Expand Down
Loading
Loading