aboutsummaryrefslogtreecommitdiffstats
path: root/backend/routes/forms/submit.py
blob: 036677eed5bf6902858aca217b06318bce5c58a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""Submit a form."""

import asyncio
import binascii
import datetime
import hashlib
import typing
import uuid
from typing import Any

import httpx
import sentry_sdk
from pydantic import ValidationError
from pydantic.main import BaseModel
from spectree import Response
from starlette.background import BackgroundTasks
from starlette.requests import Request
from starlette.responses import JSONResponse

from backend import constants
from backend.authentication.user import User
from backend.models import Form, FormResponse
from backend.route import Route
from backend.routes.auth.authorize import set_response_token
from backend.routes.forms.discover import AUTH_FORM
from backend.routes.forms.unittesting import BypassDetectedError, execute_unittest
from backend.validation import ErrorMessage, api

if typing.TYPE_CHECKING:
    import pymongo.database

HCAPTCHA_VERIFY_URL = "https://hcaptcha.com/siteverify"
HCAPTCHA_HEADERS = {
    "Content-Type": "application/x-www-form-urlencoded",
}

DISCORD_HEADERS = {
    "Authorization": f"Bot {constants.DISCORD_BOT_TOKEN}",
}


class SubmissionResponse(BaseModel):
    form: Form
    response: FormResponse


class PartialSubmission(BaseModel):
    response: dict[str, Any]
    captcha: str | None


class UnittestError(BaseModel):
    question_id: str
    question_index: int
    return_code: int
    passed: bool
    result: str


class UnittestErrorMessage(ErrorMessage):
    test_results: list[UnittestError]


class SubmitForm(Route):
    """Submit a form with the provided form ID."""

    name = "submit_form"
    path = "/submit/{form_id:str}"

    @api.validate(
        json=PartialSubmission,
        resp=Response(
            HTTP_200=SubmissionResponse,
            HTTP_404=ErrorMessage,
            HTTP_400=ErrorMessage,
            HTTP_422=UnittestErrorMessage,
        ),
        tags=["forms", "responses"],
    )
    async def post(self, request: Request) -> JSONResponse:
        """Submit a response to the form."""
        response = await self.submit(request)

        # Silently try to update user data
        try:
            if hasattr(request.user, User.refresh_data.__name__):
                old = request.user.token
                await request.user.refresh_data()

                if old != request.user.token:
                    try:
                        expiry = datetime.datetime.fromisoformat(
                            request.user.decoded_token.get("expiry"),
                        )
                    except ValueError:
                        expiry = None

                    expiry_seconds = (expiry - datetime.datetime.now()).seconds
                    set_response_token(response, request, request.user.token, expiry_seconds)

        except httpx.HTTPStatusError:
            pass

        return response

    async def submit(self, request: Request) -> JSONResponse:
        """Helper method for handling submission logic."""
        data = await request.json()
        data["timestamp"] = None

        form_id = request.path_params["form_id"]

        if form_id == AUTH_FORM.id:
            response = FormResponse(
                id="not-submitted",
                form_id=AUTH_FORM.id,
                response={question.id: None for question in AUTH_FORM.questions},
                timestamp=datetime.datetime.now().isoformat(),
            ).dict()
            return JSONResponse({"form": AUTH_FORM.dict(admin=False), "response": response})

        if form := await request.state.db.forms.find_one({"_id": form_id, "features": "OPEN"}):
            form = Form(**form)
            response = data.copy()
            response["id"] = str(uuid.uuid4())
            response["form_id"] = form.id

            if constants.FormFeatures.DISABLE_ANTISPAM.value not in form.features:
                ip_hash_ctx = hashlib.md5()
                ip_hash_ctx.update(
                    request.headers.get(
                        "Cf-Connecting-IP",
                        request.client.host,
                    ).encode(),
                )
                ip_hash = binascii.hexlify(ip_hash_ctx.digest())
                user_agent_hash_ctx = hashlib.md5()
                user_agent_hash_ctx.update(request.headers["User-Agent"].encode())
                user_agent_hash = binascii.hexlify(user_agent_hash_ctx.digest())

                async with httpx.AsyncClient() as client:
                    query_params = {
                        "secret": constants.HCAPTCHA_API_SECRET,
                        "response": data.get("captcha"),
                    }
                    r = await client.post(
                        HCAPTCHA_VERIFY_URL,
                        params=query_params,
                        headers=HCAPTCHA_HEADERS,
                    )
                    r.raise_for_status()
                    captcha_data = r.json()

                response["antispam"] = {
                    "ip_hash": ip_hash.decode(),
                    "user_agent_hash": user_agent_hash.decode(),
                    "captcha_pass": captcha_data["success"],
                }

            if constants.FormFeatures.REQUIRES_LOGIN.value in form.features:
                if request.user.is_authenticated:
                    response["user"] = request.user.payload
                    response["user"]["admin"] = request.user.admin

                    if (
                        constants.FormFeatures.COLLECT_EMAIL.value in form.features
                        and "email" not in response["user"]
                    ):
                        return JSONResponse({"error": "email_required"}, status_code=400)
                else:
                    return JSONResponse({"error": "missing_discord_data"}, status_code=400)

            if constants.FormFeatures.UNIQUE_RESPONDER.value in form.features:
                if not request.user.is_authenticated:
                    return JSONResponse({"error": "missing_discord_data"}, status_code=400)

                existing_response = await request.state.db.responses.find_one(
                    {
                        "form_id": form.id,
                        "user.id": request.user.payload["id"],
                    },
                )
                if existing_response:
                    return JSONResponse(
                        {
                            "error": "unique_responder",
                            "message": "You have already submitted this form.",
                        },
                        status_code=400,
                    )

            missing_fields = []
            for question in form.questions:
                if question.id not in response["response"]:
                    if not question.required:
                        response["response"][question.id] = None
                    else:
                        missing_fields.append(question.id)

            if missing_fields:
                return JSONResponse(
                    {
                        "error": "missing_fields",
                        "fields": missing_fields,
                    },
                    status_code=400,
                )

            try:
                response_obj = FormResponse(**response)
            except ValidationError as e:
                return JSONResponse(e.errors(), status_code=422)

            # Run unittests if needed
            if any("unittests" in question.data for question in form.questions):
                unittest_results, errors = await execute_unittest(response_obj, form)

                if len(errors):
                    username = getattr(request.user, "user_id", "Unknown")
                    sentry_sdk.capture_exception(
                        BypassDetectedError(
                            f"Detected unittest bypass attempt on form {form.id} by {username}. "
                            f"Submission has been written to reporting database ({response_obj.id}).",
                        )
                    )
                    database: pymongo.database.Database = request.state.db
                    await database.get_collection("violations").insert_one({
                        "user": username,
                        "bypasses": [error.args[0] for error in errors],
                        "submission": response_obj.dict(by_alias=True),
                        "timestamp": response_obj.timestamp,
                        "id": response_obj.id,
                    })

                failures = []
                status_code = 422

                for test in unittest_results:
                    response_obj.response[test.question_id] = {
                        "value": response_obj.response[test.question_id],
                        "passed": test.passed,
                    }

                    if test.return_code == 0:
                        failure_names = [] if test.passed else test.result.split(";")
                    elif test.return_code == 5:
                        failure_names = ["Could not parse user code."]
                    elif test.return_code == 6:
                        failure_names = ["Could not load user code."]
                    elif test.return_code == 10:
                        failure_names = ["Bypass detected."]
                    else:
                        failure_names = ["Internal error."]

                    response_obj.response[test.question_id]["failures"] = failure_names

                    # Report a failure on internal errors,
                    # or if the test suite doesn't allow failures
                    if not test.passed:
                        question = form.questions[test.question_index]
                        allow_failure = question.data["unittests"]["allow_failure"]

                        # An error while communicating with the test runner
                        if test.return_code == 99:
                            failures.append(test)
                            status_code = 500

                        elif not allow_failure:
                            failures.append(test)

                if len(failures):
                    return JSONResponse(
                        {
                            "error": "failed_tests",
                            "test_results": [test._asdict() for test in failures],
                        },
                        status_code=status_code,
                    )

            await request.state.db.responses.insert_one(
                response_obj.dict(by_alias=True),
            )

            tasks = BackgroundTasks()
            if constants.FormFeatures.WEBHOOK_ENABLED.value in form.features:
                if constants.FormFeatures.REQUIRES_LOGIN.value in form.features:
                    request_user = request.user
                else:
                    request_user = None
                tasks.add_task(
                    self.send_submission_webhook,
                    form=form,
                    response=response_obj,
                    request_user=request_user,
                )

            if constants.FormFeatures.ASSIGN_ROLE.value in form.features:
                tasks.add_task(
                    self.assign_role,
                    form=form,
                    request_user=request.user,
                )

            return JSONResponse(
                {
                    "form": form.dict(admin=False),
                    "response": response_obj.dict(),
                },
                background=tasks,
            )

        return JSONResponse({"error": "Open form not found"}, status_code=404)

    @staticmethod
    async def send_submission_webhook(
        form: Form,
        response: FormResponse,
        request_user: User | None,
    ) -> None:
        """Helper to send a submission message to a discord webhook."""
        # Stop if webhook is not available
        if form.webhook is None:
            msg = "Got empty webhook."
            raise ValueError(msg)

        try:
            mention = request_user.discord_mention
        except AttributeError:
            mention = "A user"

        # Build Embed
        embed = {
            "title": "New Form Response",
            "description": f"{mention} submitted a response to `{form.name}`.",
            "url": f"https://forms-api.pythondiscord.com/forms/{form.id}/responses/{response.id}",
            "timestamp": response.timestamp,
            "color": 7506394,
        }

        # Add author to embed
        if request_user and request_user.is_authenticated:
            user = response.user
            embed["author"] = {"name": request_user.display_name}

            if user and user.avatar:
                url = f"https://cdn.discordapp.com/avatars/{user.id}/{user.avatar}.png"
                embed["author"]["icon_url"] = url

        # Build Hook
        hook = {
            "embeds": [embed],
            "allowed_mentions": {"parse": ["users", "roles"]},
            "username": form.name or "Python Discord Forms",
        }

        # Set hook message
        message = form.webhook.message
        if message:
            # Available variables, see SCHEMA.md
            ctx = {
                "user": mention,
                "response_id": response.id,
                "form": form.name,
                "form_id": form.id,
                "time": response.timestamp,
            }

            for key, val in ctx.items():
                message = message.replace(f"{{{key}}}", str(val))

            hook["content"] = message.replace("_USER_MENTION_", mention)

        params = {}

        if form.webhook.thread_id:
            params["thread_id"] = form.webhook.thread_id

        # Post hook
        async with httpx.AsyncClient() as client:
            r = await client.post(form.webhook.url, json=hook, params=params)
            r.raise_for_status()

    @staticmethod
    async def assign_role(form: Form, request_user: User) -> None:
        """Assigns Discord role to user when user submitted response."""
        if not form.discord_role:
            msg = "Got empty Discord role ID."
            raise ValueError(msg)

        url = (
            f"{constants.DISCORD_API_BASE_URL}/guilds/{constants.DISCORD_GUILD}"
            f"/members/{request_user.payload["id"]}/roles/{form.discord_role}"
        )

        async with httpx.AsyncClient() as client:
            resp = await client.put(url, headers=DISCORD_HEADERS)
            # Handle Rate Limits
            while resp.status_code == 429:
                retry_after = float(resp.headers["X-Ratelimit-Reset-After"])
                await asyncio.sleep(retry_after)
                resp = await client.put(url, headers=DISCORD_HEADERS)

            resp.raise_for_status()