cycle / database

Database Abstraction Layer, Schema Introspection, Schema Generation, Query Builders
MIT License
53 stars 22 forks source link

🐛 Unable to create auth token #153

Open jenky opened 6 months ago

jenky commented 6 months ago

No duplicates 🥲.

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
iGrog commented 5 months ago

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

jenky commented 5 months ago

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