'nodejs disable unicode encoding of string
I have created a endpoint using express that accepts POST request from other system. This request contains sha1 hash that validates request.
Example request generated by ncat:
POST /webhook HTTP/1.1
Host: **nothing_to_see_here**
User-Agent: **nothing_to_see_here**
Content-Length: **nothing_to_see_here**
Accept: text/plain
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=utf-8
X-Forwarded-For: **nothing_to_see_here**
X-Forwarded-Proto: **nothing_to_see_here**
X-Shop-Domain: **nothing_to_see_here**
X-Shop-License: **nothing_to_see_here**
X-Shop-Version: **nothing_to_see_here**
X-Webhook-Id: **nothing_to_see_here**
X-Webhook-Name: **nothing_to_see_here**
X-Webhook-Sha1: bbebc1d43ee4eeeed251a1a899e1f2b92eab0008
{"user_id":"1","login":"","date_add":"2022-03-24 18:46:00","lastvisit":"2022-03-24 18:46:01","verify_email":"0","origin":"0","registered":"1","firstname":"\u017baneta","lastname":"\u017b\u00f3\u0142\u0107","email":"[email protected]","discount":"0.00","newsletter":0,"active":"1","lang_id":"1","comment":"asdads\r\nzxczxc\r\nqweqweq","groups":[{"group_id":"1","name":"hurtowa 1","discount":"0.00","price_level":"2"}],"addresses":[],"additional_fields":[{"field_id":"1","type":"2","locate":"7","req":"1","active":"1","order":"14","value":"1"},{"field_id":"17","type":"1","locate":"1","req":"0","active":"1","order":"2","value":""}]}
In this example in body under firstname key there is "\u017baneta" which is encoded string that translates to "Żaneta"
My problem is that hash given to me in X-Webhook-Sha1 header was generated with PHP that treat \u017b as 6 char string.
My endpoint is created with node and here \u017b is treated as 1 char (Ż) and my sha1 hash is different than what have server sent.
How can I prevent this?
What I have tryed:
utf8.encodeandutf8.decode- passing
Buffer.from(reqBody, 'utf-8')to shaSum.update - changing
cryptotosha1 - using shell commands
String.rawJSON.stringify(JSON.parse(reqBody))
Only thing that works is when I explicity replace every \u with \\u but methods like .replace() and .replaceAll() does not work on this string in node. (Or it does but I'm unable to make it work).
Examples of generating sha1 hash
(Sum is generated as sha1(sth1:sth2:reqBody})
PHP
echo sha1('1:test:{"user_id":"1","login":"","date_add":"2022-03-24 18:46:00","lastvisit":"2022-03-24 18:46:01","verify_email":"0","origin":"0","registered":"1","firstname":"\u017baneta","lastname":"\u017b\u00f3\u0142\u0107","email":"[email protected]","discount":"0.00","newsletter":0,"active":"1","lang_id":"1","comment":"asdads\r\nzxczxc\r\nqweqweq","groups":[{"group_id":"1","name":"hurtowa 1","discount":"0.00","price_level":"2"}],"addresses":[],"additional_fields":[{"field_id":"1","type":"2","locate":"7","req":"1","active":"1","order":"14","value":"1"},{"field_id":"17","type":"1","locate":"1","req":"0","active":"1","order":"2","value":""}]}');
// output: bbebc1d43ee4eeeed251a1a899e1f2b92eab0008
NodeJS
const crypto = require('crypto');
const shaSum = crypto.createHash('sha1');
shaSum.update('1:test:{"user_id":"1","login":"","date_add":"2022-03-24 18:46:00","lastvisit":"2022-03-24 18:46:01","verify_email":"0","origin":"0","registered":"1","firstname":"\u017baneta","lastname":"\u017b\u00f3\u0142\u0107","email":"[email protected]","discount":"0.00","newsletter":0,"active":"1","lang_id":"1","comment":"asdads\\r\\nzxczxc\\r\\nqweqweq","groups":[{"group_id":"1","name":"hurtowa 1","discount":"0.00","price_level":"2"}],"addresses":[],"additional_fields":[{"field_id":"1","type":"2","locate":"7","req":"1","active":"1","order":"14","value":"1"},{"field_id":"17","type":"1","locate":"1","req":"0","active":"1","order":"2","value":""}]}');
const hash = shaSum.digest('hex');
console.log(hash);
// output: 36fad2735dcbf8b1b63517cd6cff1958dae93b94
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
