UTF-8 null character in text causes "invalid byte sequence for encoding UTF8"
See original GitHub issueI 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:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jbtobar Zero bytes should work fine with
bytea
. Are you passing the parameter as aBuffer
or other typed array? That’s necessary. Also, make sure your query doesn’t try to decode thebytea
as text at any point.pg could detect it, but it would just throw the same kind of error upon doing so. Unless you’re getting accidental
\0
s in input that would warrant manual filtering, it’s probably safe to continue with default error handling.