Skip to content

Commit 53f2454

Browse files
fix: handle 404 errors from untrusted input
1 parent 5beca70 commit 53f2454

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

monty/exts/info/github/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ async def get_reply(
325325

326326
for resource_data, (match, size) in zip(fut, resources.items(), strict=True):
327327
if isinstance(resource_data, BaseException):
328+
if (
329+
isinstance(resource_data, githubkit.exception.RequestFailed)
330+
and resource_data.response.status_code == 404
331+
):
332+
log.info("GitHub resource %r not found (404).", match)
333+
continue
328334
log.warning(
329335
"GitHub resource fetch for %r resulted in an exception: %r",
330336
match,
@@ -478,7 +484,13 @@ async def github_user(self, ctx: commands.Context, user: str) -> None:
478484
)
479485
return
480486
context = ghretos.User(login=user)
481-
obj = await self.fetch_resource(context)
487+
try:
488+
obj = await self.fetch_resource(context)
489+
except githubkit.exception.RequestFailed as e:
490+
if e.response.status_code == 404:
491+
msg = "GitHub user not found."
492+
raise commands.UserInputError(msg) from e
493+
raise
482494
components: list[disnake.ui.Container | disnake.ui.ActionRow] = []
483495
components.append(github_handlers.UserRenderer().render_ogp_cv2(obj, context=context))
484496
components.append(
@@ -505,7 +517,13 @@ async def github_repo(self, ctx: commands.Context, user_and_repo: str, repo: str
505517
raise commands.UserInputError(msg)
506518

507519
context = ghretos.Repo(owner=user, name=repo)
508-
obj = await self.fetch_resource(context)
520+
try:
521+
obj = await self.fetch_resource(context)
522+
except githubkit.exception.RequestFailed as e:
523+
if e.response.status_code == 404:
524+
msg = "GitHub repository not found."
525+
raise commands.UserInputError(msg) from e
526+
raise
509527
components: list[disnake.ui.Container | disnake.ui.ActionRow] = []
510528
components.append(github_handlers.RepoRenderer().render_ogp_cv2(obj, context=context))
511529
components.append(

0 commit comments

Comments
 (0)