From 34c02ac2c4e371a86ded97bedce3a77c161fe2e6 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 13:30:31 -0500 Subject: [PATCH 1/2] feat: security reports, acknowledgements and moving submissions New reports start in Received with a timeline entry and get a private SecurityReportTriage; attachments are copied under the report and deleted with it. Fixed or disclosed reports with public credit get a SecurityAcknowledgement. MoveToSecurity decisions create a report owned by the submitter from the submission, copy its files and delete it. Admission checks credit links and external reporters' contact details, keeps the timeline append-only, and adds a timeline entry for a phase change that lacks one. --- README.md | 18 ++- config/milo/rbac/feedback-operator.yaml | 6 +- config/milo/webhook/manifests.yaml | 40 +++++ internal/controller/controller_test.go | 112 ++++++++++++++ internal/controller/securityreport.go | 195 ++++++++++++++++++++++++ internal/controller/setup.go | 2 + internal/controller/submission.go | 2 +- internal/webhook/register.go | 2 + internal/webhook/securityreport.go | 166 ++++++++++++++++++++ internal/webhook/securityreport_test.go | 101 ++++++++++++ 10 files changed, 638 insertions(+), 6 deletions(-) create mode 100644 internal/controller/securityreport.go create mode 100644 internal/webhook/securityreport.go create mode 100644 internal/webhook/securityreport_test.go diff --git a/README.md b/README.md index 7fa9457..70df3c4 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ webhooks. ## Status The CRDs are defined. The operator sets owners and reviewers in admission, -reconciles votes, follows and submission reviews, and stores attachments. -Triage, security reports and digests are tracked as sub-issues of the epic. +reconciles votes, follows, submission reviews and security reports, and +stores attachments. Triage, notifications and digests, audit and search +config are tracked as sub-issues of the epic. | Resource | Who writes it | Who reads it | | --- | --- | --- | @@ -86,6 +87,19 @@ GET /{submissions|securityreports}/{name}/attachments/{ref} (staff) - **Lifecycle rule.** The bucket's lifecycle rule should delete `uploads/` after a day. - **Configuration.** Set the bucket in the `feedback-config` ConfigMap (`attachments-bucket`). The operator and the upload service share it. +## Security reports + +- **Admission** (`securityreport` webhook): + - Credit links must be http(s), and public credit needs a name. + - Reports with no owner (logged by the Security Team for someone without an account) need the reporter's email or GitHub username. + - Status updates can add timeline entries but not change or remove them. A phase change needs an entry for the new phase; the `securitytimeline` webhook adds it if the update didn't. `NotApplicable` and `Duplicate` need a reason, credit rewards need an amount, and advisories need an https link. +- **`security-report` reconciler:** + - A new report starts in `Received` with its first timeline entry. + - It creates the report's `SecurityReportTriage` for the Security Team's private notes. + - It copies attachments under `security-reports//` and deletes them with the report. + - A `Fixed` or `Disclosed` report with public credit gets a `SecurityAcknowledgement` with the name, link, month and severity. It's deleted if that stops being true. +- **`move-to-security` reconciler:** carries out `MoveToSecurity` review decisions. It creates a report owned by the submitter and named after the submission, copies the attachments, then deletes the submission, which takes its review and files with it. + ## Roles | Role | For | diff --git a/config/milo/rbac/feedback-operator.yaml b/config/milo/rbac/feedback-operator.yaml index 85aa949..5c8e3f1 100644 --- a/config/milo/rbac/feedback-operator.yaml +++ b/config/milo/rbac/feedback-operator.yaml @@ -1,7 +1,7 @@ # feedback-operator reconciles every feedback resource: it watches them all, # writes post votes and delivery, follows and votes it creates for users, -# review outcomes, reports moved from submissions, acknowledgements and -# digests. It never writes a post's GitHub fields or a user's spec. +# review outcomes, reports moved from submissions, empty triage objects for +# new reports, acknowledgements and digests. It never writes a post's GitHub fields or a user's spec. apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: @@ -17,7 +17,7 @@ rules: resources: [posts/status, votes/status, submissions/status, submissionreviews/status, securityreports/status] verbs: [get, update, patch] - apiGroups: [feedback.miloapis.com] - resources: [votes, subscriptions, submissionreviews, securityreports, securityacknowledgements, feedbackdigests] + resources: [votes, subscriptions, submissionreviews, securityreports, securityreporttriages, securityacknowledgements, feedbackdigests] verbs: [create, update, patch, delete] # delete for reports moved to the security track; update and patch only to # add and remove the attachments finalizer. diff --git a/config/milo/webhook/manifests.yaml b/config/milo/webhook/manifests.yaml index c6ce0bc..01741df 100644 --- a/config/milo/webhook/manifests.yaml +++ b/config/milo/webhook/manifests.yaml @@ -67,12 +67,52 @@ webhooks: resources: - submissionreviews sideEffects: None +- admissionReviewVersions: + - v1 + clientConfig: + service: + name: feedback-operator-webhook + namespace: feedback-system + path: /mutate-securityreport-status + failurePolicy: Fail + name: securitytimeline.feedback.miloapis.com + rules: + - apiGroups: + - feedback.miloapis.com + apiVersions: + - v1alpha1 + operations: + - UPDATE + resources: + - securityreports/status + sideEffects: None --- apiVersion: admissionregistration.k8s.io/v1 kind: ValidatingWebhookConfiguration metadata: name: validating-webhook-configuration webhooks: +- admissionReviewVersions: + - v1 + clientConfig: + service: + name: feedback-operator-webhook + namespace: feedback-system + path: /validate-securityreport + failurePolicy: Fail + name: securityreport.feedback.miloapis.com + rules: + - apiGroups: + - feedback.miloapis.com + apiVersions: + - v1alpha1 + operations: + - CREATE + - UPDATE + resources: + - securityreports + - securityreports/status + sideEffects: None - admissionReviewVersions: - v1 clientConfig: diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index ea9d581..1d37ccd 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -446,3 +446,115 @@ func TestSubmissionAttachments(t *testing.T) { return nil }) } + +func TestSecurityReports(t *testing.T) { + ctx := context.Background() + files, err := attachments.Open(ctx, "mem://") + if err != nil { + t.Fatal(err) + } + c := start(t, &clock{now: time.Now()}, files) + get := func(obj client.Object) error { return c.Get(ctx, client.ObjectKeyFromObject(obj), obj) } + + t.Run("a new report is received, with private triage", func(t *testing.T) { + rep := &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: "sec-1"}, Spec: v1.SecurityReportSpec{ + UserRef: &v1.UserReference{Name: "u1"}, Title: "t", Description: "d", + Credit: &v1.SecurityCredit{Public: true, Name: "Ada L.", URL: "https://example.com"}}} + if err := c.Create(ctx, rep); err != nil { + t.Fatal(err) + } + eventually(t, "received", func() error { + if err := get(rep); err != nil { + return err + } + if rep.Status.Phase != v1.SecurityReportPhaseReceived || len(rep.Status.Timeline) != 1 { + return errorf("status " + string(rep.Status.Phase)) + } + return get(&v1.SecurityReportTriage{ObjectMeta: metav1.ObjectMeta{Name: "sec-1"}}) + }) + + // Fixed, with public credit: acknowledged. + rep.Status.Phase, rep.Status.Severity = v1.SecurityReportPhaseFixed, v1.SeverityHigh + rep.Status.Timeline = append(rep.Status.Timeline, v1.SecurityTimelineEntry{ + Phase: v1.SecurityReportPhaseFixed, At: metav1.NewTime(time.Date(2026, 10, 30, 0, 0, 0, 0, time.UTC))}) + if err := c.Status().Update(ctx, rep); err != nil { + t.Fatal(err) + } + ack := &v1.SecurityAcknowledgement{ObjectMeta: metav1.ObjectMeta{Name: "sec-1"}} + eventually(t, "acknowledged", func() error { + if err := get(ack); err != nil { + return err + } + if ack.Status.Name != "Ada L." || ack.Status.Month != "2026-10" || ack.Status.Severity != v1.SeverityHigh { + return errorf("ack " + ack.Status.Month) + } + return nil + }) + }) + + t.Run("no public credit, no acknowledgement", func(t *testing.T) { + rep := &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: "sec-2"}, Spec: v1.SecurityReportSpec{ + Reporter: &v1.ExternalReporter{Email: "r@example.com"}, Title: "t", Description: "d"}} + if err := c.Create(ctx, rep); err != nil { + t.Fatal(err) + } + eventually(t, "received", func() error { + if err := get(rep); err != nil { + return err + } + if rep.Status.Phase == "" { + return errorf("no phase") + } + return nil + }) + rep.Status.Phase = v1.SecurityReportPhaseDisclosed + if err := c.Status().Update(ctx, rep); err != nil { + t.Fatal(err) + } + time.Sleep(time.Second) + if err := get(&v1.SecurityAcknowledgement{ObjectMeta: metav1.ObjectMeta{Name: "sec-2"}}); !apierrors.IsNotFound(err) { + t.Errorf("acknowledgement exists: %v", err) + } + }) + + t.Run("a misfiled submission moves to the security track", func(t *testing.T) { + ref, _, err := files.Upload(ctx, attachments.Submission, "u7", "poc.txt", "text/plain", []byte("poc")) + if err != nil { + t.Fatal(err) + } + sub := &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: "sub-sec"}, Spec: v1.SubmissionSpec{ + UserRef: v1.UserReference{Name: "u7"}, Kind: v1.SubmissionKindBug, Title: "Viewer reads secrets", Body: "steps", + Attachments: []v1.AttachmentReference{{UploadRef: ref}}}} + if err := c.Create(ctx, sub); err != nil { + t.Fatal(err) + } + eventually(t, "decide", func() error { + r := &v1.SubmissionReview{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} + if err := get(r); err != nil { + return err + } + if err := get(sub); err != nil || len(sub.Finalizers) == 0 { + return errorf("submission's files not copied yet") + } + r.Spec = v1.SubmissionReviewSpec{Decision: v1.ReviewDecisionMoveToSecurity, Affected: "secrets"} + return c.Update(ctx, r) + }) + rep := &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} + eventually(t, "moved", func() error { + if err := get(rep); err != nil { + return err + } + if rep.Spec.UserRef == nil || rep.Spec.UserRef.Name != "u7" || rep.Spec.Affected != "secrets" { + return errorf("report spec") + } + if err := get(&v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}}); !apierrors.IsNotFound(err) { + return errorf("submission still there") + } + if _, err := files.Get(ctx, attachments.Submission, sub.Name, "", ref); err == nil { + return errorf("submission's copy not deleted") + } + _, err := files.Get(ctx, attachments.SecurityReport, rep.Name, "", ref) + return err + }) + }) +} diff --git a/internal/controller/securityreport.go b/internal/controller/securityreport.go new file mode 100644 index 0000000..df54dec --- /dev/null +++ b/internal/controller/securityreport.go @@ -0,0 +1,195 @@ +package controller + +import ( + "context" + "fmt" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/predicate" + + "go.miloapis.com/feedback/internal/attachments" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// SecurityReportReconciler looks after each security report: +// +// - A new report starts in Received, with a timeline entry the reporter +// sees. +// - It gets its SecurityReportTriage, owned by the report, for the +// Security Team's private notes. +// - Its attachments are copied under it and deleted with it. +// - Once it is Fixed or Disclosed and the reporter chose public credit, it +// has a SecurityAcknowledgement with the credited name, link, month and +// severity; otherwise it has none. +type SecurityReportReconciler struct { + client.Client + Options +} + +// Reconcile implements reconcile.Reconciler. +func (r *SecurityReportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var rep v1.SecurityReport + if err := r.Get(ctx, req.NamespacedName, &rep); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + owner := "" + if rep.Spec.UserRef != nil { + owner = rep.Spec.UserRef.Name + } + if done, err := keepAttachments(ctx, r.Client, r.Files, attachments.SecurityReport, &rep, owner, rep.Spec.Attachments); done || err != nil { + return ctrl.Result{}, err + } + + triage := &v1.SecurityReportTriage{ObjectMeta: metav1.ObjectMeta{Name: rep.Name}} + if err := controllerutil.SetOwnerReference(&rep, triage, r.Scheme()); err != nil { + return ctrl.Result{}, err + } + if err := r.Create(ctx, triage); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, err + } + + if rep.Status.Phase == "" { + rep.Status.Phase = v1.SecurityReportPhaseReceived + rep.Status.Timeline = append(rep.Status.Timeline, v1.SecurityTimelineEntry{ + Phase: v1.SecurityReportPhaseReceived, At: metav1.NewTime(r.now().UTC().Truncate(time.Second)), + }) + if err := r.Status().Update(ctx, &rep); err != nil { + return ctrl.Result{}, err + } + } + return ctrl.Result{}, r.acknowledge(ctx, &rep) +} + +// acknowledge creates, updates or deletes the report's acknowledgement. +func (r *SecurityReportReconciler) acknowledge(ctx context.Context, rep *v1.SecurityReport) error { + ack := &v1.SecurityAcknowledgement{ObjectMeta: metav1.ObjectMeta{Name: rep.Name}} + public := rep.Spec.Credit != nil && rep.Spec.Credit.Public && rep.Spec.Credit.Name != "" + fixedAt := fixedAt(rep) + if !public || fixedAt == nil { + return client.IgnoreNotFound(r.Delete(ctx, ack)) + } + want := v1.SecurityAcknowledgementStatus{ + Name: rep.Spec.Credit.Name, + URL: rep.Spec.Credit.URL, + Month: fixedAt.UTC().Format("2006-01"), + Severity: rep.Status.Severity, + } + err := r.Get(ctx, client.ObjectKeyFromObject(ack), ack) + switch { + case apierrors.IsNotFound(err): + ack.Status = want + if err := controllerutil.SetOwnerReference(rep, ack, r.Scheme()); err != nil { + return err + } + return r.Create(ctx, ack) + case err != nil: + return err + case ack.Status == want: + return nil + } + ack.Status = want + return r.Update(ctx, ack) +} + +// fixedAt is when the report first reached Fixed or Disclosed, or nil if it +// isn't there now. +func fixedAt(rep *v1.SecurityReport) *metav1.Time { + switch rep.Status.Phase { + case v1.SecurityReportPhaseFixed, v1.SecurityReportPhaseDisclosed: + default: + return nil + } + for _, e := range rep.Status.Timeline { + if e.Phase == v1.SecurityReportPhaseFixed || e.Phase == v1.SecurityReportPhaseDisclosed { + return &e.At + } + } + return &rep.CreationTimestamp +} + +// SetupWithManager registers the reconciler. +func (r *SecurityReportReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + Named("security-report"). + For(&v1.SecurityReport{}). + Owns(&v1.SecurityAcknowledgement{}). + Complete(r) +} + +// MoveReconciler carries out MoveToSecurity decisions: it creates a +// SecurityReport owned by the submitter from the submission, named after it so +// a retry finds it, copies the attachments under it, then deletes the +// submission (and with it its review and files). +type MoveReconciler struct { + client.Client + Options +} + +// Reconcile implements reconcile.Reconciler. +func (r *MoveReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var review v1.SubmissionReview + if err := r.Get(ctx, req.NamespacedName, &review); err != nil || review.Spec.Decision != v1.ReviewDecisionMoveToSecurity { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + var sub v1.Submission + if err := r.Get(ctx, req.NamespacedName, &sub); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + if sub.DeletionTimestamp != nil { + return ctrl.Result{}, nil + } + + var existing v1.SecurityReport + err := r.Get(ctx, types.NamespacedName{Name: sub.Name}, &existing) + switch { + case apierrors.IsNotFound(err): + rep := &v1.SecurityReport{ + ObjectMeta: metav1.ObjectMeta{Name: sub.Name}, + Spec: v1.SecurityReportSpec{ + UserRef: &v1.UserReference{Name: sub.Spec.UserRef.Name}, + Title: sub.Spec.Title, + Description: sub.Spec.Body, + Affected: review.Spec.Affected, + Attachments: sub.Spec.Attachments, + Context: v1.SecurityReportContext{ + Client: sub.Spec.Context.Client, Organization: sub.Spec.Context.Organization, + ClientVersion: sub.Spec.Context.ClientVersion, + }, + }, + } + if len(sub.Spec.Attachments) > 0 { + if r.Files == nil { + return ctrl.Result{}, fmt.Errorf("can't move %s: it has attachments and there is no attachments bucket", sub.Name) + } + if err := r.Files.CopyParent(ctx, attachments.Submission, attachments.SecurityReport, sub.Name, rep.Name); err != nil { + return ctrl.Result{}, fmt.Errorf("copying attachments: %w", err) + } + // Already copied, so the report reconciler only deletes them. + controllerutil.AddFinalizer(rep, AttachmentsFinalizer) + } + if err := r.Create(ctx, rep); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, err + } + case err != nil: + return ctrl.Result{}, err + } + return ctrl.Result{}, client.IgnoreNotFound(r.Delete(ctx, &sub)) +} + +// SetupWithManager registers the reconciler for reviews with a +// MoveToSecurity decision. +func (r *MoveReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + Named("move-to-security"). + For(&v1.SubmissionReview{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool { + return obj.(*v1.SubmissionReview).Spec.Decision == v1.ReviewDecisionMoveToSecurity + }))). + Complete(r) +} diff --git a/internal/controller/setup.go b/internal/controller/setup.go index 8e21351..ea1e114 100644 --- a/internal/controller/setup.go +++ b/internal/controller/setup.go @@ -47,6 +47,8 @@ func Setup(ctx context.Context, mgr ctrl.Manager, o Options) error { &MergeReconciler{Client: c}, &SubmissionReconciler{Client: c, Files: o.Files}, &ReviewReconciler{Client: c}, + &SecurityReportReconciler{Client: c, Options: o}, + &MoveReconciler{Client: c, Options: o}, } { if err := r.SetupWithManager(mgr); err != nil { return err diff --git a/internal/controller/submission.go b/internal/controller/submission.go index b2814ee..33733e8 100644 --- a/internal/controller/submission.go +++ b/internal/controller/submission.go @@ -111,7 +111,7 @@ func (r *SubmissionReconciler) SetupWithManager(mgr ctrl.Manager) error { // name; then the submitter follows (reason submitted) and votes, and the // submission links to the post. // -// MoveToSecurity is carried out by the security report reconciler. +// MoveToSecurity is carried out by MoveReconciler. // status.applied is set once a decision is fully carried out. type ReviewReconciler struct { client.Client diff --git a/internal/webhook/register.go b/internal/webhook/register.go index 02cd023..930d15a 100644 --- a/internal/webhook/register.go +++ b/internal/webhook/register.go @@ -32,4 +32,6 @@ func Register(mgr manager.Manager, o Options) { Submissions: c, Limit: o.SubmissionLimit, Window: o.SubmissionWindow, }}) s.Register("/validate-submissionreview", &webhook.Admission{Handler: ReviewCheck{Posts: c}}) + s.Register("/validate-securityreport", &webhook.Admission{Handler: SecurityReportCheck{}}) + s.Register("/mutate-securityreport-status", &webhook.Admission{Handler: SecurityTimeline{}}) } diff --git a/internal/webhook/securityreport.go b/internal/webhook/securityreport.go new file mode 100644 index 0000000..f0a015c --- /dev/null +++ b/internal/webhook/securityreport.go @@ -0,0 +1,166 @@ +package webhook + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + "regexp" + "time" + + admissionv1 "k8s.io/api/admission/v1" + "k8s.io/apimachinery/pkg/api/equality" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/validation/field" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +var ( + amountRE = regexp.MustCompile(`^[0-9]+(\.[0-9]{1,2})?$`) + cvssRE = regexp.MustCompile(`^CVSS:[34]\.[01]/[A-Za-z:/]+$`) + emailRE = regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`) +) + +// +kubebuilder:webhook:path=/validate-securityreport,mutating=false,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=securityreports;securityreports/status,verbs=create;update,versions=v1alpha1,name=securityreport.feedback.miloapis.com,admissionReviewVersions=v1 + +// SecurityReportCheck validates what the CRD schema can't: credit links, +// external reporters' contact details, and the Security Team's status +// updates. The timeline is the reporter's record, so entries can be added but +// not changed or removed. +type SecurityReportCheck struct{} + +var _ admission.Handler = SecurityReportCheck{} + +// Handle implements admission.Handler. +func (SecurityReportCheck) Handle(_ context.Context, req admission.Request) admission.Response { + r := &v1.SecurityReport{} + if err := json.Unmarshal(req.Object.Raw, r); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + var errs field.ErrorList + switch { + case req.Operation == admissionv1.Create: + errs = validateReportSpec(&r.Spec) + case req.SubResource == "status": + old := &v1.SecurityReport{} + if err := json.Unmarshal(req.OldObject.Raw, old); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + errs = validateReportStatus(&r.Status, &old.Status) + } + if len(errs) == 0 { + return admission.Allowed("") + } + return admission.Denied(apierrors.NewInvalid(v1.Kind("SecurityReport"), r.Name, errs).Error()) +} + +func validateReportSpec(s *v1.SecurityReportSpec) field.ErrorList { + p := field.NewPath("spec") + var errs field.ErrorList + if c := s.Credit; c != nil { + cp := p.Child("credit") + if c.Public && c.Name == "" { + errs = append(errs, field.Required(cp.Child("name"), "public credit needs a name")) + } + if len(c.Name) > 100 { + errs = append(errs, field.TooLong(cp.Child("name"), "", 100)) + } + if c.URL != "" { + if u, err := url.Parse(c.URL); err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || len(c.URL) > 2048 { + errs = append(errs, field.Invalid(cp.Child("url"), c.URL, "must be an http(s) URL")) + } + } + } + rp := p.Child("reporter") + switch r := s.Reporter; { + case s.UserRef == nil && r == nil: + errs = append(errs, field.Required(rp, "reports logged for someone without an account need their email or GitHub username")) + case r != nil && r.Email == "" && r.GitHubLogin == "": + errs = append(errs, field.Required(rp, "email or githubLogin")) + case r != nil && r.Email != "" && !emailRE.MatchString(r.Email): + errs = append(errs, field.Invalid(rp.Child("email"), r.Email, "must be an email address")) + } + return errs +} + +func validateReportStatus(st, old *v1.SecurityReportStatus) field.ErrorList { + sp := field.NewPath("status") + var errs field.ErrorList + if st.CVSSVector != "" && !cvssRE.MatchString(st.CVSSVector) { + errs = append(errs, field.Invalid(sp.Child("cvssVector"), st.CVSSVector, "must be a CVSS 3.x or 4.0 vector")) + } + switch st.Phase { + case v1.SecurityReportPhaseNotApplicable, v1.SecurityReportPhaseDuplicate: + if st.Reason == "" { + errs = append(errs, field.Required(sp.Child("reason"), "the reporter is told why")) + } + } + if len(st.Reason) > 2000 { + errs = append(errs, field.TooLong(sp.Child("reason"), "", 2000)) + } + tp := sp.Child("timeline") + if len(st.Timeline) < len(old.Timeline) || !equality.Semantic.DeepEqual(st.Timeline[:len(old.Timeline)], old.Timeline) { + errs = append(errs, field.Forbidden(tp, "entries can be added but not changed or removed")) + } + if st.Phase != old.Phase { + if l := len(st.Timeline); l == 0 || st.Timeline[l-1].Phase != st.Phase { + errs = append(errs, field.Required(tp, "a phase change needs a timeline entry for the new phase")) + } + } + for i, e := range st.Timeline { + if len(e.Note) > 2000 { + errs = append(errs, field.TooLong(tp.Index(i).Child("note"), "", 2000)) + } + } + if r := st.Reward; r != nil && r.Kind == v1.RewardKindCredits && !amountRE.MatchString(r.Amount) { + errs = append(errs, field.Invalid(sp.Child("reward", "amount"), r.Amount, "credits need an amount like 250.00")) + } + if a := st.Advisory; a != nil && a.URL != "" { + if u, err := url.Parse(a.URL); err != nil || u.Scheme != "https" || u.Host == "" { + errs = append(errs, field.Invalid(sp.Child("advisory", "url"), a.URL, "must be an https URL")) + } + } + return errs +} + +// +kubebuilder:webhook:path=/mutate-securityreport-status,mutating=true,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=securityreports/status,verbs=update,versions=v1alpha1,name=securitytimeline.feedback.miloapis.com,admissionReviewVersions=v1 + +// SecurityTimeline adds a timeline entry when a status update changes the +// phase without one, so the reporter always sees each phase. +type SecurityTimeline struct { + // Now returns the current time; tests replace it. + Now func() time.Time +} + +var _ admission.Handler = SecurityTimeline{} + +// Handle implements admission.Handler. +func (t SecurityTimeline) Handle(_ context.Context, req admission.Request) admission.Response { + r, old := &v1.SecurityReport{}, &v1.SecurityReport{} + if err := json.Unmarshal(req.Object.Raw, r); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + if err := json.Unmarshal(req.OldObject.Raw, old); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + st := &r.Status + if st.Phase == old.Status.Phase || st.Phase == "" { + return admission.Allowed("") + } + if l := len(st.Timeline); l > 0 && st.Timeline[l-1].Phase == st.Phase { + return admission.Allowed("") + } + now := time.Now + if t.Now != nil { + now = t.Now + } + st.Timeline = append(st.Timeline, v1.SecurityTimelineEntry{Phase: st.Phase, At: metav1.NewTime(now().UTC().Truncate(time.Second))}) + raw, err := json.Marshal(r) + if err != nil { + return admission.Errored(http.StatusInternalServerError, err) + } + return admission.PatchResponseFromRaw(req.Object.Raw, raw) +} diff --git a/internal/webhook/securityreport_test.go b/internal/webhook/securityreport_test.go new file mode 100644 index 0000000..6fec54c --- /dev/null +++ b/internal/webhook/securityreport_test.go @@ -0,0 +1,101 @@ +package webhook + +import ( + "context" + "strings" + "testing" + "time" + + admissionv1 "k8s.io/api/admission/v1" + authenticationv1 "k8s.io/api/authentication/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +func TestSecurityReportSpec(t *testing.T) { + staff := authenticationv1.UserInfo{Username: "sec@datum.net"} + report := func(f func(*v1.SecurityReportSpec)) *v1.SecurityReport { + r := &v1.SecurityReport{Spec: v1.SecurityReportSpec{UserRef: &v1.UserReference{Name: "u1"}, Title: "t", Description: "d"}} + f(&r.Spec) + return r + } + for _, tc := range []struct { + name string + spec func(*v1.SecurityReportSpec) + denied string + }{ + {"a user's report", func(*v1.SecurityReportSpec) {}, ""}, + {"external, with an email", func(s *v1.SecurityReportSpec) { + s.UserRef, s.Reporter = nil, &v1.ExternalReporter{Email: "r@example.com"} + }, ""}, + {"external, no contact", func(s *v1.SecurityReportSpec) { s.UserRef = nil }, "spec.reporter"}, + {"bad email", func(s *v1.SecurityReportSpec) { s.Reporter = &v1.ExternalReporter{Email: "nope"} }, "email address"}, + {"public credit without a name", func(s *v1.SecurityReportSpec) { s.Credit = &v1.SecurityCredit{Public: true} }, "needs a name"}, + {"credit link isn't http", func(s *v1.SecurityReportSpec) { + s.Credit = &v1.SecurityCredit{Name: "Ada", URL: "javascript:alert(1)"} + }, "http(s) URL"}, + } { + resp := SecurityReportCheck{}.Handle(context.Background(), request(t, admissionv1.Create, "securityreports", staff, report(tc.spec), nil)) + if tc.denied == "" && !resp.Allowed { + t.Errorf("%s: denied: %v", tc.name, resp.Result.Message) + } + if tc.denied != "" && (resp.Allowed || !strings.Contains(resp.Result.Message, tc.denied)) { + t.Errorf("%s: allowed %v, %q; want %q", tc.name, resp.Allowed, resp.Result.Message, tc.denied) + } + } +} + +func TestSecurityReportStatus(t *testing.T) { + staff := authenticationv1.UserInfo{Username: "sec@datum.net"} + at := metav1.NewTime(time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)) + received := v1.SecurityTimelineEntry{Phase: v1.SecurityReportPhaseReceived, At: at} + triaged := v1.SecurityTimelineEntry{Phase: v1.SecurityReportPhaseTriaged, At: at, Note: "Confirmed"} + old := &v1.SecurityReport{Status: v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseReceived, Timeline: []v1.SecurityTimelineEntry{received}}} + with := func(st v1.SecurityReportStatus) *v1.SecurityReport { return &v1.SecurityReport{Status: st} } + check := func(obj *v1.SecurityReport) (bool, string) { + req := request(t, admissionv1.Update, "securityreports", staff, obj, old) + req.SubResource = "status" + resp := SecurityReportCheck{}.Handle(context.Background(), req) + return resp.Allowed, resp.Result.Message + } + for _, tc := range []struct { + name string + st v1.SecurityReportStatus + denied string + }{ + {"triaged with an entry", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseTriaged, Timeline: []v1.SecurityTimelineEntry{received, triaged}}, ""}, + {"phase change without an entry", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseTriaged, Timeline: []v1.SecurityTimelineEntry{received}}, "timeline entry"}, + {"rewriting history", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseReceived, Timeline: []v1.SecurityTimelineEntry{triaged}}, "added but not changed"}, + {"not applicable without a reason", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseNotApplicable, + Timeline: []v1.SecurityTimelineEntry{received, {Phase: v1.SecurityReportPhaseNotApplicable, At: at}}}, "status.reason"}, + {"credits without an amount", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseReceived, Timeline: []v1.SecurityTimelineEntry{received}, + Reward: &v1.SecurityReward{Kind: v1.RewardKindCredits}}, "amount"}, + {"advisory over http", v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseReceived, Timeline: []v1.SecurityTimelineEntry{received}, + Advisory: &v1.SecurityAdvisory{URL: "http://example.com"}}, "https"}, + } { + ok, msg := check(with(tc.st)) + if tc.denied == "" && !ok { + t.Errorf("%s: denied: %s", tc.name, msg) + } + if tc.denied != "" && (ok || !strings.Contains(msg, tc.denied)) { + t.Errorf("%s: allowed %v, %q; want %q", tc.name, ok, msg, tc.denied) + } + } +} + +func TestSecurityTimeline(t *testing.T) { + now := time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) + tl := SecurityTimeline{Now: func() time.Time { return now }} + staff := authenticationv1.UserInfo{Username: "sec@datum.net"} + received := v1.SecurityTimelineEntry{Phase: v1.SecurityReportPhaseReceived, At: metav1.NewTime(now.Add(-time.Hour))} + old := &v1.SecurityReport{Status: v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseReceived, Timeline: []v1.SecurityTimelineEntry{received}}} + obj := &v1.SecurityReport{Status: v1.SecurityReportStatus{Phase: v1.SecurityReportPhaseFixing, Timeline: []v1.SecurityTimelineEntry{received}}} + req := request(t, admissionv1.Update, "securityreports", staff, obj, old) + resp := tl.Handle(context.Background(), req) + var got v1.SecurityReport + patched(t, req, resp, &got) + if l := got.Status.Timeline; len(l) != 2 || l[1].Phase != v1.SecurityReportPhaseFixing || !l[1].At.Time.Equal(now) { + t.Errorf("timeline %+v", l) + } +} From 347cd3a12d5698bbb0f796eb323ce260844212b3 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 16:43:31 -0500 Subject: [PATCH 2/2] fix: only delete a moved submission once its own report exists The move-to-security reconciler deleted the submission whenever any security report had the target name, so a name collision could lose the submission and its files. The moved report is now named - and annotated with the submission's name and UID. The reconciler reads the report back on both the create and AlreadyExists paths and checks name, UID and owner before it copies the attachments and deletes the submission (with a UID precondition). On a mismatch it keeps the submission and its files, sets Applied=False/ReportNameTaken on the review, and retries when a report is deleted. It also waits for the submission's files to be copied under it before moving. The security report webhook lets only the operator set the moved-from annotations and refuses changes to them. --- README.md | 2 +- internal/controller/controller_test.go | 152 +++++++++++++++- internal/controller/securityreport.go | 170 ++++++++++++++---- internal/webhook/securityreport.go | 33 +++- internal/webhook/securityreport_test.go | 41 +++++ .../feedback/v1alpha1/securityreport_types.go | 28 +++ .../v1alpha1/securityreport_types_test.go | 33 ++++ 7 files changed, 412 insertions(+), 47 deletions(-) create mode 100644 pkg/apis/feedback/v1alpha1/securityreport_types_test.go diff --git a/README.md b/README.md index 70df3c4..3a6b64c 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ GET /{submissions|securityreports}/{name}/attachments/{ref} (staff) - It creates the report's `SecurityReportTriage` for the Security Team's private notes. - It copies attachments under `security-reports//` and deletes them with the report. - A `Fixed` or `Disclosed` report with public credit gets a `SecurityAcknowledgement` with the name, link, month and severity. It's deleted if that stops being true. -- **`move-to-security` reconciler:** carries out `MoveToSecurity` review decisions. It creates a report owned by the submitter and named after the submission, copies the attachments, then deletes the submission, which takes its review and files with it. +- **`move-to-security` reconciler:** carries out `MoveToSecurity` review decisions. It creates a report owned by the submitter, named `-` and annotated with the submission's name and UID (`feedback.miloapis.com/moved-from-submission`, `-uid`). It reads the report back and checks the name, UID and owner before it copies the attachments and deletes the submission, which takes its review and files with it. If a report it didn't create already has that name, it keeps the submission and its files and sets `Applied=False` with reason `ReportNameTaken` on the review; the move is tried again when a report is deleted. Only the operator can set the moved-from annotations, and they can't change. ## Roles diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index 1d37ccd..45d93d2 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -2,13 +2,16 @@ package controller import ( "context" + "fmt" "os" "path/filepath" + "reflect" "sync" "testing" "time" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -517,29 +520,46 @@ func TestSecurityReports(t *testing.T) { } }) - t.Run("a misfiled submission moves to the security track", func(t *testing.T) { + // submit creates a submission by u7 with an attachment and waits for the + // operator to copy the file under it. + submit := func(t *testing.T, name string) (*v1.Submission, string) { + t.Helper() ref, _, err := files.Upload(ctx, attachments.Submission, "u7", "poc.txt", "text/plain", []byte("poc")) if err != nil { t.Fatal(err) } - sub := &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: "sub-sec"}, Spec: v1.SubmissionSpec{ + sub := &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: name}, Spec: v1.SubmissionSpec{ UserRef: v1.UserReference{Name: "u7"}, Kind: v1.SubmissionKindBug, Title: "Viewer reads secrets", Body: "steps", Attachments: []v1.AttachmentReference{{UploadRef: ref}}}} if err := c.Create(ctx, sub); err != nil { t.Fatal(err) } + eventually(t, "submission's files copied", func() error { + if err := get(sub); err != nil { + return err + } + if len(sub.Finalizers) == 0 { + return errorf("no finalizer") + } + return nil + }) + return sub, ref + } + decide := func(t *testing.T, sub *v1.Submission) *v1.SubmissionReview { + t.Helper() + r := &v1.SubmissionReview{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} eventually(t, "decide", func() error { - r := &v1.SubmissionReview{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} if err := get(r); err != nil { return err } - if err := get(sub); err != nil || len(sub.Finalizers) == 0 { - return errorf("submission's files not copied yet") - } r.Spec = v1.SubmissionReviewSpec{Decision: v1.ReviewDecisionMoveToSecurity, Affected: "secrets"} return c.Update(ctx, r) }) - rep := &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} + return r + } + // moved waits for sub to become the report rep, with its file. + moved := func(t *testing.T, sub *v1.Submission, ref string, rep *v1.SecurityReport) { + t.Helper() eventually(t, "moved", func() error { if err := get(rep); err != nil { return err @@ -547,6 +567,9 @@ func TestSecurityReports(t *testing.T) { if rep.Spec.UserRef == nil || rep.Spec.UserRef.Name != "u7" || rep.Spec.Affected != "secrets" { return errorf("report spec") } + if !movedFrom(rep, sub) { + return errorf("report doesn't name the submission") + } if err := get(&v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}}); !apierrors.IsNotFound(err) { return errorf("submission still there") } @@ -556,5 +579,120 @@ func TestSecurityReports(t *testing.T) { _, err := files.Get(ctx, attachments.SecurityReport, rep.Name, "", ref) return err }) + } + + t.Run("a misfiled submission moves to the security track", func(t *testing.T) { + sub, ref := submit(t, "sub-sec") + // A report filed under the submission's own name is someone else's + // and is left alone. + other := &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}, Spec: v1.SecurityReportSpec{ + UserRef: &v1.UserReference{Name: "u8"}, Title: "other", Description: "d"}} + if err := c.Create(ctx, other); err != nil { + t.Fatal(err) + } + decide(t, sub) + moved(t, sub, ref, &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: v1.MovedReportName(sub.Name, sub.UID)}}) + if err := get(other); err != nil || other.Spec.Title != "other" { + t.Errorf("the other report changed: %v", err) + } + }) + + t.Run("a move finishes when its report already exists", func(t *testing.T) { + // As if the operator stopped after creating the report. + sub, ref := submit(t, "sub-sec-retry") + rep := movedReport(sub, &v1.SubmissionReview{Spec: v1.SubmissionReviewSpec{Affected: "secrets"}}, v1.MovedReportName(sub.Name, sub.UID)) + if err := c.Create(ctx, rep); err != nil { + t.Fatal(err) + } + decide(t, sub) + moved(t, sub, ref, rep) }) + + for _, tc := range []struct { + name string + taken func(sub *v1.Submission) *v1.SecurityReport + }{ + {"an unrelated report", func(sub *v1.Submission) *v1.SecurityReport { + return &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: v1.MovedReportName(sub.Name, sub.UID)}, + Spec: v1.SecurityReportSpec{UserRef: &v1.UserReference{Name: "u8"}, Title: "other", Description: "d"}} + }}, + {"a report naming the submission, for someone else", func(sub *v1.Submission) *v1.SecurityReport { + rep := movedReport(sub, &v1.SubmissionReview{}, v1.MovedReportName(sub.Name, sub.UID)) + rep.Spec.UserRef.Name = "u8" + rep.Finalizers = nil + return rep + }}, + {"a report naming another submission with the same name", func(sub *v1.Submission) *v1.SecurityReport { + rep := movedReport(sub, &v1.SubmissionReview{}, v1.MovedReportName(sub.Name, sub.UID)) + rep.Annotations[v1.MovedFromSubmissionUIDAnnotation] = "00000000-0000-0000-0000-000000000000" + rep.Finalizers = nil + return rep + }}, + } { + t.Run("a move is refused when "+tc.name+" has the name", func(t *testing.T) { + sub, ref := submit(t, fmt.Sprintf("sub-sec-taken-%d", time.Now().UnixNano())) + taken := tc.taken(sub) + if err := c.Create(ctx, taken); err != nil { + t.Fatal(err) + } + before := taken.DeepCopy() + review := decide(t, sub) + eventually(t, "refused", func() error { + if err := get(review); err != nil { + return err + } + cond := meta.FindStatusCondition(review.Status.Conditions, ConditionApplied) + if cond == nil || cond.Status != metav1.ConditionFalse || cond.Reason != ReasonReportNameTaken { + return errorf("no ReportNameTaken condition") + } + return nil + }) + // Give a wrong move time to happen. + time.Sleep(time.Second) + if err := get(&v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}}); err != nil { + t.Fatalf("submission: %v", err) + } + if _, err := files.Get(ctx, attachments.Submission, sub.Name, "", ref); err != nil { + t.Errorf("submission's file: %v", err) + } + if _, err := files.Get(ctx, attachments.SecurityReport, taken.Name, "", ref); err == nil { + t.Errorf("file copied to the other report") + } + if err := get(taken); err != nil || taken.UID != before.UID || !reflect.DeepEqual(taken.Spec, before.Spec) { + t.Errorf("the other report changed: %v", err) + } + + // Once that report is gone, the move goes ahead. + if err := c.Delete(ctx, taken); err != nil { + t.Fatal(err) + } + moved(t, sub, ref, &v1.SecurityReport{ObjectMeta: metav1.ObjectMeta{Name: taken.Name}}) + }) + } +} + +func TestMovedFrom(t *testing.T) { + sub := &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: "s", UID: "uid-1"}, Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}} + for _, tc := range []struct { + name string + edit func(*v1.SecurityReport) + want bool + }{ + {"made from it", func(*v1.SecurityReport) {}, true}, + {"no annotations", func(r *v1.SecurityReport) { r.Annotations = nil }, false}, + {"another submission's name", func(r *v1.SecurityReport) { r.Annotations[v1.MovedFromSubmissionAnnotation] = "t" }, false}, + {"another submission's UID", func(r *v1.SecurityReport) { r.Annotations[v1.MovedFromSubmissionUIDAnnotation] = "uid-2" }, false}, + {"another owner", func(r *v1.SecurityReport) { r.Spec.UserRef.Name = "u2" }, false}, + {"no owner", func(r *v1.SecurityReport) { r.Spec.UserRef = nil }, false}, + } { + rep := movedReport(sub, &v1.SubmissionReview{}, v1.MovedReportName(sub.Name, sub.UID)) + tc.edit(rep) + if got := movedFrom(rep, sub); got != tc.want { + t.Errorf("%s: movedFrom = %v", tc.name, got) + } + } + if movedFrom(&v1.SecurityReport{Spec: v1.SecurityReportSpec{UserRef: &v1.UserReference{Name: "u1"}}}, + &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: ""}, Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}}) { + t.Error("a submission without a UID matched a report without annotations") + } } diff --git a/internal/controller/securityreport.go b/internal/controller/securityreport.go index df54dec..6425fbd 100644 --- a/internal/controller/securityreport.go +++ b/internal/controller/securityreport.go @@ -6,13 +6,18 @@ import ( "time" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" "go.miloapis.com/feedback/internal/attachments" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" @@ -123,10 +128,20 @@ func (r *SecurityReportReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } -// MoveReconciler carries out MoveToSecurity decisions: it creates a -// SecurityReport owned by the submitter from the submission, named after it so -// a retry finds it, copies the attachments under it, then deletes the -// submission (and with it its review and files). +// ReasonReportNameTaken is the Applied=False reason on a MoveToSecurity +// review when a report the move didn't create already has the moved report's +// name. The submission is kept. +const ReasonReportNameTaken = "ReportNameTaken" + +// MoveReconciler carries out MoveToSecurity decisions. It creates a +// SecurityReport owned by the submitter from the submission, named by +// v1.MovedReportName so a retry finds it and annotated with the submission's +// name and UID. Only once it has read back that report and confirmed it was +// made from this submission, for the same owner, does it copy the +// attachments under it and delete the submission (and with it its review and +// files). A report with that name that wasn't made from this submission is +// left alone, and so is the submission: the review gets Applied=False, +// ReportNameTaken, and the move is tried again when a report is deleted. type MoveReconciler struct { client.Client Options @@ -145,51 +160,134 @@ func (r *MoveReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. if sub.DeletionTimestamp != nil { return ctrl.Result{}, nil } - - var existing v1.SecurityReport - err := r.Get(ctx, types.NamespacedName{Name: sub.Name}, &existing) - switch { - case apierrors.IsNotFound(err): - rep := &v1.SecurityReport{ - ObjectMeta: metav1.ObjectMeta{Name: sub.Name}, - Spec: v1.SecurityReportSpec{ - UserRef: &v1.UserReference{Name: sub.Spec.UserRef.Name}, - Title: sub.Spec.Title, - Description: sub.Spec.Body, - Affected: review.Spec.Affected, - Attachments: sub.Spec.Attachments, - Context: v1.SecurityReportContext{ - Client: sub.Spec.Context.Client, Organization: sub.Spec.Context.Organization, - ClientVersion: sub.Spec.Context.ClientVersion, - }, - }, + if len(sub.Spec.Attachments) > 0 { + if r.Files == nil { + return ctrl.Result{}, fmt.Errorf("can't move %s: it has attachments and there is no attachments bucket", sub.Name) } - if len(sub.Spec.Attachments) > 0 { - if r.Files == nil { - return ctrl.Result{}, fmt.Errorf("can't move %s: it has attachments and there is no attachments bucket", sub.Name) - } - if err := r.Files.CopyParent(ctx, attachments.Submission, attachments.SecurityReport, sub.Name, rep.Name); err != nil { - return ctrl.Result{}, fmt.Errorf("copying attachments: %w", err) - } - // Already copied, so the report reconciler only deletes them. - controllerutil.AddFinalizer(rep, AttachmentsFinalizer) + if !controllerutil.ContainsFinalizer(&sub, AttachmentsFinalizer) { + // The submission reconciler hasn't copied the files under the + // submission yet, so there is nothing to copy to the report. + return ctrl.Result{RequeueAfter: 5 * time.Second}, nil } - if err := r.Create(ctx, rep); err != nil && !apierrors.IsAlreadyExists(err) { - return ctrl.Result{}, err + } + + key := types.NamespacedName{Name: v1.MovedReportName(sub.Name, sub.UID)} + var rep v1.SecurityReport + err := r.Get(ctx, key, &rep) + if apierrors.IsNotFound(err) { + rep = *movedReport(&sub, &review, key.Name) + err = r.Create(ctx, &rep) + if apierrors.IsAlreadyExists(err) { + // Created since the cache was read, by an earlier attempt or by + // someone else: read it back and check it like any other. + rep = v1.SecurityReport{} + err = r.Get(ctx, key, &rep) } - case err != nil: + } + if err != nil { return ctrl.Result{}, err } - return ctrl.Result{}, client.IgnoreNotFound(r.Delete(ctx, &sub)) + if !movedFrom(&rep, &sub) { + log.FromContext(ctx).Info("Not moving the submission: a report it didn't create has the name", "report", rep.Name) + return ctrl.Result{}, r.notApplied(ctx, &review, ReasonReportNameTaken, fmt.Sprintf( + "SecurityReport %s already exists and wasn't created by moving this submission, so the submission and its files were kept. "+ + "The move is tried again once that report is deleted.", rep.Name)) + } + if rep.DeletionTimestamp != nil { + // The moved report is being deleted before the submission was. Wait + // for it to go (its deletion requeues this) and create it again. + return ctrl.Result{}, nil + } + + if len(sub.Spec.Attachments) > 0 { + if err := r.Files.CopyParent(ctx, attachments.Submission, attachments.SecurityReport, sub.Name, rep.Name); err != nil { + return ctrl.Result{}, fmt.Errorf("copying attachments: %w", err) + } + } + return ctrl.Result{}, client.IgnoreNotFound(r.Delete(ctx, &sub, client.Preconditions{UID: &sub.UID})) +} + +// movedReport is the report sub moves to. +func movedReport(sub *v1.Submission, review *v1.SubmissionReview, name string) *v1.SecurityReport { + rep := &v1.SecurityReport{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Annotations: map[string]string{ + v1.MovedFromSubmissionAnnotation: sub.Name, + v1.MovedFromSubmissionUIDAnnotation: string(sub.UID), + }, + }, + Spec: v1.SecurityReportSpec{ + UserRef: &v1.UserReference{Name: sub.Spec.UserRef.Name}, + Title: sub.Spec.Title, + Description: sub.Spec.Body, + Affected: review.Spec.Affected, + Attachments: sub.Spec.Attachments, + Context: v1.SecurityReportContext{ + Client: sub.Spec.Context.Client, Organization: sub.Spec.Context.Organization, + ClientVersion: sub.Spec.Context.ClientVersion, + }, + }, + } + if len(sub.Spec.Attachments) > 0 { + // The move copies the files, so the report reconciler only deletes + // them. + controllerutil.AddFinalizer(rep, AttachmentsFinalizer) + } + return rep +} + +// movedFrom reports whether rep is the report moved from sub: it names sub +// by name and UID and has the same owner. +func movedFrom(rep *v1.SecurityReport, sub *v1.Submission) bool { + a := rep.Annotations + return sub.UID != "" && + a[v1.MovedFromSubmissionAnnotation] == sub.Name && + a[v1.MovedFromSubmissionUIDAnnotation] == string(sub.UID) && + rep.Spec.UserRef != nil && rep.Spec.UserRef.Name == sub.Spec.UserRef.Name +} + +// notApplied records on the review why the move isn't done. +func (r *MoveReconciler) notApplied(ctx context.Context, review *v1.SubmissionReview, reason, msg string) error { + if !meta.SetStatusCondition(&review.Status.Conditions, metav1.Condition{ + Type: ConditionApplied, Status: metav1.ConditionFalse, Reason: reason, Message: msg, ObservedGeneration: review.Generation, + }) { + return nil + } + return r.Status().Update(ctx, review) +} + +// pendingMoves returns every review with a MoveToSecurity decision. Their +// submissions haven't moved yet: a moved submission takes its review with +// it. A deleted report may have been in a move's way, so each is retried. +func (r *MoveReconciler) pendingMoves(ctx context.Context, _ client.Object) []reconcile.Request { + var reviews v1.SubmissionReviewList + if err := r.List(ctx, &reviews); err != nil { + log.FromContext(ctx).Error(err, "Listing reviews to retry moves") + return nil + } + var out []reconcile.Request + for _, rv := range reviews.Items { + if rv.Spec.Decision == v1.ReviewDecisionMoveToSecurity { + out = append(out, reconcile.Request{NamespacedName: types.NamespacedName{Name: rv.Name}}) + } + } + return out } // SetupWithManager registers the reconciler for reviews with a -// MoveToSecurity decision. +// MoveToSecurity decision, and for deleted reports. func (r *MoveReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). Named("move-to-security"). For(&v1.SubmissionReview{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool { return obj.(*v1.SubmissionReview).Spec.Decision == v1.ReviewDecisionMoveToSecurity }))). + Watches(&v1.SecurityReport{}, handler.EnqueueRequestsFromMapFunc(r.pendingMoves), builder.WithPredicates(predicate.Funcs{ + CreateFunc: func(event.CreateEvent) bool { return false }, + UpdateFunc: func(event.UpdateEvent) bool { return false }, + DeleteFunc: func(event.DeleteEvent) bool { return true }, + GenericFunc: func(event.GenericEvent) bool { return false }, + })). Complete(r) } diff --git a/internal/webhook/securityreport.go b/internal/webhook/securityreport.go index f0a015c..997d2d4 100644 --- a/internal/webhook/securityreport.go +++ b/internal/webhook/securityreport.go @@ -15,6 +15,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + "go.miloapis.com/feedback/internal/identity" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -27,9 +28,12 @@ var ( // +kubebuilder:webhook:path=/validate-securityreport,mutating=false,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=securityreports;securityreports/status,verbs=create;update,versions=v1alpha1,name=securityreport.feedback.miloapis.com,admissionReviewVersions=v1 // SecurityReportCheck validates what the CRD schema can't: credit links, -// external reporters' contact details, and the Security Team's status -// updates. The timeline is the reporter's record, so entries can be added but -// not changed or removed. +// external reporters' contact details, the Security Team's status updates, +// and the annotations naming the submission a report was moved from. The +// timeline is the reporter's record, so entries can be added but not changed +// or removed. Only the operator can set the moved-from annotations, when it +// creates the report, and no one can change them: the operator trusts them +// before it deletes the submission. type SecurityReportCheck struct{} var _ admission.Handler = SecurityReportCheck{} @@ -44,12 +48,21 @@ func (SecurityReportCheck) Handle(_ context.Context, req admission.Request) admi switch { case req.Operation == admissionv1.Create: errs = validateReportSpec(&r.Spec) + if identity.Of(req.UserInfo) != identity.Operator { + errs = append(errs, validateMovedFrom(r, &v1.SecurityReport{})...) + } case req.SubResource == "status": old := &v1.SecurityReport{} if err := json.Unmarshal(req.OldObject.Raw, old); err != nil { return admission.Errored(http.StatusBadRequest, err) } errs = validateReportStatus(&r.Status, &old.Status) + case req.Operation == admissionv1.Update: + old := &v1.SecurityReport{} + if err := json.Unmarshal(req.OldObject.Raw, old); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + errs = validateMovedFrom(r, old) } if len(errs) == 0 { return admission.Allowed("") @@ -57,6 +70,20 @@ func (SecurityReportCheck) Handle(_ context.Context, req admission.Request) admi return admission.Denied(apierrors.NewInvalid(v1.Kind("SecurityReport"), r.Name, errs).Error()) } +// validateMovedFrom refuses moved-from annotations that differ from old's. +func validateMovedFrom(r, old *v1.SecurityReport) field.ErrorList { + p := field.NewPath("metadata", "annotations") + var errs field.ErrorList + for _, k := range []string{v1.MovedFromSubmissionAnnotation, v1.MovedFromSubmissionUIDAnnotation} { + v, has := r.Annotations[k] + was, had := old.Annotations[k] + if v != was || has != had { + errs = append(errs, field.Forbidden(p.Key(k), "set only by the operator, when it moves a submission to a report, and can't change")) + } + } + return errs +} + func validateReportSpec(s *v1.SecurityReportSpec) field.ErrorList { p := field.NewPath("spec") var errs field.ErrorList diff --git a/internal/webhook/securityreport_test.go b/internal/webhook/securityreport_test.go index 6fec54c..1bf417e 100644 --- a/internal/webhook/securityreport_test.go +++ b/internal/webhook/securityreport_test.go @@ -9,7 +9,9 @@ import ( admissionv1 "k8s.io/api/admission/v1" authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "go.miloapis.com/feedback/internal/identity" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -99,3 +101,42 @@ func TestSecurityTimeline(t *testing.T) { t.Errorf("timeline %+v", l) } } + +func TestSecurityReportMovedFrom(t *testing.T) { + staff := authenticationv1.UserInfo{Username: "sec@datum.net"} + operator := authenticationv1.UserInfo{Username: identity.OperatorUser} + report := func(annotations map[string]string) *v1.SecurityReport { + r := &v1.SecurityReport{Spec: v1.SecurityReportSpec{UserRef: &v1.UserReference{Name: "u1"}, Title: "t", Description: "d"}} + r.Annotations = annotations + return r + } + moved := map[string]string{v1.MovedFromSubmissionAnnotation: "s", v1.MovedFromSubmissionUIDAnnotation: "uid-1"} + for _, tc := range []struct { + name string + op admissionv1.Operation + user authenticationv1.UserInfo + obj, old *v1.SecurityReport + allowed bool + }{ + {"the operator moving a submission", admissionv1.Create, operator, report(moved), nil, true}, + {"anyone else naming a submission", admissionv1.Create, staff, report(moved), nil, false}, + {"anyone else naming just a UID", admissionv1.Create, staff, + report(map[string]string{v1.MovedFromSubmissionUIDAnnotation: "uid-1"}), nil, false}, + {"a report with other annotations", admissionv1.Create, staff, report(map[string]string{"x": "y"}), nil, true}, + {"an update that keeps them", admissionv1.Update, staff, report(map[string]string{v1.MovedFromSubmissionAnnotation: "s", + v1.MovedFromSubmissionUIDAnnotation: "uid-1", "x": "y"}), report(moved), true}, + {"an update that adds them", admissionv1.Update, staff, report(moved), report(nil), false}, + {"an update that changes the UID", admissionv1.Update, operator, report(map[string]string{v1.MovedFromSubmissionAnnotation: "s", + v1.MovedFromSubmissionUIDAnnotation: "uid-2"}), report(moved), false}, + {"an update that removes them", admissionv1.Update, staff, report(nil), report(moved), false}, + } { + var old runtime.Object + if tc.old != nil { + old = tc.old + } + resp := SecurityReportCheck{}.Handle(context.Background(), request(t, tc.op, "securityreports", tc.user, tc.obj, old)) + if resp.Allowed != tc.allowed { + t.Errorf("%s: allowed %v, want %v: %v", tc.name, resp.Allowed, tc.allowed, resp.Result) + } + } +} diff --git a/pkg/apis/feedback/v1alpha1/securityreport_types.go b/pkg/apis/feedback/v1alpha1/securityreport_types.go index 9fdb057..e6733dd 100644 --- a/pkg/apis/feedback/v1alpha1/securityreport_types.go +++ b/pkg/apis/feedback/v1alpha1/securityreport_types.go @@ -1,7 +1,10 @@ package v1alpha1 import ( + "strings" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" ) // SecurityReportPhase is where a report is in handling. @@ -195,6 +198,31 @@ type CreditGrantReference struct { Name string `json:"name"` } +// Annotations the operator sets on a report it created from a submission +// with a MoveToSecurity decision: the submission's name and UID. With +// spec.userRef they tie the report to that one submission, so the operator +// deletes the submission only once its own report exists. Only the operator +// can set them, and they can't change. +const ( + MovedFromSubmissionAnnotation = "feedback.miloapis.com/moved-from-submission" + MovedFromSubmissionUIDAnnotation = "feedback.miloapis.com/moved-from-submission-uid" +) + +// maxNameLength is the longest name a cluster-scoped object can have. +const maxNameLength = 253 + +// MovedReportName is the name of the SecurityReport a submission moves to: +// "-". It is the same on every retry, and a +// report filed under the submission's own name can't take it. A long +// submission name is shortened to fit. +func MovedReportName(submission string, uid types.UID) string { + suffix := "-" + string(uid) + if len(submission)+len(suffix) > maxNameLength { + submission = strings.TrimRight(submission[:maxNameLength-len(suffix)], "-.") + } + return submission + suffix +} + // SecurityReportTriage is the Security Team's private notes on a report, // with the same name as the report. Only the Security Team can read or write // it, so nothing here reaches the reporter. diff --git a/pkg/apis/feedback/v1alpha1/securityreport_types_test.go b/pkg/apis/feedback/v1alpha1/securityreport_types_test.go new file mode 100644 index 0000000..988257a --- /dev/null +++ b/pkg/apis/feedback/v1alpha1/securityreport_types_test.go @@ -0,0 +1,33 @@ +package v1alpha1_test + +import ( + "strings" + "testing" + + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/validation" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +func TestMovedReportName(t *testing.T) { + const uid = types.UID("6f1c2a7e-3b4d-4c5e-9f60-718293a4b5c6") + if got := v1.MovedReportName("sub-1", uid); got != "sub-1-"+string(uid) { + t.Errorf("got %q", got) + } + // Too long: shortened, without leaving a dot or dash before the UID. + long := strings.Repeat("a", 215) + ".b" + strings.Repeat("c", 30) + got := v1.MovedReportName(long, uid) + if len(got) > 253 || !strings.HasSuffix(got, "a-"+string(uid)) { + t.Errorf("got %q (%d)", got, len(got)) + } + for _, name := range []string{"sub-1", long, strings.Repeat("x.", 120) + "y"} { + n := v1.MovedReportName(name, uid) + if errs := validation.IsDNS1123Subdomain(n); len(errs) > 0 { + t.Errorf("%q: %v", n, errs) + } + if n != v1.MovedReportName(name, uid) { + t.Errorf("%q: not deterministic", name) + } + } +}