Skip to content
Merged
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
77 changes: 77 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,35 @@ Open rows keyed with the legacy fingerprint are **rekeyed** automatically
on the first rescan that computes a region hash. There is no post-hoc
embedding similarity on finding text.

### Structured findings (SAST, SCA, secret scanning)

SCA and secret-scanning runs get the same open/closed lifecycle as SAST,
in their own tables (`sca_findings`, `secret_findings`). Each finding also
records:

- **`language`** and **`framework`**: detected from the files on disk
(extensions and dependency manifests, see `stack.py`), never taken from
the model's own prose. SCA findings take their language from the
package's ecosystem.
- **`construct`**: the report's `**Construct:**` line. This is a one-line,
code-free description of the vulnerable construct ("YAML loader without
a safe loader applied to an uploaded config file"). Because it has no
paths or identifiers, findings can be compared across repos.

| Skill | Identity (fingerprint) |
|---|---|
| `sca-reachability` | ecosystem + package, plus the advisory ID when the report names a real one (`CVE-…`, `GHSA-…`, `PYSEC-…`). The version is not part of the key, so a bump that doesn't fix the issue keeps the same open finding. |
| `secret-scanning` | secret type + normalized path + code-region hash (or a same-run line bucket, as with SAST) |

Secret evidence is cut back down to its first 8 characters before it is
stored, even when a report includes more.

```bash
secfoo findings export # open findings, every skill, JSON Lines to stdout
secfoo findings export -s sast --status all # one skill, open and closed
secfoo findings export --no-locations -o findings.jsonl # drop paths/lines/region hashes
```

## Install

Pick whichever fits how you work — they're all the same tool underneath.
Expand Down Expand Up @@ -432,6 +461,54 @@ secfoo mcp list # show what's configured
secfoo mcp sync --agent gemini # register into gemini's own config
```

## Pattern memory (`secfoo-memory`)

secfoo-memory is a hosted service of curated, generalized vulnerability
patterns: what triggers them, how to fix them, and what commonly looks like
them but isn't a real issue. With it connected, the agent can check a
candidate finding against known patterns during an assessment. It is
optional and off until you have a key.

```bash
secfoo memory signup --email you@example.com # free key, emailed one-time code
secfoo memory status # on/off, plan, today's usage
secfoo memory login --api-key <key> # an existing team/enterprise key
secfoo memory logout
```

Once a key is saved, secfoo registers `secfoo-memory` as a default MCP
server. `secfoo mcp list` shows it next to your own servers. `claude` picks
it up on every run. For `gemini` or Cursor (`agent`), run
`secfoo mcp sync --agent <agent>` once. The `api` agent has no tool loop,
so memory doesn't apply to it. When memory is on, the prompt tells the
agent when to use the three tools (`lookup_patterns`, `get_remediation`,
`check_false_positive`) and how to phrase a query.

**What leaves your machine:** only what the agent sends to those tools. A
query is a language, a framework, an optional CWE, and a construct
description of at most 200 characters, such as "HTML template rendering a
request parameter with autoescaping disabled". The service rejects queries
that look like source code, file paths or secrets, so your code is never
sent. Results are reference data. The prompt tells the agent to confirm
every finding in your own code and never to follow instructions found in a
result.

**Turning it off:**

| Scope | How |
|---|---|
| One run | `secfoo run --no-memory ...` |
| Shell/CI job | `SECFOO_MEMORY=off` |
| Always | `[memory] enabled = false` in `~/.secfoo/config.toml` |

Runs record whether memory was on (`runs.memory_enabled`), so you can
compare runs with memory and without it on the same target. The key is
stored in `~/.secfoo/memory.toml` with mode `0600`. A self-hosted
enterprise memory service is used by running
`secfoo memory login --url https://memory.internal.example --api-key ...`,
or by defining your own `[[mcp_servers]]` entry named `secfoo-memory`,
which replaces the default.

## Distribution

Every install channel above is built on the same foundation: standalone,
Expand Down
7 changes: 4 additions & 3 deletions src/secfoo/agents/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from pathlib import Path

from secfoo.agents.base import AgentAdapter, Usage
from secfoo.mcp import write_claude_mcp_config
from secfoo.mcp import effective_servers, write_claude_mcp_config
from secfoo.settings import load_config

# Minimal read-only tool grant: the assessment only needs to read the
Expand Down Expand Up @@ -61,8 +61,9 @@ class ClaudeAdapter(AgentAdapter):
def build_command(self, prompt: str, *, workdir: Path) -> list[str]:
# MCP servers from ~/.secfoo/config.toml are injected here, scoped
# to just this invocation (claude's --mcp-config), never touching
# the user's own global Claude config.
mcp_servers = load_config().mcp_servers
# the user's own global Claude config. Includes the default
# secfoo-memory server when memory is on (mcp.effective_servers).
mcp_servers = effective_servers(load_config().mcp_servers)
mcp_config_path = write_claude_mcp_config(mcp_servers)

# `mcp__<server-name>` (server-level, no tool suffix) grants every
Expand Down
193 changes: 188 additions & 5 deletions src/secfoo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from secfoo import cloud as cloud_module
from secfoo import mcp as mcp_module
from secfoo import memory as memory_module
from secfoo.agents.registry import ADAPTERS, get_adapter
from secfoo.aibom import AIBOMParseError, parse_ai_bom
from secfoo.attachments import infer_attachment_kind
Expand All @@ -38,7 +39,11 @@
accept_app = typer.Typer(name="accept", help="Record a human risk acceptance for a specific threat.")
cloud_app = typer.Typer(name="cloud", help="Connect to the secfoo enterprise portal.")
vendor_app = typer.Typer(name="vendor", help="Fetch optional local assets for the dashboard.")
memory_app = typer.Typer(name="memory", help="Connect to secfoo-memory, the vulnerability pattern service.")
findings_app = typer.Typer(name="findings", help="Query the structured findings recorded by past runs.")
app.add_typer(mcp_app, name="mcp")
app.add_typer(memory_app, name="memory")
app.add_typer(findings_app, name="findings")
app.add_typer(config_app, name="config")
app.add_typer(assessment_app, name="assessment")
app.add_typer(exception_app, name="exception")
Expand Down Expand Up @@ -259,6 +264,12 @@ def run(
help="Print one JSON document (per-skill outcomes, severity counts, "
"spend, gate results) to stdout instead of the table -- for CI.",
),
no_memory: bool = typer.Option(
False,
"--no-memory",
help="Don't give the agent the secfoo-memory pattern tools for this run, "
"even if `secfoo memory signup/login` has configured a key.",
),
) -> None:
"""Run one or more security skills against a target using the chosen agent.

Expand Down Expand Up @@ -293,6 +304,9 @@ def run(
err_console.print(f"[red]Error in {CONFIG_PATH}:[/] {exc}")
raise typer.Exit(code=1) from None

if no_memory:
memory_module.disable_for_this_process()

if agent is None:
agent = AgentId(file_defaults.agent) if file_defaults.agent else AgentId.CLAUDE
if depth is None:
Expand Down Expand Up @@ -1285,6 +1299,170 @@ def cloud_sync() -> None:
console.print(f"Synced {synced_count}/{len(unsynced)} run(s).")


def _memory_whoami_line(info: dict) -> str:
plan = info.get("plan", "unknown")
used, quota = info.get("used_today"), info.get("daily_quota")
usage = f", {used}/{quota} lookups used today" if used is not None and quota is not None else ""
return f"plan [bold]{plan}[/]{usage}"


@memory_app.command(name="signup")
def memory_signup(
email: Optional[str] = typer.Option(None, "--email", help="Where to send the one-time verification code."),
code: Optional[str] = typer.Option(
None, "--code", help="The emailed code, for a non-interactive second step (skips sending a new one)."
),
url: str = typer.Option(memory_module.DEFAULT_MEMORY_URL, "--url", help="Override the secfoo-memory service URL."),
) -> None:
"""Get a free secfoo-memory key (email verification) and save it.

