sailthru / sailthru-node-client

Sailthru Node.js client
http://docs.sailthru.com
MIT License
17 stars 16 forks source link

Error when using apiPost with multipart in v5.0.1: "Signature hash does not match" #49

Open hendaniel opened 4 months ago

hendaniel commented 4 months ago

Description

When attempting to make a POST request using the apiPost function with multipart/form-data in Node.js 18.16.1 and using sailthru-client version 5.0.1, I encounter a signature hash mismatch error. The error message returned is {"error":5,"errormsg":"Signature hash does not match"}. This issue does not occur when using sailthru-client version 5.0.0.

Steps to Reproduce

  1. Set up a Node.js environment with version 18.16.1.
  2. Use sailthru-client version 5.0.1.
  3. Implement a POST request using the apiPost function with multipart/form-data.
  4. Execute the request.

Expected Behavior

The API should successfully process the multipart POST request without returning a signature hash mismatch error.

Actual Behavior

The API call fails and returns the following error message: {"error":5,"errormsg":"Signature hash does not match"}. The error does not occur when downgrading to sailthru-client version 5.0.0.

Environment

marcelhamel commented 4 months ago

After digging in, it seems that the issue is potentially this piece of code, line 300 in lib/sailthru.js:

json_payload_to_log = json_payload;

This appears to cause both variables to point to the same spot in memory, which means that when the following for/in loop runs immediately afterward....

for (param in binary_data) {
      value = binary_data[param];
      json_payload[param] = value;
      json_payload_to_log[param] = '[TRUNCATED]';
}

... the payload's file data is simply changed to a string value of '[TRUNCATED]' rather than the actual file object which needs to be passed. The signature hash itself is valid, but the invalid file format is throwing this error on the Sailthru backend for whatever reason. Changing the above variable definition to json_payload_to_log = this._json_payload(data) appears to completely remedy this issue.

hendaniel commented 4 months ago

Thanks for looking into this! I'll be on the lookout for the fix release. We are also dealing with the JavaScript memory issue that was addressed in v5.0.1, so any updates would be greatly appreciated.

expe commented 3 months ago

Thanks @marcelhamel, I've merged your PR and released 5.0.2. @hendaniel could you try 5.0.2 to see if this fixes multipart POST requests for you?