Skip to content

Commit 7dde791

Browse files
committed
fixes from review
1 parent ecb6827 commit 7dde791

5 files changed

Lines changed: 16 additions & 14 deletions

File tree

pkg/vmcp/server/sessionmanager/session_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (sm *Manager) CreateSession(
165165
// Build the fully-formed MultiSession using the SDK-assigned session ID.
166166
// Sessions created with an identity are bound to that identity (allowAnonymous=false).
167167
// Sessions created without an identity allow anonymous access (allowAnonymous=true).
168-
allowAnonymous := identity == nil || identity.Token == ""
168+
allowAnonymous := vmcpsession.ShouldAllowAnonymous(identity)
169169
sess, err := sm.factory.MakeSessionWithID(ctx, sessionID, identity, allowAnonymous, backends)
170170
if err != nil {
171171
return nil, fmt.Errorf("Manager.CreateSession: failed to create multi-session: %w", err)

pkg/vmcp/session/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (f *defaultMultiSessionFactory) MakeSession(
270270
) (MultiSession, error) {
271271
// Sessions created with an identity are bound to that identity (allowAnonymous=false).
272272
// Sessions created without an identity allow anonymous access (allowAnonymous=true).
273-
allowAnonymous := security.ShouldAllowAnonymous(identity)
273+
allowAnonymous := ShouldAllowAnonymous(identity)
274274
return f.makeSession(ctx, uuid.New().String(), identity, allowAnonymous, backends)
275275
}
276276

pkg/vmcp/session/internal/security/security.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ const (
4040
MetadataKeyTokenSalt = "vmcp.token.salt" //nolint:gosec // This is a metadata key name, not a credential.
4141
)
4242

43-
// ShouldAllowAnonymous determines if a session should allow anonymous access
44-
// based on the creator's identity. Sessions without an identity (nil) or with
45-
// an empty token are anonymous; sessions with a non-empty bearer token are
46-
// bound to that token.
47-
//
48-
// This helper consolidates the anonymous session logic and aligns with the
49-
// validation logic in PreventSessionHijacking.
50-
func ShouldAllowAnonymous(identity *auth.Identity) bool {
51-
return identity == nil || identity.Token == ""
52-
}
53-
5443
// GenerateSalt generates a cryptographically secure random salt for token hashing.
5544
// Returns 16 bytes of random data from crypto/rand.
5645
//

pkg/vmcp/session/internal/security/security_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stretchr/testify/require"
1313

1414
"github.com/stacklok/toolhive/pkg/auth"
15+
"github.com/stacklok/toolhive/pkg/vmcp/session"
1516
"github.com/stacklok/toolhive/pkg/vmcp/session/internal/security"
1617
)
1718

@@ -306,7 +307,7 @@ func TestShouldAllowAnonymous_EdgeCases(t *testing.T) {
306307
for _, tt := range tests {
307308
t.Run(tt.name, func(t *testing.T) {
308309
t.Parallel()
309-
got := security.ShouldAllowAnonymous(tt.identity)
310+
got := session.ShouldAllowAnonymous(tt.identity)
310311
assert.Equal(t, tt.want, got)
311312
})
312313
}

pkg/vmcp/session/session.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package session
55

66
import (
7+
"github.com/stacklok/toolhive/pkg/auth"
78
transportsession "github.com/stacklok/toolhive/pkg/transport/session"
89
"github.com/stacklok/toolhive/pkg/vmcp"
910
sessiontypes "github.com/stacklok/toolhive/pkg/vmcp/session/types"
@@ -58,3 +59,14 @@ type MultiSession interface {
5859
// sessions for debugging and auditing.
5960
BackendSessions() map[string]string
6061
}
62+
63+
// ShouldAllowAnonymous determines if a session should allow anonymous access
64+
// based on the creator's identity. Sessions without an identity (nil) or with
65+
// an empty token are anonymous; sessions with a non-empty bearer token are
66+
// bound to that token.
67+
//
68+
// This function consolidates the anonymous session logic and aligns with the
69+
// validation logic in session hijacking prevention.
70+
func ShouldAllowAnonymous(identity *auth.Identity) bool {
71+
return identity == nil || identity.Token == ""
72+
}

0 commit comments

Comments
 (0)