Once saved, every `secfoo run` with claude (or gemini/Cursor, after
`secfoo mcp sync`) can look up known vulnerability patterns while it
assesses. Only short, code-free descriptions are ever sent -- the
service rejects anything that looks like source code.
"""
if email is None:
if not _is_interactive():
err_console.print("[red]--email is required when not running in a terminal.[/]")
raise typer.Exit(code=1)
email = Prompt.ask("Email", console=console)
email = email.strip()

if code is None:
try:
memory_module.request_signup(email, url=url)
except memory_module.MemoryServiceError as exc:
err_console.print(f"[red]Signup failed:[/] {exc}")
raise typer.Exit(code=1) from None
if not _is_interactive():
console.print(
f"Verification code sent to {email}. Finish with: "
f"[bold]secfoo memory signup --email {email} --code <code>[/]"
)
return
code = Prompt.ask(f"Verification code sent to {email}", console=console)

try:
api_key = memory_module.verify_signup(email, code.strip(), url=url)
except memory_module.MemoryServiceError as exc:
err_console.print(f"[red]Verification failed:[/] {exc}")
raise typer.Exit(code=1) from None

memory_module.save_memory_config(memory_module.MemoryConfig(api_key=api_key, url=url))
console.print(f"Saved your secfoo-memory key to {memory_module.MEMORY_CONFIG_PATH}.")
console.print("Pattern lookups are now on for claude runs. Opt out per run with [bold]--no-memory[/].")


@memory_app.command(name="login")
def memory_login(
api_key: str = typer.Option(..., "--api-key", help="A secfoo-memory key (free, team, or enterprise)."),
url: str = typer.Option(
memory_module.DEFAULT_MEMORY_URL, "--url",
help="Override for a self-hosted enterprise memory service or a non-default region.",
),
) -> None:
"""Save an existing secfoo-memory key after validating it."""
config = memory_module.MemoryConfig(api_key=api_key.strip(), url=url)
try:
info = memory_module.whoami(config)
except memory_module.MemoryServiceError as exc:
err_console.print(f"[red]Login failed:[/] {exc}")
raise typer.Exit(code=1) from None
memory_module.save_memory_config(config)
console.print(f"Connected to secfoo-memory at {url} -- {_memory_whoami_line(info)}.")


@memory_app.command(name="logout")
def memory_logout() -> None:
"""Remove the saved secfoo-memory key (runs go back to no pattern lookups)."""
if not memory_module.MEMORY_CONFIG_PATH.exists():
console.print("No secfoo-memory key saved.")
return
memory_module.delete_memory_config()
console.print("Removed the secfoo-memory key. Runs no longer use pattern lookups.")


@memory_app.command(name="status")
def memory_status() -> None:
"""Show whether pattern lookups are on, and this key's plan/usage."""
try:
config = memory_module.load_memory_config()
except memory_module.MemoryServiceError as exc:
err_console.print(f"[red]{exc}[/]")
raise typer.Exit(code=1) from None
if config is None:
console.print("Not set up. Run [bold]secfoo memory signup[/] for a free key.")
return
if memory_module.disabled_by_env():
console.print(f"[yellow]Disabled by {memory_module.ENV_TOGGLE} in the environment.[/]")
elif memory_module.memory_server(config) is None:
console.print(f"[yellow]Disabled in {CONFIG_PATH}[/] ([memory] enabled = false).")
else:
console.print("Pattern lookups: [green]on[/] for claude (gemini/Cursor after `secfoo mcp sync`).")
try:
info = memory_module.whoami(config)
except memory_module.MemoryServiceError as exc:
err_console.print(f"[yellow]Key saved, but couldn't reach {config.url} just now:[/] {exc}")
return
console.print(f"Service {config.url} -- {_memory_whoami_line(info)}.")


