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 expired trials in hosted subscription gate

rcourtman0f4c831fabec
diff --git a/internal/api/middleware_tenant.go b/internal/api/middleware_tenant.go
index f8909968e2..529eb6da80 100644
--- a/internal/api/middleware_tenant.go
+++ b/internal/api/middleware_tenant.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"strings"
+ "time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
@@ -269,16 +270,27 @@ func GetOrganization(ctx context.Context) *models.Organization {
// isHostedSubscriptionValid checks whether a hosted Cloud tenant has a valid subscription
// or a bounded trial. This replaces the FeatureMultiTenant check for hosted mode, where
// tenant routing is infrastructure rather than a paid feature.
+//
+// Trial validity requires both that an end timestamp exists AND that the
+// current time has not passed it. A non-nil TrialEndsAt alone is not enough —
+// a stale "trial" subscription state with an expired timestamp on disk would
+// otherwise grant indefinite access if lease refresh ever fails.
func isHostedSubscriptionValid(ctx context.Context) bool {
svc := getLicenseServiceForContext(ctx)
subState := strings.ToLower(strings.TrimSpace(svc.SubscriptionState()))
eval := svc.Evaluator()
- hasTrialEnd := eval != nil && eval.TrialEndsAt() != nil
switch subState {
case subStateActive, subStateGrace:
return true
case subStateTrial:
- return hasTrialEnd
+ if eval == nil {
+ return false
+ }
+ trialEnd := eval.TrialEndsAt()
+ if trialEnd == nil {
+ return false
+ }
+ return time.Now().Unix() < *trialEnd
default:
return false
}
diff --git a/internal/api/middleware_tenant_additional_test.go b/internal/api/middleware_tenant_additional_test.go
index 92a800d8e2..8b69e7d51d 100644
--- a/internal/api/middleware_tenant_additional_test.go
+++ b/internal/api/middleware_tenant_additional_test.go
@@ -258,6 +258,32 @@ func TestTenantMiddleware_HostedMode_UnboundedTrial_Blocked(t *testing.T) {
}
}
+// TestTenantMiddleware_HostedMode_ExpiredTrial_Blocked is the regression test
+// for a fix where isHostedSubscriptionValid only checked that TrialEndsAt was
+// non-nil, not that the trial had not yet expired. A tenant whose trial ended
+// in the past but whose stored subscription state was still "trial" with a
+// non-nil TrialEndsAt would otherwise retain full Cloud access indefinitely.
+func TestTenantMiddleware_HostedMode_ExpiredTrial_Blocked(t *testing.T) {
+ trialEnd := time.Now().Add(-24 * time.Hour).Unix() // ended yesterday
+ setupHostedLicenseProvider(t, pkglicensing.SubStateTrial, &trialEnd)
+
+ mw := NewTenantMiddlewareWithConfig(TenantMiddlewareConfig{
+ HostedMode: true,
+ })
+
+ req := httptest.NewRequest(http.MethodGet, "/", nil)
+ req.Header.Set("X-Pulse-Org-ID", "cloud-expired-trial")
+ rec := httptest.NewRecorder()
+
+ mw.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Fatalf("handler should NOT be called for expired trial")
+ })).ServeHTTP(rec, req)
+
+ if rec.Code != http.StatusPaymentRequired {
+ t.Fatalf("expected 402 for expired trial, got %d", rec.Code)
+ }
+}
+
func TestTenantMiddleware_HostedMode_ExpiredSubscription_Blocked(t *testing.T) {
setupHostedLicenseProvider(t, pkglicensing.SubStateExpired, nil)