fix(github): require webhook sender identity and clarify org trigger docs

- Require a valid dict sender with non-empty login in _author_login() and
  _author_association() so missing or null sender fields fail closed.
- Deep-copy resolved trigger configs in _resolved_trigger() so default
  allow_author_associations lists are never shared across bindings.
- Clarify organization-repo scoping in GITHUB_AGENTS.md and keep
  require_mention exercised in third-party bot dispatcher tests.

Signed-off-by: Eason Meng <eason.meng@bytedance.com>
This commit is contained in:
Eason Meng 2026-11-27 14:53:04 +08:00
parent 3b246c9426
commit 57a5f5f022
4 changed files with 48 additions and 35 deletions

View File

@ -72,24 +72,14 @@ def _comment_body(event: str, payload: dict[str, Any]) -> str:
def _author_login(event: str, payload: dict[str, Any]) -> str | None:
"""Login of the human who triggered the event, for ``allow_authors``."""
# Object authors need not be the actor on edits, labels, or dismissals.
# Only fall back to the authored object when the payload has no sender.
if "sender" in payload and payload["sender"] is not None:
sender = payload["sender"]
if not isinstance(sender, dict):
return None
sender_login = sender.get("login")
if isinstance(sender_login, str) and sender_login.strip():
return sender_login.strip()
"""Login of the webhook ``sender`` who triggered the event."""
del event
sender = payload.get("sender")
if not isinstance(sender, dict):
return None
authored = _authored_object(event, payload)
if authored is None:
return None
user = authored.get("user")
login = user.get("login") if isinstance(user, dict) else None
if isinstance(login, str) and login.strip():
return login.strip()
sender_login = sender.get("login")
if isinstance(sender_login, str) and sender_login.strip():
return sender_login.strip()
return None
@ -111,23 +101,22 @@ def _author_association(event: str, payload: dict[str, Any]) -> str | None:
"""Return the GitHub ``author_association`` attributable to the actor.
GitHub attaches ``author_association`` to the authored object rather than
``sender``. When ``sender`` is present, only accept the object's
association if ``sender.login`` matches the object's ``user.login``
(case-insensitively), so a different actor editing or labeling an owner's
issue/PR/comment never inherits the owner's association.
``sender``. Require a valid ``sender.login`` and only accept the object's
association when ``sender.login`` matches the object's ``user.login``
(case-insensitively), so missing/null senders fail closed and a different
actor editing or labeling an owner's issue/PR/comment never inherits the
owner's association.
"""
authored = _authored_object(event, payload)
if authored is None:
return None
if "sender" in payload and payload["sender"] is not None:
sender = payload["sender"]
sender_login = sender.get("login") if isinstance(sender, dict) else None
authored_user = authored.get("user")
authored_login = authored_user.get("login") if isinstance(authored_user, dict) else None
if not isinstance(sender_login, str) or not sender_login.strip() or not isinstance(authored_login, str) or not authored_login.strip():
return None
if sender_login.strip().lower() != authored_login.strip().lower():
return None
sender_login = _author_login(event, payload)
authored_user = authored.get("user")
authored_login = authored_user.get("login") if isinstance(authored_user, dict) else None
if not sender_login or not isinstance(authored_login, str) or not authored_login.strip():
return None
if sender_login.lower() != authored_login.strip().lower():
return None
raw = authored.get("author_association")
if not isinstance(raw, str):
return None
@ -183,12 +172,12 @@ def _resolved_trigger(
default = DEFAULT_TRIGGERS.get(event)
if default is None:
return override
return override.model_copy(deep=True)
# Field-wise merge: take fields the binding explicitly set,
# backfill the rest from the per-event default.
explicit = override.model_dump(exclude_unset=True)
merged = default.model_copy(update=explicit)
merged = default.model_copy(update=explicit, deep=True)
return merged

View File

@ -147,9 +147,9 @@ Webhook HMAC verification only proves that a delivery came from GitHub; it does
- **`allow_authors`** (default `[]`): GitHub logins (case-insensitive) explicitly authorized to trigger the agent. Use this for specific humans or third-party bots (for example `Copilot` or `coderabbitai[bot]`).
- **`allow_authors_bypass_mention`** (default `true`): Controls whether `allow_authors` matches skip `require_mention`. Set to `false` when using `allow_authors` strictly as an authorization allowlist that must still `@mention` the bot.
- **`allow_author_associations`** (default `["OWNER", "MEMBER", "COLLABORATOR"]`): GitHub `author_association` values authorized when the actor is not listed in `allow_authors`. Association-authorized actors remain subject to `require_mention` when enabled.
- **Sender binding**: GitHub attaches `author_association` to the authored object (`comment`, `issue`, `pull_request`, `review`). When `sender` is present, the dispatcher accepts the object's `author_association` only if `sender.login` matches the object's `user.login` (case-insensitively), preventing another actor on edit or update deliveries from inheriting the original author's association.
- **Sender binding**: Every user-authored delivery must carry a valid `sender.login`. Because GitHub attaches `author_association` to the authored object (`comment`, `issue`, `pull_request`, `review`), the dispatcher accepts that association only when `sender.login` matches the object's `user.login` (case-insensitively), failing closed when `sender` is missing or `null` and preventing another actor on edit or update deliveries from inheriting the original author's association.
- **Fail-closed default**: External associations (`NONE`, `CONTRIBUTOR`, `FIRST_TIME_CONTRIBUTOR`, `FIRST_TIMER`, `MANNEQUIN`) and missing or unattributable associations are rejected with `unauthorized_author` even when the body mentions `@bot`.
- **Stricter scoping in large organizations**: Because GitHub reports `MEMBER` for any member of the owning organization regardless of repository write access, narrow `allow_author_associations` to `["OWNER", "COLLABORATOR"]` or `[]` paired with `allow_authors` (and `allow_authors_bypass_mention: false` if `@mention` should still be required) when org-wide read-only members should not trigger the agent.
- **Stricter scoping in large organizations**: On organization-owned repositories, GitHub reports `MEMBER` (not `OWNER`) for internal org admins and maintainers, while `COLLABORATOR` only covers outside collaborators. When org-wide read-only members should not trigger the agent, set `allow_author_associations: []` paired with an explicit `allow_authors` list (and `allow_authors_bypass_mention: false` if `@mention` should still be required), adding `"COLLABORATOR"` only when outside collaborators should also be permitted.
## GitHub Tool Setup

View File

@ -230,6 +230,7 @@ async def test_self_event_skips_the_owning_agent_only(base_dir: Path) -> None:
"require_mention": True,
"mention_login": "coding-llm-gateway-ai",
"allow_authors": ["llm-gateway-ai[bot]"],
"allow_authors_bypass_mention": False,
}
},
}
@ -283,6 +284,7 @@ async def test_third_party_bot_events_are_not_skipped(base_dir: Path) -> None:
"require_mention": True,
"mention_login": "llm-gateway-ai",
"allow_authors": ["Copilot"],
"allow_authors_bypass_mention": False,
}
},
}

