Fixes #261 and generally supports Unicode better in Python 2 (#263)

* Fixing #261 and improving Unicode in Python2
* Fixing PEP8 issues
This commit is contained in:
Kevin Chung
2017-05-21 12:43:31 -04:00
committed by GitHub
parent 28f669be05
commit 59afacce69
7 changed files with 80 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
language: python language: python
python: python:
- 2.7 - 2.7
- 3.5 - 3.6
install: install:
- pip install -r development.txt - pip install -r development.txt
script: script:

View File

@@ -1,3 +1,4 @@
import sys
import os import os
from distutils.version import StrictVersion from distutils.version import StrictVersion
@@ -11,6 +12,11 @@ from six.moves import input
from CTFd.utils import cache, migrate, migrate_upgrade, migrate_stamp from CTFd.utils import cache, migrate, migrate_upgrade, migrate_stamp
from CTFd import utils from CTFd import utils
# Hack to support Unicode in Python 2 properly
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding("utf-8")
__version__ = '1.0.2' __version__ = '1.0.2'

View File

@@ -258,21 +258,21 @@ def chal(chalid):
db.session.close() db.session.close()
logger.warn("[{0}] {1} submitted {2} with kpm {3} [TOO FAST]".format(*data)) logger.warn("[{0}] {1} submitted {2} with kpm {3} [TOO FAST]".format(*data))
# return '3' # Submitting too fast # return '3' # Submitting too fast
return jsonify({'status': '3', 'message': "You're submitting keys too fast. Slow down."}) return jsonify({'status': 3, 'message': "You're submitting keys too fast. Slow down."})
solves = Solves.query.filter_by(teamid=session['id'], chalid=chalid).first() solves = Solves.query.filter_by(teamid=session['id'], chalid=chalid).first()
# Challange not solved yet # Challange not solved yet
if not solves: if not solves:
chal = Challenges.query.filter_by(id=chalid).first_or_404() chal = Challenges.query.filter_by(id=chalid).first_or_404()
provided_key = unicode(request.form['key'].strip()) provided_key = request.form['key'].strip()
saved_keys = Keys.query.filter_by(chal=chal.id).all() saved_keys = Keys.query.filter_by(chal=chal.id).all()
# Hit max attempts # Hit max attempts
max_tries = chal.max_attempts max_tries = chal.max_attempts
if max_tries and fails >= max_tries > 0: if max_tries and fails >= max_tries > 0:
return jsonify({ return jsonify({
'status': '0', 'status': 0,
'message': "You have 0 tries remaining" 'message': "You have 0 tries remaining"
}) })
@@ -284,7 +284,7 @@ def chal(chalid):
db.session.commit() db.session.commit()
db.session.close() db.session.close()
logger.info("[{0}] {1} submitted {2} with kpm {3} [CORRECT]".format(*data)) logger.info("[{0}] {1} submitted {2} with kpm {3} [CORRECT]".format(*data))
return jsonify({'status': '1', 'message': 'Correct'}) return jsonify({'status': 1, 'message': 'Correct'})
if utils.ctftime(): if utils.ctftime():
wrong = WrongKeys(teamid=session['id'], chalid=chalid, flag=provided_key) wrong = WrongKeys(teamid=session['id'], chalid=chalid, flag=provided_key)
@@ -298,17 +298,17 @@ def chal(chalid):
tries_str = 'tries' tries_str = 'tries'
if attempts_left == 1: if attempts_left == 1:
tries_str = 'try' tries_str = 'try'
return jsonify({'status': '0', 'message': 'Incorrect. You have {} {} remaining.'.format(attempts_left, tries_str)}) return jsonify({'status': 0, 'message': 'Incorrect. You have {} {} remaining.'.format(attempts_left, tries_str)})
else: else:
return jsonify({'status': '0', 'message': 'Incorrect'}) return jsonify({'status': 0, 'message': 'Incorrect'})
# Challenge already solved # Challenge already solved
else: else:
logger.info("{0} submitted {1} with kpm {2} [ALREADY SOLVED]".format(*data)) logger.info("{0} submitted {1} with kpm {2} [ALREADY SOLVED]".format(*data))
# return '2' # challenge was already solved # return '2' # challenge was already solved
return jsonify({'status': '2', 'message': 'You already solved this'}) return jsonify({'status': 2, 'message': 'You already solved this'})
else: else:
return jsonify({ return jsonify({
'status': '-1', 'status': -1,
'message': "You must be logged in to solve a challenge" 'message': "You must be logged in to solve a challenge"
}) })

