Sicherheit
- install_db.php war ohne Authentifizierung erreichbar und setzte bei jedem
Aufruf das Admin-Passwort auf einen fest im Code stehenden Wert zurück.
Jetzt Auth-Pflicht; ein Konto wird nur bei leerer Benutzertabelle angelegt.
- Stored XSS im Bugtracker-Detail-Modal: Titel, Beschreibung, Fehlermeldung,
Stacktrace und Kommentare gingen ungefiltert durch innerHTML.
- report.php, projects.php und das Veröffentlichen von Releases verlangen jetzt
zwingend ein Token. Publish war zuvor völlig ungeschützt.
- CSRF-Token in allen Formularen, Session-Regenerierung nach Login,
Drosselung fehlgeschlagener Anmeldeversuche.
- Zugangsdaten aus der Versionskontrolle entfernt (Serverdaten.txt,
config.php, .htpasswd, deploy_config.json). Historie enthält sie weiterhin,
Rotation erforderlich (siehe docs/UPGRADE.md).
- Token-Validierung nur noch über SHA-256-Hash; expires_at wird ausgewertet.
Behobene 500er
- Audit::log() war in index.php weder eingebunden noch importiert. Jeder
Klick auf "Aktivierung freigeben" endete in einem Fatal Error.
- Derselbe benannte PDO-Platzhalter mehrfach je Statement (:id in
revokeToken/deleteToken, :q siebenfach in der Volltextsuche). Bei
EMULATE_PREPARES=false ist das nicht zulässig und warf HY093.
- Migration 005 nutzte dynamisches SQL, dessen Semikolons in String-Literalen
vom alten explode(';')-Installer als Statement-Ende gelesen wurden. Sie
schlug still fehl, wodurch push_id/target_agent/tags dauerhaft fehlten.
- Monitor-Umbenennung ohne Transaktion, verschachtelte Transaktionen im
RateLimiter.
Funktionale Korrekturen
- Der Watchdog-Evaluator fehlte vollständig: Monitor-Zustände änderten sich nur
beim Eintreffen eines Heartbeats, ein ausgefallenes System blieb dauerhaft
"up". Erster Lauf auf dem Produktivsystem: 7 von 10 Monitoren waren
tatsächlich seit über einem Tag nicht erreichbar.
- Das Feld "os" fehlte im Monitor-Dialog, wurde aber gespeichert und löschte
damit bei jedem Speichern das Betriebssystem.
- Der Resolve-Dialog existierte im HTML nicht; der Button war funktionslos.
- Versionsvergleich erfolgte lexikografisch, wodurch 1.9.0 als neuer galt
als 1.10.0.
- Schreiboperationen meldeten Erfolg auch für nicht existierende IDs.
- Post/Redirect/Get gegen doppelte Einträge beim Neuladen.
Neue Struktur
- src/bootstrap.php mit PSR-4-Autoloader ersetzt die require-Ketten.
- Core: Config, Http, Csrf, ApiAuth, Logger, Migrator, ErrorReporter.
- Migrator mit zeichenweisem SQL-Parser, dc_migrations und Baseline-Verfahren,
damit bestehende Installationen keine Beispieldaten zurückbekommen.
Agenten-Workflow
- Claim/Lease: Items werden exklusiv übernommen, damit nicht zwei Agenten am
selben Problem arbeiten. action=next holt und reserviert in einem Zug.
- Idempotenz über client_ref, Deduplizierung auch für Feature Requests,
Erkennung von Regressionen, automatische Eskalation des Schweregrads.
- Strukturierter Code-Kontext (repo_url, commit_sha, file_path, line_no).
- Delta-Abfragen über updated_since, Pagination, Bulk-Update.
- Beim Veröffentlichen eines Releases schließen sich Items mit passendem
resolved_in_build selbst.
- Ausgehende Webhooks mit HMAC-Signatur, /api/health, /api/openapi.json.
- Unbehandelte Fehler meldet die Plattform in ihren eigenen Bugtracker.
WebUI
- Serverseitige Filterung mit Pagination statt Rendern aller Datensätze.
- Migrations-Schranke, Evaluator-Warnung, Übersicht aktiver Agenten.
Zeitstempel liegen in der Datenbank durchgängig in UTC und werden für die
Anzeige in die App-Zeitzone umgerechnet.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
335 lines
11 KiB
PHP
335 lines
11 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Bugtracker Management-API - die Schnittstelle fuer Coding-Agenten.
|
|
*
|
|
* Basis: /api/bugtracker/v1/manage
|
|
*
|
|
* Lesen (Scope bugtracker:read):
|
|
* GET ?action=list Gefilterte Liste mit Pagination und Delta-Abfrage
|
|
* GET ?action=get&id=42 Einzelnes Item samt Kommentar-Historie
|
|
* GET ?action=stats Kennzahlen
|
|
* GET ?action=projects Projektliste
|
|
*
|
|
* Schreiben (Scope bugtracker:manage):
|
|
* POST ?action=claim&id=42 Item exklusiv uebernehmen
|
|
* POST ?action=next Naechste offene Items holen und uebernehmen
|
|
* POST ?action=release&id=42 Item wieder freigeben
|
|
* POST ?action=comment&id=42 Kommentar / Ermittlungsschritt anhaengen
|
|
* POST ?action=status&id=42 Status setzen
|
|
* POST ?action=update&id=42 Mehrere Felder aendern
|
|
* POST ?action=resolve&id=42 Als geloest markieren
|
|
* POST ?action=bulk_update Mehrere Items auf einmal aendern
|
|
*
|
|
* ROUTING-AENDERUNG: Die Zuordnung erfolgt jetzt ueber eine feste Aktionsliste.
|
|
* Zuvor wurde per str_contains() im Pfad gesucht, wodurch jede URL, die
|
|
* zufaellig "/status" oder "/update" enthielt, die Route uebernahm.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../../../../src/bootstrap.php';
|
|
|
|
use Deploymentcenter\Core\ApiAuth;
|
|
use Deploymentcenter\Core\Db;
|
|
use Deploymentcenter\Core\Http;
|
|
use Deploymentcenter\Modules\Bugtracker\BugRepo;
|
|
|
|
Http::beginJson(['GET', 'POST', 'OPTIONS'], true);
|
|
|
|
const READ_ACTIONS = ['list', 'get', 'stats', 'projects'];
|
|
const WRITE_ACTIONS = ['claim', 'next', 'release', 'comment', 'status', 'update', 'resolve', 'bulk_update'];
|
|
|
|
$db = Db::init();
|
|
$repo = new BugRepo($db);
|
|
|
|
$action = resolveAction();
|
|
$method = Http::method();
|
|
|
|
if (!in_array($action, READ_ACTIONS, true) && !in_array($action, WRITE_ACTIONS, true)) {
|
|
Http::fail(404, 'unknown_action', sprintf('Unbekannte Aktion "%s".', $action), null, [
|
|
'available' => array_merge(READ_ACTIONS, WRITE_ACTIONS),
|
|
]);
|
|
}
|
|
|
|
$isWrite = in_array($action, WRITE_ACTIONS, true);
|
|
|
|
if ($isWrite && $method !== 'POST') {
|
|
Http::fail(405, 'method_not_allowed', sprintf('Die Aktion "%s" erwartet POST.', $action));
|
|
}
|
|
|
|
$context = ApiAuth::requireScope($db, $isWrite ? 'bugtracker:manage' : 'bugtracker:read');
|
|
$author = $context['actor'];
|
|
$boundProject = ApiAuth::projectFilter($context);
|
|
|
|
$itemId = resolveItemId();
|
|
|
|
switch ($action) {
|
|
|
|
// ---------------------------------------------------------------- lesen
|
|
case 'projects':
|
|
Http::ok(['projects' => $repo->getProjects()]);
|
|
// no break - Http::ok beendet die Anfrage
|
|
|
|
case 'stats':
|
|
$slug = Http::str('project_slug') ?? $boundProject;
|
|
Http::ok(['stats' => $repo->getStats($slug)]);
|
|
|
|
case 'get':
|
|
requireItemId($itemId);
|
|
$item = $repo->getItemDetails($itemId);
|
|
if ($item === null) {
|
|
Http::fail(404, 'not_found', sprintf('Item #%d existiert nicht.', $itemId));
|
|
}
|
|
ApiAuth::enforceProject($context, (string)$item['project_slug']);
|
|
Http::ok(['item' => $item]);
|
|
|
|
case 'list':
|
|
$filters = collectFilters($boundProject);
|
|
$result = $repo->getItems($filters);
|
|
Http::ok([
|
|
'count' => count($result['items']),
|
|
'total' => $result['total'],
|
|
'limit' => $result['limit'],
|
|
'offset' => $result['offset'],
|
|
'has_more' => $result['has_more'],
|
|
'filters' => $filters,
|
|
'items' => $result['items'],
|
|
]);
|
|
|
|
// -------------------------------------------------------------- schreiben
|
|
case 'claim':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
$claimed = $repo->claimItem($itemId, $author, Http::int('lease_minutes', 0) ?: null);
|
|
if ($claimed === null) {
|
|
Http::fail(409, 'already_claimed', sprintf(
|
|
'Item #%d ist bereits vergeben oder nicht mehr offen.',
|
|
$itemId
|
|
));
|
|
}
|
|
Http::ok(['item' => $claimed, 'message' => sprintf('Item #%d uebernommen.', $itemId)]);
|
|
|
|
case 'next':
|
|
$filters = collectFilters($boundProject);
|
|
$limit = Http::int('limit', 1);
|
|
$claimedItems = $repo->claimNext($author, $filters, $limit);
|
|
Http::ok([
|
|
'count' => count($claimedItems),
|
|
'items' => $claimedItems,
|
|
'message' => $claimedItems === []
|
|
? 'Aktuell keine offenen Items verfuegbar.'
|
|
: sprintf('%d Item(s) uebernommen.', count($claimedItems)),
|
|
]);
|
|
|
|
case 'release':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
if (!$repo->releaseItem($itemId, $author, Http::str('note'))) {
|
|
Http::fail(409, 'not_claimed', sprintf(
|
|
'Item #%d ist nicht von "%s" beansprucht.',
|
|
$itemId,
|
|
$author
|
|
));
|
|
}
|
|
Http::ok(['message' => sprintf('Item #%d freigegeben.', $itemId)]);
|
|
|
|
case 'comment':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
$comment = Http::str('comment');
|
|
if ($comment === null) {
|
|
Http::fail(400, 'missing_comment', 'Das Feld "comment" darf nicht leer sein.');
|
|
}
|
|
|
|
$meta = Http::input('meta');
|
|
$created = $repo->addComment(
|
|
$itemId,
|
|
$author,
|
|
$comment,
|
|
Http::str('action_taken') ?? 'commented',
|
|
is_array($meta) ? $meta : null
|
|
);
|
|
Http::ok(['comment' => $created], 201);
|
|
|
|
case 'status':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
$status = Http::str('status');
|
|
if ($status === null) {
|
|
Http::fail(400, 'missing_status', 'Das Feld "status" fehlt.', null, [
|
|
'allowed' => BugRepo::STATUSES,
|
|
]);
|
|
}
|
|
|
|
if (!$repo->updateStatus($itemId, $status, Http::str('notes'), $author)) {
|
|
Http::fail(400, 'invalid_status', sprintf(
|
|
'Status "%s" ist unbekannt oder Item #%d existiert nicht.',
|
|
$status,
|
|
$itemId
|
|
), null, ['allowed' => BugRepo::STATUSES]);
|
|
}
|
|
|
|
Http::ok(['message' => sprintf('Status von #%d auf "%s" gesetzt.', $itemId, $status)]);
|
|
|
|
case 'update':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
if (!$repo->updateItemDetails($itemId, Http::body(), $author)) {
|
|
Http::fail(404, 'not_found', sprintf('Item #%d existiert nicht.', $itemId));
|
|
}
|
|
Http::ok(['message' => sprintf('Item #%d aktualisiert.', $itemId)]);
|
|
|
|
case 'resolve':
|
|
requireItemId($itemId);
|
|
assertProject($repo, $context, $itemId);
|
|
|
|
$build = Http::str('resolved_in_build');
|
|
if ($build === null) {
|
|
Http::fail(400, 'missing_build', 'Das Feld "resolved_in_build" wird benoetigt.');
|
|
}
|
|
|
|
if (!$repo->resolveItem($itemId, $build, Http::str('resolution_notes'), $author)) {
|
|
Http::fail(404, 'not_found', sprintf('Item #%d existiert nicht.', $itemId));
|
|
}
|
|
Http::ok(['message' => sprintf('Item #%d in Build "%s" geloest.', $itemId, $build)]);
|
|
|
|
case 'bulk_update':
|
|
$ids = Http::input('ids');
|
|
if (!is_array($ids) || $ids === []) {
|
|
Http::fail(400, 'missing_ids', 'Das Feld "ids" muss eine nicht leere Liste sein.');
|
|
}
|
|
if (count($ids) > 200) {
|
|
Http::fail(400, 'too_many_ids', 'Maximal 200 Items pro Aufruf.');
|
|
}
|
|
|
|
$updates = Http::input('updates');
|
|
if (!is_array($updates) || $updates === []) {
|
|
Http::fail(400, 'missing_updates', 'Das Feld "updates" muss die zu setzenden Felder enthalten.');
|
|
}
|
|
|
|
$result = $repo->bulkUpdate($ids, $updates, $author);
|
|
Http::ok([
|
|
'updated' => $result['updated'],
|
|
'failed' => $result['failed'],
|
|
'message' => sprintf('%d Item(s) aktualisiert.', $result['updated']),
|
|
]);
|
|
}
|
|
|
|
// ======================================================================
|
|
// Hilfsfunktionen
|
|
// ======================================================================
|
|
|
|
/**
|
|
* Ermittelt die Aktion aus ?action= oder aus dem letzten Pfadsegment.
|
|
* Ohne Angabe: "get" bei vorhandener ID, sonst "list".
|
|
*/
|
|
function resolveAction(): string
|
|
{
|
|
$explicit = $_GET['action'] ?? null;
|
|
if (is_string($explicit) && $explicit !== '') {
|
|
return strtolower(trim($explicit));
|
|
}
|
|
|
|
// Pfadform: /manage/items/42/comment -> "comment"
|
|
$path = trim(Http::path(), '/');
|
|
$segments = array_values(array_filter(explode('/', $path), static fn(string $s): bool => $s !== ''));
|
|
|
|
$last = end($segments);
|
|
if (is_string($last)) {
|
|
$candidate = strtolower($last);
|
|
if (in_array($candidate, READ_ACTIONS, true) || in_array($candidate, WRITE_ACTIONS, true)) {
|
|
return $candidate;
|
|
}
|
|
}
|
|
|
|
return resolveItemId() > 0 ? 'get' : 'list';
|
|
}
|
|
|
|
/** Item-ID aus Query, Body oder Pfad (/items/42). */
|
|
function resolveItemId(): int
|
|
{
|
|
$fromRequest = $_GET['id'] ?? null;
|
|
if (is_numeric($fromRequest)) {
|
|
return (int)$fromRequest;
|
|
}
|
|
|
|
if (Http::method() === 'POST') {
|
|
$body = Http::body();
|
|
if (isset($body['id']) && is_numeric($body['id'])) {
|
|
return (int)$body['id'];
|
|
}
|
|
if (isset($body['item_id']) && is_numeric($body['item_id'])) {
|
|
return (int)$body['item_id'];
|
|
}
|
|
}
|
|
|
|
if (preg_match('#/items/(\d+)#', Http::path(), $m) === 1) {
|
|
return (int)$m[1];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
function requireItemId(int $itemId): void
|
|
{
|
|
if ($itemId <= 0) {
|
|
Http::fail(400, 'missing_id', 'Es wurde keine Item-ID uebergeben (?id=... oder /items/<id>).');
|
|
}
|
|
}
|
|
|
|
/** Stellt sicher, dass ein projektgebundenes Token das Item anfassen darf. */
|
|
function assertProject(BugRepo $repo, array $context, int $itemId): void
|
|
{
|
|
if (ApiAuth::projectFilter($context) === null) {
|
|
return;
|
|
}
|
|
|
|
$item = $repo->getItemDetails($itemId);
|
|
if ($item === null) {
|
|
Http::fail(404, 'not_found', sprintf('Item #%d existiert nicht.', $itemId));
|
|
}
|
|
|
|
ApiAuth::enforceProject($context, (string)$item['project_slug']);
|
|
}
|
|
|
|
/**
|
|
* Sammelt Filter aus Query und Body.
|
|
*
|
|
* @return array<string,mixed>
|
|
*/
|
|
function collectFilters(?string $boundProject): array
|
|
{
|
|
$filters = [
|
|
'project_slug' => Http::str('project_slug') ?? Http::str('project') ?? 'all',
|
|
'environment' => Http::str('environment') ?? Http::str('env') ?? 'all',
|
|
'type' => Http::str('type') ?? 'all',
|
|
'status' => Http::str('status') ?? 'all',
|
|
'severity' => Http::str('severity') ?? 'all',
|
|
'push_id' => Http::str('push_id') ?? '',
|
|
'target_agent' => Http::str('target_agent') ?? Http::str('agent') ?? '',
|
|
'claimed_by' => Http::str('claimed_by') ?? '',
|
|
'search' => Http::str('search') ?? Http::str('q') ?? '',
|
|
'updated_since' => Http::str('updated_since') ?? '',
|
|
'order' => Http::str('order') ?? 'newest',
|
|
'limit' => Http::int('limit', 100),
|
|
'offset' => Http::int('offset', 0),
|
|
];
|
|
|
|
if (Http::input('unclaimed_only') !== null) {
|
|
$filters['unclaimed_only'] = filter_var(Http::input('unclaimed_only'), FILTER_VALIDATE_BOOLEAN);
|
|
}
|
|
|
|
// Ein projektgebundenes Token kann den Projektfilter nicht umgehen.
|
|
if ($boundProject !== null) {
|
|
$filters['project_slug'] = $boundProject;
|
|
}
|
|
|
|
return $filters;
|
|
}
|