Skip to content

Commit 97c9369

Browse files
committed
Fix zipfile handling in testruns (issue #123)
1 parent ae04323 commit 97c9369

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

oioioi/testrun/controllers.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,20 @@ def adjust_submission_form(self, request, form, problem_instance):
123123
if form.kind != 'TESTRUN':
124124
return
125125

126+
# We need to check using is_zipfile, as sioworkers do the same.
127+
# Otherwise one could bypass checks and limits for example by
128+
# uploading a zipfile without the '.zip' extension.
126129
def validate_file_size(file):
127130
if (
128-
file.name.upper().endswith(".ZIP")
131+
is_zipfile(file)
129132
and file.size > self.get_testrun_zipped_input_limit()
130133
):
131134
raise ValidationError(_("Zipped input file size limit exceeded."))
132135
elif file.size > self.get_testrun_input_limit():
133136
raise ValidationError(_("Input file size limit exceeded."))
134137

135138
def validate_zip(file):
136-
if file.name.upper().endswith(".ZIP"):
139+
if is_zipfile(file):
137140
archive = Archive(file, '.zip')
138141
if len(archive.filenames()) != 1:
139142
raise ValidationError(_("Archive should have only 1 file inside."))
@@ -279,7 +282,7 @@ def render_submission(self, request, submission):
279282
context={
280283
'submission': submission_template_context(request, sbm_testrun),
281284
'supported_extra_args': self.get_supported_extra_args(submission),
282-
'input_is_zip': is_zipfile(sbm_testrun.input_file),
285+
'input_is_zip': is_zipfile(sbm_testrun.input_file.read_using_cache()),
283286
},
284287
)
285288

@@ -295,7 +298,7 @@ def _render_testrun_report(
295298
input_is_zip = False
296299
if testrun_report:
297300
input_is_zip = is_zipfile(
298-
testrun_report.submission_report.submission.programsubmission.testrunprogramsubmission.input_file
301+
testrun_report.submission_report.submission.programsubmission.testrunprogramsubmission.input_file.read_using_cache()
299302
)
300303

301304
return render_to_string(

oioioi/testrun/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from zipfile import is_zipfile
2+
13
from django.shortcuts import get_object_or_404, redirect
24
from django.template.response import TemplateResponse
35
from django.urls import reverse
@@ -127,7 +129,10 @@ def download_input_file_view(request, submission_id):
127129
submission = get_submission_or_error(
128130
request, submission_id, TestRunProgramSubmission
129131
)
130-
return stream_file(submission.input_file, name='input.in')
132+
filename = 'input.in'
133+
if is_zipfile(submission.input_file.read_using_cache()):
134+
filename = 'input.zip'
135+
return stream_file(submission.input_file, name=filename)
131136

132137

133138
@enforce_condition(contest_exists & can_enter_contest)

0 commit comments

Comments
 (0)