node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

UTF-8 null character in text causes "invalid byte sequence for encoding UTF8"

Open jan-osch opened this issue 6 years ago • 7 comments

I detected this issue when user input (sanitized for SQL-injection with knex) started causing postgres errors.

Here is a sample that reproduces this behavior

client.query('SELECT $1::text as message', ['Hello world\u0000!']);

Output:

(node:94484) UnhandledPromiseRejectionWarning: error: invalid byte sequence for encoding "UTF8": 0x00

Is this something we can detect/prevent/sanitize on driver level or should I sanitize all strings that touch text type in postgreSQL on application level? (probably using some cumbersome REGEXP). The knex guys suggested that I should contact you.

https://github.com/knex/knex/issues/2284#issuecomment-575685625

jan-osch avatar Jan 21 '20 15:01 jan-osch

A text field in PostgreSQL cannot contain Unicode symbol \u0000. It is reserved by PostgreSQL to represent the end of the string internally.

You can however successfully use \u0001 and so on symbols, i.e. everything else.

And because of that, this library does not need to sanitize knowingly invalid SQL.

vitaly-t avatar Jan 21 '20 22:01 vitaly-t

pg could detect it, but it would just throw the same kind of error upon doing so. Unless you’re getting accidental \0s in input that would warrant manual filtering, it’s probably safe to continue with default error handling.

charmander avatar Jan 21 '20 23:01 charmander

The same error is produced upon inserting '\x00' on a 'bytea' field. Shouldn't allow ''\x00" on a bytea field?

jbtobar avatar Sep 15 '20 15:09 jbtobar

@jbtobar I recommend the solution that worked for me: adding a stricter validation on the application level.

jan-osch avatar Sep 15 '20 18:09 jan-osch

Thank you for such a quick answer!

What I ended up doing is replacing the byte '\x00' with null. The issue is that I am processing a stream of thousands of messages per minute and even though it's a tiny replacement it still adds up.

Can you expand on what you mean by a stricter validation on the application level?

Cheers!

jbtobar avatar Sep 16 '20 03:09 jbtobar

@jbtobar Zero bytes should work fine with bytea. Are you passing the parameter as a Buffer or other typed array? That’s necessary. Also, make sure your query doesn’t try to decode the bytea as text at any point.

charmander avatar Sep 16 '20 09:09 charmander

I see - thanks!

I believe I am passing the parameter as a string - like so:

if (singleLoad[3] === '\x00') singleLoad[3] = null await client.query(text, singleLoad)

where text, singleLoad are:

const text =  "INSERT INTO trades(
  eventSymbol,
  eventTime,
  time,
  exchangeCode,
  price,
  change,
  size
) VALUES($1, $2, $3, $4, $5, $6, $7)"

const singleLoad = [
  '.PINS230120C37',
  0,
  1600180827366,
  '\x00',
  14, 
  0.4,
  3
] 

and the table was created like so:

CREATE TABLE trades (
  eventSymbol varchar(50),
  eventTime bigint,
  time bigint,
  exchangeCode bytea,
  price decimal,
  change decimal,
  size bigint
);

Would passing the parameter as buffer mean something like this?: singleLoad[3] = new Buffer(singleLoad[3]) I was hoping to not do any computation in the process of inserting this data because its a lot of data coming very fast so it would take a toll - even if small - on the process.

Is there an option or parameter i can alter once so that i dont have to transform every inserted payload?

jbtobar avatar Sep 16 '20 11:09 jbtobar