Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Connection type in scoped signing keys not honored #5789

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,33 +883,6 @@ func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (au
// If we have a jwt and a userClaim, make sure we have the Account, etc associated.
// We need to look up the account. This will use an account resolver if one is present.
if juc != nil {
allowedConnTypes, err := convertAllowedConnectionTypes(juc.AllowedConnectionTypes)
if err != nil {
// We got an error, which means some connection types were unknown. As long as
// a valid one is returned, we proceed with auth. If not, we have to reject.
// In other words, suppose that JWT allows "WEBSOCKET" in the array. No error
// is returned and allowedConnTypes will contain "WEBSOCKET" only.
// Client will be rejected if not a websocket client, or proceed with rest of
// auth if it is.
// Now suppose JWT allows "WEBSOCKET, MQTT" and say MQTT is not known by this
// server. In this case, allowedConnTypes would contain "WEBSOCKET" and we
// would get `err` indicating that "MQTT" is an unknown connection type.
// If a websocket client connects, it should still be allowed, since after all
// the admin wanted to allow websocket and mqtt connection types.
// However, say that the JWT only allows "MQTT" (and again suppose this server
// does not know about MQTT connection type), then since the allowedConnTypes
// map would be empty (no valid types found), and since empty means allow-all,
// then we should reject because the intent was to allow connections for this
// user only as an MQTT client.
c.Debugf("%v", err)
if len(allowedConnTypes) == 0 {
return false
}
}
if !c.connectionTypeAllowed(allowedConnTypes) {
c.Debugf("Connection type not allowed")
return false
}
issuer := juc.Issuer
if juc.IssuerAccount != _EMPTY_ {
issuer = juc.IssuerAccount
Expand Down Expand Up @@ -952,6 +925,36 @@ func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (au
c.Debugf("Account does not allow bearer tokens")
return false
}
// We check the allowed connection types, but only after processing
// of scoped signer (so that it updates `juc` with what is defined
// in the account.
allowedConnTypes, err := convertAllowedConnectionTypes(juc.AllowedConnectionTypes)
if err != nil {
// We got an error, which means some connection types were unknown. As long as
// a valid one is returned, we proceed with auth. If not, we have to reject.
// In other words, suppose that JWT allows "WEBSOCKET" in the array. No error
// is returned and allowedConnTypes will contain "WEBSOCKET" only.
// Client will be rejected if not a websocket client, or proceed with rest of
// auth if it is.
// Now suppose JWT allows "WEBSOCKET, MQTT" and say MQTT is not known by this
// server. In this case, allowedConnTypes would contain "WEBSOCKET" and we
// would get `err` indicating that "MQTT" is an unknown connection type.
// If a websocket client connects, it should still be allowed, since after all
// the admin wanted to allow websocket and mqtt connection types.
// However, say that the JWT only allows "MQTT" (and again suppose this server
// does not know about MQTT connection type), then since the allowedConnTypes
// map would be empty (no valid types found), and since empty means allow-all,
// then we should reject because the intent was to allow connections for this
// user only as an MQTT client.
c.Debugf("%v", err)
if len(allowedConnTypes) == 0 {
return false
}
}
if !c.connectionTypeAllowed(allowedConnTypes) {
c.Debugf("Connection type not allowed")
return false
}
// skip validation of nonce when presented with a bearer token
// FIXME: if BearerToken is only for WSS, need check for server with that port enabled
if !juc.BearerToken {
Expand Down
17 changes: 17 additions & 0 deletions server/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5779,6 +5779,7 @@ func TestJWScopedSigningKeys(t *testing.T) {
signer.Key = aSignScopedPub
signer.Template.Pub.Deny.Add("denied")
signer.Template.Payload = 5
signer.Template.AllowedConnectionTypes.Add(jwt.ConnectionTypeStandard)
accClaim.SigningKeys.AddScopedSigner(signer)
accJwt := encodeClaim(t, accClaim, aExpPub)

Expand All @@ -5795,11 +5796,16 @@ func TestJWScopedSigningKeys(t *testing.T) {
type: full
dir: '%s'
}
websocket {
listen: 127.0.0.1:-1
no_tls: true
}
`, ojwt, syspub, dirSrv)))
s, opts := RunServerWithConfig(cf)
defer s.Shutdown()

url := fmt.Sprintf("nats://%s:%d", opts.Host, opts.Port)
wsUrl := fmt.Sprintf("ws://%s:%d", opts.Websocket.Host, opts.Websocket.Port)
errChan := make(chan error, 1)
defer close(errChan)
awaitError := func(expected bool) {
Expand Down Expand Up @@ -5853,6 +5859,10 @@ func TestJWScopedSigningKeys(t *testing.T) {
nc.Flush()
awaitError(true)
})
t.Run("scoped-signing-key-allowed-conn-types", func(t *testing.T) {
_, err := nats.Connect(wsUrl, nats.UserCredentials(aScopedCreds))
require_Error(t, err)
})
t.Run("scoped-signing-key-reload", func(t *testing.T) {
reconChan := make(chan struct{}, 1)
defer close(reconChan)
Expand All @@ -5867,6 +5877,7 @@ func TestJWScopedSigningKeys(t *testing.T) {
nats.ReconnectHandler(func(conn *nats.Conn) {
reconChan <- struct{}{}
}),
nats.ReconnectWait(100*time.Millisecond),
)
defer nc.Close()
_, err := nc.ChanSubscribe("denied", msgChan)
Expand All @@ -5879,6 +5890,7 @@ func TestJWScopedSigningKeys(t *testing.T) {
// Alter scoped permissions and update
signer.Template.Payload = -1
signer.Template.Pub.Deny.Remove("denied")
signer.Template.AllowedConnectionTypes.Add(jwt.ConnectionTypeWebsocket)
accClaim.SigningKeys.AddScopedSigner(signer)
accUpdatedJwt := encodeClaim(t, accClaim, aExpPub)
if updateJwt(t, url, sysCreds, accUpdatedJwt, 1) != 1 {
Expand All @@ -5894,6 +5906,11 @@ func TestJWScopedSigningKeys(t *testing.T) {
msg := <-msgChan
require_Equal(t, string(msg.Data), "way.too.long.for.old.payload.limit")
require_Len(t, len(msgChan), 0)
nc.Close()
nc, err = nats.Connect(wsUrl, nats.UserCredentials(aScopedCreds))
require_NoError(t, err)
nc.Flush()
defer nc.Close()
})
require_Len(t, len(errChan), 0)
}
Expand Down