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
44 changes: 38 additions & 6 deletions src/secfoo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import shutil
import sys
from datetime import datetime, timezone
from datetime import date, datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -55,6 +55,28 @@ def _now_iso() -> str:
return datetime.now(timezone.utc).isoformat()


def _validate_date(value: Optional[str]) -> Optional[str]:
"""Typer callback for the `--expires-at` / `--discovered-at` style
options that are documented as YYYY-MM-DD: rejects anything that
doesn't parse as that shape at input time.

Without this, an unparseable value (a typo, a different format) was
accepted and stored as-is, then silently ignored by every downstream
consumer that parses it with `datetime.fromisoformat` (the aging
buckets in storage/repository.py, the past-due check in
web/top_findings.py) -- the exception/finding looked normal in
`list`/`show` but its expiry could never be tracked, detected as
past-due, or surfaced on the dashboard.
"""
if value is None:
return None
try:
date.fromisoformat(value)
except ValueError:
raise typer.BadParameter(f"'{value}' is not a valid date -- expected YYYY-MM-DD.") from None
return value


# Derived from the skill definition files rather than hand-listed, so
# dropping a new definitions/*.md in registers it with the CLI (and its
# --help choices) automatically. Hand-maintaining this enum alongside the
Expand Down Expand Up @@ -609,7 +631,9 @@ def exception_create(
title: str = typer.Option(..., "--title", help="Short description of what's being accepted."),
justification: str = typer.Option(..., "--justification", help="Why this risk is being accepted."),
granted_by: str = typer.Option(..., "--granted-by", help="Who approved this exception."),
expires_at: str = typer.Option(..., "--expires-at", help="Expiry date, YYYY-MM-DD."),
expires_at: str = typer.Option(
..., "--expires-at", help="Expiry date, YYYY-MM-DD.", callback=_validate_date
),
standard_or_control: Optional[str] = typer.Option(
None, "--control", help="CCM domain code or standard clause this exception covers, e.g. IAM or 'SOC 2 CC6.1'."
),
Expand Down Expand Up @@ -689,7 +713,9 @@ def exception_show(exception_id: int) -> None:
def exception_update(
exception_id: int,
status: Optional[str] = typer.Option(None, "--status", help="active or revoked."),
expires_at: Optional[str] = typer.Option(None, "--expires-at"),
expires_at: Optional[str] = typer.Option(
None, "--expires-at", help="Expiry date, YYYY-MM-DD.", callback=_validate_date
),
justification: Optional[str] = typer.Option(None, "--justification"),
) -> None:
"""Update an exception -- e.g. revoke it, or extend its expiry."""
Expand Down Expand Up @@ -730,7 +756,9 @@ def miss_create(
None, "--project", "-p", help="Public GitHub repo URL or local directory. Defaults to the current directory."
),
title: str = typer.Option(..., "--title", help="What was found."),
discovered_at: str = typer.Option(..., "--discovered-at", help="When it was found, YYYY-MM-DD."),
discovered_at: str = typer.Option(
..., "--discovered-at", help="When it was found, YYYY-MM-DD.", callback=_validate_date
),
description: Optional[str] = typer.Option(None, "--description"),
discovered_by: Optional[str] = typer.Option(None, "--discovered-by", help="Who/what found it (incident, pen test, ...)."),
run: Optional[str] = typer.Option(None, "--run", help="Run UUID of the threat model that should have caught this, if known."),
Expand Down Expand Up @@ -821,7 +849,9 @@ def accept_create(
title: str = typer.Option(..., "--title", help="Short description of the threat being accepted."),
justification: str = typer.Option(..., "--justification", help="Why this risk is being accepted."),
accepted_by: str = typer.Option(..., "--accepted-by", help="Who is accepting this risk."),
expires_at: Optional[str] = typer.Option(None, "--expires-at", help="Optional review-by date, YYYY-MM-DD."),
expires_at: Optional[str] = typer.Option(
None, "--expires-at", help="Optional review-by date, YYYY-MM-DD.", callback=_validate_date
),
run: Optional[str] = typer.Option(None, "--run", help="Run UUID this threat came from, if known."),
) -> None:
"""Record a human risk acceptance for one threat from a Threat Register.
Expand Down Expand Up @@ -898,7 +928,9 @@ def accept_show(acceptance_id: int) -> None:
def accept_update(
acceptance_id: int,
status: Optional[str] = typer.Option(None, "--status", help="active or revoked."),
expires_at: Optional[str] = typer.Option(None, "--expires-at"),
expires_at: Optional[str] = typer.Option(
None, "--expires-at", help="Review-by date, YYYY-MM-DD.", callback=_validate_date
),
justification: Optional[str] = typer.Option(None, "--justification"),
) -> None:
"""Update a threat acceptance -- e.g. revoke it, or extend its review date."""
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli_accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ def test_accept_create_with_unknown_run_exits_nonzero(monkeypatch, tmp_path):
assert "No run found" in result.output


def test_accept_create_rejects_malformed_expires_at(monkeypatch, tmp_path):
result = _create(monkeypatch, tmp_path, expires_at="12/31/2027")
assert result.exit_code != 0
assert "not a valid date" in result.output

result = runner.invoke(app, ["accept", "list"])
assert "No threat acceptances recorded" in result.stdout


def test_accept_update_rejects_malformed_expires_at(monkeypatch, tmp_path):
_create(monkeypatch, tmp_path, expires_at="2099-01-01")
result = runner.invoke(app, ["accept", "update", "1", "--expires-at", "not-a-date"])
assert result.exit_code != 0
assert "not a valid date" in result.output

result = runner.invoke(app, ["accept", "show", "1"])
assert "Expires: 2099-01-01" in result.stdout


def test_accept_update_status(monkeypatch, tmp_path):
_create(monkeypatch, tmp_path)
result = runner.invoke(app, ["accept", "update", "1", "--status", "revoked"])
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ def test_exception_delete_unknown_exits_nonzero(monkeypatch, tmp_path):
assert result.exit_code == 1


def test_exception_create_rejects_malformed_expires_at(monkeypatch, tmp_path):
result = _create(monkeypatch, tmp_path, expires_at="not-a-date")
assert result.exit_code != 0
assert "not a valid date" in result.output

# Nothing should have been created.
result = runner.invoke(app, ["exception", "list"])
assert "No exceptions found" in result.stdout


def test_exception_update_rejects_malformed_expires_at(monkeypatch, tmp_path):
_create(monkeypatch, tmp_path)
result = runner.invoke(app, ["exception", "update", "1", "--expires-at", "31/12/2027"])
assert result.exit_code != 0
assert "not a valid date" in result.output

# The original (valid) expiry must be untouched.
result = runner.invoke(app, ["exception", "show", "1"])
assert "Expires: 2099-01-01" in result.stdout


def test_exception_list_filters_by_status(monkeypatch, tmp_path):
_create(monkeypatch, tmp_path, title="one")
_create(monkeypatch, tmp_path, title="two")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli_miss.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def test_miss_create_with_unknown_run_exits_nonzero(monkeypatch, tmp_path):
assert "No run found" in result.output


def test_miss_create_rejects_malformed_discovered_at(monkeypatch, tmp_path):
result = _create(monkeypatch, tmp_path, discovered_at="yesterday")
assert result.exit_code != 0
assert "not a valid date" in result.output

result = runner.invoke(app, ["miss", "list"])
assert "No post-build findings recorded" in result.stdout


def test_miss_delete(monkeypatch, tmp_path):
_create(monkeypatch, tmp_path)
result = runner.invoke(app, ["miss", "delete", "1"])
Expand Down
Loading