Skip to content

Commit

Permalink
Fixes CTFd#261 and generally supports Unicode better in Python 2 (CTF…
Browse files Browse the repository at this point in the history
…d#263)

* Fixing CTFd#261 and improving Unicode in Python2
* Fixing PEP8 issues
  • Loading branch information
ColdHeat authored May 21, 2017
1 parent 28f669b commit 59afacc
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: python
python:
- 2.7
- 3.5
- 3.6
install:
- pip install -r development.txt
script:
Expand Down
6 changes: 6 additions & 0 deletions CTFd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import os

from distutils.version import StrictVersion
Expand All @@ -11,6 +12,11 @@
from CTFd.utils import cache, migrate, migrate_upgrade, migrate_stamp
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'


Expand Down
16 changes: 8 additions & 8 deletions CTFd/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,21 @@ def chal(chalid):
db.session.close()
logger.warn("[{0}] {1} submitted {2} with kpm {3} [TOO FAST]".format(*data))
# 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()

# Challange not solved yet
if not solves:
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()

# Hit max attempts
max_tries = chal.max_attempts
if max_tries and fails >= max_tries > 0:
return jsonify({
'status': '0',
'status': 0,
'message': "You have 0 tries remaining"
})

Expand All @@ -284,7 +284,7 @@ def chal(chalid):
db.session.commit()
db.session.close()
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():
wrong = WrongKeys(teamid=session['id'], chalid=chalid, flag=provided_key)
Expand All @@ -298,17 +298,17 @@ def chal(chalid):
tries_str = 'tries'
if attempts_left == 1:
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:
return jsonify({'status': '0', 'message': 'Incorrect'})
return jsonify({'status': 0, 'message': 'Incorrect'})

# Challenge already solved
else:
logger.info("{0} submitted {1} with kpm {2} [ALREADY SOLVED]".format(*data))
# 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:
return jsonify({
'status': '-1',
'status': -1,
'message': "You must be logged in to solve a challenge"
})
2 changes: 1 addition & 1 deletion development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ coverage>=4.1
mock>=2.0.0
nose>=1.3.7
rednose>=1.1.1
pep8==1.7.0
pep8>=1.7.0
2 changes: 1 addition & 1 deletion populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def random_date(start, end):
for x in range(AMT_CHALS_WITH_FILES):
chal = random.randint(1, CHAL_AMOUNT)
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.commit()
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def gen_file():
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)
db.session.add(key)
db.session.commit()
Expand Down
63 changes: 62 additions & 1 deletion tests/test_user_facing.py
Original file line number Diff line number Diff line change
@@ -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
import json

Expand Down Expand Up @@ -205,3 +208,61 @@ def test_viewing_challenges():
r = client.get('/chals')
chals = json.loads(r.get_data(as_text=True))
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"

0 comments on commit 59afacc

Please sign in to comment.