Skip to content

Commit c9afc75

Browse files
authored
feat: added incoming requests verification (#143)
1 parent a84236c commit c9afc75

File tree

5 files changed

+465
-248
lines changed

5 files changed

+465
-248
lines changed

app/api/endpoints/botx.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
BotXMethodCallbackNotFoundError,
1010
UnknownBotAccountError,
1111
UnsupportedBotAPIVersionError,
12+
UnverifiedRequestError,
1213
build_bot_disabled_response,
1314
build_command_accepted_response,
15+
build_unverified_request_response,
1416
)
1517
from pybotx.constants import BOT_API_VERSION
1618

@@ -25,8 +27,11 @@
2527
async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
2628
"""Receive commands from users. Max timeout - 5 seconds."""
2729

28-
try:
29-
bot.async_execute_raw_bot_command(await request.json())
30+
try: # noqa: WPS225
31+
bot.async_execute_raw_bot_command(
32+
await request.json(),
33+
request_headers=request.headers,
34+
)
3035
except ValueError:
3136
error_label = "Bot command validation error"
3237

@@ -58,6 +63,14 @@ async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONRe
5863
build_bot_disabled_response(error_label),
5964
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
6065
)
66+
except UnverifiedRequestError as exc:
67+
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
68+
return JSONResponse(
69+
content=build_unverified_request_response(
70+
status_message=exc.args[0],
71+
),
72+
status_code=HTTPStatus.UNAUTHORIZED,
73+
)
6174

6275
return JSONResponse(
6376
build_command_accepted_response(), status_code=HTTPStatus.ACCEPTED
@@ -69,7 +82,10 @@ async def status_handler(request: Request, bot: Bot = bot_dependency) -> JSONRes
6982
"""Show bot status and commands list."""
7083

7184
try:
72-
status = await bot.raw_get_status(dict(request.query_params))
85+
status = await bot.raw_get_status(
86+
dict(request.query_params),
87+
request_headers=request.headers,
88+
)
7389
except UnknownBotAccountError as exc:
7490
error_label = f"Unknown bot_id: {exc.bot_id}"
7591
logger.warning(exc)
@@ -83,6 +99,14 @@ async def status_handler(request: Request, bot: Bot = bot_dependency) -> JSONRes
8399
return JSONResponse(
84100
build_bot_disabled_response(error_label), status_code=HTTPStatus.BAD_REQUEST
85101
)
102+
except UnverifiedRequestError as exc:
103+
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
104+
return JSONResponse(
105+
content=build_unverified_request_response(
106+
status_message=exc.args[0],
107+
),
108+
status_code=HTTPStatus.UNAUTHORIZED,
109+
)
86110

87111
return JSONResponse(status)
88112

@@ -92,7 +116,10 @@ async def callback_handler(request: Request, bot: Bot = bot_dependency) -> JSONR
92116
"""Process BotX methods callbacks."""
93117

94118
try:
95-
await bot.set_raw_botx_method_result(await request.json())
119+
await bot.set_raw_botx_method_result(
120+
await request.json(),
121+
verify_request=False,
122+
)
96123
except BotXMethodCallbackNotFoundError as exc:
97124
error_label = f"Unexpected callback with sync_id: {exc.sync_id}"
98125
logger.warning(error_label)

0 commit comments

Comments
 (0)