Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show error when flag format is invalid #11

Merged
merged 2 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ctfpad/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,21 @@ class Meta:
"tags",
]

is_update = True

def cleaned_tags(self):
data = [x.lower() for x in self.cleaned_data['tags'].split()]
return data

def clean_flag(self):
flag = self.cleaned_data.get("flag")
prefix = self.instance.ctf.flag_prefix

if flag and prefix and not flag.startswith(prefix):
self.add_error("flag", f"Unexpected format for flag (missing '{prefix}')")

return flag


class ChallengeSetFlagForm(forms.ModelForm):
class ChallengeSetFlagForm(ChallengeUpdateForm):
class Meta:
model = Challenge
fields = [
Expand Down
6 changes: 3 additions & 3 deletions ctfpad/templates/ctfpad/challenges/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h5 class="card-title">
<textarea id="{{form.description.id_for_label}}" name="{{form.description.html_name }}" placeholder="{{form.description.label}}" value="{{form.description.value}}" class="form-control">{% if form.description %}{{form.description.value}}{%endif%}</textarea>
</div>

{% if form.is_update %}
{% if form.instance.creation_time %}
hugsy marked this conversation as resolved.
Show resolved Hide resolved
<label class="label"><strong>Flag</strong></label>
<div class="input-group mb-3">
<div class="input-group-append">
Expand Down Expand Up @@ -108,7 +108,7 @@ <h5 class="card-title">
{% endif %}
{% endif %}

{% if form.is_update %}
{% if form.instance.creation_time %}
<label class="label"><strong>HedgeDoc ID </strong></label>
<div class="input-group mb-3">
<div class="input-group-append">
Expand Down Expand Up @@ -138,7 +138,7 @@ <h5 class="card-title">

<div class="card-footer text-muted">
<div class="control card-footer-item">
{% if form.is_update %}
{% if form.instance.creation_time %}
<button type="button" class="btn-primary btn-sm btn-block" onclick="this.form.submit();">Update Challenge</button>
{% else %}
<button type="button" class="btn-primary btn-sm btn-block" onclick="this.form.submit();">Create Challenge</button>
Expand Down
2 changes: 1 addition & 1 deletion ctfpad/templates/ctfpad/challenges/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<div class="row">
<div class="col-md-3" id="challenge_info_left_menu">

{{ form.errors }}
<div class="card card-body">
<a class="btn btn-warning btn-sm btn-block" href="{% url 'ctfpad:challenges-edit' challenge.id %}"><strong>Edit Challenge</strong></a>
<ul class="list-group">
Expand Down
4 changes: 1 addition & 3 deletions ctfpad/views/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_success_url(self):

class ChallengeSetFlagView(ChallengeUpdateView):
form_class = ChallengeSetFlagForm
template_name = "ctfpad/challenges/detail.html"

def get_success_url(self):
return reverse("ctfpad:challenges-detail", kwargs={'pk': self.object.pk})
Expand All @@ -97,9 +98,6 @@ def form_valid(self, form):
messages.error(self.request, f"Cannot score when CTF is over")
return redirect("ctfpad:challenges-detail", self.object.id)

if not form.instance.flag.startswith( form.instance.ctf.flag_prefix ):
messages.warning(self.request, f"Unexpected format for flag (missing '{form.instance.ctf.flag_prefix}')")

return super().form_valid(form)


Expand Down