Skip to content

Commit 7b63c84

Browse files
committed
Add rate-limiting
I've updated the button with some rate-limiting. This involved adding a couple columns (REAL last_click, INTEGER spamming) to the db table. On click, delta from last_click is checked; if less than minimum time, spamming flag is set and team can't submit for 10s (variable). If spamming is set, but it's been longer than spam_ban_time, spamming is unset, and click is allowed. Whether click succeeds or not, last_click is updated.
1 parent ba4499a commit 7b63c84

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

schema.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ CREATE TABLE teams(
66
id INTEGER PRIMARY KEY NOT NULL,
77
name TEXT(20) NOT NULL UNIQUE,
88
score REAL(10, 2),
9-
hash TEXT(100) NOT NULL
9+
hash TEXT(100) NOT NULL,
10+
last_click REAL,
11+
spamming INTEGER
1012
);
1113

1214
DROP TABLE IF EXISTS flags;
@@ -22,6 +24,6 @@ INSERT into flags (value) VALUES ('congrats flag3');
2224
INSERT into flags (value) VALUES ('congrats flag4');
2325
INSERT into flags (value) VALUES ('congrats flag5');
2426

25-
INSERT into teams (name, score, hash) VALUES ('aaa', 0, '$2a$04$v6PKN3tpOyaiKV/3VZOjh.RRoDUDvITVLZuSwhzyRVbK82ANFMQOi');
26-
INSERT into teams (name, score, hash) VALUES ('bbb', 0, '$2a$12$ydNsdi763MvDytMGdiBNE.rqWeoJxx9pRYHKyIRZ3l/E.x6pLOLmi');
27-
INSERT into teams (name, score, hash) VALUES ('ccc', 0, '$2a$12$uW7DWD3n497ZlVA1gJiuhOftfIudF/nINoiQKwm2/3rnvjuCg6Ldy');
27+
INSERT into teams (name, score, hash, last_click, spamming) VALUES ('aaa', 0, '$2a$04$v6PKN3tpOyaiKV/3VZOjh.RRoDUDvITVLZuSwhzyRVbK82ANFMQOi', 10000, 0);
28+
INSERT into teams (name, score, hash, last_click, spamming) VALUES ('bbb', 0, '$2a$12$ydNsdi763MvDytMGdiBNE.rqWeoJxx9pRYHKyIRZ3l/E.x6pLOLmi', 10000, 0);
29+
INSERT into teams (name, score, hash, last_click, spamming) VALUES ('ccc', 0, '$2a$12$uW7DWD3n497ZlVA1gJiuhOftfIudF/nINoiQKwm2/3rnvjuCg6Ldy', 10000, 0);

src/button.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
db = sqlite3.connect(app_dir + '/../database.db')
1919
#TODO Change this to 1800 for production
2020
time_in_round = 1800
21+
min_time_between_clicks = 1
22+
spam_ban_time = 10
2123
flag_index = 0
2224

2325
class BaseHandler(tornado.web.RequestHandler):
@@ -48,6 +50,31 @@ def post(self):
4850
if(str(md5.new(team + captcha + captcha_id).hexdigest()) != token):
4951
raise tornado.web.HTTPError(403)
5052

53+
# is the team being spammy?
54+
now = time.time()
55+
cursor = db.cursor()
56+
packaged = (team, ) #no idea why you have to do this
57+
cursor.execute("SELECT * from teams WHERE name=? LIMIT 1", packaged)
58+
row = cursor.fetchone()
59+
if not row:
60+
raise tornado.web.HTTPError(403)
61+
last_click = row[4]
62+
spamming = row[5]
63+
64+
since_last_click = now - last_click
65+
66+
if (spamming != 0):
67+
if (since_last_click > spam_ban_time):
68+
self.set_spamming(team, 0)
69+
else:
70+
raise tornado.web.HTTPError(403)
71+
else:
72+
if (since_last_click < min_time_between_clicks):
73+
self.set_spamming(team, 1)
74+
raise tornado.web.HTTPError(403)
75+
76+
self.set_click_time(team, now)
77+
5178
#check the captcha
5279
captcha_id_int = int(captcha_id)
5380
captcha_answer = captchas[captcha_id_int]
@@ -72,6 +99,18 @@ def get(self):
7299
teamname = self.get_current_user()
73100
self.render(app_dir + "/public/button.html", scoreboard=scoreboard, teamname=teamname)
74101

102+
def set_spamming(self, team, spamming):
103+
cursor = db.cursor()
104+
packaged = (spamming, team) #no idea why you have to do this
105+
cursor.execute("UPDATE teams SET spamming=? WHERE name=?", packaged)
106+
db.commit()
107+
108+
def set_click_time(self, team, now):
109+
cursor = db.cursor()
110+
packaged = (now, team) #no idea why you have to do this
111+
cursor.execute("UPDATE teams SET last_click=? WHERE name=?", packaged)
112+
db.commit()
113+
75114
class ScoreSocketHandler(tornado.websocket.WebSocketHandler):
76115
buttoneers = set()
77116

0 commit comments

Comments
 (0)