-
Notifications
You must be signed in to change notification settings - Fork 0
fix(middleware): guard typed-nil *fiber.Error matched by errors.As #48
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a1aba12
fix(middleware): guard typed-nil *fiber.Error matched by errors.As
fredcamaral 7103562
fix(middleware): skip typed-nil *fiber.Error shadowing a valid one in…
fredcamaral 7cefe45
fix(middleware): satisfy errorlint and inamedparam in asFiberError
fredcamaral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| //go:build unit | ||
|
|
||
| package middleware | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gofiber/fiber/v3" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type typedNilProbeError struct{ msg string } | ||
|
|
||
| func (e *typedNilProbeError) Error() string { return e.msg } | ||
|
|
||
| // A wrapper whose Unwrap delegates to a nil cause, so Error() panics when | ||
| // the chain is stringified but errors.As can still walk it. | ||
| type delegatingProbeError struct{ cause *typedNilProbeError } | ||
|
|
||
| func (e *delegatingProbeError) Error() string { return "wrapped: " + e.cause.Error() } | ||
| func (e *delegatingProbeError) Unwrap() error { return e.cause } | ||
|
|
||
| // Regression for the typed-nil *fiber.Error substitution panic: errors.As | ||
| // matches a typed-nil *fiber.Error inside a joined chain and used to leave | ||
| // fiberErr nil, so reading .Code panicked inside the finalizer - the very | ||
| // defect class WithHTTPErrorHandling exists to close. | ||
| func TestWithHTTPErrorHandling_UnsafeChains_NeverPanic(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| typedNilFiber := (*fiber.Error)(nil) | ||
| typedNilProbe := (*typedNilProbeError)(nil) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| handlerErr error | ||
| wantStatus int | ||
| wantBody string | ||
| }{ | ||
| { | ||
| name: "joined typed-nil fiber errors only", | ||
| handlerErr: errors.Join(error(typedNilFiber), error(typedNilFiber)), | ||
| wantStatus: fiber.StatusInternalServerError, | ||
| wantBody: "internal error", | ||
| }, | ||
| { | ||
| name: "typed-nil fiber error joined with real error", | ||
| handlerErr: errors.Join(error(typedNilFiber), errors.New("boom")), | ||
| wantStatus: fiber.StatusInternalServerError, | ||
| wantBody: "internal error", | ||
| }, | ||
| { | ||
| name: "valid fiber error joined with typed-nil probe keeps code and message", | ||
| handlerErr: errors.Join(fiber.NewError(fiber.StatusBadRequest, "bad request payload"), error(typedNilProbe)), | ||
| wantStatus: fiber.StatusBadRequest, | ||
| wantBody: "bad request payload", | ||
| }, | ||
| { | ||
| name: "delegating wrapper with nil cause and no fiber error", | ||
| handlerErr: &delegatingProbeError{}, | ||
| wantStatus: fiber.StatusInternalServerError, | ||
| wantBody: "internal error", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Fiber's DEFAULT ErrorHandler calls err.Error() unconditionally, | ||
| // which is exactly the render path the substitution protects. | ||
| app := fiber.New() | ||
| app.Use(WithHTTPErrorHandling()) | ||
| app.Get("/probe", func(fiber.Ctx) error { return tt.handlerErr }) | ||
|
|
||
| var resp *httptest.ResponseRecorder | ||
| require.NotPanics(t, func() { | ||
| req := httptest.NewRequest(fiber.MethodGet, "/probe", nil) | ||
| res, err := app.Test(req) | ||
| require.NoError(t, err) | ||
| defer res.Body.Close() | ||
|
|
||
| body, readErr := io.ReadAll(res.Body) | ||
| require.NoError(t, readErr) | ||
|
|
||
| resp = httptest.NewRecorder() | ||
| resp.Code = res.StatusCode | ||
| _, _ = resp.Body.Write(body) | ||
| }) | ||
|
|
||
| assert.Equal(t, tt.wantStatus, resp.Code) | ||
| assert.Equal(t, tt.wantBody, resp.Body.String()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // httpStatusCode is the second site that dereferenced a typed-nil | ||
| // *fiber.Error matched by errors.As. | ||
| func TestHTTPStatusCode_TypedNilFiberErrorInChain_DoesNotPanic(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| app := fiber.New() | ||
| joined := errors.Join(error((*fiber.Error)(nil)), errors.New("boom")) | ||
|
|
||
| app.Get("/probe", func(c fiber.Ctx) error { | ||
| require.NotPanics(t, func() { | ||
| status := httpStatusCode(c, joined) | ||
| assert.Equal(t, fiber.StatusInternalServerError, status) | ||
| }) | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(fiber.MethodGet, "/probe", nil) | ||
| res, err := app.Test(req) | ||
| require.NoError(t, err) | ||
| defer res.Body.Close() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.