Skip to content
New issue

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

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

Already on GitHub? # 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 1 commit
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
19 changes: 10 additions & 9 deletions ctfpad/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,28 @@ 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 = [
"flag",
"last_update_by",
]

def clean_flag(self):
flag = self.cleaned_data.get("flag")
prefix = self.instance.ctf.flag_prefix
if not flag.startswith(prefix):
self.add_error("flag", f"Unexpected format for flag (missing '{prefix}')")
return flag

class ChallengeFileCreateForm(forms.ModelForm):
class Meta:
Expand Down
8 changes: 4 additions & 4 deletions ctfpad/templates/ctfpad/challenges/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="card" style="width: 35rem;">
<div class="card-header">
<h5 class="card-title">
{% if form.instance.creation_time %}
{% if form.name.value %}
<p class="card-header-title">Updating Challenge {{form.name.value}}</p>
{% else %}
<p class="card-header-title">New Challenge</p>
Expand Down 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
4 changes: 0 additions & 4 deletions ctfpad/templates/ctfpad/challenges/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@

<div class="row">
<div class="col-md-3" id="challenge_info_left_menu">
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}

{{ 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>
Expand Down