database icon indicating copy to clipboard operation
database copied to clipboard

🐛 Unable to create auth token

Open jenky opened this issue 1 year ago • 2 comments

No duplicates 🥲.

  • [X] I have searched for a similar issue in our bug tracker and didn't find any solutions.

Database

PostgreSQL

What happened?

Original issue https://github.com/spiral/cycle-bridge/issues/85

SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type bytea
CONTEXT:  unnamed portal parameter $5 = '...'

The payload contains an url. Upon json_encode, it will add backslashes to escape the uri e.g http:\/\/127.0.0.1:8080\/ which I guess somewhat invalid for bytea column. Removing the URL from the payload resolves the issue.

Version

Postgres 16.1
PHP 8.3

jenky avatar Dec 28 '23 15:12 jenky

In PostgreSQL, the BYTEA data type is designed to store binary data, such as images, sounds, and other raw byte sequences. The BYTEA type is intended for representing data in binary form, and inserting strings with backslashes may cause issues due to the intricacies of byte handling and escaping in SQL.

If you attempt to insert a string with backslashes directly into a BYTEA column, it can lead to confusion, as the backslash () in SQL is used for escaping special characters. Therefore, when inserting a string with backslashes, they should be properly escaped.

Example of a problematic string:

INSERT INTO your_table (binary_column) VALUES ('some data with a slash \');

In this case, PostgreSQL might interpret the backslash as the beginning of an escaped sequence, leading to an error or misinterpretation of the data.

To avoid such situations, you can use the E'...' function, which allows the use of escape sequences in strings:

INSERT INTO your_table (binary_column) VALUES (E'some data with a slash \\');

In this example, E'...' enables the use of an escaped backslash in the string. Note that each backslash must be doubled to prevent interpretation errors.

Or check this function https://www.php.net/manual/en/function.pg-escape-bytea.php

iGrog avatar Jan 22 '24 15:01 iGrog

Thank you. However, if you refer to the original issue, I do not have control over how the data is being serialized.

https://github.com/spiral/cycle-bridge/blob/e0bda2ca40c696ecd82de3d49fb32b40307bc63b/src/Auth/Token.php#L50

jenky avatar Jan 23 '24 10:01 jenky