Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Binary behaviour extensible which might also be used for mysql adapter #105

Merged
merged 6 commits into from
May 15, 2023

Conversation

yhabteab
Copy link
Member

@yhabteab yhabteab commented Apr 12, 2023

This now makes it possible to extend the Binary behaviour and to additionally apply other behaviours to the mysql adapter as well, such as the IP behaviour of the x509 module.

fixes Icinga/icingaweb2-module-x509#172

nilmerg
nilmerg previously approved these changes Apr 18, 2023
@nilmerg nilmerg added this to the v0.5.2 milestone Apr 18, 2023
@nilmerg nilmerg added the bug Something isn't working label Apr 18, 2023
Copy link
Member

@lippserd lippserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this PR tomorrow, please.

Copy link
Member

@lippserd lippserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update PR title and description as this change actually allows for subclassing the behavior.

@yhabteab yhabteab changed the title Binary: Don't reset properties when mysql adapter is used Make Binary behaviour extensible which might also be used for mysql adapter. Apr 19, 2023
@yhabteab yhabteab changed the title Make Binary behaviour extensible which might also be used for mysql adapter. Make Binary behaviour extensible which might also be used for mysql adapter Apr 19, 2023
@yhabteab yhabteab requested a review from lippserd April 19, 2023 09:11
lippserd
lippserd previously approved these changes Apr 19, 2023
@yhabteab
Copy link
Member Author

We discussed offline with @nilmerg and agreed not to consider all characters that are not prefixed with \x as hex number. The x509 module already does this correctly by displaying in the search bar (when searching for the subject hash) as hex prefixed with \x. So, since icingadb-web will need a new release soon, binary data should be displayed there as hex number with \x prefixed as well. And thus makes the rewriteCondition implementation obsolete and is dropped, which never actually worked properly for what it was intended.

@yhabteab yhabteab requested a review from lippserd April 19, 2023 15:34
@lippserd
Copy link
Member

agreed not to consider all characters that are not prefixed with \x as hex number

This is bytea hex format. \x$whatever is valid binary. Only ctype_xdigit() is safe. But I don't see why we want to support hex strings for inserts and updates.

binary data should be displayed ... as hex number with \x

No. Didn't even notice that this is already the case in the certificate monitoring module. If you really want to show that this is a hex string, prefix it with 0x. Though I strongly suggest not do that. If you cant see that the suggestions are hex strings, \x or 0x won't help you further. Also, this would require to add the prefix for manual values. And we would have to strip them in the behavior and still check whether it's a hex string since the prefix may just be the first characters of a binary string.

rewriteCondition ... which never actually worked properly for what it was intended

Please elaborate. Using hex strings worked perfectly before with the recent fixes I made in this PR. Of course only without the \x prefix.

Copy link
Member

@lippserd lippserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilmerg
Copy link
Member

nilmerg commented Apr 20, 2023

This is bytea hex format. \x$whatever is valid binary. Only ctype_xdigit() is safe.

bytea is a PostgreSQL type, which uses \xHEX by chance to represent binary as printable string. If you search for the \x prefix, you find nothing which hints at this being used strictly for binary strings, besides PostgreSQL of course.
ctype_xdigit is not safe. It only tells you that all characters are hexadecimal. Though, if you ask ctype_print it tells you it's ASCII. I know, I know, it's the same, but it shows why ctype_xdigit is not safe.

We need a prefix. Be it \x or 0x. To being able to independently detect that we have to decode hex in order to get the binary representation.

@yhabteab
Copy link
Member Author

yhabteab commented Apr 20, 2023

Please elaborate. Using hex strings worked perfectly before with the recent fixes I made in this PR. Of course only without the \x prefix.

Yes, the unit tests did run successfully, but in which use cases would rewriteCondition be useful at all? Binary#toDb() is always called before rewriteCondtion() and thus gets the already transformed(or binary for mysql adapter) data, which results in ctype_xdigit() always returning false.

@lippserd
Copy link
Member

bytea is a PostgreSQL type, which uses \xHEX by chance to represent binary as printable string. If you search for the \x prefix, you find nothing which hints at this being used strictly for binary strings, besides PostgreSQL of course.

Yes, the unit tests did run successfully, but in which use cases would rewriteCondition be useful at all? Binary#toDb() is always called before rewriteCondtion() and thus gets the already transformed(or binary for mysql adapter) data, which results in ctype_xdigit() always returning false.

My point was that \x$whatever is valid binary data where hex2bin() must not be called just because it starts with \x. The same goes for any other prefix, e.g. 0x. That's why toDb() must not try to do any hex transformation.

Nothing must stop me from inserting hex values like 76616c7565 ('value') or 0x76616c7565. Now it's true that we would have a problem if we just used ctype_xdigit() to do the hex to binary transformation for filters. A prefix makes sense here. Still, we would have to remove the prefix and use ctype_xidigit() to check whether the remainders are hex digits. For example, 0xfoobar is valid binary and not hexadecimal. If I would've inserted 0x76616c7565 my filter must be 0x0x76616c7565 then where no transformation takes place. ctype_print() is of no use. You're thinking of binary data being unprintable characters only but that is just not the case.

@nilmerg
Copy link
Member

nilmerg commented Apr 20, 2023

Let's discuss this in person. Should prevent any further misunderstandings.......

Copy link
Member

@nilmerg nilmerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't test, though.

Copy link
Member

@lippserd lippserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the records, do we use a prefix now or not? I have no problem not using a prefix. In the rare case that actual hex values are stored in the database and filters are composed manually, the user has to hex hex 😆. But this is fine for me.

@yhabteab Don't forget to adjust X.509 to not add the \x prefix for value suggestions in the search bar.

@yhabteab
Copy link
Member Author

Just for the records, do we use a prefix now or not?

Only for PostgreSQL adapter.

@nilmerg nilmerg merged commit f8cd49c into main May 15, 2023
12 checks passed
@nilmerg nilmerg deleted the fix-binary-behaviour branch May 15, 2023 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cla/signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IP Text wrong symbols
3 participants