Skip to content

Agent Mesh

controlplane: refuse enrollment without a subject, answer 413 for oversized bodies

Antonio Ojeaf40c34821961
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index d275fbc4..b0843c00 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -698,6 +698,13 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
http.Error(w, "JWT validation failed: "+err.Error(), http.StatusUnauthorized)
return
}
+ // Bans and ownership are keyed on issuer|subject; a token without a
+ // subject would enroll an identity that can never be banned.
+ if oidcIdentityKey(claims) == "" {
+ logger.Warnw("JWT without a subject refused", "peer_id", req.PeerId)
+ http.Error(w, "JWT validation failed: token has no sub claim", http.StatusUnauthorized)
+ return
+ }
// An email the issuer marks unverified must not resolve bindings or be
// minted as an email() fact.
if verifiedEmail(claims) == "" {
@@ -1756,6 +1763,11 @@ func readProtoJSON(w http.ResponseWriter, r *http.Request, msg proto.Message) bo
defer func() { _ = r.Body.Close() }()
body, err := io.ReadAll(r.Body)
if err != nil {
+ var tooLarge *http.MaxBytesError
+ if errors.As(err, &tooLarge) {
+ http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
+ return false
+ }
http.Error(w, "Failed to read body", http.StatusBadRequest)
return false
}
@@ -3259,6 +3271,11 @@ func SetNodeBan(ctx context.Context, store storage.Store, node *storage.Enrolled
return fmt.Errorf("stored claims for %s are unreadable: %w", node.PeerID, err)
}
identity = oidcIdentityKey(claims)
+ if identity == "" {
+ // /register refuses a token without a subject, so this is a record
+ // from before that check. The device ban must still land.
+ logger.Warnw("Enrollment record carries claims without a subject; banning the device only", "peer_id", node.PeerID)
+ }
}
return store.SetNodeAndIdentityBanned(ctx, node.PeerID, identity, banned)
}
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index c2129993..bb917958 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -2586,6 +2586,37 @@ func TestAuthDenialPaths(t *testing.T) {
}
})
+ t.Run("oversized operator-plane body answers 413", func(t *testing.T) {
+ oversized := bytes.Repeat([]byte("a"), maxRequestBodyBytes+1)
+ req, _ := http.NewRequest(http.MethodPost, baseURL+"/admin/bootstrap-tokens", bytes.NewReader(oversized))
+ req.Header.Set("Authorization", "Bearer super-secret-admin-token")
+ req.Header.Set("Content-Type", "application/json")
+ resp, err := client.Do(req)
+ if err != nil {
+ t.Fatalf("request failed: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+ if resp.StatusCode != http.StatusRequestEntityTooLarge {
+ t.Errorf("expected 413 for oversized JSON body, got %d", resp.StatusCode)
+ }
+ })
+
+ t.Run("JWT without a subject is rejected", func(t *testing.T) {
+ // Bans are keyed on issuer|subject; an identity without one could
+ // never be banned, so it never enrolls.
+ noSubJWT := mintToken(map[string]interface{}{
+ "email": "[email protected]",
+ })
+ resp, err := client.Post(baseURL+"/register", "application/x-protobuf", bytes.NewReader(newEnrollBody(noSubJWT)))
+ if err != nil {
+ t.Fatalf("request failed: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+ if resp.StatusCode != http.StatusUnauthorized {
+ t.Errorf("expected 401 for a JWT without sub, got %d", resp.StatusCode)
+ }
+ })
+
t.Run("admin endpoint rejects wrong token", func(t *testing.T) {
req, _ := http.NewRequest("GET", baseURL+"/admin/status", nil)
req.Header.Set("Authorization", "Bearer this-is-not-the-admin-token")