hscontrol: answer 413 only for an oversized c2n response

Updates #3629
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 15:51:04 +01:00
1 parent 6245fbe30b
commit fda2a123c7
2 files changed
+32 -1

No files matched your search

+8 -1
View File
@@ -356,7 +356,14 @@ func (ns *noiseServer) C2NResponseHandler(
body, err := io.ReadAll(http.MaxBytesReader(writer, req.Body, maxC2NResponseBytes))
if err != nil {
httpError(writer, NewHTTPError(http.StatusRequestEntityTooLarge, "reading c2n response", err))
status := http.StatusBadRequest
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
status = http.StatusRequestEntityTooLarge
}
httpError(writer, NewHTTPError(status, "reading c2n response", err))
return
}
+24
View File
@@ -21,6 +21,7 @@ import (
"strings"
"sync"
"testing"
"testing/iotest"
"time"
"github.com/go-chi/chi/v5"
@@ -2599,3 +2600,26 @@ func TestC2NResponseHandler_OversizedBody(t *testing.T) {
_, pending := app.state.C2NTarget(c2nID)
assert.True(t, pending)
}
// TestC2NResponseHandler_BrokenBody verifies that a body that fails to read
// for another reason than its size is a 400, not a 413.
func TestC2NResponseHandler_BrokenBody(t *testing.T) {
t.Parallel()
app := createTestApp(t)
user := app.state.CreateUserForTest("c2n-broken-user")
node := putTestNodeInStore(t, app, user, "c2n-node")
c2nID, _ := app.state.RegisterC2N(node.ID)
defer app.state.CancelC2N(c2nID)
ns := &noiseServer{headscale: app, machineKey: node.MachineKey}
req := newC2NResponseRequest(c2nID, nil)
req.Body = io.NopCloser(iotest.ErrReader(io.ErrUnexpectedEOF))
rec := httptest.NewRecorder()
ns.C2NResponseHandler(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
}