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

Add warning for the length of the group name #2122

Merged
merged 12 commits into from
Jan 28, 2025
22 changes: 18 additions & 4 deletions channels/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import string
import time
from copy import deepcopy

import logging
logger = logging.getLogger(__name__)
from django.conf import settings
from django.core.signals import setting_changed
from django.utils.module_loading import import_string
Expand Down Expand Up @@ -160,7 +161,10 @@ def valid_channel_name(self, name, receive=False):
raise TypeError(self.invalid_name_error.format("Channel", name))

def valid_group_name(self, name):
if self.match_type_and_length(name):
logger.debug(f"Validating group name: {name}, Length: {len(name)}") # Log group name length
if isinstance(name, str):
if len(name) >= self.MAX_NAME_LENGTH:
raise False
if bool(self.group_name_regex.match(name)):
return True
raise TypeError(self.invalid_name_error.format("Group", name))
Expand Down Expand Up @@ -201,6 +205,7 @@ async def flush(self):
raise NotImplementedError("flush() not implemented (flush extension)")

async def group_add(self, group, channel):
print("Hello")
Copy link
Member

Choose a reason for hiding this comment

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

This (and others) need removing.

raise NotImplementedError("group_add() not implemented (groups extension)")

async def group_discard(self, group, channel):
Expand Down Expand Up @@ -336,19 +341,28 @@ def _remove_from_groups(self, channel):

# Groups extension


async def group_add(self, group, channel):
"""
Adds the channel name to a group.
"""
# Check the inputs
assert self.valid_group_name(group), "Group name not valid"
print(f"Validating group name: {group}")
assert self.valid_group_name(group), f"Group name must be less than {self.MAX_NAME_LENGTH} characters."
assert self.valid_channel_name(channel), "Channel name not valid"
# Add to group dict

# Check the length of the group name
if len(group) >= self.MAX_NAME_LENGTH:
raise TypeError(f"Group name must be less than {self.MAX_NAME_LENGTH} characters, but got {len(group)}.")

# Add to group dict
self.groups.setdefault(group, {})
self.groups[group][channel] = time.time()


async def group_discard(self, group, channel):
# Both should be text and valid
print(f"Discarding channel {channel} from group {group}") # Log group name
assert self.valid_channel_name(channel), "Invalid channel name"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here as aboev

assert self.valid_group_name(group), "Invalid group name"
# Remove from group set
Expand Down
Loading