From 73cf9a220b1b213d3923054cded0f5286a75349d Mon Sep 17 00:00:00 2001 From: samy Date: Tue, 14 Apr 2026 22:57:53 +0800 Subject: [PATCH] fix: handle dict config in is_allowed() and _validate_allow_from() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getattr() on a dict never finds custom keys — it only searches object attributes, not dict keys. When channel config is loaded as a Pydantic extra field (which is a plain dict), getattr(config, 'allow_from', []) always returns the default [], causing all access to be denied regardless of the allowFrom configuration. Fix both is_allowed() and _validate_allow_from() to use isinstance checks, falling back to dict.get() for dict configs while preserving getattr() for object-style configs. --- nanobot/channels/base.py | 5 ++++- nanobot/channels/manager.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/nanobot/channels/base.py b/nanobot/channels/base.py index dd29c0851..b6b50681c 100644 --- a/nanobot/channels/base.py +++ b/nanobot/channels/base.py @@ -116,7 +116,10 @@ class BaseChannel(ABC): def is_allowed(self, sender_id: str) -> bool: """Check if *sender_id* is permitted. Empty list → deny all; ``"*"`` → allow all.""" - allow_list = getattr(self.config, "allow_from", []) + if isinstance(self.config, dict): + allow_list = self.config.get("allow_from") or self.config.get("allowFrom") or [] + else: + allow_list = getattr(self.config, "allow_from", []) if not allow_list: logger.warning("{}: allow_from is empty — all access denied", self.name) return False diff --git a/nanobot/channels/manager.py b/nanobot/channels/manager.py index aaec5e335..58531c412 100644 --- a/nanobot/channels/manager.py +++ b/nanobot/channels/manager.py @@ -75,7 +75,12 @@ class ChannelManager: def _validate_allow_from(self) -> None: for name, ch in self.channels.items(): - if getattr(ch.config, "allow_from", None) == []: + cfg = ch.config + if isinstance(cfg, dict): + allow = cfg.get("allow_from") or cfg.get("allowFrom") + else: + allow = getattr(cfg, "allow_from", None) + if allow == []: raise SystemExit( f'Error: "{name}" has empty allowFrom (denies all). ' f'Set ["*"] to allow everyone, or add specific user IDs.'