I have a Loopback 3 app with a PGSQL database. Certain enpoints require authorization in the form of a JWT token. The token is generated by a separate service written in PHP and is usually sent to loopback as a Authorization Bearer header. Every now and then, a new token will cause the entire app to break with this error:
error: invalid byte sequence for encoding \"UTF8\": 0x00
at Connection.parseE (C:\...\node_modules\pg\lib\connection.js:545:11)
at Connection.parseMessage (C:\...\node_modules\pg\lib\connection.js:370:19)
at Socket.<anonymous> (C:\...\node_modules\pg\lib\connection.js:113:22)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:602:20)
This will happen whenever the authorization header is present, even if the endpoint does not require authorization. There is user role that validates this token against a different service, but this is never called for endpoints that don't require it, so it's not there. What usually works is generating a new token of the same length. Then the app starts responding again. Here's a sample of a token
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6Ly9leGFtcGxlLm9yZy9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIzMzk5Mjk4LCJleHAiOjE1MjM0MTAwOTgsIm5iZiI6MTUyMzM5OTI5OCwianRpIjoiYjBhZDZmOGQ2NWY2MWUwYTMzZmNmZWNmYTI0YTA2M2EifQ.nHYlfX7vkVU8NIioYfqSSAHGj1y2OyGhhmEazU-wCTY
I've searched for \0
characters in the "bad" tokens and found nothing. I don't know why the token would even come near the pg lib, there's nothing trying to store it. All other posts I've found about this error are usually about trying to insert invalid characters to the DB, which is not what I'm doing here.
I have tried looking at the files in the stack trace but have no idea where the issue could be and how to avoid it. I was thinking of intersecting the token somehow and trimming trailing \0
but don't know where either. Any help?