Skip to content

Commit

Permalink
Check project.json size before extracting Scratch submission
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip authored and leduythuccs committed Aug 25, 2023
1 parent e809bfc commit 0cadcb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
13 changes: 13 additions & 0 deletions judge/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import zipfile
from operator import attrgetter, itemgetter

import pyotp
Expand Down Expand Up @@ -357,6 +358,18 @@ def check_submission(self):
raise forms.ValidationError(_('File size is too big! Maximum file size is %s')
% filesizeformat(max_file_size))

if lang_obj.key == 'SCRATCH':
try:
archive = zipfile.ZipFile(content.file)
info = archive.getinfo('project.json')
if info.file_size > max_file_size:
raise forms.ValidationError(_('project.json is too big! Maximum file size is %s')
% filesizeformat(max_file_size))

self.files['submission_file'].file = archive.open('project.json')
except (zipfile.BadZipFile, KeyError):
pass

def __init__(self, *args, judge_choices=(), **kwargs):
super(ProblemSubmitForm, self).__init__(*args, **kwargs)
self.fields['language'].empty_label = None
Expand Down
22 changes: 5 additions & 17 deletions judge/views/problem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import os
import re
import zipfile
from datetime import timedelta
from operator import itemgetter
from random import randrange
Expand Down Expand Up @@ -670,22 +669,11 @@ def form_valid(self, form):
self.new_submission.save()

submission_file = form.files.get('submission_file', None)
if submission_file is not None:
if self.new_submission.language.key == 'SCRATCH':
try:
archive = zipfile.ZipFile(submission_file.file)
submission_file.file = archive.open('project.json')
submission_file.name = 'dummy.json'
except (zipfile.BadZipFile, KeyError):
pass

source_url = submission_uploader(
submission_file=submission_file,
problem_code=self.new_submission.problem.code,
user_id=self.new_submission.user.user.id,
)
else:
source_url = ''
source_url = submission_uploader(
submission_file=submission_file,
problem_code=self.new_submission.problem.code,
user_id=self.new_submission.user.user.id,
) if submission_file else ''

source = SubmissionSource(submission=self.new_submission, source=form.cleaned_data['source'] + source_url)
source.save()
Expand Down

0 comments on commit 0cadcb8

Please sign in to comment.