Copilot commented on code in PR #977:
URL: https://github.com/apache/dubbo-go-pixiu/pull/977#discussion_r3393176048


##########
pkg/hotreload/http_handler_test.go:
##########
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package hotreload
+
+import (
+       "net/http"
+       "net/http/httptest"
+       "strings"
+       "testing"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)

Review Comment:
   Go style/gofmt convention is to use a single import block (grouped by stdlib 
vs third-party with a blank line). Keeping multiple `import` declarations is 
unusual and makes formatting/linting noisy; merge these into one `import (...)` 
block.



##########
pkg/hotreload/http_handler.go:
##########
@@ -95,14 +98,20 @@ func (h *ReloadHandler) ServeHTTP(w http.ResponseWriter, r 
*http.Request) {
        // Try to read from body first (handles chunked encoding where 
ContentLength == -1)
        // If body is empty, fallback to file reload
        if r.Body != nil {
-               content, readErr := io.ReadAll(r.Body)
+               content, readErr := io.ReadAll(io.LimitReader(r.Body, 
maxReloadBodyBytes+1))

Review Comment:
   Prefer enforcing request body limits with `http.MaxBytesReader(w, r.Body, 
maxReloadBodyBytes)` (and then `io.ReadAll(r.Body)`), instead of 
`io.LimitReader`. `MaxBytesReader` is the standard net/http mechanism and 
ensures consistent server-side behavior (including well-known error semantics) 
when clients exceed the limit.



##########
pkg/hotreload/http_handler.go:
##########
@@ -95,14 +98,20 @@ func (h *ReloadHandler) ServeHTTP(w http.ResponseWriter, r 
*http.Request) {
        // Try to read from body first (handles chunked encoding where 
ContentLength == -1)
        // If body is empty, fallback to file reload
        if r.Body != nil {
-               content, readErr := io.ReadAll(r.Body)
+               content, readErr := io.ReadAll(io.LimitReader(r.Body, 
maxReloadBodyBytes+1))
 
                if readErr != nil {
                        logger.Errorf("Failed to read request body: %v", 
readErr)
                        http.Error(w, fmt.Sprintf("Failed to read request body: 
%v", readErr), http.StatusBadRequest)
                        return
                }
 
+               if len(content) > maxReloadBodyBytes {
+                       logger.Warnf("Reload request body from %s exceeded %d 
bytes", r.RemoteAddr, maxReloadBodyBytes)
+                       http.Error(w, "Request body too large", 
http.StatusRequestEntityTooLarge)
+                       return
+               }

Review Comment:
   The new size-limit behavior is only tested for the `> max` case. Add tests 
for the boundary condition (`== maxReloadBodyBytes` should be accepted) and for 
an empty body (should fall back to file reload path) to lock in the intended 
semantics and prevent off-by-one regressions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to