{"record":{"id":"1de18464bad2dc4f","repo":"nats-io/nats-server","slug":"malformed-variable-int","errorCode":null,"errorMessage":"malformed variable int","messagePattern":"malformed variable int","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/mqtt.go","lineNumber":227,"sourceCode":"\nvar (\n\tsparkbNamespaceTopicPrefix    = []byte(\"spBv1.0/\")\n\tsparkbCertificatesTopicPrefix = []byte(\"$sparkplug/certificates/\")\n)\n\nvar (\n\tmqttPingResponse     = []byte{mqttPacketPingResp, 0x0}\n\tmqttProtoName        = []byte(\"MQTT\")\n\tmqttOldProtoName     = []byte(\"MQIsdp\")\n\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\")","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/mqtt.go#L209-L245","documentation":"`errMQTTMalformedVarInt` is returned when decoding an MQTT Remaining Length variable-byte integer that exceeds the maximum allowed value (the multiplier exceeds 0x200000, i.e. more than 4 varint bytes or an overlong encoding). It indicates the packet's length encoding is invalid per the MQTT spec (server/mqtt.go:6394).","triggerScenarios":"A peer sending more than four continuation bytes for the remaining-length field; an oversized or corrupt frame on the wire; a client encoding the length incorrectly (e.g. little-endian or with an invalid final byte).","commonSituations":"Hand-written MQTT encoders; corrupted TCP stream after a partial read/protocol desync; fuzzing or malicious clients sending garbage length fields.","solutions":["Fix the client's remaining-length encoder: encode as MQTT varint (7 bits per byte, high bit = continuation, max 4 bytes / 268435455)","Resend the packet; if the stream desynced the connection is closed anyway — reconnect with a fresh session","Validate packet sizes before sending so remaining length stays under 256MB"],"exampleFix":"// before: wrong length encoding (plain 4-byte int)\nlenBytes := []byte{0x00, 0x00, 0x01, 0x00}\n// after: proper MQTT varint for 256\nlenBytes := []byte{0x80, 0x02}","handlingStrategy":"validation","validationCode":"// Validate remaining-length encoding before send\nfunction encodeVarint(n) { if (n < 0 || n > 268435455) throw new Error('length out of range'); /* 7-bit chunks, high-bit continuation */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap message sizes well below 256MB","Test encoders against the MQTT spec varint examples","Log and drop corrupt frames; never continue on a desynced stream"],"tags":["mqtt","protocol","varint","malformed-packet"],"backgroundTag":"mqtt-protocol-violation","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}