Skip to content

Commit 3a9cc7a

Browse files
committed
fix(github): treat collaborator 404 as unauthorized for ignore
GitHub returns 404 from the permission endpoint when the commenter is not a collaborator. That is the drive-by case the ignore gate exists to block, so it must not fail open under the default enforce policy.
1 parent dd02c76 commit 3a9cc7a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

socketsecurity/core/scm/github.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dataclasses import dataclass
55

66
from git import Optional
7+
from socketdev.exceptions import APIFailure
78

89
from socketsecurity import USER_AGENT
910
from socketsecurity.core import log
@@ -273,6 +274,18 @@ def is_ignore_authorized(self, comment: Comment) -> bool:
273274
permission = (
274275
result["permission"].casefold() in self.WRITE_PERMISSIONS
275276
)
277+
except APIFailure as error:
278+
# GitHub returns 404 when the user is not a collaborator (or
279+
# the caller cannot see the repo). Comments were already listed
280+
# with this token, so 404 is the outsider case, not missing
281+
# scope, and must not fail open under enforce.
282+
if error.status_code == 404:
283+
permission = False
284+
else:
285+
log.warning(
286+
f"Could not read GitHub repository permission for {author}: {error}"
287+
)
288+
permission = None
276289
except Exception as error:
277290
log.warning(
278291
f"Could not read GitHub repository permission for {author}: {error}"

tests/unit/test_ignore_authorization.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from types import SimpleNamespace
99

1010
import pytest
11+
from socketdev.exceptions import APIFailure
1112

1213
from socketsecurity.core.classes import Comment
1314
from socketsecurity.core.scm.github import Github
@@ -81,6 +82,17 @@ def test_github_unreadable_permission_honors_the_command_with_a_warning(caplog):
8182
assert "without verifying write access" in caplog.text
8283

8384

85+
def test_github_404_means_the_commenter_is_not_a_collaborator():
86+
"""GitHub returns 404 when the user is not a collaborator. That is the
87+
drive-by case this gate exists to block, not an undetermined lookup."""
88+
github = _github(
89+
raises=APIFailure("Request failed: 404 Client Error", status_code=404)
90+
)
91+
comment = _comment(user={"login": "outsider"}, author_association="NONE")
92+
93+
assert github.is_ignore_authorized(comment) is False
94+
95+
8496
def test_github_strict_rejects_when_permission_cannot_be_read(caplog):
8597
github = _github(raises=Exception("403 Forbidden"), policy="strict")
8698

0 commit comments

Comments
 (0)