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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ stricter subset of Keep a Changelog).

### Changed

- WebUI endpoints (`/api/runs/{run_id}/*`, `/run/{run_id}/*`) now validate that `run_id` contains only alphanumeric characters, underscores, and hyphens, rejecting invalid identifiers with HTTP 400.
- Auditing CRS type: does not require a target harness and produces bug-candidates
- `oss-crs export` and `oss-crs import` commands — imports/exports docker images/CRS source code to transfer to another host.
- Run-phase modules now default to `target_dependent: true`, so their images are built once per target during `build-target`. Set `target_dependent: false` for modules that can be built once during `prepare`.
Expand Down
19 changes: 19 additions & 0 deletions oss-crs-infra/webui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import logging
import os
import re
import signal
import threading
import time
Expand Down Expand Up @@ -37,6 +38,8 @@
# A run with no snapshot for this long is considered "stale" (publisher gone /
# unreachable). Publishers push every ~5s, so this tolerates a few missed polls.
STALE_AFTER_SECONDS = 25
# Enforce safe alphanumeric run_id format to prevent path traversal or malformed files
RUN_ID_REGEX = re.compile(r"^[a-zA-Z0-9_\-]+$")

logging.basicConfig(
level=logging.INFO,
Expand Down Expand Up @@ -89,7 +92,16 @@ def status(self) -> str:
_runs: dict[str, RunState] = {}


def _validate_run_id(run_id: str) -> None:
if not run_id or not RUN_ID_REGEX.match(run_id):
raise HTTPException(
status_code=400,
detail="Invalid run_id: must contain only alphanumeric characters, underscores, and hyphens",
)


def _get_or_create_run(run_id: str) -> RunState:
_validate_run_id(run_id)
if run_id not in _runs:
_runs[run_id] = RunState(run_id=run_id)
return _runs[run_id]
Expand Down Expand Up @@ -388,6 +400,7 @@ def list_runs():

@app.get("/api/runs/{run_id}/status")
def run_status(run_id: str):
_validate_run_id(run_id)
with _lock:
run = _runs.get(run_id)
if not run:
Expand All @@ -403,6 +416,7 @@ def run_status(run_id: str):

@app.get("/api/runs/{run_id}/history")
def run_history(run_id: str, last: int = 360):
_validate_run_id(run_id)
with _lock:
run = _runs.get(run_id)
if not run:
Expand All @@ -422,6 +436,7 @@ def dashboard():

@app.get("/run/{run_id}", response_class=HTMLResponse)
def run_detail(run_id: str):
_validate_run_id(run_id)
with _lock:
run = _runs.get(run_id)
if not run:
Expand Down Expand Up @@ -490,6 +505,7 @@ def collect(base: str) -> dict[str, Path]:

@app.get("/api/runs/{run_id}/artifacts")
def artifact_counts(run_id: str):
_validate_run_id(run_id)
# Live per-type counts read straight from the workdir, so Net Artifacts
# always matches what's actually downloadable (and catches late artifacts
# like a report produced right at teardown, which the publisher can miss).
Expand All @@ -498,6 +514,7 @@ def artifact_counts(run_id: str):

@app.get("/api/runs/{run_id}/artifacts/{atype}")
def list_artifacts(run_id: str, atype: str):
_validate_run_id(run_id)
if atype not in ARTIFACT_TYPES:
raise HTTPException(status_code=404, detail="unknown artifact type")
files = _find_artifact_files(run_id, atype)
Expand All @@ -510,6 +527,7 @@ def list_artifacts(run_id: str, atype: str):

@app.get("/api/runs/{run_id}/artifacts/{atype}/download")
def download_artifact(run_id: str, atype: str, name: str):
_validate_run_id(run_id)
# Only serve a file whose name exactly matches one we listed — this both
# resolves the real path and prevents path traversal via `name`.
for f in _find_artifact_files(run_id, atype):
Expand All @@ -522,6 +540,7 @@ def download_artifact(run_id: str, atype: str, name: str):

@app.get("/run/{run_id}/artifacts/{atype}", response_class=HTMLResponse)
def artifacts_page(run_id: str, atype: str):
_validate_run_id(run_id)
if atype not in ARTIFACT_TYPES:
return HTMLResponse(status_code=404, content="unknown artifact type")
template = _templates.get_template("artifacts.html")
Expand Down
100 changes: 100 additions & 0 deletions oss_crs/tests/unit/test_webui_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# SPDX-License-Identifier: MIT
"""Tests for the webui server endpoints and run_id validation."""

import importlib.util
from pathlib import Path
from types import ModuleType
import urllib.parse

import pytest
from starlette.testclient import TestClient

REPO_ROOT = Path(__file__).resolve().parents[3]
WEBUI_DIR = REPO_ROOT / "oss-crs-infra" / "webui"


@pytest.fixture()
def webui_module(tmp_path: Path) -> ModuleType:
# Use isolated LOG_DIR and WORKDIR
log_dir = tmp_path / "webui_logs"
workdir = tmp_path / "workdir"
log_dir.mkdir(parents=True)
workdir.mkdir(parents=True)

spec = importlib.util.spec_from_file_location("webui_main", WEBUI_DIR / "main.py")
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
setattr(module, "LOG_DIR", log_dir)
setattr(module, "WORKDIR", workdir)
spec.loader.exec_module(module)
return module


@pytest.fixture()
def client(webui_module: ModuleType) -> TestClient:
return TestClient(webui_module.app)


def test_health(client: TestClient) -> None:
resp = client.get("/health")
assert resp.status_code == 200
assert resp.json() == {"status": "ok"}


def test_register_and_get_status_valid_run_id(client: TestClient) -> None:
valid_id = "run-123_test"
register_payload = {
"target": "target-a",
"crs_names": ["crs-1", "crs-2"],
"crs_resources": {},
"harness": "fuzz_target",
}
resp = client.post(f"/api/runs/{valid_id}/register", json=register_payload)
assert resp.status_code == 200
assert resp.json() == {"status": "ok"}

status_resp = client.get(f"/api/runs/{valid_id}/status")
assert status_resp.status_code == 200
data = status_resp.json()
assert data["run_id"] == valid_id
assert data["target"] == "target-a"
assert data["status"] == "live"


@pytest.mark.parametrize(
"invalid_id",
[
"run;injection",
"run*glob",
"run space",
"run$bad",
"run|pipe",
"run!exclamation",
"run`backtick`",
],
)
def test_endpoints_reject_invalid_run_id(client: TestClient, invalid_id: str) -> None:
encoded = urllib.parse.quote(invalid_id, safe="")
resp = client.post(f"/api/runs/{encoded}/register", json={})
assert resp.status_code == 400
assert "Invalid run_id" in resp.text

resp = client.get(f"/api/runs/{encoded}/status")
assert resp.status_code == 400
assert "Invalid run_id" in resp.text

resp = client.get(f"/api/runs/{encoded}/history")
assert resp.status_code == 400
assert "Invalid run_id" in resp.text

resp = client.get(f"/api/runs/{encoded}/artifacts")
assert resp.status_code == 400
assert "Invalid run_id" in resp.text

resp = client.get(f"/api/runs/{encoded}/artifacts/povs")
assert resp.status_code == 400
assert "Invalid run_id" in resp.text

resp = client.get(f"/api/runs/{encoded}/artifacts/povs/download?name=test")
assert resp.status_code == 400
assert "Invalid run_id" in resp.text