{"record":{"id":"55e11b66546db84f","repo":"nats-io/nats-server","slug":"password-flag-set-but-username-flag-is-not","errorCode":null,"errorMessage":"password flag set but username flag is not","messagePattern":"password flag set but username flag is not","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/mqtt.go","lineNumber":237,"sourceCode":"\tmqttSessJailDur      = mqttSessFlappingJailDur\n\tmqttFlapCleanItvl    = mqttSessFlappingCleanupInterval\n\tmqttRetainedCacheTTL = mqttDefaultRetainedCacheTTL\n)\n\nvar (\n\terrMQTTNotWebsocketPort           = errors.New(\"MQTT clients over websocket must connect to the Websocket port, not the MQTT port\")\n\terrMQTTTopicFilterCannotBeEmpty   = errors.New(\"topic filter cannot be empty\")\n\terrMQTTMalformedVarInt            = errors.New(\"malformed variable int\")\n\terrMQTTSecondConnectPacket        = errors.New(\"received a second CONNECT packet\")\n\terrMQTTServerNameMustBeSet        = errors.New(\"mqtt requires server name to be explicitly set\")\n\terrMQTTUserMixWithUsersNKeys      = errors.New(\"mqtt authentication username not compatible with presence of users/nkeys\")\n\terrMQTTTokenMixWIthUsersNKeys     = errors.New(\"mqtt authentication token not compatible with presence of users/nkeys\")\n\terrMQTTAckWaitMustBePositive      = errors.New(\"ack wait must be a positive value\")\n\terrMQTTJSAPITimeoutMustBePositive = errors.New(\"JS API timeout must be a positive value\")\n\terrMQTTStandaloneNeedsJetStream   = errors.New(\"mqtt requires JetStream to be enabled if running in standalone mode\")\n\terrMQTTConnFlagReserved           = errors.New(\"connect flags reserved bit not set to 0\")\n\terrMQTTWillAndRetainFlag          = errors.New(\"if Will flag is set to 0, Will Retain flag must be 0 too\")\n\terrMQTTPasswordFlagAndNoUser      = errors.New(\"password flag set but username flag is not\")\n\terrMQTTCIDEmptyNeedsCleanFlag     = errors.New(\"when client ID is empty, clean session flag must be set to 1\")\n\terrMQTTEmptyWillTopic             = errors.New(\"empty Will topic not allowed\")\n\terrMQTTEmptyUsername              = errors.New(\"empty user name not allowed\")\n\terrMQTTTopicIsEmpty               = errors.New(\"topic cannot be empty\")\n\terrMQTTPacketIdentifierIsZero     = errors.New(\"packet identifier cannot be 0\")\n\terrMQTTUnsupportedCharacters      = errors.New(\"character not supported for MQTT topics\")\n\terrMQTTInvalidSession             = errors.New(\"invalid MQTT session\")\n\terrMQTTInvalidRetainFlags         = errors.New(\"invalid retained message flags\")\n\terrMQTTInvalidRetainedMessage     = errors.New(\"invalid retained message\")\n\terrMQTTSessionCollision           = errors.New(\"stored session does not match client ID\")\n\terrMQTTInvalidPublishLength       = errors.New(\"invalid publish message, variable header exceeds remaining length\")\n\terrMQTTAckPipelineStopped         = errors.New(\"QoS1 PUBACK pipeline has shut down while admitting a message, \" +\n\t\t\"abandoning the wait for its JetStream ack; failing the connection, \" +\n\t\t\"the client will re-send unacknowledged PUBLISH packets on reconnect\")\n)\n\ntype srvMQTT struct {\n\tlistener     net.Listener","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/mqtt.go#L219-L255","documentation":"MQTT forbids sending a password without a username: per spec [MQTT-3.1.2-22], the password flag requires the username flag. During CONNECT parsing (server/mqtt.go:3856), if hasPassword is true but hasUser is false, the connection is rejected with this error.","triggerScenarios":"A CONNECT packet sets bit 1 (password flag) but not bit 7 (username flag), e.g. flags byte mqttConnFlagPasswordFlag alone as in the test case.","commonSituations":"Clients configured with only a password (no username) in the connection options, or hand-built packets setting the password bit independently of the username bit.","solutions":["Configure the MQTT client with both username and password, or drop the password.","Fix hand-encoded packets so the username flag (bit 7) accompanies the password flag (bit 1).","Check the client's broker URI/config: many clients silently omit the username when it is empty."],"exampleFix":"// before\nflags := mqttConnFlagPasswordFlag // password without username\n// after\nflags := mqttConnFlagUsernameFlag | mqttConnFlagPasswordFlag","handlingStrategy":"validation","validationCode":"// client side\nif hasPassword && !hasUsername {\n    return fmt.Errorf(\"password requires a username per MQTT spec\")\n}","typeGuard":null,"tryCatchPattern":"// client side: on 'password flag set but username flag is not', supply both credentials or drop the password","preventionTips":["Always configure username+password as a pair in client configs.","Treat empty username with non-empty password as a config error at load time.","Avoid hand-assembling connect flags."],"tags":["mqtt","protocol","authentication","client-error"],"backgroundTag":"malformed-packet","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}