View File

@@ -3,4 +3,4 @@ coverage>=4.1
mock>=2.0.0 mock>=2.0.0
nose>=1.3.7 nose>=1.3.7
rednose>=1.1.1 rednose>=1.1.1
pep8==1.7.0 pep8>=1.7.0

View File

@@ -231,7 +231,7 @@ if __name__ == '__main__':
for x in range(AMT_CHALS_WITH_FILES): for x in range(AMT_CHALS_WITH_FILES):
chal = random.randint(1, CHAL_AMOUNT) chal = random.randint(1, CHAL_AMOUNT)
filename = gen_file() filename = gen_file()
md5hash = hashlib.md5(filename).hexdigest() md5hash = hashlib.md5(filename.encode('utf-8')).hexdigest()
db.session.add(Files(chal, md5hash + '/' + filename)) db.session.add(Files(chal, md5hash + '/' + filename))
db.session.commit() db.session.commit()

View File

@@ -86,7 +86,7 @@ def gen_file():
pass pass
def gen_key(db, chal, flag='flag', key_type=0): def gen_flag(db, chal, flag='flag', key_type=0):
key = Keys(chal, flag, key_type) key = Keys(chal, flag, key_type)
db.session.add(key) db.session.add(key)
db.session.commit() db.session.commit()

View File

@@ -1,4 +1,7 @@
from tests.helpers import create_ctfd, register_user, login_as_user, gen_challenge #!/usr/bin/env python
# -*- coding: utf-8 -*-
from tests.helpers import *
from CTFd.models import Teams from CTFd.models import Teams
import json import json
@@ -205,3 +208,61 @@ def test_viewing_challenges():
r = client.get('/chals') r = client.get('/chals')
chals = json.loads(r.get_data(as_text=True)) chals = json.loads(r.get_data(as_text=True))
assert len(chals['game']) == 1 assert len(chals['game']) == 1
def test_submitting_correct_flag():
"""Test that correct flags are correct"""
app = create_ctfd()
with app.app_context():
register_user(app)
client = login_as_user(app)
chal = gen_challenge(app.db)
flag = gen_flag(app.db, chal=chal.id, flag='flag')
with client.session_transaction() as sess:
data = {
"key": 'flag',
"nonce": sess.get('nonce')
}
r = client.post('/chal/{}'.format(chal.id), data=data)
assert r.status_code == 200
resp = json.loads(r.data.decode('utf8'))
assert resp.get('status') == 1 and resp.get('message') == "Correct"
def test_submitting_incorrect_flag():
"""Test that incorrect flags are incorrect"""
app = create_ctfd()
with app.app_context():
register_user(app)
client = login_as_user(app)
chal = gen_challenge(app.db)
flag = gen_flag(app.db, chal=chal.id, flag='flag')
with client.session_transaction() as sess:
data = {
"key": 'notflag',
"nonce": sess.get('nonce')
}
r = client.post('/chal/{}'.format(chal.id), data=data)
assert r.status_code == 200
resp = json.loads(r.data.decode('utf8'))
assert resp.get('status') == 0 and resp.get('message') == "Incorrect"
def test_submitting_unicode_flag():
"""Test that users can submit a unicode flag"""
print("Test that users can submit a flag")
app = create_ctfd()
with app.app_context():
register_user(app)
client = login_as_user(app)
chal = gen_challenge(app.db)
flag = gen_flag(app.db, chal=chal.id, flag=u'你好')
with client.session_transaction() as sess:
data = {
"key": '你好',
"nonce": sess.get('nonce')
}
r = client.post('/chal/{}'.format(chal.id), data=data)
assert r.status_code == 200
resp = json.loads(r.data.decode('utf8'))
assert resp.get('status') == 1 and resp.get('message') == "Correct"