Skip to content

Commit fffb66f

Browse files
committed
feat: add 400 checks
1 parent 446a654 commit fffb66f

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ import topgg
165165
import asyncio
166166
import os
167167

168-
port = 8080
169-
secret = os.getenv('MY_TOPGG_WEBHOOK_SECRET')
170-
171-
webhooks = topgg.Webhooks(secret, port)
168+
webhooks = topgg.Webhooks(os.getenv('MY_TOPGG_WEBHOOK_SECRET'), 8080)
172169

173170
@webhooks.on_vote('/votes')
174171
def voted(vote: topgg.Vote) -> None:

topgg/webhooks.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
"""
2525

2626
from collections.abc import Awaitable, Callable
27+
from aiohttp import web, ContentTypeError
2728
from typing import Any, Optional, Union
29+
from json import JSONDecodeError
2830
from inspect import isawaitable
2931
from urllib import parse
30-
from aiohttp import web
3132

3233

3334
RawCallback = Callable[[web.Request], Awaitable[web.StreamResponse]]
@@ -134,7 +135,12 @@ async def handler(request: web.Request) -> web.Response:
134135
if request.headers.get('Authorization', '') != auth:
135136
return web.Response(status=401, text='Unauthorized')
136137

137-
response = inner_callback(Vote(await request.json()))
138+
try:
139+
vote = Vote(await request.json())
140+
except (JSONDecodeError, ContentTypeError):
141+
return web.Response(status=400, text='Bad request')
142+
143+
response = inner_callback(vote)
138144

139145
if isawaitable(response):
140146
await response

0 commit comments

Comments
 (0)