Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Reject invalid reporting time ranges

rcourtman87594e6701f1
diff --git a/docs/API.md b/docs/API.md
index 3d4cf99713..3f194c5f10 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -417,6 +417,7 @@ Triggers a test alert to all configured channels.
Validation and limits:
- `start` and `end` must be RFC3339 when provided.
+- Malformed `start`/`end` values, or `end` earlier than `start`, return `400 invalid_time_range`.
- `start` must be before `end`.
- Maximum report window is 366 days.
- `metricType` must match `[a-zA-Z0-9._:-]+` and be <= 64 chars.
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 747894806f..0384722119 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -284,6 +284,12 @@ deep-link, and validation reads in `internal/api/router_routes_ai_relay.go`
must accept that `relay:mobile:access` scope directly instead of demanding the
broader settings-read privilege that the pairing token was never meant to
carry.
+That same adjacent `internal/api/` reporting surface also keeps lifecycle-
+adjacent automation on the canonical time-window transport contract. Any setup,
+handoff, or scheduled lifecycle flow that triggers performance reports must
+treat reporting `start`/`end` values as optional RFC3339 fields owned by the
+API contract, with malformed or inverted ranges rejected as
+`400 invalid_time_range` rather than silently drifting to a fallback window.
The direct Proxmox fallback workspace now also lives explicitly inside this
lifecycle boundary: `InfrastructureWorkspace.tsx`,
`infrastructureWorkspaceModel.ts`,
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 99cff1c002..5857bb9b0c 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -290,6 +290,10 @@ consumers may validate or reject malformed payloads, but they must not invent
replacement report endpoints, filename prefixes, export routes, or default
range windows from frontend-local fallback constants once the catalog has been
accepted.
+Reporting time windows follow the same rule: `start` and `end` stay optional,
+but when present they must parse as RFC3339 and `end` must not be earlier than
+`start`; invalid values are a `400 invalid_time_range` transport failure, not a
+silent fallback to the default reporting window.
The catalog route itself is intentionally metadata-readable without the
`advanced_reporting` feature gate so locked admin surfaces can present the same
canonical reporting definition before upsell, while report generation and
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 403c55c1c0..d4c979500c 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -293,6 +293,11 @@ downloads, but they must treat allowed formats, multi-resource caps, optional
metric/title support, default fallback range windows, attachment filename
stems, and invalid-format validation copy as API/reporting contract rather than
rebuilding local reporting constants.
+That same transport contract also owns report time-window validation. Storage-
+and recovery-adjacent flows may omit `start`/`end` to use the canonical default
+window, but when they provide either bound it must be RFC3339 and `end` must
+not be earlier than `start`; invalid values fail as `400 invalid_time_range`
+instead of silently shifting the exported reporting window.
That adjacent export contract now also includes canonical Proxmox pool
membership for each VM row. Storage and recovery flows may use those current-
state facts when they need fleet context, but they must consume the API-owned
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index bdbd56ff10..701d979644 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -558,6 +558,40 @@ func TestContract_PerformanceReportTransportUsesCatalogDefaultRange(t *testing.T
}
}
+func TestContract_PerformanceReportTransportRejectsInvalidTimeRange(t *testing.T) {
+ engine := &stubReportingEngine{data: []byte("report"), contentType: "application/pdf"}
+ original := reporting.GetEngine()
+ reporting.SetEngine(engine)
+ t.Cleanup(func() { reporting.SetEngine(original) })
+
+ handler := NewReportingHandlers(nil, nil)
+ req := httptest.NewRequest(
+ http.MethodGet,
+ "/api/reporting?resourceType=node&resourceId=node-1&start=2026-03-25T12:00:00Z&end=2026-03-25T11:00:00Z",
+ nil,
+ )
+ rec := httptest.NewRecorder()
+
+ handler.HandleGenerateReport(rec, req)
+
+ if rec.Code != http.StatusBadRequest {
+ t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String())
+ }
+ var payload struct {
+ Code string `json:"code"`
+ Error string `json:"error"`
+ }
+ if err := json.NewDecoder(rec.Body).Decode(&payload); err != nil {
+ t.Fatalf("decode invalid-time-range response: %v", err)
+ }
+ if payload.Code != "invalid_time_range" {
+ t.Fatalf("expected invalid_time_range code, got %q", payload.Code)
+ }
+ if payload.Error != "end must be after start" {
+ t.Fatalf("expected canonical invalid_time_range message, got %q", payload.Error)
+ }
+}
+
func TestContract_ReportingInvalidFormatErrorsUseCatalogDefinitions(t *testing.T) {
engine := &stubReportingEngine{data: []byte("report"), contentType: "application/pdf"}
original := reporting.GetEngine()
diff --git a/internal/api/metrics_reporting_handlers.go b/internal/api/metrics_reporting_handlers.go
index 130941cd16..6d5abe0fc9 100644
--- a/internal/api/metrics_reporting_handlers.go
+++ b/internal/api/metrics_reporting_handlers.go
@@ -81,22 +81,30 @@ func normalizePerformanceReportTimeRange(
startRaw string,
endRaw string,
now time.Time,
-) (time.Time, time.Time) {
+) (time.Time, time.Time, error) {
end := now
if strings.TrimSpace(endRaw) != "" {
- if parsed, err := time.Parse(time.RFC3339, endRaw); err == nil {
- end = parsed
+ parsed, err := time.Parse(time.RFC3339, endRaw)
+ if err != nil {
+ return time.Time{}, time.Time{}, fmt.Errorf("end must be RFC3339")
}
+ end = parsed
}
start := end.Add(-definition.DefaultRangeDuration())
if strings.TrimSpace(startRaw) != "" {
- if parsed, err := time.Parse(time.RFC3339, startRaw); err == nil {
- start = parsed
+ parsed, err := time.Parse(time.RFC3339, startRaw)
+ if err != nil {
+ return time.Time{}, time.Time{}, fmt.Errorf("start must be RFC3339")
}
+ start = parsed
}
- return start, end
+ if end.Before(start) {
+ return time.Time{}, time.Time{}, fmt.Errorf("end must be after start")
+ }
+
+ return start, end, nil
}
type reportingEnrichmentSnapshot struct {
@@ -350,12 +358,16 @@ func (h *ReportingHandlers) HandleGenerateReport(w http.ResponseWriter, r *http.
q.Get("title"),
)
- start, end := normalizePerformanceReportTimeRange(
+ start, end, err := normalizePerformanceReportTimeRange(
definition,
q.Get("start"),
q.Get("end"),
time.Now(),
)
+ if err != nil {
+ writeErrorResponse(w, http.StatusBadRequest, "invalid_time_range", err.Error(), nil)
+ return
+ }
req := reporting.MetricReportRequest{
ResourceType: resourceType,
@@ -687,7 +699,11 @@ func (h *ReportingHandlers) HandleGenerateMultiReport(w http.ResponseWriter, r *
}
metricType, title := normalizePerformanceReportOptionalFields(definition, body.MetricType, body.Title)
- start, end := normalizePerformanceReportTimeRange(definition, body.Start, body.End, time.Now())
+ start, end, err := normalizePerformanceReportTimeRange(definition, body.Start, body.End, time.Now())
+ if err != nil {
+ writeErrorResponse(w, http.StatusBadRequest, "invalid_time_range", err.Error(), nil)
+ return
+ }
// Build multi-report request
multiReq := reporting.MultiReportRequest{
diff --git a/internal/api/reporting_handlers_test.go b/internal/api/reporting_handlers_test.go
index f33d0266e8..d79f595daa 100644
--- a/internal/api/reporting_handlers_test.go
+++ b/internal/api/reporting_handlers_test.go
@@ -416,6 +416,106 @@ func TestReportingHandlers_GenerateReport_UsesCatalogDefaultRangeDuration(t *tes
}
}
+func TestReportingHandlers_GenerateReport_RejectsInvalidTimeRange(t *testing.T) {
+ engine := &stubReportingEngine{data: []byte("report"), contentType: "application/pdf"}
+ original := reporting.GetEngine()
+ reporting.SetEngine(engine)
+ t.Cleanup(func() { reporting.SetEngine(original) })
+
+ handler := NewReportingHandlers(nil, nil)
+
+ tests := []struct {
+ name string
+ url string
+ wantMessage string
+ }{
+ {
+ name: "invalid start",
+ url: "/api/reporting?format=pdf&resourceType=node&resourceId=node-1&start=not-a-time",
+ wantMessage: "start must be RFC3339",
+ },
+ {
+ name: "invalid end",
+ url: "/api/reporting?format=pdf&resourceType=node&resourceId=node-1&end=not-a-time",
+ wantMessage: "end must be RFC3339",
+ },
+ {
+ name: "end before start",
+ url: "/api/reporting?format=pdf&resourceType=node&resourceId=node-1&start=2026-03-25T12:00:00Z&end=2026-03-25T11:00:00Z",
+ wantMessage: "end must be after start",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ req := httptest.NewRequest(http.MethodGet, tc.url, nil)
+ rr := httptest.NewRecorder()
+
+ handler.HandleGenerateReport(rr, req)
+
+ if rr.Code != http.StatusBadRequest {
+ t.Fatalf("expected status %d, got %d body=%s", http.StatusBadRequest, rr.Code, rr.Body.String())
+ }
+ if !strings.Contains(rr.Body.String(), tc.wantMessage) {
+ t.Fatalf("expected error %q, got %s", tc.wantMessage, rr.Body.String())
+ }
+ if engine.lastReq.ResourceID != "" {
+ t.Fatalf("expected engine not to be called, got %+v", engine.lastReq)
+ }
+ })
+ }
+}
+
+func TestReportingHandlers_GenerateMultiReport_RejectsInvalidTimeRange(t *testing.T) {
+ engine := &stubReportingEngine{data: []byte("report"), contentType: "application/pdf"}
+ original := reporting.GetEngine()
+ reporting.SetEngine(engine)
+ t.Cleanup(func() { reporting.SetEngine(original) })
+
+ handler := NewReportingHandlers(nil, nil)
+
+ tests := []struct {
+ name string
+ body string
+ wantMessage string
+ }{
+ {
+ name: "invalid start",
+ body: `{"resources":[{"resourceType":"vm","resourceId":"vm-1"}],"format":"pdf","start":"not-a-time"}`,
+ wantMessage: "start must be RFC3339",
+ },
+ {
+ name: "invalid end",
+ body: `{"resources":[{"resourceType":"vm","resourceId":"vm-1"}],"format":"pdf","end":"not-a-time"}`,
+ wantMessage: "end must be RFC3339",
+ },
+ {
+ name: "end before start",
+ body: `{"resources":[{"resourceType":"vm","resourceId":"vm-1"}],"format":"pdf","start":"2026-03-25T12:00:00Z","end":"2026-03-25T11:00:00Z"}`,
+ wantMessage: "end must be after start",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ req := httptest.NewRequest(http.MethodPost, "/api/reporting/generate-multi", strings.NewReader(tc.body))
+ rr := httptest.NewRecorder()
+
+ handler.HandleGenerateMultiReport(rr, req)
+
+ if rr.Code != http.StatusBadRequest {
+ t.Fatalf("expected status %d, got %d body=%s", http.StatusBadRequest, rr.Code, rr.Body.String())
+ }
+ if !strings.Contains(rr.Body.String(), tc.wantMessage) {
+ t.Fatalf("expected error %q, got %s", tc.wantMessage, rr.Body.String())
+ }
+ if len(engine.lastMulti.Resources) != 0 {
+ t.Fatalf("expected engine not to be called, got %+v", engine.lastMulti)
+ }
+ })
+ }
+}
+
func TestReportingHandlers_ExportVMInventory_EmptySnapshotStillReturnsCSVHeader(t *testing.T) {
handler := NewReportingHandlers(nil, nil)
req := httptest.NewRequest(http.MethodGet, "/api/admin/reports/inventory/vms/export", nil)