Files
CTFd/CTFd/schemas/challenges.py
Kevin Chung 7f115bf458 Add length error content that is too long (#1787)
* Add length checking to some sensitive fields in Pages and Challenges.
* Works on #1786

This is enough to fix most of the issues but this is really a systemic problem for most of the API endpoints. We should have something that verifies data consistency. Marshmallow is not good enough at this. Pydantic seems like it would be superior here.
2021-01-28 16:55:15 -05:00

38 lines
1.1 KiB
Python

from marshmallow import ValidationError, pre_load
from CTFd.models import Challenges, ma
class ChallengeSchema(ma.ModelSchema):
class Meta:
model = Challenges
include_fk = True
dump_only = ("id",)
@pre_load
def validate_name(self, data):
name = data.get("name", "")
if len(name) > 80:
raise ValidationError(
"Challenge could not be saved. Challenge name too long",
field_names=["name"],
)
@pre_load
def validate_category(self, data):
category = data.get("category", "")
if len(category) > 80:
raise ValidationError(
"Challenge could not be saved. Challenge category too long",
field_names=["category"],
)
@pre_load
def validate_description(self, data):
description = data.get("description", "")
if len(description) >= 65536:
raise ValidationError(
"Challenge could not be saved. Challenge description is too long.",
field_names=["description"],
)