question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
charmandercommented, Sep 16, 2020

@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.

1reaction
charmandercommented, Jan 21, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres error on insert - ERROR: invalid byte sequence for ...
Is there a way to get postgres to do this for me? ERROR: invalid byte sequence for encoding "UTF8": 0x00. postgresql.
Read more >
No NULLs, yet invalid byte sequence for encoding "UTF8": 0x00
ERROR: invalid byte sequence for encoding "UTF8": 0x00 HINT: This error can also happen if the byte sequence does not match the encoding...
Read more >
When loading the data to the target database, getting an error ...
invalid byte sequence for encoding utf8. Root cause. You may be sending NULL characters to the PostgreSQL database.
Read more >
Invalid byte sequence for encoding "UTF8": 0x00 | Bitbucket ...
This problem occurs because PostgreSQL does not allow null characters (U+0000) in its text data types. As a result, when migrating or restoring ......
Read more >
libpq and mysterious "invalid byte sequence for encoding UTF8".
> invalid byte sequence for encoding "UTF8". ... execute the SQL query. ... receive the error with a random byte sequence. ... create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found