class FindingsSkill(str, Enum):
SAST = "sast"
SCA = "sca-reachability"
SECRET = "secret-scanning"


class FindingsStatus(str, Enum):
OPEN = "open"
CLOSED = "closed"
ALL = "all"


# Lifecycle bookkeeping that means nothing outside this one SQLite file.
_EXPORT_DROP_COLUMNS = {"id", "project_id", "first_seen_run_id", "last_seen_run_id", "closed_in_run_id"}
_EXPORT_LOCATION_COLUMNS = {"location_file", "location_line", "code_region_hash", "reachability_evidence"}


@findings_app.command(name="export")
def findings_export(
skill: list[FindingsSkill] = typer.Option([], "--skill", "-s", help="Limit to these skills. Repeatable."),
project: Optional[int] = typer.Option(None, "--project", help="Limit to one project id."),
status: FindingsStatus = typer.Option(FindingsStatus.OPEN, "--status", help="open, closed, or all."),
output: Optional[Path] = typer.Option(None, "--output", "-o", help="Write JSONL here instead of stdout."),
no_locations: bool = typer.Option(
False, "--no-locations",
help="Drop file paths, line numbers, code-region hashes and reachability evidence.",
),
) -> None:
"""Export structured findings (SAST, SCA, secret scanning) as JSON Lines.

One record per finding, with its skill, severity, stack (language/
framework) and code-free construct description -- the same shape for
every skill, for feeding other tools.
"""
with RunRepository() as repo:
rows = repo.list_structured_findings(
skill_ids=[s.value for s in skill] or None,
project_id=project,
status=None if status is FindingsStatus.ALL else status.value,
)
lines = []
for skill_id, row in rows:
record = {"skill_id": skill_id, "project": row.pop("display_name", None)}
for column, value in row.items():
if column in _EXPORT_DROP_COLUMNS or (no_locations and column in _EXPORT_LOCATION_COLUMNS):
continue
record[column] = value
lines.append(json.dumps(record, sort_keys=True))
text = "\n".join(lines) + ("\n" if lines else "")
if output is None:
sys.stdout.write(text)
return
output.write_text(text, encoding="utf-8")
err_console.print(f"Wrote {len(lines)} finding(s) to {output}.")