View File

@ -328,6 +328,7 @@ def test_issues_allow_authors_bypasses_mention() -> None:
"action": "opened",
"issue": {"number": 1, "user": {"login": "zhfeng"}, "body": "no mention here"},
"repository": {"full_name": "a/b"},
"sender": {"login": "zhfeng"},
},
trigger,
BOT,
@ -543,7 +544,7 @@ def test_sender_mismatch_does_not_inherit_object_author_association(event: str,
assert "unauthorized_author" in reason
@pytest.mark.parametrize("malformed_sender", [{}, {"login": ""}, {"login": " "}, "not-a-dict"])
@pytest.mark.parametrize("malformed_sender", [None, {}, {"login": ""}, {"login": " "}, "not-a-dict"])
def test_malformed_sender_fails_closed_even_when_object_is_owner(malformed_sender: object) -> None:
trigger = _resolve("issue_comment")
payload = {
@ -558,6 +559,27 @@ def test_malformed_sender_fails_closed_even_when_object_is_owner(malformed_sende
assert "unauthorized_author" in reason
def test_missing_sender_fails_closed_even_when_object_is_owner_or_allowlisted() -> None:
trigger = _resolve("issue_comment", GitHubTriggerConfig(require_mention=True, allow_authors=["repo-owner"]))
payload = {
"action": "created",
"issue": {"number": 1},
"comment": {"body": f"@{BOT} hi", "user": {"login": "repo-owner"}, "author_association": "OWNER"},
"repository": {"full_name": "a/b"},
}
fire, reason = event_should_fire("issue_comment", payload, trigger, BOT)
assert fire is False
assert "unauthorized_author" in reason
def test_resolved_trigger_deep_copies_default_lists() -> None:
first = _resolve("issue_comment")
second = _resolve("issue_comment")
first.allow_author_associations.append("NONE")
assert second.allow_author_associations == ["OWNER", "MEMBER", "COLLABORATOR"]
assert DEFAULT_TRIGGERS["issue_comment"].allow_author_associations == ["OWNER", "MEMBER", "COLLABORATOR"] # type: ignore[union-attr]
def test_empty_allow_author_associations_requires_explicit_allow_authors() -> None:
trigger = _resolve("issue_comment", GitHubTriggerConfig(require_mention=True, allow_authors=["alice"], allow_author_associations=[]))
fire_owner, reason = event_should_fire(