|
| 1 | +From 5ad836f2e5705b1dbf87437f681c8f9a85b07adf Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark <allspark@microsoft.com> |
| 3 | +Date: Thu, 2 Apr 2026 15:18:09 +0000 |
| 4 | +Subject: [PATCH] Backport: Fix MQTT password exposed in JWT by deferring |
| 5 | + setting JWT for MQTT and using local ujwt in auth; remove setting JWT in |
| 6 | + mqttParseConnect |
| 7 | + |
| 8 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 9 | +Upstream-reference: AI Backport of https://github.com/nats-io/nats-server/commit/b5b63cfc35a57075e09c1f57503d31721bed8099.patch |
| 10 | +--- |
| 11 | + .../nats-io/nats-server/v2/server/auth.go | 16 ++++++++++++++-- |
| 12 | + .../nats-io/nats-server/v2/server/mqtt.go | 1 - |
| 13 | + 2 files changed, 14 insertions(+), 3 deletions(-) |
| 14 | + |
| 15 | +diff --git a/vendor/github.com/nats-io/nats-server/v2/server/auth.go b/vendor/github.com/nats-io/nats-server/v2/server/auth.go |
| 16 | +index 97106343..dc783409 100644 |
| 17 | +--- a/vendor/github.com/nats-io/nats-server/v2/server/auth.go |
| 18 | ++++ b/vendor/github.com/nats-io/nats-server/v2/server/auth.go |
| 19 | +@@ -586,6 +586,7 @@ func processUserPermissionsTemplate(lim jwt.UserPermissionLimits, ujwt *jwt.User |
| 20 | + func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (authorized bool) { |
| 21 | + var ( |
| 22 | + nkey *NkeyUser |
| 23 | ++ ujwt string |
| 24 | + juc *jwt.UserClaims |
| 25 | + acc *Account |
| 26 | + user *User |
| 27 | +@@ -729,13 +730,19 @@ func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (au |
| 28 | + |
| 29 | + // Check if we have trustedKeys defined in the server. If so we require a user jwt. |
| 30 | + if s.trustedKeys != nil { |
| 31 | +- if c.opts.JWT == _EMPTY_ { |
| 32 | ++ ujwt = c.opts.JWT |
| 33 | ++ if ujwt == _EMPTY_ && c.isMqtt() { |
| 34 | ++ // For MQTT, we pass the password as the JWT too, but do so here so it's not |
| 35 | ++ // publicly exposed in the client options if it isn't a JWT. |
| 36 | ++ ujwt = c.opts.Password |
| 37 | ++ } |
| 38 | ++ if ujwt == _EMPTY_ { |
| 39 | + s.mu.Unlock() |
| 40 | + c.Debugf("Authentication requires a user JWT") |
| 41 | + return false |
| 42 | + } |
| 43 | + // So we have a valid user jwt here. |
| 44 | +- juc, err = jwt.DecodeUserClaims(c.opts.JWT) |
| 45 | ++ juc, err = jwt.DecodeUserClaims(ujwt) |
| 46 | + if err != nil { |
| 47 | + s.mu.Unlock() |
| 48 | + c.Debugf("User JWT not valid: %v", err) |
| 49 | +@@ -995,6 +1002,11 @@ func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (au |
| 50 | + // Hold onto the user's public key. |
| 51 | + c.mu.Lock() |
| 52 | + c.pubKey = juc.Subject |
| 53 | ++ // If this is a MQTT client, we purposefully didn't populate the JWT as it could contain |
| 54 | ++ // a password or token. Now we know it's a valid JWT, we can populate it. |
| 55 | ++ if c.isMqtt() { |
| 56 | ++ c.opts.JWT = ujwt |
| 57 | ++ } |
| 58 | + c.tags = juc.Tags |
| 59 | + c.nameTag = juc.Name |
| 60 | + c.mu.Unlock() |
| 61 | +diff --git a/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go b/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go |
| 62 | +index 7ca49081..f5ef29e6 100644 |
| 63 | +--- a/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go |
| 64 | ++++ b/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go |
| 65 | +@@ -3561,7 +3561,6 @@ func (c *client) mqttParseConnect(r *mqttReader, hasMappings bool) (byte, *mqttC |
| 66 | + return 0, nil, err |
| 67 | + } |
| 68 | + c.opts.Token = c.opts.Password |
| 69 | +- c.opts.JWT = c.opts.Password |
| 70 | + } |
| 71 | + return 0, cp, nil |
| 72 | + } |
| 73 | +-- |
| 74 | +2.45.4 |
| 75 | + |
0 commit comments