-
Notifications
You must be signed in to change notification settings - Fork 52.7k
fix(whatsapp): read WHATSAPP_GROUP_ALLOW_FROM env var as group allowlist fallback #56769
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
Open
liuhao1024
wants to merge
3
commits into
NousResearch:main
Choose a base branch
from
liuhao1024:liuhao/cron-bugfix-56767-whatsapp-group-env-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
β1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f8dcf22
fix(whatsapp): read WHATSAPP_GROUP_ALLOW_FROM env var as group allowlβ¦
liuhao1024 896034f
test(whatsapp): exercise real adapter constructor for env-only group β¦
liuhao1024 d5c5907
Merge branch 'main' into liuhao/cron-bugfix-56767-whatsapp-group-env-β¦
liuhao1024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Verify that the WhatsApp group allowlist reads env vars as fallback. | ||
|
|
||
| Regression test for #56767: after the built-in β plugin migration, | ||
| ``WHATSAPP_GROUP_ALLOW_FROM`` / ``WHATSAPP_GROUP_ALLOWED_USERS`` were no | ||
| longer read, causing group messages to be silently dropped when the user | ||
| configured the allowlist via ``.env`` instead of ``config.yaml``. | ||
| """ | ||
|
|
||
| from gateway.config import Platform, PlatformConfig | ||
|
|
||
| from plugins.platforms.whatsapp.adapter import WhatsAppAdapter | ||
|
|
||
|
|
||
| # --- Env-only fallback (the core regression) --- | ||
|
|
||
|
|
||
| def test_group_allowlist_reads_whatsapp_group_allow_from_env(monkeypatch): | ||
| """Env-only setup: ``WHATSAPP_GROUP_ALLOW_FROM`` must populate the | ||
| group allowlist when ``config.yaml`` has no ``group_allow_from``.""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOW_FROM", "12036300111@g.us, 12036300222@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={})) | ||
| assert adapter._group_allow_from == {"12036300111@g.us", "12036300222@g.us"} | ||
|
|
||
|
|
||
| def test_group_allowlist_reads_whatsapp_group_allowed_users_env(monkeypatch): | ||
| """Env-only setup: ``WHATSAPP_GROUP_ALLOWED_USERS`` (setup-wizard name) | ||
| must also populate the group allowlist.""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "12036300333@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={})) | ||
| assert adapter._group_allow_from == {"12036300333@g.us"} | ||
|
|
||
|
|
||
| # --- Config takes precedence over env --- | ||
|
|
||
|
|
||
| def test_group_allowlist_config_extra_wins_over_env(monkeypatch): | ||
| """Explicit ``group_allow_from`` in config must not be widened by a | ||
| stale env var.""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOW_FROM", "12036300999@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig( | ||
| enabled=True, extra={"group_allow_from": ["12036300444@g.us"]}, | ||
| )) | ||
| assert adapter._group_allow_from == {"12036300444@g.us"} | ||
|
|
||
|
|
||
| def test_group_allowlist_camelcase_config_wins_over_env(monkeypatch): | ||
| """CamelCase ``groupAllowFrom`` in config extra also takes precedence.""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "12036300999@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig( | ||
| enabled=True, extra={"groupAllowFrom": ["12036300555@g.us"]}, | ||
| )) | ||
| assert adapter._group_allow_from == {"12036300555@g.us"} | ||
|
|
||
|
|
||
| # --- Env var priority: GROUP_ALLOW_FROM checked first --- | ||
|
|
||
|
|
||
| def test_group_allow_from_checked_before_group_allowed_users(monkeypatch): | ||
| """When both env vars are set, ``WHATSAPP_GROUP_ALLOW_FROM`` wins | ||
| (checked first in the ``or`` chain).""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOW_FROM", "12036300666@g.us") | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOWED_USERS", "12036300777@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={})) | ||
| assert adapter._group_allow_from == {"12036300666@g.us"} | ||
|
|
||
|
|
||
| # --- Empty / missing env var --- | ||
|
|
||
|
|
||
| def test_group_allowlist_empty_when_no_config_no_env(monkeypatch): | ||
| """No config and no env β empty allowlist (default).""" | ||
| adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={})) | ||
| assert adapter._group_allow_from == set() | ||
|
|
||
|
|
||
| # --- End-to-end: env-only allowlist gates group messages --- | ||
|
|
||
|
|
||
| def test_env_only_allowlist_accepts_listed_group(monkeypatch): | ||
| """Env-only ``group_policy=allowlist`` + ``group_allow_from`` lets a | ||
| listed group through via ``_is_group_allowed``.""" | ||
| monkeypatch.setenv("WHATSAPP_GROUP_POLICY", "allowlist") | ||
| monkeypatch.setenv("WHATSAPP_GROUP_ALLOW_FROM", "120363001234567890@g.us") | ||
| adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={})) | ||
| assert adapter._group_allow_from == {"120363001234567890@g.us"} | ||
| assert adapter._is_group_allowed("120363001234567890@g.us") is True | ||
| assert adapter._is_group_allowed("999999999999@g.us") is False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This truthiness chain makes an explicit
group_allow_from: []or""fall through to the environment and can widen an allowlist from a stale env value. Select an env fallback only when neither config key is present, and add regressions for explicit empty snake_case and camelCase config values.