-
Notifications
You must be signed in to change notification settings - Fork 24
HYPERFLEET-1376 - fix: avoid leaking internal type names in validation errors #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,25 @@ func TestResourceHandler_Patch(t *testing.T) { | |
| }, | ||
| expectedStatusCode: http.StatusOK, | ||
| }, | ||
| { | ||
| // References-only patch must be accepted, not rejected as "no field provided" — HYPERFLEET-1376 finding 8. | ||
| name: "Success - references only", | ||
| id: "ch-123", | ||
| body: `{"references":{"parents":[{"kind":"Cluster","id":"cluster-1"}]}}`, | ||
| setupMock: func(mock *services.MockResourceService) { | ||
| mock.EXPECT().Patch(gomock.Any(), "Channel", "ch-123", gomock.AssignableToTypeOf(&api.ResourcePatch{})). | ||
| Return(&api.Resource{ | ||
| Meta: api.Meta{ID: "ch-123", CreatedTime: now, UpdatedTime: now}, | ||
| Kind: "Channel", | ||
| Name: "stable", | ||
| Spec: datatypes.JSON(`{"is_default":false}`), | ||
| Generation: 2, | ||
| CreatedBy: "user@test.com", | ||
| UpdatedBy: "user@test.com", | ||
| }, nil) | ||
| }, | ||
| expectedStatusCode: http.StatusOK, | ||
| }, | ||
|
Comment on lines
+279
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win Assert the references payload passed to Line 285 accepts any As per path instructions, “New exported functions and critical logic paths SHOULD have tests.” 🤖 Prompt for AI AgentsSources: Path instructions, Linked repositories |
||
| { | ||
| name: "Not found", | ||
| id: "nonexistent", | ||
|
|
@@ -728,6 +747,15 @@ func TestResourceHandler_ForceDelete(t *testing.T) { | |
| }, | ||
| expectedStatusCode: http.StatusBadRequest, | ||
| }, | ||
| { | ||
| // Wrong JSON type for "reason" must return a clean message without leaking | ||
| // the Go struct name (ForceDeleteRequest) — HYPERFLEET-1376. | ||
| name: "Error 400 - reason wrong type", | ||
| body: `{"reason": 123}`, | ||
| setupMock: func(mock *services.MockResourceService) { | ||
| }, | ||
| expectedStatusCode: http.StatusBadRequest, | ||
| }, | ||
| { | ||
| name: "Error 400 - empty reason", | ||
| body: `{"reason": ""}`, | ||
|
|
@@ -797,6 +825,10 @@ func TestResourceHandler_ForceDelete(t *testing.T) { | |
| if tt.expectedStatusCode == http.StatusNoContent { | ||
| Expect(rr.Body.Len()).To(Equal(0)) | ||
| } | ||
| if tt.name == "Error 400 - reason wrong type" { | ||
| Expect(rr.Body.String()).To(ContainSubstring("field 'reason' must be a string")) | ||
| Expect(rr.Body.String()).ToNot(ContainSubstring("ForceDeleteRequest")) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -933,6 +965,46 @@ func TestResourceHandler_Patch_RejectsUnknownFields(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // TestResourceHandler_Patch_LabelsTypeMismatch verifies that a wrong JSON type for | ||
| // "labels" returns a clean validation message without leaking the Go struct name | ||
| // (e.g. "ResourcePatchRequest") or type (e.g. "map[string]string") — HYPERFLEET-1376 | ||
| // findings 5 & 6. | ||
| func TestResourceHandler_Patch_LabelsTypeMismatch(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| body string | ||
| }{ | ||
| {"labels as string", `{"labels":"not-an-object"}`}, | ||
| {"labels as array", `{"labels":["a","b"]}`}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| handler, _ := newTestResourceHandler(ctrl) | ||
|
|
||
| reqURL := "/api/hyperfleet/v1/channels/ch-123" | ||
| req := httptest.NewRequest(http.MethodPatch, reqURL, strings.NewReader(tt.body)) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| req = mux.SetURLVars(req, map[string]string{"id": "ch-123"}) | ||
|
|
||
| rr := httptest.NewRecorder() | ||
| handler.Patch(rr, req) | ||
|
|
||
| Expect(rr.Code).To(Equal(http.StatusBadRequest)) | ||
| Expect(rr.Body.String()).To(ContainSubstring("field 'labels' must be an object")) | ||
| Expect(rr.Body.String()).ToNot(ContainSubstring("ResourcePatchRequest")) | ||
| Expect(rr.Body.String()).ToNot(ContainSubstring("openapi.")) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestResourceHandler_PatchByOwner_RejectsUnknownFields(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.