HYPERFLEET-649 - fix: Reject cluster creation with missing spec field#56
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughThe PR adds validation for the Spec field on cluster and nodepool creation requests. A new private validateSpec function (using reflection) was introduced to ensure the Spec field is present and non-nil; handlers for cluster and cluster nodepools now call this validator during Create. Unit tests were added for valid, empty, and nil Spec cases. Integration tests were added to ensure POST requests with a null or missing spec return 400 with error code HYPERFLEET-VAL-000 and appropriate error details. Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Comment |
| validateEmpty(&req, "Id", "id"), | ||
| validateName(&req, "Name", "name", 3, 53), | ||
| validateKind(&req, "Kind", "kind", "Cluster"), | ||
| validateSpec(&req, "Spec", "spec"), |
There was a problem hiding this comment.
The validateSpec check was added to the cluster creation handler, but the nodepool creation
handler in cluster_nodepools.go has the same gap. The NodePoolCreateRequest schema also
defines spec as a required field, and the SchemaValidationMiddleware skips validation when the
spec key is entirely absent from the JSON body (it only catches spec: null). This means a
nodepool can still be created without a spec field.
Consider adding validateSpec(&req, "Spec", "spec") to the validators list in
clusterNodePoolsHandler.Create:
[]validate{
validateEmpty(&req, "Id", "id"),
validateName(&req, "Name", "name", 3, 15),
validateKind(&req, "Kind", "kind", "NodePool"),
validateSpec(&req, "Spec", "spec"),
},This may be intentionally out of scope for this ticket, but worth noting since it's the same
vulnerability
There was a problem hiding this comment.
Great catch! I will include it in this PR since its same bug with same solution both violating OpenAPI specification.
| } | ||
|
|
||
| // validateSpec validates that the spec field is not nil | ||
| func validateSpec(i interface{}, fieldName string, field string) validate { |
There was a problem hiding this comment.
The validateSpec function doesn't follow the same defensive pattern used by all other
validators in this file. Every other validator checks value.Kind() == reflect.Ptr before
calling methods like IsNil() and dereferences the pointer when needed. Consider adding the
same guard for consistency and safety:
func validateSpec(i interface{}, fieldName string, field string) validate {
return func() *errors.ServiceError {
value := reflect.ValueOf(i).Elem().FieldByName(fieldName)
if value.Kind() == reflect.Ptr {
if value.IsNil() {
return errors.Validation("%s is required", field)
}
value = value.Elem()
}
if !value.IsValid() || value.IsNil() {
return errors.Validation("%s is required", field)
}
return nil
}
}This makes the function safe for reuse if the Spec type ever changes (e.g., to a pointer to a
struct) and keeps the codebase consistent.
There was a problem hiding this comment.
Done. validateSpec now follows the same defensive pattern.
Added validateSpec() to cluster and nodepool creation handlers. The API was accepting requests with missing spec field, violating openAPI specification.
9e95000 to
e8ff1ba
Compare
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: rh-amarin The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
d66757d
into
openshift-hyperfleet:main
https://issues.redhat.com/browse/HYPERFLEET-649
Summary by CodeRabbit