Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: read twice for http request body (#5273)

h3n4l37b7474becf9
diff --git a/backend/server/acl.go b/backend/server/acl.go
index 6a8eb38bec..2142d43fba 100644
--- a/backend/server/acl.go
+++ b/backend/server/acl.go
@@ -1,6 +1,7 @@
package server
import (
+ "bytes"
"context"
"fmt"
"io"
@@ -412,14 +413,19 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.
}
aclErr = enforceWorkspaceDeveloperDatabaseRouteACL(path, method, principalID, isMemberOfAnyProjectOwnsDatabase)
} else if strings.HasPrefix(path, "/issue") {
- var bodyCopy strings.Builder
- if _, err := io.Copy(&bodyCopy, c.Request().Body); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
- }
+ // We need to copy the body because it will be consumed by the next middleware.
+ // And TeeReader require us the write must complete before the read completes.
+ // The body under the /issue route is a JSON object, and always not too large, so using ioutil.ReadAll is fine here.
+ bodyBytes, err := io.ReadAll(c.Request().Body)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read request body.").SetInternal(err)
+ }
+ if err := c.Request().Body.Close(); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to close request body.").SetInternal(err)
}
- aclErr = enforceWorkspaceDeveloperIssueRouteACL(path, method, bodyCopy.String(), c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), getRetrieveProjectMemberIDs(ctx, s.store))
+ c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
+
+ aclErr = enforceWorkspaceDeveloperIssueRouteACL(path, method, string(bodyBytes), c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), getRetrieveProjectMemberIDs(ctx, s.store))
}
if aclErr != nil {
return aclErr