Skip to content

Conversation

@AlanCoding
Copy link
Member

@AlanCoding AlanCoding commented May 16, 2025

Fixes #170

I originally made this because I anticipated raising an error if "-" was in the channel name.

But frustratingly, I couldn't make the code error in that case and came away with the conclusion that it is actually valid. Some things are invalid, but that's a more narrow case, still good to get ahead of it.

@AlanCoding AlanCoding requested a review from Alex-Izquierdo May 19, 2025 17:30
Copy link
Collaborator

@Alex-Izquierdo Alex-Izquierdo left a comment

Choose a reason for hiding this comment

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

@AlanCoding
Copy link
Member Author

It would be easy to agree, but I'm trying to take an evidence-based approach.

diff --git a/tests/integration/brokers/test_pg_notify.py b/tests/integration/brokers/test_pg_notify.py
index 8a052b6..9fc79e2 100644
--- a/tests/integration/brokers/test_pg_notify.py
+++ b/tests/integration/brokers/test_pg_notify.py
@@ -121,9 +121,11 @@ VALID_CHANNEL_NAMES = [
 
 
 BAD_CHANNEL_NAMES = [
-    '🔥' * 22,  # under 64 but expanded unicode is over
+    'a' + '🔥' * 22,  # under 64 but expanded unicode is over
     'a' * 64,  # over the limit of 63
-    'a' * 120
+    'a' * 120,
+    '',
+    '🔥aa'  # First char must be alphabetical
 ]

The blank string fails to send as expected. I don't know the specific exception, but an exception is raised so that all checks out. However, see the failure on 🔥aa:

    @pytest.mark.parametrize('channel_name', BAD_CHANNEL_NAMES)
    def test_can_not_receive_over_invalid_channels(self, can_receive_notification, channel_name):
>       assert not can_receive_notification(channel_name)
E       AssertionError: assert not True
E        +  where True = <function TestChannelSanitization.can_receive_notification.<locals>._rf at 0x7fdf5ba74400>('🔥aa')

In this test, it legitimately sent and then received a message over the 🔥aa channel.

If I put this in the valid channel list, the tests pass. I'll push a commit to try and make the sanity tests (testing psycopg) more clear.

@Alex-Izquierdo
Copy link
Collaborator

@AlanCoding I think it is because psycopg use quoted names by default (which supports unicode, for example). I just thought that we would want to be conservative and use unquoted ones, thinking on other potential clients or raw queries. But I don't have a strong opinion on this.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanitize or validate pg_notify channel names

2 participants