MERMAID_CDN_URL = "https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"


Expand Down Expand Up @@ -1350,16 +1528,20 @@ def mcp_list() -> None:
err_console.print(f"[red]Error in {CONFIG_PATH}:[/] {exc}")
raise typer.Exit(code=1) from None

if not cfg.mcp_servers:
servers = mcp_module.effective_servers(cfg.mcp_servers)
if not servers:
console.print(f"No MCP servers configured. Add a [[mcp_servers]] entry to {CONFIG_PATH}.")
return

user_names = {server.name for server in cfg.mcp_servers}
table = Table(title="Configured MCP servers")
table.add_column("Name")
table.add_column("Target")
table.add_column("Transport")
for server in cfg.mcp_servers:
table.add_row(server.name, server.url or server.command or "", server.transport)
table.add_column("Source")
for server in servers:
source = "config.toml" if server.name in user_names else "secfoo memory (default)"
table.add_row(server.name, server.url or server.command or "", server.transport, source)
console.print(table)
console.print(
"\n[bold]claude[/] picks these up automatically on every run (--mcp-config, "
Expand All @@ -1383,7 +1565,8 @@ def mcp_sync(
err_console.print(f"[red]Error in {CONFIG_PATH}:[/] {exc}")
raise typer.Exit(code=1) from None

if not cfg.mcp_servers:
servers = mcp_module.effective_servers(cfg.mcp_servers)
if not servers:
console.print(f"No MCP servers configured in {CONFIG_PATH} -- nothing to sync.")
return

Expand All @@ -1392,7 +1575,7 @@ def mcp_sync(
return

try:
results = mcp_module.sync(agent.value, cfg.mcp_servers)
results = mcp_module.sync(agent.value, servers)
except ValueError as exc:
err_console.print(f"[red]Error:[/] {exc}")
raise typer.Exit(code=1) from None
Expand Down
12 changes: 12 additions & 0 deletions src/secfoo/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ depth = "quick" # quick | standard
# cost noticeably on large trees.
# exclude = ["vendor/", "third_party/", "some-cloned-repo/"]

[memory]
# secfoo-memory: a hosted service of curated vulnerability patterns the
# agent can consult mid-assessment (known trigger conditions, remediation,
# known false positives). Only active once you have a key -- run
# `secfoo memory signup` for a free one. The agent sends short, code-free
# descriptions only ("SQL query built from a request parameter"); the
# service rejects anything that looks like source code. Registered as the
# default `secfoo-memory` MCP server; define your own [[mcp_servers]] entry
# with that name to point at a self-hosted instance instead.
# Opt out per run with `secfoo run --no-memory`, or entirely here:
enabled = true

# Define MCP servers once here instead of wiring each agent CLI separately.
# How this reaches an agent depends on what that agent's CLI supports:
# - claude: picked up automatically on every `secfoo run` (via
Expand Down
Loading
Loading