frontend-tech

Month: 2021-08

2021-08-17

kelvinho84 10:14:26
Anyone faced this buffer to string with startswith issues ?

``` const strPass = password.toString();
console.log(password);
console.log(password.toString().length);
console.log(strPass.length);
console.log("PASSPSS :", strPass);
console.log("ADD A!", strPass.startsWith("Bearer "));
console.log(
"ADD B!",
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4wLjFcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE2MjkxNjE1NDQsImV4cCI6MTYyOTE5MDM0NCwibmJmIjoxNjI5MTYxNTQ0LCJqdGkiOiJLV01URGV1dkl1bVNROHJ2Iiwic3ViIjoxNiwicHJ2IjoiODdlMGFmMWVmOWZkMTU4MTJmZGVjOTcxNTNhMTRlMGIwNDc1NDZhYSJ9.7sr4qjHJKwt6ZEnfatstNjf5J8vudHxXXTVrp8qAU3E".startsWith(
"Bearer "
)
);```

```<Buffer 42 65 61 72 65 72 c2 a0 65 79 4a 30 65 58 41 69 4f 69 4a 4b 56 31 51 69 4c 43 4a 68 62 47 63 69 4f 69 4a 49 55 7a 49 31 4e 69 4a 39 2e 65 79 4a 70 63 ... 279 more bytes>
328
328
PASSPSS : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xMjcuMC4wLjFcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE2MjkxNjE1NDQsImV4cCI6MTYyOTE5MDM0NCwibmJmIjoxNjI5MTYxNTQ0LCJqdGkiOiJLV01URGV1dkl1bVNROHJ2Iiwic3ViIjoxNiwicHJ2IjoiODdlMGFmMWVmOWZkMTU4MTJmZGVjOTcxNTNhMTRlMGIwNDc1NDZhYSJ9.7sr4qjHJKwt6ZEnfatstNjf5J8vudHxXXTVrp8qAU3E
ADD A! false
ADD B! true```
mrorz 10:51:48
The space after “Bearer” in `password` is weird.
mrorz 10:52:51
Ordinary space is 32 (`0x20`) but the one in your buffer is `0xC2`
kelvinho84 2021-08-17 10:56:13
thanks! its really the space!
😮 1
mrorz 10:53:17
That’s why it fails `startsWith("Bearer ")` check
mrorz 10:55:38
Ohh it’s `0xC2A0` , Non-breaking space in UTF-8 encoding
https://en.wikipedia.org/wiki/Non-breaking_space

Non-breaking space

In word processing and digital typesetting, a non-breaking space, , also called NBSP, required space, hard space, or fixed space (though it is not of fixed width), is a space character that prevents an automatic line break at its position. In some formats, including HTML, it also prevents consecutive whitespace characters from collapsing into a single space. Non-breaking space characters with other widths also exist.

😍 1 1