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

[Bug]: TypeError when setting Packet Property to undefined #152

Closed
SoulKa opened this issue Feb 14, 2025 · 3 comments · Fixed by #155
Closed

[Bug]: TypeError when setting Packet Property to undefined #152

SoulKa opened this issue Feb 14, 2025 · 3 comments · Fixed by #155
Labels

Comments

@SoulKa
Copy link
Contributor

SoulKa commented Feb 14, 2025

MQTTjs Version

5.10.3

Broker

mosquitto 2.0.20

Environment

NodeJS

Description

Having a packet property set to undefined results in a TypeError:

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received undefined
    at Function.byteLength (node:buffer:777:11)
    at getLengthProperty (/node_modules/mqtt-packet/writeToStream.js:929:30)
    at getProperties (/node_modules/mqtt-packet/writeToStream.js:1003:27)
    at publish (/node_modules/mqtt-packet/writeToStream.js:350:22)
    at Object.generate [as writeToStream] (/node_modules/mqtt-packet/writeToStream.js:35:14)
    at MqttClient._writePacket (/node_modules/mqtt/src/lib/client.ts:1961:29)
    at <anonymous> (/node_modules/mqtt/src/lib/client.ts:1862:9)
    at Store.put (/node_modules/mqtt/src/lib/store.ts:81:4)
    at MqttClient._storeAndSend (/node_modules/mqtt/src/lib/client.ts:1857:22)
    at MqttClient._sendPacket (/node_modules/mqtt/src/lib/client.ts:2040:10) {
  code: 'ERR_INVALID_ARG_TYPE'
}

This is allowed by the IClientPublishOptions interface and can easily happen if optional properties are passed along into the publish() method of the MqttClient.

Minimal Reproduction

    await client.publishAsync('somewhere', '', {
      properties: {
        correlationData: undefined
      }
    });

Debug logs

  mqttjs:client publish :: message `{"value":205}` to topic `somewhere` +0ms
  mqttjs:client publish :: qos 1 +0ms
  mqttjs:client MqttClient:publish: packet cmd: publish +0ms
  mqttjs:client _sendPacket :: (CLIENT) ::  start +0ms
  mqttjs:client storeAndSend :: store packet with cmd publish to outgoingStore +0ms
  mqttjs:client _removeTopicAliasAndRecoverTopicName :: alias NaN, topic 'somewhere' +0ms
  mqttjs:client noop :: undefined +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'publish',
  mqttjs:client   topic: 'somewhere',
  mqttjs:client   payload: '{"value":205}',
  mqttjs:client   qos: 1,
  mqttjs:client   retain: true,
  mqttjs:client   messageId: 10561,
  mqttjs:client   dup: false,
  mqttjs:client   properties: {
  mqttjs:client     contentType: 'application/json; charset=utf-8',
  mqttjs:client     correlationData: undefined
  mqttjs:client   }
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +0ms
  mqttjs:client _writePacket :: writing to stream +0ms
@robertsLando
Copy link
Member

@mcollina I transferred the issue here as I think the properties should just be ignored when them are undefined

@mcollina
Copy link
Member

A PR would be nice ;)

@SoulKa
Copy link
Contributor Author

SoulKa commented Feb 18, 2025

I'll try to find some time for a PR this week.

SoulKa added a commit to SoulKa/mqtt-packet that referenced this issue Mar 3, 2025
SoulKa added a commit to SoulKa/mqtt-packet that referenced this issue Mar 3, 2025
mcollina pushed a commit that referenced this issue Mar 4, 2025
* #152 - ignore undefined properties

* #152 - add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants