diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3599f56..9c36207 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,9 +3,16 @@ on: [push, pull_request] jobs: test: runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.10", "3.12"] steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 - with: {python-version: "3.12"} + with: + python-version: ${{ matrix.python-version }} - run: pip install -e ".[dev]" + - run: ruff check src tests + - run: ruff format --check src tests + - run: mypy src - run: pytest diff --git a/.gitignore b/.gitignore index 0a84c4b..73696bf 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ __pycache__/ dist/ build/ *.egg-info/ + +.codeproof/workspace/ diff --git a/README.md b/README.md index 19b927a..3cab90c 100644 --- a/README.md +++ b/README.md @@ -20,18 +20,74 @@ pip install -e ".[checkers]" codeproof scan . ``` + + + + ## Commands + ```bash -codeproof doctor -codeproof init -codeproof inventory . +codeproof tools # supported checks and execution behavior +codeproof doctor # availability and install guidance +codeproof init # write a default policy +codeproof inventory . # public symbols, not coverage codeproof generate-tests . --output tests/generated +codeproof scan . --format all --fail-under 70 --jobs 3 +codeproof scan . --config path/to/policy.yml --checks ruff,pytest,syntax +``` + +Reports are stored under `.codeproof/reports/`. Open `codeproof-report.html` for offline search, status filters, and expandable evidence. JSON and Markdown remain available. Paths are positional; the earlier `--path` option is also supported. Exit codes: **0** policy passed, **1** policy failed, **2** invalid usage or configuration. + +### Policy and additional tools + +Partial policies overlay the six default checks; explicitly disable any you do not want. A selected or configured check cannot bypass a required tool gate. Remove a tool from `required_tools` only when intentionally changing the policy. For a quick execution-free syntax-only scan, use: + +```yaml +# .codeproof.yml +required_tools: [] +source_paths: [src] +checks: + syntax: {enabled: true, weight: 10} +``` + +```bash +codeproof scan . --checks syntax +``` + +To add dependency auditing, install `pip install 'codeproof-ai[audit]'`, provide a root `requirements.txt`, and add `pip-audit: {enabled: true, weight: 15}` under `checks`. This opt-in tool queries vulnerability services and may invoke package tooling while resolving dependencies. Unsupported manifests are reported unavailable; the developer environment is never audited as a fallback. + +`jobs: 1` (default) runs serially; `--jobs 2` through `--jobs 8` allow concurrency. Tests and imports can share resources, so choose concurrency to suit the repository. Invalid thresholds, negative/nonfinite weights, unknown tools, and unknown policy keys are rejected. + +Generated tests check symbol exposure and execute imports; review them before running. Existing files are preserved unless `--force` is passed. See the [audit and prioritized roadmap](docs/roadmap.md) for delivered changes, compatibility notes, and follow-up work. + + + + + + + +## Developer workspace + +```bash +codeproof serve . +# Open http://127.0.0.1:8765/ +``` + +Run scans from a responsive browser workspace with four presets, durable history, searchable findings, report comparison, tool readiness and policy inspection. Export JSON, Markdown, HTML or SARIF. Required policy gates remain active. Browser scans require opt-in for code execution and network checks. History keeps the latest 50 runs locally. + +```bash +codeproof scan . --format sarif +``` + +Read the [workspace guide and use cases](docs/developer-workspace.md) for setup, workflows, storage and deployment boundaries. This is a local single-user workspace. codeproof scan . --format all --fail-under 70 codeproof gui . --host 127.0.0.1 --port 8787 ``` Reports are stored under `.codeproof/reports/`. The GUI runs locally and uses the same core engine/policy as the CLI. See `ROADMAP.md` for planned enhancements. ## Confidence model + + The score is the weighted percentage of included checks that pass. Missing required tools fail policy. Missing optional tools are marked unavailable and excluded from the denominator. A passing report is evidence that configured checks passed, not proof of correctness. ## Security diff --git a/action.yml b/action.yml index c6d49dc..2cb0ea1 100644 --- a/action.yml +++ b/action.yml @@ -3,13 +3,23 @@ description: Evidence-backed Python repository verification inputs: path: {description: Repository path, required: false, default: .} fail-under: {description: Minimum score, required: false, default: "70"} + jobs: {description: Concurrent check workers (1-8), required: false, default: "1"} runs: using: composite steps: - shell: bash - run: python -m pip install "${{ github.action_path }}[checkers]" + env: + CODEPROOF_ACTION_PATH: ${{ github.action_path }} + run: python -m pip install "${CODEPROOF_ACTION_PATH}[checkers]" - shell: bash - run: codeproof scan "${{ inputs.path }}" --fail-under "${{ inputs.fail-under }}" + env: + CODEPROOF_TARGET: ${{ inputs.path }} + CODEPROOF_THRESHOLD: ${{ inputs.fail-under }} + CODEPROOF_JOBS: ${{ inputs.jobs }} + run: codeproof scan "$CODEPROOF_TARGET" --fail-under "$CODEPROOF_THRESHOLD" --jobs "$CODEPROOF_JOBS" - if: always() uses: actions/upload-artifact@v4 - with: {name: codeproof-report, path: "${{ inputs.path }}/.codeproof/"} + with: + name: codeproof-report + path: "${{ inputs.path }}/.codeproof/reports/" + include-hidden-files: true diff --git a/docs/developer-workspace.md b/docs/developer-workspace.md new file mode 100644 index 0000000..c38238c --- /dev/null +++ b/docs/developer-workspace.md @@ -0,0 +1,59 @@ + + +# CodeProof Developer Workspace + +## Start here + +```bash +pip install -e ".[checkers]" +codeproof serve . +``` + +Open **http://127.0.0.1:8765/**. The same repository-scoped engine powers the browser, CLI and GitHub Action. The workspace uses Python's standard library HTTP server and SQLite; there is no frontend build step or new service dependency. All CSS, scripts and fonts are local. Dates follow the browser locale and stored timestamps use UTC. + +Use `codeproof doctor` to check availability. Add `pip install 'codeproof-ai[audit]'` for dependency auditing and a root requirements.txt. The CLI and existing static reports remain supported. `--format all` still generates the original JSON, Markdown and HTML files; `--format sarif` explicitly generates codeproof-report.sarif. + +## Developer workflows and product use cases + +| Audience / problem | Workflow | Evidence delivered | +|---|---|---| +| Python developers catching errors before commit | Fast feedback preset: syntax, Ruff and mypy plus required gates | Tool statuses, structured findings, file locations and raw output | +| Reviewers evaluating a pull request | Code quality preset: syntax, lint, types, pytest and Bandit plus required gates | Weighted score, required-check result, downloadable report and SARIF | +| Maintainers tracking regressions | Scan history and baseline/current comparison | Score difference and new/resolved normalized findings, with a warning if check sets/weights/thresholds differ | +| Security champions triaging Python code | Security preset: syntax, Bandit, Semgrep and pip-audit plus required gates | Security-pattern and dependency findings; unavailable tools remain visible | +| Release engineers applying shared standards | Repository policy preset and Quality policy page | The existing policy, explicit threshold, required gates and repeatable scan evidence | +| New contributors learning a repository's quality workflow | Tool readiness, installation guidance, use-case guides and downloadable YAML | Clear setup steps and one scan dialog instead of remembering CLI options | +| Teams moving evidence between CI tools | Export JSON, Markdown, HTML or SARIF 2.1.0 | Portable snapshots; SARIF exports normalized findings only, not raw-only diagnostics | + +The product is a Python verification workspace. It does not write application code, prove correctness, measure test coverage, offer an AI chat model, or certify compliance. Generated tests in the CLI check symbol exposure and should be reviewed before running. + +## Interface and behavior + +- Repository overview: actual score, check counts, findings and policy outcome; no simulated statistics. +- Quality trend: latest 12 completed runs with scores and timestamps. Different presets can measure different check sets; compare their configurations before treating a trend as improvement. +- Findings explorer: search message/rule/file, filter by tool/severity, and page through 25 rows at a time. Check evidence includes commands, structured findings, diagnostics and execution time. +- Scan dialog: four presets, threshold 0–100, workers 1–8, and explicit code-execution/network opt-ins. Existing required checks are included and disabled required checks still fail policy. Mypy plugins and package resolution can execute code, so their profiles require execution consent. +- Run history: statuses include running, complete, error and interrupted; a failing policy is a completed scan with a failed policy outcome. Reports survive application restarts. +- Comparison: tool, rule, path, line and message identify a normalized finding. Location shifts may count as new/resolved. Up to 100 changes per group are rendered. Raw-only errors are outside finding comparison. Complete environment/rule snapshots are not yet recorded, so identical check sets do not establish identical execution conditions. +- Toolchain, policy downloads and command helpers support onboarding and terminal/CI use. Browser changes apply only to that scan and never overwrite .codeproof.yml. +- Responsive navigation, native dialogs, keyboard focus styles, live status messaging, locale-sensitive timestamps and no external asset requests keep the app useful across devices and networks. + +## Local scale and storage + +History is stored in `.codeproof/workspace/history.sqlite3`, ignored by Git. The oldest run is deleted when admitting a run beyond the 50-run retention limit. Keep exported reports elsewhere if longer retention is needed. History reads summary columns rather than loading every report payload. Browser caching is capped at 50 reports. Check execution uses the existing bounded pool of up to eight workers. + +One active scan is admitted per server process; further submissions receive HTTP 409. There is no unbounded execution queue. Run one workspace server per repository. A restart marks unfinished records interrupted; it does not resume or cancel orphan subprocess trees. Existing per-tool timeouts still apply. Subprocess output is still buffered in memory before truncation; large-repository and load benchmarks remain follow-up work. No throughput improvement or distributed execution is claimed. + +## Access and deployment + +The default bind address is loopback. The API pins the repository at startup; requests cannot supply arbitrary target paths or shell commands. Mutations require a per-session token, matching Origin when present, and a permitted Host. Downloads are attachments and untrusted report text is escaped. Requests are limited to 64 KB and connections have a 15-second timeout. Assets use a restrictive content-security policy. + +For a trusted development reverse proxy, specify its exact hostname using `--allowed-host` and use `--host 0.0.0.0` only when that proxy is appropriately restricted. The workspace token prevents cross-site scan submission; it is not user authentication. This release is a local single-user app. Public team deployment requires user authentication, authorization, isolated execution workers, shared storage/queues, quotas, observability and a separate deployment review. + +## Product expansion priorities + +1. Validate adoption with the four developer workflows, improve onboarding from real feedback, and add richer normalization for pytest and mypy. +2. Add report provenance for source revision, tool versions and effective policy; improve comparison equivalence checks and configure retention. +3. Add measured process/output limits and process-tree cancellation before raising concurrency or introducing incremental caching. +4. Add localization using an explicit translation catalog and validate keyboard/screen-reader workflows; English and browser-locale date formatting are delivered now. +5. Build a hosted team edition only after defining tenant isolation, identity, billing, data residency and operating budgets. Corporate visual design alone does not establish enterprise security or compliance. diff --git a/docs/roadmap.md b/docs/roadmap.md new file mode 100644 index 0000000..7b10c8a --- /dev/null +++ b/docs/roadmap.md @@ -0,0 +1,58 @@ + + +# CodeProof AI — repository audit and delivery roadmap + +## Product direction + +CodeProof is a local Python CLI and reusable GitHub Action. Its strongest use cases are developer preflight checks, CI policy gates, and portable evidence for code review. Keep the local CLI as the execution engine and make its reports useful to maintainers before introducing a hosted product. A score summarizes configured checks; it cannot establish correctness or replace behavioral tests. + +Audit baseline: `7043a711daefaed5a2f973d51ec856a5ef00d703`. The initial package had six adapters, a weighted score, an AST symbol inventory, exposure-test generation, three report formats, and two tests. There was no application server, database, hosted account system, or test-coverage analysis. The inventory should not be described as identifying untested behavior. + +## Audit findings and delivered repairs + +| Priority | Finding and evidence | Delivered improvement | +|---|---|---| +| P0 | `core.py` checked required gates only for executed results; disabling or omitting one bypassed its gate | Emit required skipped results and fail policy; no weighted evidence cannot pass, including at threshold zero | +| P0 | `config.py` accepted invalid weights/timeouts and replaced all default checks when a partial file existed | Validate policy ranges, unknown settings/tools and version; merge partial overrides with defaults | +| P0 | `action.yml` interpolated inputs directly into shell scripts | Pass inputs through quoted environment variables; upload report artifacts including the hidden report directory | +| P0 | Semgrep ran without `--error`, permitting successful exit despite findings | Findings now produce a failed check; this may lower previously misleading scores | +| P1 | Documented positional CLI paths were interpreted as options | Support documented positional paths and retain the existing `--path` option for compatibility | +| P1 | `adapters.py` returned almost exclusively raw output and import timeouts escaped the adapter | Normalize Ruff/Bandit/Semgrep/pip-audit findings; retain raw diagnostics; contain launch and timeout errors; enforce total import time budget | +| P1 | All adapters ran serially | Configurable worker pool (1–8), stable result order; serial remains default | +| P1 | HTML had only an unstyled summary table | Offline responsive report, policy explanation, search, status filters, expandable findings and raw evidence; keyboard focus styles and live count | +| P1 | No dependency vulnerability or execution-free syntax tools | Opt-in `pip-audit` for root requirements.txt; built-in `syntax` without execution or bytecode writes; discoverable tools and install guidance | +| P1 | Test generation wrote only the first symbol per module without force, duplicated on force, and mishandled package imports | Group symbols by module, replace once on force, preserve existing files, handle package exports and avoid module filename collisions | +| P1 | File discovery descended into environments and caches | Shared deterministic traversal prunes common generated trees, skips symlinks and deduplicates overlapping roots | +| P1 | Only grade and total weights had tests | Regression coverage for policy gates, config, parallel execution, adapter errors, structured evidence, generation, escaping and CLI workflows | + +## Compatibility and intentional changes + +- Existing six default checks and their weights (100 total) remain. Both new tools are opt-in; all report filenames and JSON schema 1.0 fields remain. Existing `--path` callers remain supported. +- A partial YAML policy now overlays defaults. Previously, specifying only `fail_under` could run no checks. To run a smaller set, disable the other checks explicitly or use `--checks`; remove a tool from `required_tools` only when that policy change is intended. +- Disabled or absent required checks fail policy, matching the README's required-tool guarantee. Empty evidence fails even if the threshold is zero. Invalid or misspelled policies now fail with CLI exit code 2. +- Semgrep findings now fail the check. Optional unavailable checks remain excluded from the score; required unavailable checks block policy. +- Generated filenames now use length-prefixed module components to avoid collisions. Existing generated files are preserved; review and remove obsolete files before regenerating into the same directory. +- Concurrent execution is opt-in because tests and imports execute repository code and may share resources. Each tool has a time budget; concurrency is not a sandbox or a process/memory quota. + +## Prioritized roadmap + +| Phase | Work | Exit criteria | Effort | +|---|---|---|---| +| 1 — Delivered | Policy integrity, actionable reports, opt-in tools, bounded workers, generation repairs and regression tests | Local CLI and Action retain compatibility; policy bypass regressions pass; report controls work on desktop and mobile | This change | +| 2 — CI adoption | SARIF export and GitHub annotations; machine-readable doctor; configurable local Semgrep rules; lockfile support for pip-audit; supported Python-version matrix | SARIF validates against its schema; annotations point to source lines; offline configuration completes without rule downloads; locked dependency fixtures yield stable findings | M | +| 3 — Faster large repositories | Benchmarked discovery, streamed/limited subprocess output, process-tree cancellation, bounded findings, incremental cache | Representative small/medium/large fixtures publish timings; no orphan processes after timeout; cache keys include source, config, dependency locks, tool/Python versions; changed inputs invalidate results | L | +| 4 — Stronger evidence | Real pytest counts and coverage ingestion; mypy normalization; severity-aware policy; changed-code baselines; versioned report schema | Inventory remains distinct from coverage; original evidence retained; high-severity gates cannot be offset by unrelated passes; schema migration and consumer contract tests pass | L | +| 5 — Team workflows | Report history and comparison; reviewable policy templates; distributable static viewer; accessible navigation/contrast audit | Compare two saved reports without rerunning code; policy changes show a clear diff; keyboard and screen-reader checks pass; report artifacts require no external assets | M | + +M and L are relative planning estimates, not delivery dates. Phase 2 should precede persistent history or a hosted service. Hosted authentication, queues, object storage and multi-tenant execution need separate product demand and a security design; this repository does not currently contain those facilities. + +## Engineering tools and delivery discipline + +- Use Ruff format/lint, mypy with PyYAML stubs, pytest regression tests, and Bandit for changes to process execution and reporting. Keep dependency auditing as an explicit extra rather than a mandatory network dependency for every local scan. +- Add packaging smoke tests so the HTML template is verified inside built wheels. Pin a reproducible CI dependency set in the next phase and document an update cadence. +- Measure p50/p95 scan time, peak memory, adapter error rates, policy pass rates with missing tools, and report size on synthetic fixtures before implementing caching or claiming speedups. Worker parallelism is delivered; no benchmarked speedup is claimed. +- Preserve the schema and exit-code contract (0 pass, 1 policy fail, 2 usage/config error). Add integration coverage for supported checker versions and hostile JSON/text fixtures. + +## Remaining limits + +Dependency auditing currently supports only a root requirements.txt and requires the optional pip-audit executable and network access. Resolution can invoke package build tooling; use isolated runners for untrusted requirements. Semgrep auto may retrieve rules. Tool processes still capture output in memory before retaining the final 50 KB; nested child processes are not comprehensively cancelled. The import adapter skips standalone package `__init__.py` files, as before. Syntax checks do not establish runtime importability, and generated tests only check symbol exposure. Reports are snapshots; filters never alter the policy score. Live GitHub Action execution requires pushing and running the workflow. Broader minimum-Python and accessibility coverage remain roadmap work. diff --git a/pyproject.toml b/pyproject.toml index ced11d2..39e2abb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,8 @@ license={text="MIT"} dependencies=["typer>=0.12,<1","rich>=13.7,<15","pydantic>=2.7,<3","PyYAML>=6,<7"] [project.optional-dependencies] checkers=["pytest>=8,<10","ruff>=0.6,<1","mypy>=1.11,<2","bandit>=1.7,<2","semgrep>=1.80,<2"] -dev=["codeproof-ai[checkers]"] +audit=["pip-audit>=2.7,<3"] +dev=["codeproof-ai[checkers]", "types-PyYAML>=6,<7"] [project.scripts] codeproof="codeproof.cli:app" [tool.hatch.build.targets.wheel] @@ -23,3 +24,8 @@ packages=["src/codeproof"] [tool.pytest.ini_options] testpaths=["tests"] +[tool.ruff] +target-version = "py310" + +[tool.ruff.lint] +select = ["E4", "E7", "E9", "F", "I", "B", "UP"] diff --git a/src/codeproof/__init__.py b/src/codeproof/__init__.py index 40692a7..3dc1f76 100644 --- a/src/codeproof/__init__.py +++ b/src/codeproof/__init__.py @@ -1 +1 @@ -__version__="0.1.0" +__version__ = "0.1.0" diff --git a/src/codeproof/adapters.py b/src/codeproof/adapters.py index 0224e9b..42f5d02 100644 --- a/src/codeproof/adapters.py +++ b/src/codeproof/adapters.py @@ -1,25 +1,258 @@ -import json,os,shutil,subprocess,sys,time +"""Tool boundaries: normalize findings and contain process failures.""" + +import json +import os +import shutil +import subprocess +import sys +import time from pathlib import Path -from .models import CheckResult,Finding,Status -def run(name:str,root:Path,cfg:str|None,timeout:int,sources:list[str],tests:list[str])->CheckResult: - src=[x for x in sources if (root/x).exists()] or ["."] - commands={"ruff":["ruff","check",*src,"--output-format","json"],"mypy":["mypy",*src,"--no-error-summary"],"pytest":["pytest",*([x for x in tests if (root/x).exists()] or ["."]),"-q"],"bandit":["bandit","-r",*src,"-f","json","-q"],"semgrep":["semgrep","scan","--config",cfg or "auto","--json",*src]} - if name=="imports": return imports(root,src,timeout) - cmd=commands.get(name) - if not cmd:return CheckResult(name=name,status=Status.skipped,summary="unknown adapter") - if not shutil.which(cmd[0]):return CheckResult(name=name,status=Status.unavailable,command=cmd,summary=f"{cmd[0]} is not installed") - started=time.perf_counter() + +from .files import python_files +from .models import CheckResult, Finding, Status + + +def parse_findings(name: str, output: str) -> list[Finding]: + """Retain raw evidence if a tool emits text or an unfamiliar JSON schema.""" try: - cp=subprocess.run(cmd,cwd=root,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,timeout=timeout,env={**os.environ,"NO_COLOR":"1"}) - return CheckResult(name=name,status=Status.passed if cp.returncode==0 else Status.failed,command=cmd,duration_seconds=round(time.perf_counter()-started,3),exit_code=cp.returncode,summary="passed" if cp.returncode==0 else "reported findings",raw_output=(cp.stdout or "")[-50000:]) - except subprocess.TimeoutExpired as e:return CheckResult(name=name,status=Status.error,command=cmd,summary=f"timed out after {timeout}s") -def imports(root:Path,sources:list[str],timeout:int)->CheckResult: - started=time.perf_counter(); findings=[]; count=0 - for src in sources: - base=root/src - for p in base.rglob("*.py") if base.exists() else []: - if p.name=="__init__.py":continue - module=".".join(p.relative_to(base).with_suffix("").parts); count+=1 - cp=subprocess.run([sys.executable,"-c",f"import sys;sys.path.insert(0,{str(base)!r});__import__({module!r})"],cwd=root,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,timeout=timeout) - if cp.returncode:findings.append(Finding(level="error",message=(cp.stdout or "failed")[-800:],path=str(p.relative_to(root)),rule="import")) - return CheckResult(name="imports",status=Status.passed if not findings else Status.failed,duration_seconds=round(time.perf_counter()-started,3),summary=f"{count} modules checked; {len(findings)} failed",findings=findings,evidence={"modules_checked":count}) + data = json.loads(output) + if name == "ruff": + return [ + Finding( + level="warning", + message=item["message"], + path=item.get("filename"), + line=item.get("location", {}).get("row"), + rule=item.get("code"), + ) + for item in data + ] + if name == "bandit": + return [ + Finding( + level=item.get("issue_severity", "warning").lower(), + message=item["issue_text"], + path=item.get("filename"), + line=item.get("line_number"), + rule=item.get("test_id"), + ) + for item in data.get("results", []) + ] + if name == "semgrep": + return [ + Finding( + level=item["extra"].get("severity", "warning").lower(), + message=item["extra"]["message"], + path=item.get("path"), + line=item.get("start", {}).get("line"), + rule=item.get("check_id"), + ) + for item in data.get("results", []) + ] + if name == "pip-audit": + return [ + Finding( + level="warning", + message=f"{dep['name']} {dep['version']}: {vuln.get('description', vuln['id'])}", + rule=vuln["id"], + path="requirements.txt", + ) + for dep in data.get("dependencies", []) + for vuln in dep.get("vulns", []) + ] + except (ValueError, KeyError, TypeError, AttributeError): + pass + return [] + + +def run( + name: str, + root: Path, + cfg: str | None, + timeout: int, + sources: list[str], + tests: list[str], +) -> CheckResult: + if name == "syntax": + return syntax(root, sources) + if name == "imports": + return imports(root, sources, timeout) + src = [x for x in sources if (root / x).exists()] or ["."] + commands = { + "ruff": ["ruff", "check", *src, "--output-format", "json"], + "mypy": ["mypy", *src, "--no-error-summary"], + "pytest": [ + "pytest", + *([x for x in tests if (root / x).exists()] or ["."]), + "-q", + ], + "bandit": ["bandit", "-r", *src, "-f", "json", "-q"], + "semgrep": [ + "semgrep", + "scan", + "--config", + cfg or "auto", + "--json", + "--error", + *src, + ], + "pip-audit": [ + "pip-audit", + "-r", + "requirements.txt", + "--format", + "json", + "--progress-spinner", + "off", + ], + } + cmd = commands.get(name) + if not cmd: + return CheckResult(name=name, status=Status.skipped, summary="unknown adapter") + if name == "pip-audit" and not (root / "requirements.txt").is_file(): + return CheckResult( + name=name, + status=Status.unavailable, + summary="Requires requirements.txt at the project root; no environment audit performed", + ) + if not shutil.which(cmd[0]): + return CheckResult( + name=name, + status=Status.unavailable, + command=cmd, + summary=f"{cmd[0]} is not installed; install codeproof-ai[{'audit' if name == 'pip-audit' else 'checkers'}]", + ) + started = time.perf_counter() + try: + cp = subprocess.run( + cmd, + check=False, + cwd=root, + text=True, + errors="replace", + capture_output=True, + timeout=timeout, + env={**os.environ, "NO_COLOR": "1"}, + ) + findings = parse_findings(name, cp.stdout or "") + return CheckResult( + name=name, + status=Status.passed if cp.returncode == 0 else Status.failed, + command=cmd, + duration_seconds=round(time.perf_counter() - started, 3), + exit_code=cp.returncode, + summary=f"{len(findings)} findings" + if findings + else ( + "passed" + if cp.returncode == 0 + else "Check failed; expand output for details" + ), + findings=findings, + raw_output=((cp.stdout or "") + ("\n" + cp.stderr if cp.stderr else ""))[ + -50000: + ], + ) + except (subprocess.TimeoutExpired, OSError) as exc: + return CheckResult( + name=name, + status=Status.error, + command=cmd, + duration_seconds=round(time.perf_counter() - started, 3), + summary=f"timed out after {timeout}s" + if isinstance(exc, subprocess.TimeoutExpired) + else str(exc), + ) + + +def syntax(root: Path, sources: list[str]) -> CheckResult: + started = time.perf_counter() + findings: list[Finding] = [] + count = 0 + for path, _ in python_files(root, sources): + count += 1 + try: + compile(path.read_bytes(), str(path), "exec") + except (SyntaxError, UnicodeError, OSError) as exc: + findings.append( + Finding( + level="error", + message=str(exc), + path=str(path.relative_to(root)), + line=getattr(exc, "lineno", None), + rule="syntax", + ) + ) + return CheckResult( + name="syntax", + status=Status.failed + if findings + else (Status.passed if count else Status.skipped), + duration_seconds=round(time.perf_counter() - started, 3), + summary=f"{count} files compiled; {len(findings)} errors", + findings=findings, + evidence={"files_checked": count}, + ) + + +def imports(root: Path, sources: list[str], timeout: int) -> CheckResult: + started = time.perf_counter() + findings: list[Finding] = [] + count = 0 + for path, base in python_files(root, sources): + if path.name == "__init__.py": + continue + remaining = timeout - (time.perf_counter() - started) + if remaining <= 0: + return CheckResult( + name="imports", + status=Status.error, + summary=f"timed out after {timeout}s", + findings=findings, + ) + module = ".".join(path.relative_to(base).with_suffix("").parts) + count += 1 + try: + cp = subprocess.run( + [ + sys.executable, + "-c", + f"import sys;sys.path.insert(0,{str(base)!r});__import__({module!r})", + ], + check=False, + cwd=root, + text=True, + errors="replace", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + timeout=remaining, + ) + except (subprocess.TimeoutExpired, OSError) as exc: + return CheckResult( + name="imports", + status=Status.error, + duration_seconds=round(time.perf_counter() - started, 3), + findings=findings, + summary=f"timed out after {timeout}s" + if isinstance(exc, subprocess.TimeoutExpired) + else str(exc), + ) + if cp.returncode: + findings.append( + Finding( + level="error", + message=(cp.stdout or "failed")[-800:], + path=str(path.relative_to(root)), + rule="import", + ) + ) + return CheckResult( + name="imports", + status=Status.failed + if findings + else (Status.passed if count else Status.skipped), + duration_seconds=round(time.perf_counter() - started, 3), + summary=f"{count} modules checked; {len(findings)} failed", + findings=findings, + evidence={"modules_checked": count}, + ) diff --git a/src/codeproof/cli.py b/src/codeproof/cli.py index 34f8fc4..f4d1831 100644 --- a/src/codeproof/cli.py +++ b/src/codeproof/cli.py @@ -1,40 +1,198 @@ +import shutil +from enum import Enum from pathlib import Path -import shutil,typer,yaml +from typing import Annotated + +import typer +import yaml from rich.console import Console from rich.table import Table -from .config import load,DEFAULT + +from .config import DEFAULT, TOOL_INFO, CheckConfig, load from .core import scan as execute -from .discovery import symbols,generate +from .discovery import generate, symbols from .reporting import write -app=typer.Typer(no_args_is_help=True);console=Console() + +app = typer.Typer( + no_args_is_help=True, + help="Evidence-backed Python repository checks. Start with doctor or init.", +) +console = Console() + + +class Format(str, Enum): + all = "all" + json = "json" + markdown = "markdown" + html = "html" + sarif = "sarif" + + +def policy(root: Path, config: Path | None = None): + if not root.is_dir(): + raise typer.BadParameter(f"Repository directory does not exist: {root}") + try: + return load(root, config) + except (ValueError, TypeError, OSError, yaml.YAMLError) as exc: + raise typer.BadParameter(f"Invalid policy: {exc}") from exc + + +@app.command() +def tools(): + """List supported tools, execution behavior, and installation extras.""" + table = Table("Tool", "Purpose", "Behavior", "Install extra") + for name, (purpose, description, extra) in TOOL_INFO.items(): + table.add_row(name, purpose, description, extra or "built in") + console.print(table) + + @app.command() def doctor(): - t=Table("Tool","Status") - for n in ["ruff","mypy","pytest","bandit","semgrep"]:t.add_row(n,"available" if shutil.which(n) else "missing") - console.print(t) + """Check tool availability and show how to install missing tools.""" + table = Table("Tool", "Status", "Next step") + for name, (_, _, extra) in TOOL_INFO.items(): + available = extra is None or bool(shutil.which(name)) + table.add_row( + name, + "available" if available else "missing", + "Ready" if available else f"pip install 'codeproof-ai[{extra}]'", + ) + console.print(table) + console.print( + "Dependency auditing needs requirements.txt. Semgrep auto and pip-audit use the network." + ) + + @app.command() -def init(path:Path=Path("."),force:bool=False): - p=path.resolve()/".codeproof.yml" - if p.exists() and not force:raise typer.BadParameter("policy exists; use --force") - p.write_text(yaml.safe_dump(DEFAULT.model_dump(mode="json"),sort_keys=False));console.print(f"Created {p}") +def init( + path: Annotated[Path, typer.Argument()] = Path("."), + force: bool = False, + path_option: Annotated[Path | None, typer.Option("--path", hidden=True)] = None, +): + """Create a commented policy without overwriting an existing one.""" + root = (path_option or path).resolve() + if not root.is_dir(): + raise typer.BadParameter(f"Repository directory does not exist: {root}") + dest = root / ".codeproof.yml" + if dest.exists() and not force: + raise typer.BadParameter("policy exists; use --force") + dest.write_text( + "# Weights are relative. Required checks must pass even with weight 0.\n" + "# Add syntax or pip-audit under checks to opt in; jobs: 1 is serial.\n" + + yaml.safe_dump(DEFAULT.model_dump(mode="json"), sort_keys=False), + encoding="utf-8", + ) + console.print(f"Created {dest}") + + @app.command() -def inventory(path:Path=Path(".")): - root=path.resolve();cfg=load(root);t=Table("Symbol","File","Line") - data=symbols(root,cfg.source_paths) - for n,p,l in data:t.add_row(n,str(p),str(l)) - console.print(t);console.print(f"{len(data)} public symbols") +def inventory( + path: Annotated[Path, typer.Argument()] = Path("."), + path_option: Annotated[Path | None, typer.Option("--path", hidden=True)] = None, +): + """List public top-level symbols; this is an inventory, not coverage analysis.""" + root = (path_option or path).resolve() + cfg = policy(root) + table = Table("Symbol", "File", "Line") + data = symbols(root, cfg.source_paths) + for name, file, line in data: + table.add_row(name, str(file), str(line)) + console.print(table) + console.print(f"{len(data)} public symbols") + + @app.command("generate-tests") -def generate_tests(path:Path=Path("."),output:Path=Path("tests/generated"),force:bool=False): - root=path.resolve();cfg=load(root);made=generate(root,cfg.source_paths,root/output,force);console.print(f"Created {len(made)} files") +def generate_tests( + path: Annotated[Path, typer.Argument()] = Path("."), + path_option: Annotated[Path | None, typer.Option("--path", hidden=True)] = None, + output: Path = Path("tests/generated"), + force: bool = False, +): + """Generate reviewable exposure checks. These are not behavioral tests.""" + root = (path_option or path).resolve() + cfg = policy(root) + made = generate(root, cfg.source_paths, root / output, force) + console.print( + f"Created {len(made)} files; review before running (imports execute code)." + ) + + @app.command() -def scan(path:Path=Path("."),format:str="all",fail_under:float|None=None,output:Path=Path(".codeproof/reports")): - if format not in {"all","json","markdown","html"}:raise typer.BadParameter("invalid format") - root=path.resolve();report=execute(root,load(root),fail_under);t=Table("Check","Status","Weight","Summary") - for r in report.results:t.add_row(r.name,r.status.value,str(r.weight),r.summary) - console.print(t);console.print(f"Score {report.score}/100 ({report.grade}); policy {'PASS' if report.policy_passed else 'FAIL'}") - for p in write(report,root/output,format):console.print(f"Report: {p}") +def scan( + path: Annotated[Path, typer.Argument()] = Path("."), + path_option: Annotated[Path | None, typer.Option("--path", hidden=True)] = None, + format: Format = Format.all, + fail_under: float | None = typer.Option(None, min=0, max=100), + output: Path = Path(".codeproof/reports"), + config: Path | None = None, + jobs: int | None = typer.Option(None, min=1, max=8), + checks: str | None = typer.Option( + None, help="Comma-separated tools to run. Required tools remain policy gates." + ), +): + """Run checks and write reports. Exit 0: policy passes; 1: fails; 2: invalid input.""" + root = (path_option or path).resolve() + cfg = policy(root, config) + if checks is not None: + selected = {name.strip() for name in checks.split(",") if name.strip()} + if not selected or selected - TOOL_INFO.keys(): + raise typer.BadParameter( + "--checks must name supported tools; see codeproof tools" + ) + for name in sorted(selected): + cfg.checks.setdefault(name, CheckConfig(weight=10)) + for name, check in cfg.checks.items(): + check.enabled = name in selected + with console.status("Running repository checks…"): + report = execute(root, cfg, fail_under, jobs) + table = Table("Check", "Status", "Required", "Time", "Summary") + for result in report.results: + table.add_row( + result.name, + result.status.value, + "yes" if result.required else "no", + f"{result.duration_seconds:.2f}s", + result.summary, + ) + console.print(table) + console.print( + f"Score {report.score}/100 ({report.grade}); policy {'PASS' if report.policy_passed else 'FAIL'} (threshold {report.fail_under:g})" + ) + for dest in write(report, root / output, format.value): + console.print(f"Report: {dest}") raise typer.Exit(0 if report.policy_passed else 1) + +@app.command() +def serve( + path: Annotated[Path, typer.Argument()] = Path("."), + host: str = "127.0.0.1", + port: int = typer.Option(8765, min=1, max=65535), + allowed_host: str | None = typer.Option( + None, help="Additional hostname for a trusted local reverse proxy" + ), +): + """Open the local developer workspace with scan history and findings.""" + from .workspace import Workspace, make_server + + root = path.resolve() + policy(root) + workspace = Workspace(root) + try: + server = make_server(workspace, host, port, allowed_host) + except OSError as exc: + raise typer.BadParameter(str(exc)) from exc + console.print(f"CodeProof workspace: http://{host}:{port} · {root.name}") + try: + server.serve_forever() + except KeyboardInterrupt: + pass + finally: + server.server_close() + + +if __name__ == "__main__": + app() @app.command() def gui(path:Path=Path("."),host:str="127.0.0.1",port:int=8787,no_open:bool=False): """Launch the local CodeProof GUI dashboard.""" diff --git a/src/codeproof/config.py b/src/codeproof/config.py index e13d346..a307bbb 100644 --- a/src/codeproof/config.py +++ b/src/codeproof/config.py @@ -1,11 +1,95 @@ +"""Validated scan policies, with partial files layered over safe defaults.""" + from pathlib import Path +from typing import Literal + import yaml -from pydantic import BaseModel,Field +from pydantic import BaseModel, ConfigDict, Field, model_validator + +TOOL_INFO = { + "imports": ("Import smoke", "Executes each source module in a subprocess", None), + "ruff": ("Lint", "Fast Python lint checks", "checkers"), + "mypy": ("Types", "Static type analysis", "checkers"), + "pytest": ("Tests", "Executes the repository test suite", "checkers"), + "bandit": ("Security", "Python security analysis", "checkers"), + "semgrep": ( + "Patterns", + "Security rules (auto rules may use the network)", + "checkers", + ), + "syntax": ( + "Syntax", + "Compiles source without executing it or writing bytecode", + None, + ), + "pip-audit": ( + "Dependencies", + "Audits requirements.txt against vulnerability services", + "audit", + ), +} + + class CheckConfig(BaseModel): - enabled:bool=True; weight:float=1; config:str|None=None + model_config = ConfigDict(extra="forbid") + enabled: bool = True + weight: float = Field(default=1, ge=0, allow_inf_nan=False) + config: str | None = None + + class Config(BaseModel): - version:int=1; fail_under:float=70; source_paths:list[str]=Field(default_factory=lambda:["src"]); test_paths:list[str]=Field(default_factory=lambda:["tests"]); timeout_seconds:int=300; required_tools:list[str]=Field(default_factory=lambda:["ruff","pytest"]); checks:dict[str,CheckConfig]=Field(default_factory=dict) -DEFAULT=Config(checks={"imports":CheckConfig(weight=10),"ruff":CheckConfig(weight=15),"mypy":CheckConfig(weight=15),"pytest":CheckConfig(weight=30),"bandit":CheckConfig(weight=15),"semgrep":CheckConfig(weight=15,config="auto")}) -def load(root:Path, explicit:Path|None=None)->Config: - p=explicit or root/".codeproof.yml" - return DEFAULT.model_copy(deep=True) if not p.exists() else Config.model_validate(yaml.safe_load(p.read_text()) or {}) + model_config = ConfigDict(extra="forbid") + version: Literal[1] = 1 + fail_under: float = Field(default=70, ge=0, le=100, allow_inf_nan=False) + source_paths: list[str] = Field(default_factory=lambda: ["src"]) + test_paths: list[str] = Field(default_factory=lambda: ["tests"]) + timeout_seconds: int = Field(default=300, ge=1) + jobs: int = Field(default=1, ge=1, le=8) + required_tools: list[str] = Field(default_factory=lambda: ["ruff", "pytest"]) + checks: dict[str, CheckConfig] = Field(default_factory=dict) + + @model_validator(mode="after") + def valid_policy(self): + unknown = (set(self.checks) | set(self.required_tools)) - TOOL_INFO.keys() + if unknown: + raise ValueError("Unknown tools: " + ", ".join(sorted(unknown))) + if not self.source_paths or not self.test_paths: + raise ValueError("source_paths and test_paths must not be empty") + if any(not path.strip() for path in self.source_paths + self.test_paths): + raise ValueError("Source and test paths must not be blank") + return self + + +DEFAULT = Config( + checks={ + "imports": CheckConfig(weight=10), + "ruff": CheckConfig(weight=15), + "mypy": CheckConfig(weight=15), + "pytest": CheckConfig(weight=30), + "bandit": CheckConfig(weight=15), + "semgrep": CheckConfig(weight=15, config="auto"), + } +) + + +def load(root: Path, explicit: Path | None = None) -> Config: + path = explicit if explicit is not None else root / ".codeproof.yml" + if not path.exists(): + if explicit is not None: + raise ValueError(f"Policy file not found: {path}") + return DEFAULT.model_copy(deep=True) + data = yaml.safe_load(path.read_text(encoding="utf-8")) + if data is None: + data = {} + if not isinstance(data, dict): + raise TypeError("Policy must be a YAML mapping") + defaults = DEFAULT.model_dump() + overrides = data.get("checks", {}) + if not isinstance(overrides, dict): + raise TypeError("checks must be a YAML mapping") + checks = defaults["checks"] + for name, settings in overrides.items(): + if not isinstance(settings, dict): + raise TypeError(f"Check {name} must be a YAML mapping") + checks[name] = {**checks.get(name, {}), **settings} + return Config.model_validate({**defaults, **data, "checks": checks}) diff --git a/src/codeproof/core.py b/src/codeproof/core.py index b767863..a92b010 100644 --- a/src/codeproof/core.py +++ b/src/codeproof/core.py @@ -1,19 +1,97 @@ -from datetime import datetime,timezone -from pathlib import Path +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timezone + from . import __version__ from .adapters import run -from .models import ScanReport,Status -def grade(x:float)->str:return "A" if x>=90 else "B" if x>=80 else "C" if x>=70 else "D" if x>=60 else "F" -def scan(root,cfg,fail_under=None): - results=[] - for name,c in cfg.checks.items(): - if not c.enabled:continue - r=run(name,root,c.config,cfg.timeout_seconds,cfg.source_paths,cfg.test_paths);r.weight=c.weight;r.required=name in cfg.required_tools;results.append(r) - included=[r for r in results if r.status not in (Status.unavailable,Status.skipped) or r.required] - denominator=sum(r.weight for r in included);score=100*sum(r.weight for r in included if r.status==Status.passed)/denominator if denominator else 0 - threshold=cfg.fail_under if fail_under is None else fail_under - required_ok=all(not r.required or r.status==Status.passed for r in results) - unavailable=[r.name for r in results if r.status==Status.unavailable and not r.required] - limitations=["Passing configured checks is not proof of correctness.","pytest and import checks may execute repository code."] - if unavailable:limitations.append("Optional checks excluded from score: "+", ".join(unavailable)) - return ScanReport(project=root.name,target=str(root),generated_at=datetime.now(timezone.utc).isoformat(),codeproof_version=__version__,score=round(score,2),grade=grade(score),policy_passed=score>=threshold and required_ok,fail_under=threshold,results=results,limitations=limitations) +from .models import CheckResult, ScanReport, Status + + +def grade(x: float) -> str: + return ( + "A" + if x >= 90 + else "B" + if x >= 80 + else "C" + if x >= 70 + else "D" + if x >= 60 + else "F" + ) + + +def scan(root, cfg, fail_under=None, jobs=None): + threshold = cfg.fail_under if fail_under is None else fail_under + workers = cfg.jobs if jobs is None else jobs + if not 0 <= threshold <= 100: + raise ValueError("fail_under must be between 0 and 100") + if not 1 <= workers <= 8: + raise ValueError("jobs must be between 1 and 8") + selected = [(name, check) for name, check in cfg.checks.items() if check.enabled] + + def execute(item): + name, check = item + result = run( + name, + root, + check.config, + cfg.timeout_seconds, + cfg.source_paths, + cfg.test_paths, + ) + result.weight = check.weight + result.required = name in cfg.required_tools + return result + + # map preserves policy order even when workers finish out of order. + with ThreadPoolExecutor(max_workers=workers) as pool: + results = list(pool.map(execute, selected)) + for name in dict.fromkeys(cfg.required_tools): + if not any(result.name == name for result in results): + results.append( + CheckResult( + name=name, + status=Status.skipped, + required=True, + summary="Required check is disabled or absent from the policy", + ) + ) + included = [ + r + for r in results + if r.status not in (Status.unavailable, Status.skipped) or r.required + ] + denominator = sum(r.weight for r in included) + score = ( + 100 * sum(r.weight for r in included if r.status == Status.passed) / denominator + if denominator + else 0 + ) + required_ok = all(not r.required or r.status == Status.passed for r in results) + unavailable = [ + r.name for r in results if r.status == Status.unavailable and not r.required + ] + limitations = [ + "Passing configured checks is not proof of correctness.", + "pytest and import checks may execute repository code.", + ] + if unavailable: + limitations.append( + "Optional checks excluded from score: " + ", ".join(unavailable) + ) + if denominator == 0: + limitations.append( + "No weighted checks contributed evidence; policy cannot pass." + ) + return ScanReport( + project=root.name, + target=str(root), + generated_at=datetime.now(timezone.utc).isoformat(), + codeproof_version=__version__, + score=round(score, 2), + grade=grade(score), + policy_passed=score >= threshold and required_ok and denominator > 0, + fail_under=threshold, + results=results, + limitations=limitations, + ) diff --git a/src/codeproof/discovery.py b/src/codeproof/discovery.py index ef78d1b..f010d17 100644 --- a/src/codeproof/discovery.py +++ b/src/codeproof/discovery.py @@ -1,19 +1,46 @@ import ast +from collections import defaultdict from pathlib import Path -def symbols(root:Path,sources:list[str]): - out=[] - for src in sources: - base=root/src - for p in base.rglob("*.py") if base.exists() else []: - try:tree=ast.parse(p.read_text()) - except SyntaxError:continue - for n in tree.body: - if isinstance(n,(ast.FunctionDef,ast.AsyncFunctionDef,ast.ClassDef)) and not n.name.startswith("_"):out.append((n.name,p.relative_to(root),n.lineno)) + +from .files import python_files + + +def symbols(root: Path, sources: list[str]): + out = [] + for path, _ in python_files(root, sources): + try: + tree = ast.parse(path.read_bytes()) + except (SyntaxError, UnicodeError): + continue + for node in tree.body: + if isinstance( + node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef) + ) and not node.name.startswith("_"): + out.append((node.name, path.relative_to(root), node.lineno)) return out -def generate(root:Path,sources:list[str],output:Path,force=False): - output.mkdir(parents=True,exist_ok=True);made=[] - for name,p,line in symbols(root,sources): - src=next((s for s in sources if str(p).startswith(s+"/")),sources[0]);mod=".".join(Path(p).relative_to(src).with_suffix("").parts);dest=output/f"test_{mod.replace('.','_')}_generated.py" - text=(dest.read_text() if dest.exists() else f'import importlib\nmodule=importlib.import_module("{mod}")\n')+f'\ndef test_{name}_is_exposed():\n assert hasattr(module,"{name}")\n' - if force or not dest.exists():dest.write_text(text);made.append(dest) + + +def generate(root: Path, sources: list[str], output: Path, force=False): + output.mkdir(parents=True, exist_ok=True) + groups = defaultdict(list) + bases = {path.relative_to(root): base for path, base in python_files(root, sources)} + for name, path, _ in symbols(root, sources): + parts = list((root / path).relative_to(bases[path]).with_suffix("").parts) + if parts[-1] == "__init__": + parts.pop() + module = ".".join(parts) + if module: + groups[module].append(name) + made = [] + for module, names in groups.items(): + # Length-prefix components to avoid collisions such as a_b.c and a.b_c. + slug = "_".join(f"{len(part)}_{part}" for part in module.split(".")) + dest = output / f"test_{slug}_generated.py" + if dest.exists() and not force: + continue + text = f"import importlib\n\nmodule = importlib.import_module({module!r})\n" + for name in names: + text += f"\n\ndef test_{name}_is_exposed():\n assert hasattr(module, {name!r})\n" + dest.write_text(text, encoding="utf-8") + made.append(dest) return made diff --git a/src/codeproof/files.py b/src/codeproof/files.py new file mode 100644 index 0000000..842eda8 --- /dev/null +++ b/src/codeproof/files.py @@ -0,0 +1,49 @@ +"""Shared deterministic traversal; never descend into caches or environments.""" + +import os +from pathlib import Path + +EXCLUDED = { + ".git", + ".venv", + "venv", + "env", + "__pycache__", + ".pytest_cache", + ".mypy_cache", + ".ruff_cache", + ".codeproof", + "node_modules", + "build", + "dist", +} + + +def python_files(root: Path, sources: list[str]): + seen = set() + for source in sources: + base = root / source + if base.is_file(): + candidates = [base] if base.suffix == ".py" else [] + else: + candidates = [] + for directory, folders, names in os.walk(base, followlinks=False): + folders[:] = sorted( + name + for name in folders + if name not in EXCLUDED + and not (Path(directory) / name).is_symlink() + ) + candidates.extend( + Path(directory) / name + for name in sorted(names) + if name.endswith(".py") + ) + for path in candidates: + if path.is_symlink(): + continue + resolved = path.resolve() + if not resolved.is_relative_to(root.resolve()) or resolved in seen: + continue + seen.add(resolved) + yield path, base if base.is_dir() else base.parent diff --git a/src/codeproof/models.py b/src/codeproof/models.py index bdf46fb..b35a30f 100644 --- a/src/codeproof/models.py +++ b/src/codeproof/models.py @@ -1,11 +1,48 @@ from enum import Enum from typing import Any + from pydantic import BaseModel, Field -class Status(str,Enum): - passed="passed"; failed="failed"; unavailable="unavailable"; error="error"; skipped="skipped" + + +class Status(str, Enum): + passed = "passed" + failed = "failed" + unavailable = "unavailable" + error = "error" + skipped = "skipped" + + class Finding(BaseModel): - level:str="info"; message:str; path:str|None=None; line:int|None=None; rule:str|None=None + level: str = "info" + message: str + path: str | None = None + line: int | None = None + rule: str | None = None + + class CheckResult(BaseModel): - name:str; status:Status; command:list[str]=Field(default_factory=list); duration_seconds:float=0; exit_code:int|None=None; summary:str=""; findings:list[Finding]=Field(default_factory=list); evidence:dict[str,Any]=Field(default_factory=dict); raw_output:str|None=None; weight:float=0; required:bool=False + name: str + status: Status + command: list[str] = Field(default_factory=list) + duration_seconds: float = 0 + exit_code: int | None = None + summary: str = "" + findings: list[Finding] = Field(default_factory=list) + evidence: dict[str, Any] = Field(default_factory=dict) + raw_output: str | None = None + weight: float = 0 + required: bool = False + + class ScanReport(BaseModel): - schema_version:str="1.0"; project:str; target:str; generated_at:str; codeproof_version:str; score:float; grade:str; policy_passed:bool; fail_under:float; results:list[CheckResult]; limitations:list[str]=Field(default_factory=list) + schema_version: str = "1.0" + project: str + target: str + generated_at: str + codeproof_version: str + score: float + grade: str + policy_passed: bool + fail_under: float + results: list[CheckResult] + limitations: list[str] = Field(default_factory=list) diff --git a/src/codeproof/report_template.html b/src/codeproof/report_template.html new file mode 100644 index 0000000..e62b987 --- /dev/null +++ b/src/codeproof/report_template.html @@ -0,0 +1,64 @@ + + + + +@@project@@ · CodeProof report + + + +
CodeProof / Repository insights
Local first · Evidence driven
+
+
Verification report

@@project@@

A clearer picture of your repository’s health.

Report snapshot · v@@version@@
+
+
@@score@@OUT OF 100
Weighted confidence

Grade @@grade@@

@@passed@@ of @@total@@ checks passed

+
Policy result
@@policy@@

Threshold: @@threshold@@ / 100
All required checks must pass

+
Needs attention
@@attention@@ checks

@@findings@@ normalized findings
Expand a check for its evidence

+
+

Check results

Inspect the evidence behind your score.

+ +
@@cards@@

+ + +
+ + + diff --git a/src/codeproof/reporting.py b/src/codeproof/reporting.py index f68a561..9bf1809 100644 --- a/src/codeproof/reporting.py +++ b/src/codeproof/reporting.py @@ -1,17 +1,204 @@ -from pathlib import Path from html import escape -def markdown(r): - rows=["| Check | Status | Weight | Summary |","|---|---|---:|---|"]+[f"| {x.name} | {x.status.value} | {x.weight:g} | {x.summary} |" for x in r.results] - return f"# CodeProof: {r.project}\n\n**Score:** {r.score}/100 ({r.grade}) \n**Policy:** {'PASS' if r.policy_passed else 'FAIL'}\n\n"+"\n".join(rows)+"\n\n## Limitations\n"+"\n".join('- '+x for x in r.limitations) -def html(r): - rows=''.join(f'{escape(x.name)}{x.status.value}{x.weight:g}{escape(x.summary)}' for x in r.results) - return f'CodeProof

CodeProof: {escape(r.project)}

{r.score}/100 ({r.grade})

Policy: {"PASS" if r.policy_passed else "FAIL"}

{rows}
CheckStatusWeightSummary
' -def write(r,out:Path,fmt:str): - out.mkdir(parents=True,exist_ok=True);made=[] - if fmt in ("all","json"):p=out/"codeproof-report.json";p.write_text(r.model_dump_json(indent=2));made.append(p) - if fmt in ("all","markdown"):p=out/"codeproof-report.md";p.write_text(markdown(r));made.append(p) - if fmt in ("all","html"):p=out/"codeproof-report.html";p.write_text(html(r));made.append(p) - raw=out.parent/"raw";raw.mkdir(parents=True,exist_ok=True) - for x in r.results: - if x.raw_output:(raw/f"{x.name}.txt").write_text(x.raw_output) +from pathlib import Path +from typing import Any + +from .models import ScanReport, Status + + +def cell(value): + return ( + str(value) + .replace("\\", "\\\\") + .replace("|", "\\|") + .replace("\n", "
") + .replace("\r", "") + ) + + +def markdown(report: ScanReport): + rows = [ + "| Check | Status | Required | Weight | Duration | Summary |", + "|---|---|---|---:|---:|---|", + ] + for result in report.results: + rows.append( + f"| {cell(result.name)} | {result.status.value} | {'yes' if result.required else 'no'} | " + f"{result.weight:g} | {result.duration_seconds:.2f}s | {cell(result.summary)} |" + ) + text = ( + f"# CodeProof: {cell(report.project)}\n\n**Score:** {report.score}/100 ({report.grade}) \n" + f"**Policy:** {'PASS' if report.policy_passed else 'FAIL'} · threshold {report.fail_under:g}\n\n" + + "\n".join(rows) + ) + for result in report.results: + if result.findings: + text += f"\n\n## {cell(result.name)} findings\n" + for finding in result.findings: + location = f"{finding.path or ''}{':' + str(finding.line) if finding.line else ''}" + text += f"\n- {cell(finding.rule or finding.level)} · {cell(location)}: {cell(finding.message)}" + return ( + text + + "\n\n## Limitations\n" + + "\n".join("- " + cell(item) for item in report.limitations) + + "\n" + ) + + +def html(report: ScanReport): + cards = [] + for result in report.results: + findings = "".join( + f'
  • {escape(item.rule or item.level)}' + f"{escape(item.path or 'No file location')}{':' + str(item.line) if item.line else ''}
    " + f"

    {escape(item.message)}

  • " + for item in result.findings + ) + detail = ( + f'

    Findings ({len(result.findings)})

    ' + if findings + else "" + ) + if result.command: + detail += ( + "

    Command arguments

    "
    +                + escape(str(result.command))
    +                + "
    " + ) + if result.raw_output: + detail += "

    Tool output

    " + escape(result.raw_output) + "
    " + if result.evidence: + detail += "

    Evidence

    " + escape(str(result.evidence)) + "
    " + cards.append( + f'
    ' + f'
    ' + f'{escape(result.name)}{escape(result.summary)}' + f'{"Required" if result.required else "Optional"}' + f'{result.duration_seconds:.2f}s' + f'{result.status.value}⌄' + f'

    Policy weight: {result.weight:g}

    {detail or "

    No additional evidence.

    "}
    ' + "
    " + ) + template = ( + Path(__file__).with_name("report_template.html").read_text(encoding="utf-8") + ) + passed = sum(result.status == Status.passed for result in report.results) + attention = sum( + result.status in (Status.failed, Status.error) + or (result.required and result.status != Status.passed) + for result in report.results + ) + replacements = { + "project": escape(report.project), + "target": escape(report.target), + "generated": escape(report.generated_at), + "score": f"{report.score:g}", + "grade": escape(report.grade), + "policy": "Passed" if report.policy_passed else "Failed", + "policy_class": "passed" if report.policy_passed else "failed", + "threshold": f"{report.fail_under:g}", + "passed": str(passed), + "total": str(len(report.results)), + "attention": str(attention), + "findings": str(sum(len(result.findings) for result in report.results)), + "cards": "".join(cards), + "limitations": "".join( + f"
  • {escape(item)}
  • " for item in report.limitations + ), + "version": escape(report.codeproof_version), + } + # One replacement pass prevents report text from being interpreted as template tokens. + import re + + return re.sub(r"@@(\w+)@@", lambda match: replacements[match[1]], template) + + +def write(report: ScanReport, out: Path, fmt: str): + if fmt not in {"all", "json", "markdown", "html", "sarif"}: + raise ValueError(f"Unsupported report format: {fmt}") + out.mkdir(parents=True, exist_ok=True) + made = [] + renderers = { + "json": ("json", lambda: report.model_dump_json(indent=2)), + "markdown": ("md", lambda: markdown(report)), + "html": ("html", lambda: html(report)), + } + for name, (suffix, render) in renderers.items(): + if fmt in ("all", name): + path = out / f"codeproof-report.{suffix}" + path.write_text(render(), encoding="utf-8") + made.append(path) + if fmt == "sarif": + import json + + path = out / "codeproof-report.sarif" + path.write_text(json.dumps(sarif(report), indent=2), encoding="utf-8") + made.append(path) + raw = out.parent / "raw" + raw.mkdir(parents=True, exist_ok=True) + for result in report.results: + if result.raw_output: + (raw / f"{result.name}.txt").write_text(result.raw_output, encoding="utf-8") return made + + +def sarif(report: ScanReport): + """Export normalized findings for SARIF 2.1.0 consumers.""" + rules: dict[str, Any] = {} + results = [] + root = Path(report.target).resolve() + for check in report.results: + for finding in check.findings: + rule = f"{check.name}/{finding.rule or 'finding'}" + rules.setdefault( + rule, + {"id": rule, "shortDescription": {"text": finding.rule or check.name}}, + ) + level = ( + "error" + if finding.level.lower() in {"error", "high", "critical"} + else "note" + if finding.level.lower() in {"info", "low"} + else "warning" + ) + item: dict[str, Any] = { + "ruleId": rule, + "level": level, + "message": {"text": finding.message}, + } + if finding.path: + original = Path(finding.path) + path: Path | None = original + if original.is_absolute(): + try: + path = original.relative_to(root) + except ValueError: + path = None + if path is not None and ".." not in path.parts: + from urllib.parse import quote + + location: dict[str, Any] = { + "artifactLocation": { + "uri": quote(path.as_posix()), + "uriBaseId": "%SRCROOT%", + } + } + if finding.line and finding.line > 0: + location["region"] = {"startLine": finding.line} + item["locations"] = [{"physicalLocation": location}] + results.append(item) + return { + "version": "2.1.0", + "$schema": "https://json.schemastore.org/sarif-2.1.0.json", + "runs": [ + { + "tool": { + "driver": { + "name": "CodeProof AI", + "version": report.codeproof_version, + "rules": list(rules.values()), + } + }, + "results": results, + } + ], + } diff --git a/src/codeproof/web/app.css b/src/codeproof/web/app.css new file mode 100644 index 0000000..1bbfb91 --- /dev/null +++ b/src/codeproof/web/app.css @@ -0,0 +1,4 @@ +:root{--bg:#f6f8fa;--ink:#1c2939;--muted:#5e6e80;--line:#e5eaef;--teal:#087d6b;--teal-light:#e6f5ef;--sidebar:#111e2b;--red:#ba3b47;--orange:#a36311;--shadow:0 3px 12px #13293b04}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:14px/1.55 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}button,input,select{font:inherit}button,a,input,select{ -webkit-tap-highlight-color:transparent}button{cursor:pointer}a{color:inherit;text-decoration:none}[hidden]{display:none!important}button:focus-visible,a:focus-visible,input:focus-visible,select:focus-visible,summary:focus-visible{outline:3px solid #279f8e;outline-offset:3px}.skip{position:fixed;top:-60px;left:10px;z-index:10;background:white;padding:10px}.skip:focus{top:10px}.sidebar{position:fixed;inset:0 auto 0 0;width:244px;background:var(--sidebar);color:#b5c2ce;padding:27px 17px;display:flex;flex-direction:column;z-index:2}.brand{display:flex;gap:11px;align-items:center;padding:0 8px;color:#fff;font-size:23px;font-weight:700;letter-spacing:-.7px}.brand-mark{display:flex;align-items:center;justify-content:center;width:36px;height:38px;border-radius:9px;background:#20a68c;color:#fff;font-size:26px;position:relative}.brand-mark span{position:absolute;font-size:13px;right:2px;bottom:0}.brand small{display:block;letter-spacing:1.45px;color:#9baebf;font-size:8px;font-weight:500;margin-top:0}.workspace-label,.nav-label{font-size:10px;letter-spacing:1.6px;color:#8497a9;font-weight:650}.workspace-label{margin:37px 12px 10px}.repo-switch{display:flex;align-items:center;gap:10px;background:#1c2b3a;border:1px solid #2d3d4c;padding:11px 10px;border-radius:7px;margin:0 5px 27px}.repo-icon{display:grid;place-items:center;background:#304351;color:#b9ccda;border-radius:5px;width:28px;height:30px;font-size:20px}.repo-switch strong{display:block;color:#e5edf3;font-size:12px;max-width:132px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.repo-switch small{font-size:11px;color:#95a8b9}.repo-switch .online-dot{margin-left:auto}.online-dot{width:6px;height:6px;background:#38b68e;border-radius:50%;display:inline-block;flex-shrink:0}.nav-label{padding:0 13px;margin-bottom:11px}.resources{margin-top:33px;margin-bottom:11px}nav a{display:flex;align-items:center;gap:12px;padding:11px 13px;margin:3px 0;border-radius:6px;font-size:18px}nav a span{font-size:12px}nav a:hover{background:#1b2e3d}nav a.active{background:#1b3d3b;color:#7ee1c3;box-shadow:inset 3px 0 #42c5a2}nav .nav-count{margin-left:auto;background:#2c3c4b;color:#c7d3dd;padding:0 6px;border-radius:4px;font-size:11px}.sidebar-bottom{margin-top:auto;padding:24px 12px 0}.local-icon{color:#7bc6ac;font-size:24px}.sidebar-bottom strong{display:block;color:#dae7ed;font-size:12px;margin:5px 0}.sidebar-bottom p{font-size:11px;line-height:1.7;color:#92a6b8}.sidebar-foot{border-top:1px solid #293a49;margin-top:24px;padding-top:17px;display:flex;align-items:center;gap:7px;font-size:10px}.sidebar-foot>span:last-child{margin-left:auto;color:#a9bac8}.shell{margin-left:244px}.topbar{height:70px;background:#fff;border-bottom:1px solid var(--line);display:flex;align-items:center;justify-content:space-between;padding:0 36px}.breadcrumb{font-size:12px;color:var(--muted)}.breadcrumb span{margin:0 12px;color:#c3ccd5}.breadcrumb strong{color:#394a5c;font-weight:500}.top-actions{display:flex;align-items:center;gap:22px}.local-badge{display:flex;gap:7px;align-items:center;font-size:11px;color:#547767}.avatar{width:29px;height:29px;background:#edf2f7;border:1px solid #dbe3ed;border-radius:50%;font-size:11px;font-weight:650;color:#516476}main{padding:31px 36px 0;max-width:1600px;margin:auto}.page-heading{display:flex;justify-content:space-between;align-items:center;margin-bottom:28px;gap:20px}.eyebrow{font-size:10px;font-weight:700;letter-spacing:1.5px;color:var(--teal)}h1{font-size:27px;line-height:1.25;letter-spacing:-.8px;margin:6px 0 7px}h2,h3,p{margin:0}h2{font-size:15px;letter-spacing:-.2px}h3{font-size:13px}.page-heading p,.muted{color:var(--muted);font-size:12px}.primary,.secondary,.hero-button{border:0;border-radius:6px;padding:10px 16px;font-size:12px;font-weight:600;display:inline-flex;align-items:center;justify-content:center;gap:8px;white-space:nowrap}.primary{background:var(--teal);color:#fff;box-shadow:0 2px 3px #003b2e15}.primary:hover{background:#086454}.secondary{background:white;border:1px solid #dce3e9;color:#45566a}.secondary:hover{background:#f0f5f7}.small{padding:6px 10px;font-size:11px}button:disabled{opacity:.5;cursor:not-allowed}.hero{background:#eaf4ef;border:1px solid #d9e9df;border-radius:10px;min-height:217px;padding:28px 32px;display:flex;align-items:center;justify-content:space-between;overflow:hidden;position:relative}.hero-label{color:#456d5a;letter-spacing:1.5px;font-size:10px;font-weight:700}.hero h2{font-size:31px;letter-spacing:-1px;color:#173f31;margin:8px 0}.hero p{font-size:12px;color:#537664;line-height:1.75}.hero-button{background:#fff;border:1px solid #ccded2;color:#285e47;margin-top:18px;padding:8px 13px;font-size:11px}.hero-button span{margin-left:12px}.hero-graphic{width:330px;height:195px;position:relative;display:grid;place-items:center;margin-right:18px}.orbit{position:absolute;border:1px solid #cce2d4;border-radius:50%}.outer{width:220px;height:220px}.inner{width:154px;height:154px}.graphic-center{display:grid;place-items:center;width:84px;height:84px;background:#c2e2cd;border:9px solid #d9eddf;border-radius:25px;font-size:40px;color:#257950;transform:rotate(-8deg)}.graphic-tag{position:absolute;background:#ffffffed;border:1px solid #dfeae1;box-shadow:0 5px 15px #245b3510;border-radius:7px;padding:8px 13px;font-size:11px;color:#397155}.tag-one{top:12px;left:13px}.tag-two{right:0;top:65px}.tag-three{left:6px;bottom:17px}.section-label{display:flex;justify-content:space-between;align-items:center;margin:26px 0 13px}.section-label>span{font-size:11px;color:var(--muted)}.metrics{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:14px}.metric{background:#fff;border:1px solid var(--line);border-radius:8px;padding:17px 19px;box-shadow:var(--shadow)}.metric-top{display:flex;justify-content:space-between;font-size:11px;color:#5a6c7e}.metric-icon{color:#6b8998}.metric-value{font-size:29px;font-weight:650;letter-spacing:-1px;margin:8px 0 2px;line-height:1.35}.metric-value small{font-size:12px;font-weight:400;letter-spacing:0;color:var(--muted)}.metric-foot{font-size:10px;color:#596e80}.good{color:var(--teal)}.bad{color:var(--red)}.warn{color:var(--orange)}.overview-grid{display:grid;grid-template-columns:1.55fr 1fr;gap:18px;margin:20px 0}.panel{background:#fff;border:1px solid var(--line);border-radius:9px;box-shadow:var(--shadow)}.panel-heading{display:flex;justify-content:space-between;align-items:center;padding:19px 21px;gap:15px}.panel-heading p{font-size:11px;color:var(--muted);margin-top:4px}.text-link{font-size:11px;font-weight:600;color:var(--teal);white-space:nowrap}.trend{padding:0 22px 14px;min-height:145px}.trend svg{width:100%;height:110px;display:block}.trend-labels{display:flex;justify-content:space-between;color:#627689;font-size:10px}.chart-grid{stroke:#edf0f4;stroke-width:1}.chart-line{stroke:#19846f;stroke-width:2.5;fill:none}.chart-area{fill:#edf7f2}.chart-dot{fill:#19846f;stroke:#fff;stroke-width:2}.tool-row{display:flex;align-items:center;padding:7px 21px;gap:10px;font-size:12px}.tool-row .tool-status{margin-left:auto;font-size:10px}.tool-letter{display:grid;place-items:center;width:24px;height:24px;border:1px solid #e5eaf0;border-radius:5px;font-size:11px;color:#557083;background:#f8fafb}.tool-link{display:block;border-top:1px solid var(--line);padding:11px 21px;margin-top:12px}.mini-icon{color:var(--teal);background:#eaf5ef;border-radius:7px;padding:5px 10px}.results-panel{margin-top:20px;overflow:hidden}.export-links{display:flex;gap:6px;flex-wrap:wrap}.export-links a{font-size:10px;color:#5c7082;border:1px solid #e1e6ec;padding:4px 8px;border-radius:4px}.export-links a:hover{color:var(--teal);background:#eaf6ef}.check-row{display:flex;gap:12px;align-items:center;padding:12px 21px;border-top:1px solid #edf0f3}.check-name{font-size:12px;font-weight:600;min-width:70px}.check-summary{font-size:11px;color:var(--muted);flex:1}.check-required{font-size:10px;color:#5a6e80}.badge{font-size:10px;padding:3px 8px;border-radius:4px;background:#eff2f5;color:#56697b;white-space:nowrap}.badge.passed,.badge.complete{color:#126e50;background:#e7f5ed}.badge.failed,.badge.error,.badge.interrupted{color:#aa3345;background:#fcecef}.badge.running{color:#815914;background:#fff4df}.evidence-button{border:0;background:transparent;color:var(--teal);font-size:11px;padding:3px}.findings-heading{display:flex;gap:10px;align-items:center;border-top:1px solid var(--line);padding:21px 21px 12px}.tag{font-size:10px;background:#f0f4f7;color:#596d7c;border:1px solid #e5eaef;padding:2px 7px;border-radius:4px}.findings-toolbar{display:flex;gap:9px;padding:0 21px 15px}input,select{border:1px solid #dce3e9;border-radius:6px;padding:9px 11px;font-size:12px;color:var(--ink);background:#fff;min-width:0}.findings-toolbar input{flex:1}.table-wrap{overflow:auto}table{width:100%;border-collapse:collapse;text-align:left;font-size:12px}th{background:#f8fafb;color:#576c80;font-size:10px;font-weight:500;padding:10px 20px;border-top:1px solid var(--line);border-bottom:1px solid var(--line);white-space:nowrap}td{padding:13px 20px;border-bottom:1px solid #edf0f3;vertical-align:top}.finding-message{max-width:500px;overflow-wrap:anywhere}.finding-message strong{font-size:12px;font-weight:500;display:block}.finding-message small{font-size:10px;color:var(--muted)}.severity{display:inline-flex;align-items:center;gap:5px;font-size:11px}.severity:before{content:"";width:5px;height:5px;border-radius:50%;background:#d69a40}.severity.error:before,.severity.high:before,.severity.critical:before{background:#cf5161}.severity.low:before,.severity.info:before{background:#608aa7}.pagination{display:flex;justify-content:space-between;align-items:center;padding:13px 21px;color:var(--muted);font-size:10px}.pagination>div{display:flex;gap:6px}.empty{padding:34px 25px;text-align:center;color:var(--muted);font-size:12px}.empty strong{display:block;font-size:14px;color:#456071;margin-bottom:5px}.empty .primary{margin-top:14px}.comparison{margin-top:20px}.compare-controls{display:flex;align-items:end;gap:15px;padding:0 21px 20px}.compare-controls label{flex:1;display:flex;flex-direction:column;gap:6px;font-size:11px;color:var(--muted)}.comparison-body{border-top:1px solid var(--line);padding:20px}.comparison-metrics{display:flex;flex-wrap:wrap;gap:15px;margin-bottom:16px}.comparison-metrics>div{background:#f7f9fa;padding:15px 20px;border-radius:6px;flex:1}.comparison-metrics strong{display:block;font-size:23px}.comparison-metrics span{font-size:11px;color:var(--muted)}.tool-grid,.guide-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:17px}.tool-card,.guide-card{background:#fff;border:1px solid var(--line);border-radius:9px;padding:23px}.tool-card-top{display:flex;align-items:center;gap:10px;margin-bottom:12px}.tool-card-top .badge{margin-left:auto}.tool-card p,.guide-card p{font-size:12px;color:var(--muted);margin:8px 0 18px}.tool-card code{font-size:11px;overflow-wrap:anywhere;background:#f5f7fa;padding:7px 9px;border-radius:4px;display:block}.command-panel{margin-top:23px;padding:23px}.command-panel>p{color:var(--muted);font-size:12px;margin-top:5px}.command{display:flex;gap:10px;align-items:center;justify-content:space-between;background:#f5f8fa;padding:12px;border-radius:5px;margin-top:13px}.command code{font-size:12px;overflow-wrap:anywhere}.policy-grid{display:grid;grid-template-columns:1fr 1.15fr;gap:20px}.policy-explainer{padding:26px}.policy-explainer h2{font-size:23px;margin:12px 0}.policy-explainer p{font-size:12px;color:var(--muted);line-height:1.8;margin-bottom:20px}.policy-fact{display:flex;justify-content:space-between;border-top:1px solid var(--line);padding:11px 0;font-size:12px;gap:15px}.policy-explainer .secondary{margin-top:15px}pre{white-space:pre-wrap;overflow-wrap:anywhere;max-height:450px;overflow:auto;background:#f5f7fa;border:1px solid #e5eaf0;border-radius:6px;font:11px/1.8 ui-monospace,SFMono-Regular,Consolas,monospace;padding:18px}.policy-code pre{margin:0 21px 21px;font-size:11px;max-height:650px}.guide-intro{text-align:center;padding:25px 0 35px}.guide-intro h2{font-size:28px;margin:9px 0}.guide-intro p{font-size:12px;color:var(--muted)}.guide-number{font:11px ui-monospace,monospace;color:var(--teal);background:#eaf5ef;padding:7px 10px;display:inline-block;border-radius:5px;margin-bottom:17px}.guide-card h3{font-size:18px}.guide-card .text-link{border:0;background:none;padding:0}.getting-started{padding:25px;margin-top:22px}.getting-started ol{display:grid;grid-template-columns:repeat(3,1fr);gap:30px;padding-left:20px}.getting-started li{color:var(--teal);font-size:12px;padding-left:6px}.getting-started strong{display:block;color:var(--ink);font-size:12px}.getting-started li span{display:block;color:var(--muted);font-size:12px;margin-top:7px}footer{display:flex;justify-content:space-between;gap:15px;font-size:10px;color:#586b7c;border-top:1px solid var(--line);margin-top:32px;padding:18px 0 22px}.footer-dot{margin:0 5px}.error{background:#fff0f1;border:1px solid #efc6cc;color:#a02b3b;padding:13px;border-radius:6px;font-size:12px;margin-bottom:18px;overflow-wrap:anywhere}.activity{display:flex;align-items:center;gap:10px;background:#eaf5ef;color:#216e53;padding:12px 16px;margin-bottom:18px;border-radius:6px;font-size:12px}.spinner{display:inline-block;width:13px;height:13px;border:2px solid #94cbb8;border-top-color:#087d6b;border-radius:50%;animation:spin 1s linear infinite}@keyframes spin{to{transform:rotate(360deg)}}dialog{border:1px solid var(--line);border-radius:12px;padding:28px;max-width:650px;width:calc(100% - 32px);max-height:90vh;overflow:auto;box-shadow:0 15px 80px #0d263a30;color:var(--ink)}dialog::backdrop{background:#11253670;backdrop-filter:blur(3px)}.dialog-heading{display:flex;justify-content:space-between;align-items:start;gap:20px}.dialog-heading h2{font-size:22px;margin-top:5px}.close{border:0;background:#f2f5f7;font-size:24px;width:29px;height:29px;line-height:1;border-radius:5px;color:#657786}.profiles{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin:22px 0}.profile{display:block;border:1px solid #dce4eb;padding:13px;border-radius:7px;cursor:pointer;font-size:12px}.profile:has(input:checked){border-color:#208c75;background:#edf8f2}.profile input{accent-color:var(--teal);margin:0 5px 0 0}.profile p{font-size:11px;color:var(--muted);margin-top:5px}.form-grid{display:grid;grid-template-columns:1fr 1fr;gap:15px}.form-grid label{display:flex;flex-direction:column;font-size:12px;color:#5d7082;gap:7px}.scan-scope{background:#f4f7fa;padding:13px 15px;border-radius:6px;font-size:12px;margin-top:20px}.scan-scope p{margin:6px 0;color:#3b7062}.scan-scope small{font-size:11px;color:var(--muted)}.consent{display:flex;align-items:start;gap:10px;font-size:12px;margin-top:18px}.consent input{accent-color:var(--teal);margin-top:4px}.consent small{display:block;color:var(--muted);font-size:11px;margin-top:2px}.dialog-actions{display:flex;justify-content:end;gap:10px;border-top:1px solid var(--line);padding-top:20px;margin-top:25px}#scan-dialog form>.muted{margin-top:7px}#scan-error{margin-top:15px}#evidence{margin-top:18px;font-size:12px;overflow-wrap:anywhere}#evidence h3{margin-top:20px}#evidence ul{padding-left:18px}#evidence li{margin:10px 0}#evidence code{font-size:12px}.notice{font-size:12px;color:#80601d;background:#fff7e6;border:1px solid #eddfbb;padding:12px;border-radius:6px;margin-bottom:15px} +@media(min-width:1600px){main{padding-top:40px}.hero{min-height:250px}.hero h2{font-size:37px}}@media(max-width:1100px){.sidebar{width:210px}.shell{margin-left:210px}.brand{font-size:21px}main{padding:25px 22px 0}.topbar{padding:0 22px}.hero-graphic{width:260px;margin-right:0}.graphic-tag{font-size:10px;padding:6px 8px}.overview-grid{grid-template-columns:1.2fr 1fr}.metric{padding:15px 12px}.metric-top{font-size:10px}.metric-value{font-size:25px}.check-summary{max-width:240px}}@media(max-width:800px){.sidebar{position:static;width:auto;padding:14px 18px;display:block}.brand{font-size:22px}.brand small{font-size:7px}.brand-mark{width:30px;height:31px}.workspace-label,.repo-switch,.sidebar>.nav-label,.resources,.sidebar-bottom{display:none}nav{display:flex;gap:5px;overflow:auto;margin-top:15px}nav a{white-space:nowrap;font-size:14px;padding:8px 10px}nav a span{font-size:11px}nav .nav-count{display:none}.shell{margin-left:0}.topbar{height:51px;padding:0 18px}.top-actions{gap:12px}main{padding:24px 18px 0}.page-heading{align-items:start}.page-heading .primary{font-size:11px;padding:9px 11px}h1{font-size:24px}.hero{padding:24px}.hero-graphic{width:220px;flex-shrink:0;transform:scale(.9)}.hero h2{font-size:28px}.metrics{grid-template-columns:repeat(2,1fr)}.overview-grid{grid-template-columns:1fr}.readiness{display:none}.metric{padding:16px}.policy-grid{grid-template-columns:1fr}.getting-started ol{grid-template-columns:1fr;gap:20px}.check-required{display:none}footer{flex-direction:column;gap:4px}.compare-controls{flex-wrap:wrap}.compare-controls label{min-width:150px}}@media(max-width:520px){.page-heading p{font-size:12px;max-width:220px}.eyebrow{font-size:8px}.hero{min-height:210px}.hero-graphic{display:none}.hero h2{font-size:30px}.tool-grid,.guide-grid{grid-template-columns:1fr}.panel-heading{padding:17px}.findings-toolbar{flex-wrap:wrap;padding:0 17px 15px}.findings-toolbar input{flex-basis:100%}.findings-toolbar select{flex:1}.check-row{padding:12px 17px;flex-wrap:wrap;gap:8px}.check-summary{order:4;flex-basis:100%;max-width:none}.check-name{flex:1}.check-required{display:none}.results-panel>.panel-heading{align-items:start;flex-direction:column;gap:10px}.form-grid,.profiles{grid-template-columns:1fr}.profiles{gap:7px}.profile{padding:10px}.guide-intro h2{font-size:24px}.pagination{padding:12px 15px}.comparison-metrics>div{min-width:100px}.comparison-metrics strong{font-size:20px}dialog{padding:20px}.dialog-heading h2{font-size:20px}.section-label>span{font-size:10px}.tool-card{padding:19px}.metric-value{font-size:28px}}@media(prefers-reduced-motion:reduce){.spinner{animation:none}} + +pre:focus-visible,.table-wrap:focus-visible{outline:3px solid #279f8e;outline-offset:2px} diff --git a/src/codeproof/web/app.js b/src/codeproof/web/app.js new file mode 100644 index 0000000..ef535a9 --- /dev/null +++ b/src/codeproof/web/app.js @@ -0,0 +1,28 @@ +'use strict'; +const $ = selector => document.querySelector(selector); +const escapeHTML = value => String(value ?? '').replace(/[&<>"']/g, char => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[char])); +const state = {meta:null,runs:[],report:null,selected:null,profile:'quality',page:0,findings:[],cache:new Map(),compareTouched:false}; +const pages = {overview:['Repository overview','Your code quality, security, and test evidence. In one place.'],history:['Scan history','Track your progress. Compare the evidence behind every release.'],tools:['Developer tools','A connected toolchain for cleaner, safer Python code.'],policy:['Quality policy','Make your quality expectations explicit and repeatable.'],guides:['Use cases & guides','Put verification to work throughout your development cycle.']}; +const date = value => new Intl.DateTimeFormat(undefined,{month:'short',day:'numeric',hour:'2-digit',minute:'2-digit'}).format(new Date(value)); +const badge = value => `${escapeHTML(value)}`; +function error(message){$('#error').textContent=message;$('#error').hidden=!message;} +function guard(fn){return async (...args)=>{try{await fn(...args);}catch(exc){error(exc.message);}};} +async function api(path,options={}){const response=await fetch(path,options);const data=await response.json();if(!response.ok)throw Error(data.error||'Request failed. Please try again.');return data;} +function navigate(){const page=location.hash.slice(1) in pages?location.hash.slice(1):'overview';for(const id of Object.keys(pages))$(`#page-${id}`).hidden=id!==page;document.querySelectorAll('[data-page]').forEach(link=>{link.classList.toggle('active',link.dataset.page===page);if(link.dataset.page===page)link.setAttribute('aria-current','page');else link.removeAttribute('aria-current');});$('#page-title').textContent=pages[page][0];$('#page-description').textContent=pages[page][1];$('#breadcrumb').textContent=page==='overview'?'Overview':pages[page][0];} +async function getReport(id){if(!state.cache.has(id)){if(state.cache.size>=50)state.cache.delete(state.cache.keys().next().value);state.cache.set(id,await api(`/api/runs/${id}`));}return state.cache.get(id);} +async function refresh(){state.runs=await api('/api/runs');const running=state.runs.some(run=>run.status==='running');$('#activity').hidden=!running;$('#new-scan').disabled=running;$('#hero-scan').disabled=running;$('#run-count').textContent=state.runs.length;let selected=state.runs.find(run=>run.id===state.selected);if(!selected){selected=state.runs.find(run=>run.status==='complete');state.selected=selected?.id||null;}state.report=selected?.status==='complete'?await getReport(selected.id):null;renderOverview();renderHistory();} +function renderOverview(){const report=state.report;const count=report?report.results.filter(check=>check.status==='passed').length:0;const findings=report?report.results.flatMap(check=>check.findings.map(finding=>({...finding,tool:check.name}))):[];state.findings=findings;$('#snapshot-label').textContent=report?`Updated ${date(report.generated_at)}`:'Ready when you are';const required=report?.results.filter(check=>check.required).length||0;const values=[['Confidence score',report?`${report.score} / 100`:'—',report?`Grade ${report.grade} · threshold ${report.fail_under}`:'Weighted checks, transparent evidence','◉'],['Checks passed',report?`${count} / ${report.results.length}`:'—',report?`${required} required policy checks`:'Your results will appear here','✓'],['Findings',report?String(findings.length):'—',report?'Across normalized tool output':'Searchable, file-level evidence','◇'],['Policy result',report?`${report.policy_passed?'Passed':'Failed'}`:'—',report?'Score and required tool gates':'A clear release signal','⚑']];$('#metrics').innerHTML=values.map(([label,value,foot,icon])=>`
    ${escapeHTML(label)}${icon}
    ${value}
    ${escapeHTML(foot)}
    `).join('');renderTrend();$('#tool-preview').innerHTML=state.meta.tools.filter(tool=>['ruff','mypy','pytest'].includes(tool.id)).map(tool=>`
    ${escapeHTML(tool.id[0].toUpperCase())}${escapeHTML(tool.id)}${tool.available?'● Available':'○ Not installed'}
    `).join('');$('#results-description').textContent=report?`${state.meta.profiles[state.runs.find(run=>run.id===state.selected)?.profile]?.label||'Scan'} · ${date(report.generated_at)}`:'Run a scan to start building your verification history.';$('#exports').innerHTML=report?['json','html','markdown','sarif'].map(format=>`${format.toUpperCase()} ↓`).join(''):'';$('#check-results').innerHTML=report?report.results.map((check,index)=>`
    ${escapeHTML(check.name)}${badge(check.status)}${escapeHTML(check.summary)}${check.required?'Required':'Optional'} · ${check.duration_seconds.toFixed(2)}s
    `).join(''):'
    Your first scan is a fresh start.Choose a preset to check this repository and create a report.
    ';const savedTool=$('#finding-tool').value;$('#finding-tool').innerHTML=''+[...new Set(findings.map(finding=>finding.tool))].map(tool=>``).join('');if([...$('#finding-tool').options].some(option=>option.value===savedTool))$('#finding-tool').value=savedTool;$('#finding-count').textContent=`${findings.length} findings`;renderFindings();document.querySelectorAll('[data-evidence]').forEach(button=>button.onclick=()=>showEvidence(Number(button.dataset.evidence)));} +function renderTrend(){const runs=state.runs.filter(run=>run.status==='complete').slice(0,12).reverse();if(!runs.length){$('#trend').innerHTML='
    Your progress, made visible.Completed scans will build your quality trend.
    ';return;}const points=runs.map((run,index)=>[runs.length===1?250:20+index*460/(runs.length-1),100-run.score*.8]);const line=points.map(point=>point.join(',')).join(' ');$('#trend').innerHTML=`${points.map((point,index)=>`${escapeHTML(date(runs[index].created))}: ${runs[index].score}/100`).join('')}
    ${escapeHTML(date(runs[0].created))}Latest ${runs.at(-1).score}/100
    `;} +function renderFindings(){const query=$('#finding-search').value.toLowerCase();const tool=$('#finding-tool').value;const severity=$('#finding-severity').value;const filtered=state.findings.filter(finding=>(!tool||finding.tool===tool)&&(!severity||finding.level===severity)&&`${finding.message} ${finding.rule} ${finding.path} ${finding.tool}`.toLowerCase().includes(query));const maxPage=Math.max(0,Math.ceil(filtered.length/25)-1);state.page=Math.min(state.page,maxPage);const slice=filtered.slice(state.page*25,(state.page+1)*25);$('#findings').innerHTML=slice.length?`
    ${slice.map(finding=>``).join('')}
    SEVERITYFINDING / LOCATIONTOOL
    ${escapeHTML(finding.level)}${escapeHTML(finding.message)}${escapeHTML(finding.rule||'Finding')} · ${escapeHTML(finding.path||'No location')}${finding.line?':'+finding.line:''}${escapeHTML(finding.tool)}
    `:`
    ${state.report?'No matching findings.':'Your findings will appear here.'}${state.report?'Some tools provide raw output only. Open check evidence for full diagnostics.':'Run a scan to explore check results and tool evidence.'}
    `;$('#finding-summary').textContent=`${filtered.length?state.page*25+1:0}–${Math.min((state.page+1)*25,filtered.length)} of ${filtered.length} findings`;$('#prev').disabled=state.page===0;$('#next').disabled=state.page>=maxPage;} +function showEvidence(index){const check=state.report.results[index];$('#evidence-title').textContent=`${check.name} evidence`;$('#evidence').innerHTML=`${badge(check.status)}

    ${escapeHTML(check.summary)}

    Weight ${check.weight} · ${check.required?'Required policy gate':'Optional check'}

    ${check.findings.length?`

    Normalized findings

    `:''}${check.command.length?`

    Command arguments

    ${escapeHTML(JSON.stringify(check.command,null,2))}
    `:''}${check.raw_output?`

    Raw tool output

    ${escapeHTML(check.raw_output)}
    `:''}

    Evidence

    ${escapeHTML(JSON.stringify(check.evidence,null,2))}
    `;$('#evidence-dialog').showModal();} +function renderHistory(){$('#history').innerHTML=state.runs.length?`
    ${state.runs.map(run=>``).join('')}
    RUNPRESETSTATUSSCOREPOLICY
    ${escapeHTML(date(run.created))}
    ${run.id.slice(0,8)}${run.error?`

    ${escapeHTML(run.error)}

    `:''}
    ${escapeHTML(state.meta.profiles[run.profile]?.label||run.profile)}${badge(run.status)}${run.score??'—'}${run.policy_passed===null?'—':badge(run.policy_passed?'passed':'failed')}${run.status==='complete'?``:''}
    `:'
    A clean slate for better engineering.Run your first scan to create a durable local history.
    ';document.querySelectorAll('[data-run]').forEach(button=>button.onclick=guard(async()=>{state.selected=button.dataset.run;await refresh();location.hash='overview';}));const complete=state.runs.filter(run=>run.status==='complete');for(const id of ['baseline','current']){const old=$(`#${id}`).value;$(`#${id}`).innerHTML=complete.map(run=>``).join('');if(state.compareTouched&&complete.some(run=>run.id===old))$(`#${id}`).value=old;else $(`#${id}`).value=complete[id==='baseline'?Math.min(1,complete.length-1):0]?.id||'';}$('#compare').disabled=complete.length<2;} +function renderTools(){$('#tools').innerHTML=state.meta.tools.map(tool=>`
    ${escapeHTML(tool.id[0].toUpperCase())}

    ${escapeHTML(tool.id)}

    ${badge(tool.available?'passed':'unavailable')}
    ${escapeHTML(tool.label)}

    ${escapeHTML(tool.description)}

    ${escapeHTML(tool.available?'Ready to use in a scan preset':tool.install)}
    `).join('');const commands=['codeproof scan . --jobs 3','codeproof scan . --format sarif','codeproof inventory .','codeproof generate-tests . --output tests/generated'];$('#commands').innerHTML=commands.map((command,index)=>`
    ${escapeHTML(command)}
    `).join('');document.querySelectorAll('[data-copy]').forEach(button=>button.onclick=guard(async()=>{if(!navigator.clipboard){button.textContent='Select text to copy';return;}await navigator.clipboard.writeText(commands[Number(button.dataset.copy)]);button.textContent='Copied ✓';}));const cfg=state.meta.policy;$('#policy-yaml').textContent=state.meta.policy_yaml;$('#policy-facts').innerHTML=[['Minimum score',`${cfg.fail_under} / 100`],['Required tools',cfg.required_tools.join(', ')||'None'],['Tool timeout',`${cfg.timeout_seconds} seconds`],['Configured workers',cfg.jobs]].map(([label,value])=>`
    ${escapeHTML(label)}${escapeHTML(value)}
    `).join('');const guides=[['Pre-commit confidence','Catch syntax, lint, and type issues early. Required policy checks are always included.','quick','Explore fast feedback'],['Pull-request verification','Run a repeatable code quality gate and export the evidence alongside your review.','quality','Run a quality scan'],['Security triage','Inspect security patterns and dependency advisories. Review unavailable tools and network requirements.','security','Review security'],['Release readiness','Apply the repository policy, compare the latest runs, and share a portable release snapshot.','policy','Verify a release']];$('#guides').innerHTML=guides.map(([title,description,profile,action],index)=>`
    0${index+1}

    ${title}

    ${description}

    `).join('');document.querySelectorAll('[data-guide]').forEach(button=>button.onclick=()=>openScan(button.dataset.guide));$('#profiles').innerHTML=Object.entries(state.meta.profiles).map(([key,profile])=>``).join('');document.querySelectorAll('[name="profile"]').forEach(radio=>radio.onchange=()=>{state.profile=radio.value;updateScope();});$('#threshold').value=cfg.fail_under;$('#jobs').innerHTML=Array.from({length:8},(_,index)=>``).join('');$('#jobs').value=String(cfg.jobs);updateScope();} +function updateScope(){const profile=state.meta.profiles[state.profile];$('#scan-tools').textContent=profile.tools.join(' · ')||'No enabled checks';$('#execution-label').hidden=!profile.execution;$('#network-label').hidden=!profile.network;$('#allow-execution').checked=false;$('#allow-network').checked=false;} +function openScan(profile='quality'){if(!state.meta)return;state.profile=profile;document.querySelector(`[name="profile"][value="${profile}"]`).checked=true;updateScope();$('#scan-error').hidden=true;$('#scan-dialog').showModal();} +$('#new-scan').onclick=()=>openScan();$('#hero-scan').onclick=()=>openScan();$('#about').onclick=()=>{location.hash='guides';};window.addEventListener('hashchange',navigate);document.querySelectorAll('[data-close]').forEach(button=>button.onclick=()=>document.getElementById(button.dataset.close).close());for(const id of ['finding-search','finding-tool','finding-severity'])$(`#${id}`).addEventListener(id==='finding-search'?'input':'change',()=>{state.page=0;renderFindings();});$('#prev').onclick=()=>{state.page--;renderFindings();};$('#next').onclick=()=>{state.page++;renderFindings();};$('#refresh').onclick=guard(refresh); +$('#scan-form').onsubmit=async event=>{event.preventDefault();$('#submit-scan').disabled=true;$('#scan-error').hidden=true;try{const result=await api('/api/scan',{method:'POST',headers:{'Content-Type':'application/json','X-CodeProof-Token':state.meta.token},body:JSON.stringify({profile:state.profile,fail_under:Number($('#threshold').value),jobs:Number($('#jobs').value),allow_execution:$('#allow-execution').checked,allow_network:$('#allow-network').checked})});state.selected=result.id;state.page=0;$('#scan-dialog').close();location.hash='overview';error('');await refresh();}catch(exc){$('#scan-error').textContent=exc.message;$('#scan-error').hidden=false;}finally{$('#submit-scan').disabled=false;}}; +for(const id of ['baseline','current'])$(`#${id}`).onchange=()=>{state.compareTouched=true;$('#comparison').textContent='Choose Compare runs to update the comparison.';}; +$('#compare').onclick=guard(async()=>{const a=$('#baseline').value,b=$('#current').value;if(a===b){$('#comparison').textContent='Choose two different runs to compare.';return;}const [before,after]=await Promise.all([getReport(a),getReport(b)]);const entries=report=>report.results.flatMap(check=>check.findings.map(finding=>({key:JSON.stringify([check.name,finding.rule,finding.path,finding.line,finding.message]),finding,tool:check.name})));const previous=entries(before),current=entries(after);const oldKeys=new Set(previous.map(entry=>entry.key)),newKeys=new Set(current.map(entry=>entry.key));const added=current.filter(entry=>!oldKeys.has(entry.key)),resolved=previous.filter(entry=>!newKeys.has(entry.key));const signature=report=>JSON.stringify([report.fail_under,report.results.map(check=>[check.name,check.weight,check.required])]);const changed=signature(before)!==signature(after);$('#comparison').innerHTML=`${changed?'

    Check sets, weights, or thresholds differ. Scores are not directly comparable.

    ':''}
    ${(after.score-before.score)>0?'+':''}${(after.score-before.score).toFixed(2)}Score difference
    ${added.length}New findings
    ${resolved.length}Resolved findings
    ${[['New findings',added],['Resolved findings',resolved]].map(([label,entries])=>`

    ${label}

    ${entries.length?``:'

    None in the normalized evidence.

    '}`).join('')}

    Comparison uses tool, rule, location, and message. Raw-only diagnostics are not included; up to 100 findings per group are shown.

    `;}); +$('#download-policy').onclick=()=>{const url=URL.createObjectURL(new Blob([state.meta.policy_yaml],{type:'text/yaml'}));const anchor=document.createElement('a');anchor.href=url;anchor.download='.codeproof.yml';anchor.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}; +async function poll(){try{await refresh();}catch(exc){error(`Cannot refresh workspace: ${exc.message}`);}setTimeout(poll,state.runs.some(run=>run.status==='running')?1500:10000);} +navigate();guard(async()=>{state.meta=await api('/api/workspace');$('#repo-name').textContent=state.meta.project;renderTools();await poll();})(); diff --git a/src/codeproof/web/index.html b/src/codeproof/web/index.html new file mode 100644 index 0000000..ac9744e --- /dev/null +++ b/src/codeproof/web/index.html @@ -0,0 +1,19 @@ + +CodeProof · Developer workspace + + + +
    Local mode
    +
    BUILD WITH CONFIDENCE

    Repository overview

    Your code quality, security, and test evidence. In one place.

    +
    YOUR NEXT RELEASE STARTS HERE

    Ship with evidence.

    Find issues earlier. Understand what changed.
    Give every release a clear quality signal.

    +
    +

    Quality trend

    Your latest completed runs

    View history ↗

    Developer toolchain

    Ready for your next check

    ◇
    Explore all tools →
    +

    Latest scan results

    Start a scan to build your first report.

    Findings explorer

    0 findings
    + + + + +
    +
    START A VERIFICATION RUN

    Make your next commit count.

    Choose the right checks for this stage of your workflow.

    Checks in this run

    Required policy gates remain in effect.
    +

    Check evidence

    + diff --git a/src/codeproof/workspace.py b/src/codeproof/workspace.py new file mode 100644 index 0000000..2828dc4 --- /dev/null +++ b/src/codeproof/workspace.py @@ -0,0 +1,373 @@ +"""A repository-scoped local workspace with bounded, durable scan history.""" + +import json +import secrets +import shutil +import sqlite3 +import threading +from contextlib import contextmanager +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from typing import Literal +from urllib.parse import urlsplit +from uuid import uuid4 + +import yaml +from pydantic import BaseModel, ConfigDict, Field + +from .config import TOOL_INFO, CheckConfig, load +from .core import scan +from .models import ScanReport +from .reporting import html, markdown, sarif + +PROFILES = { + "quality": { + "label": "Code quality", + "description": "Lint, types, tests and security before you commit.", + "tools": ["syntax", "ruff", "mypy", "pytest", "bandit"], + }, + "quick": { + "label": "Fast feedback", + "description": "Static checks plus your required policy gates.", + "tools": ["syntax", "ruff", "mypy"], + }, + "security": { + "label": "Security review", + "description": "Security patterns and dependency vulnerabilities.", + "tools": ["syntax", "bandit", "semgrep", "pip-audit"], + }, + "policy": { + "label": "Repository policy", + "description": "Run every enabled check in your policy.", + "tools": [], + }, +} + + +class ScanRequest(BaseModel): + model_config = ConfigDict(extra="forbid") + profile: Literal["quality", "quick", "security", "policy"] = "quality" + fail_under: float | None = Field(default=None, ge=0, le=100, allow_inf_nan=False) + jobs: int = Field(default=1, ge=1, le=8) + allow_execution: bool = False + allow_network: bool = False + + +def selected_policy(root: Path, request: ScanRequest): + cfg = load(root) + if request.profile != "policy": + selected = set(PROFILES[request.profile]["tools"]) | set(cfg.required_tools) + for name in PROFILES[request.profile]["tools"]: + if name not in cfg.checks: + cfg.checks[name] = CheckConfig(weight=10) + for name, check in cfg.checks.items(): + check.enabled = check.enabled and name in selected + cfg.jobs = request.jobs + if request.fail_under is not None: + cfg.fail_under = request.fail_under + return cfg + + +def capabilities(cfg, root: Path): + enabled = [name for name, check in cfg.checks.items() if check.enabled] + execution = bool(set(enabled) & {"pytest", "imports", "pip-audit", "mypy"}) + semgrep_config = ( + cfg.checks["semgrep"].config or "auto" if "semgrep" in cfg.checks else "auto" + ) + network = "pip-audit" in enabled or ( + "semgrep" in enabled + and (semgrep_config == "auto" or not (root / semgrep_config).exists()) + ) + return enabled, execution, network + + +class Workspace: + def __init__(self, root: Path): + self.root = root.resolve() + if not self.root.is_dir(): + raise ValueError("Repository directory does not exist") + self.directory = self.root / ".codeproof" / "workspace" + self.directory.mkdir(parents=True, exist_ok=True) + self.db = self.directory / "history.sqlite3" + self.lock = threading.Lock() + self.active: str | None = None + self.token = secrets.token_urlsafe(32) + with self.connect() as db: + db.execute( + "CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, created TEXT NOT NULL, status TEXT NOT NULL, profile TEXT NOT NULL, report TEXT, error TEXT, score REAL, passed INTEGER, findings INTEGER)" + ) + db.execute( + "UPDATE runs SET status='interrupted', error='Workspace stopped before this scan completed' WHERE status='running'" + ) + + @contextmanager + def connect(self): + connection = sqlite3.connect(self.db, timeout=10) + try: + with connection: + yield connection + finally: + connection.close() + + def metadata(self): + cfg = load(self.root) + profiles = {} + for key, info in PROFILES.items(): + selected = selected_policy(self.root, ScanRequest(profile=key)) + enabled, execution, network = capabilities(selected, self.root) + profiles[key] = { + **info, + "tools": enabled, + "execution": execution, + "network": network, + } + return { + "project": self.root.name, + "target": str(self.root), + "token": self.token, + "policy": cfg.model_dump(mode="json"), + "policy_yaml": yaml.safe_dump(cfg.model_dump(mode="json"), sort_keys=False), + "profiles": profiles, + "tools": [ + { + "id": name, + "label": info[0], + "description": info[1], + "available": info[2] is None or bool(shutil.which(name)), + "install": f"pip install 'codeproof-ai[{info[2]}]'" + if info[2] + else "Included", + } + for name, info in TOOL_INFO.items() + ], + } + + def history(self): + with self.connect() as db: + rows = db.execute( + "SELECT id,created,status,profile,score,passed,findings,error FROM runs ORDER BY created DESC LIMIT 50" + ).fetchall() + summaries = [] + for id_, created, status, profile, score, passed, findings, error in rows: + summaries.append( + { + "id": id_, + "created": created, + "status": status, + "profile": profile, + "score": score, + "policy_passed": bool(passed) if passed is not None else None, + "findings": findings or 0, + "error": error, + } + ) + return summaries + + def report(self, id_: str): + with self.connect() as db: + row = db.execute("SELECT report FROM runs WHERE id=?", (id_,)).fetchone() + if not row or row[0] is None: + raise KeyError("Completed report not found") + return ScanReport.model_validate_json(row[0]) + + def start(self, request: ScanRequest): + cfg = selected_policy(self.root, request) + _, execution, network = capabilities(cfg, self.root) + if execution and not request.allow_execution: + raise ValueError( + "This profile executes repository or package code. Enable execution to continue." + ) + if network and not request.allow_network: + raise ValueError( + "This profile uses network services. Enable network access to continue." + ) + with self.lock: + if self.active: + raise RuntimeError("A scan is already running. Wait for it to finish.") + id_ = uuid4().hex + with self.connect() as db: + db.execute( + "INSERT INTO runs(id,created,status,profile) VALUES(?,?,?,?)", + ( + id_, + datetime.now(timezone.utc).isoformat(), + "running", + request.profile, + ), + ) + db.execute( + "DELETE FROM runs WHERE id NOT IN (SELECT id FROM runs ORDER BY created DESC LIMIT 50)" + ) + self.active = id_ + threading.Thread(target=self._execute, args=(id_, cfg), daemon=True).start() + return id_ + + def _execute(self, id_, cfg): + try: + report = scan(self.root, cfg) + with self.connect() as db: + db.execute( + "UPDATE runs SET status='complete',report=?,score=?,passed=?,findings=? WHERE id=?", + ( + report.model_dump_json(), + report.score, + int(report.policy_passed), + sum(len(r.findings) for r in report.results), + id_, + ), + ) + except Exception as exc: + with self.connect() as db: + db.execute( + "UPDATE runs SET status='error',error=? WHERE id=?", (str(exc), id_) + ) + finally: + with self.lock: + self.active = None + + +def make_server( + workspace: Workspace, host="127.0.0.1", port=8765, allowed_host: str | None = None +): + assets = Path(__file__).with_name("web") + allowed = {"localhost", "127.0.0.1", "::1"} + if allowed_host: + allowed.add(allowed_host.lower()) + + class Handler(BaseHTTPRequestHandler): + def setup(self): + super().setup() + self.connection.settimeout(15) + + def log_message(self, *args): + pass + + def respond( + self, data, status=200, content_type="application/json", attachment=None + ): + body = ( + json.dumps(data).encode() + if content_type == "application/json" + else data.encode() + ) + self.send_response(status) + self.send_header("Content-Type", content_type + "; charset=utf-8") + self.send_header("Content-Length", str(len(body))) + self.send_header("Cache-Control", "no-store") + self.send_header("X-Content-Type-Options", "nosniff") + self.send_header("X-Frame-Options", "DENY") + self.send_header("Referrer-Policy", "no-referrer") + self.send_header( + "Content-Security-Policy", + "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'", + ) + if attachment: + self.send_header( + "Content-Disposition", f'attachment; filename="{attachment}"' + ) + self.end_headers() + self.wfile.write(body) + + def valid_host(self): + try: + return ( + urlsplit("http://" + self.headers.get("Host", "")).hostname + in allowed + ) + except ValueError: + return False + + def do_GET(self): + if not self.valid_host(): + self.respond({"error": "Host is not allowed"}, 403) + return + path = urlsplit(self.path).path + try: + if path == "/api/workspace": + self.respond(workspace.metadata()) + elif path == "/api/runs": + self.respond(workspace.history()) + elif path.startswith("/api/runs/"): + parts = path.split("/") + if len(parts) not in (4, 5): + raise KeyError("Not found") + report = workspace.report(parts[3]) + if len(parts) == 4: + self.respond(report.model_dump(mode="json")) + else: + exports = { + "json": ( + report.model_dump_json(indent=2), + "application/json", + ), + "html": (html(report), "text/html"), + "markdown": (markdown(report), "text/markdown"), + "sarif": ( + json.dumps(sarif(report), indent=2), + "application/sarif+json", + ), + } + if parts[4] not in exports: + raise KeyError("Unknown export") + content, kind = exports[parts[4]] + # Download documents rather than render tool output under the workspace origin. + self.respond( + content, + content_type=kind + if kind != "application/json" + else "application/octet-stream", + attachment=f"codeproof-{parts[3]}.{ {'markdown': 'md'}.get(parts[4], parts[4]) }", + ) + elif path in ("/", "/app.css", "/app.js"): + filename = { + "/": "index.html", + "/app.css": "app.css", + "/app.js": "app.js", + }[path] + kind = { + "/": "text/html", + "/app.css": "text/css", + "/app.js": "text/javascript", + }[path] + self.respond( + (assets / filename).read_text(encoding="utf-8"), + content_type=kind, + ) + else: + self.respond({"error": "Not found"}, 404) + except KeyError: + self.respond({"error": "Report not found"}, 404) + except (ValueError, TypeError, OSError, yaml.YAMLError) as exc: + self.respond({"error": str(exc)}, 400) + + def do_POST(self): + if not self.valid_host() or not secrets.compare_digest( + self.headers.get("X-CodeProof-Token", ""), workspace.token + ): + self.respond({"error": "Invalid workspace session"}, 403) + return + origin = self.headers.get("Origin") + if origin and origin not in ( + "http://" + self.headers.get("Host", ""), + "https://" + self.headers.get("Host", ""), + ): + self.respond({"error": "Origin is not allowed"}, 403) + return + if self.path != "/api/scan": + self.respond({"error": "Not found"}, 404) + return + try: + length = int(self.headers.get("Content-Length", "0")) + if length <= 0 or length > 65536: + raise ValueError("Request must contain 1–65536 bytes") + request = ScanRequest.model_validate_json(self.rfile.read(length)) + self.respond({"id": workspace.start(request)}, 202) + except RuntimeError as exc: + self.respond({"error": str(exc)}, 409) + except (ValueError, TypeError, OSError, yaml.YAMLError) as exc: + self.respond({"error": str(exc)}, 400) + + server = ThreadingHTTPServer((host, port), Handler) + server.daemon_threads = True + return server diff --git a/tests/test_adapters.py b/tests/test_adapters.py new file mode 100644 index 0000000..607cb11 --- /dev/null +++ b/tests/test_adapters.py @@ -0,0 +1,92 @@ +import subprocess + +from codeproof.adapters import imports, parse_findings, run, syntax + + +def test_syntax_does_not_execute_or_write_bytecode(tmp_path): + (tmp_path / "valid.py").write_text('raise RuntimeError("must not execute")\n') + result = syntax(tmp_path, ["."]) + assert result.status == "passed" + assert not (tmp_path / "__pycache__").exists() + (tmp_path / "bad.py").write_text("def broken(:\n") + result = syntax(tmp_path, ["."]) + assert result.status == "failed" + assert result.findings[0].path == "bad.py" + assert result.findings[0].line == 1 + + +def test_traversal_prunes_environments_and_duplicates(tmp_path): + (tmp_path / ".venv").mkdir() + (tmp_path / ".venv" / "bad.py").write_text("def broken(:") + (tmp_path / "ok.py").write_text("x = 1") + result = syntax(tmp_path, [".", "."]) + assert result.status == "passed" + assert result.evidence["files_checked"] == 1 + assert syntax(tmp_path, ["absent"]).status == "skipped" + + +def test_import_timeout_is_a_result(monkeypatch, tmp_path): + (tmp_path / "slow.py").write_text("") + + def timeout(*args, **kwargs): + raise subprocess.TimeoutExpired(args[0], 1) + + monkeypatch.setattr(subprocess, "run", timeout) + assert imports(tmp_path, ["."], 1).status == "error" + + +def test_subprocess_timeout_and_launch_failure(monkeypatch, tmp_path): + monkeypatch.setattr("shutil.which", lambda name: "/bin/tool") + for error in (subprocess.TimeoutExpired("ruff", 1), OSError("cannot start")): + + def fail(*args, error=error, **kwargs): + raise error + + monkeypatch.setattr(subprocess, "run", fail) + assert run("ruff", tmp_path, None, 1, ["."], ["tests"]).status == "error" + + +def test_findings_parsed_before_raw_truncation_and_stderr(monkeypatch, tmp_path): + monkeypatch.setattr("shutil.which", lambda name: "/bin/ruff") + payload = ( + '[{"message":"unused","filename":"app.py","location":{"row":3},"code":"F401"}]' + ) + monkeypatch.setattr( + subprocess, + "run", + lambda *a, **k: subprocess.CompletedProcess(a[0], 1, payload, "diagnostic"), + ) + result = run("ruff", tmp_path, None, 1, ["."], ["tests"]) + assert result.findings[0].rule == "F401" + assert result.findings[0].line == 3 + assert "diagnostic" in result.raw_output + + +def test_supported_json_schemas(): + assert ( + parse_findings( + "bandit", + '{"results":[{"issue_text":"unsafe","test_id":"B1","line_number":4}]}', + )[0].rule + == "B1" + ) + assert ( + parse_findings( + "semgrep", '{"results":[{"extra":{"message":"unsafe"},"check_id":"S1"}]}' + )[0].rule + == "S1" + ) + assert ( + parse_findings( + "pip-audit", + '{"dependencies":[{"name":"demo","version":"1","vulns":[{"id":"CVE-TEST"}]}]}', + )[0].rule + == "CVE-TEST" + ) + assert parse_findings("ruff", "invalid output") == [] + + +def test_dependency_audit_does_not_fall_back_to_environment(tmp_path): + result = run("pip-audit", tmp_path, None, 1, ["."], ["tests"]) + assert result.status == "unavailable" + assert "requirements.txt" in result.summary diff --git a/tests/test_config.py b/tests/test_config.py index a0712e4..bc107ca 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,2 +1,5 @@ from codeproof.config import DEFAULT -def test_weights():assert sum(x.weight for x in DEFAULT.checks.values())==100 + + +def test_weights(): + assert sum(x.weight for x in DEFAULT.checks.values()) == 100 diff --git a/tests/test_core.py b/tests/test_core.py index 1ca5caf..266646e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,3 +1,5 @@ from codeproof.core import grade + + def test_grade(): - assert grade(90)=="A" and grade(70)=="C" and grade(59)=="F" + assert grade(90) == "A" and grade(70) == "C" and grade(59) == "F" diff --git a/tests/test_discovery.py b/tests/test_discovery.py new file mode 100644 index 0000000..fb1ba1c --- /dev/null +++ b/tests/test_discovery.py @@ -0,0 +1,25 @@ +import ast + +from codeproof.discovery import generate + + +def test_generates_all_symbols_once_and_preserves_manual_edits(tmp_path): + src = tmp_path / "src" / "pkg" + src.mkdir(parents=True) + (src / "api.py").write_text( + "def first(): pass\nasync def second(): pass\nclass Third: pass\n" + ) + (src / "__init__.py").write_text("def exported(): pass\n") + out = tmp_path / "generated" + made = generate(tmp_path, ["src"], out) + assert len(made) == 2 + text = next(p.read_text() for p in made if "first" in p.read_text()) + tree = ast.parse(text) + assert len([n for n in tree.body if isinstance(n, ast.FunctionDef)]) == 3 + package = next(p for p in made if "exported" in p.read_text()) + assert "import_module('pkg')" in package.read_text() + package.write_text("# manual") + assert generate(tmp_path, ["src"], out) == [] + assert package.read_text() == "# manual" + assert len(generate(tmp_path, ["src"], out, force=True)) == 2 + assert "exported" in package.read_text() diff --git a/tests/test_policy.py b/tests/test_policy.py new file mode 100644 index 0000000..cae49a2 --- /dev/null +++ b/tests/test_policy.py @@ -0,0 +1,97 @@ +from pathlib import Path +from threading import Barrier + +import pytest +from pydantic import ValidationError + +from codeproof.config import DEFAULT, CheckConfig, Config, load +from codeproof.core import scan +from codeproof.models import CheckResult, Status + + +def test_partial_policy_keeps_checks_and_weights(tmp_path): + (tmp_path / ".codeproof.yml").write_text("checks:\n ruff:\n weight: 22\n") + config = load(tmp_path) + assert config.checks["ruff"].weight == 22 + assert config.checks["pytest"].weight == 30 + assert config.checks["semgrep"].config == "auto" + config.checks["pytest"].enabled = False + assert DEFAULT.checks["pytest"].enabled + + +@pytest.mark.parametrize( + "text", + [ + "[]", + "checks: []", + "checks:\n ruff: false", + "jobs: 0", + "fail_under: 101", + "fail_under: .nan", + "required_tools: [typo]", + "checks:\n ruff:\n weight: -1", + "version: 2", + "typo: true", + ], +) +def test_invalid_policy(tmp_path, text): + (tmp_path / ".codeproof.yml").write_text(text) + with pytest.raises((ValueError, TypeError, ValidationError)): + load(tmp_path) + + +def test_explicit_missing_policy(tmp_path): + with pytest.raises(ValueError, match="not found"): + load(tmp_path, Path("missing.yml")) + + +def result(name, root, cfg, timeout, sources, tests): + return CheckResult(name=name, status=Status.passed) + + +def test_disabled_and_absent_required_tools_fail(monkeypatch, tmp_path): + monkeypatch.setattr("codeproof.core.run", result) + cfg = Config( + checks={"syntax": CheckConfig(weight=100), "ruff": CheckConfig(enabled=False)} + ) + report = scan(tmp_path, cfg) + assert report.score == 100 + assert not report.policy_passed + assert {r.name for r in report.results if r.required} == {"ruff", "pytest"} + + +def test_empty_evidence_never_passes_at_zero(tmp_path): + assert not scan(tmp_path, Config(required_tools=[], fail_under=0)).policy_passed + + +def test_optional_unavailable_excluded_but_required_blocks(monkeypatch, tmp_path): + def run(name, *args): + return CheckResult( + name=name, status=Status.passed if name == "syntax" else Status.unavailable + ) + + monkeypatch.setattr("codeproof.core.run", run) + cfg = Config( + required_tools=[], checks={"syntax": CheckConfig(), "ruff": CheckConfig()} + ) + report = scan(tmp_path, cfg) + assert report.score == 100 and report.policy_passed + cfg.required_tools = ["ruff"] + report = scan(tmp_path, cfg) + assert report.score == 50 and not report.policy_passed + + +def test_parallel_checks_preserve_order(monkeypatch, tmp_path): + barrier = Barrier(2, timeout=5) + + def run(name, *args): + barrier.wait() + return CheckResult(name=name, status=Status.passed) + + monkeypatch.setattr("codeproof.core.run", run) + cfg = Config( + required_tools=[], checks={"syntax": CheckConfig(), "ruff": CheckConfig()} + ) + report = scan(tmp_path, cfg, jobs=2) + assert [r.name for r in report.results] == ["syntax", "ruff"] + assert report.policy_passed diff --git a/tests/test_reports_cli.py b/tests/test_reports_cli.py new file mode 100644 index 0000000..7933d2e --- /dev/null +++ b/tests/test_reports_cli.py @@ -0,0 +1,85 @@ +import json + +from typer.testing import CliRunner + +from codeproof.cli import app +from codeproof.config import Config +from codeproof.core import scan +from codeproof.models import CheckResult, Finding, Status +from codeproof.reporting import html, markdown, write + +runner = CliRunner() + + +def test_report_escapes_untrusted_evidence(tmp_path): + report = scan(tmp_path, Config(required_tools=[])) + report.project = "@@cards@@" + report.results = [ + CheckResult( + name="ruff", + status=Status.failed, + summary="a | b\nnext", + raw_output="", + findings=[Finding(message="", path="a.py", line=2)], + ) + ] + page = html(report) + assert "