From c4872ecb07c89b2d8a01d51e8abf987626443951 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 30 Dec 2022 20:43:13 +0200 Subject: [PATCH] Revert "attempt to fix ratelimiting user" This reverts commit 99597fc36ccb032f5c4b1d5bc728b5945db2fb0d. --- files/helpers/config/const.py | 1 - files/routes/awards.py | 2 +- files/routes/chat.py | 2 +- files/routes/comments.py | 16 ++++++------ files/routes/login.py | 2 +- files/routes/mail.py | 2 +- files/routes/notifications.py | 4 +-- files/routes/oauth.py | 12 ++++----- files/routes/posts.py | 14 +++++----- files/routes/reporting.py | 4 +-- files/routes/settings.py | 48 +++++++++++++++++------------------ files/routes/static.py | 2 +- files/routes/subs.py | 16 ++++++------ files/routes/users.py | 20 +++++++-------- files/routes/votes.py | 4 +-- files/routes/wrappers.py | 13 ---------- 16 files changed, 74 insertions(+), 88 deletions(-) diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index f410d1740..700a2c096 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -57,7 +57,6 @@ CLOUDFLARE_COOKIE_VALUE = "yes." # remember to change this in CloudFlare too DEFAULT_RATELIMIT = "3/second;30/minute;200/hour;1000/day" DEFAULT_RATELIMIT_SLOWER = "1/second;30/minute;200/hour;1000/day" -DEFAULT_RATELIMIT_USER = DEFAULT_RATELIMIT_SLOWER PUSH_NOTIF_LIMIT = 1000 diff --git a/files/routes/awards.py b/files/routes/awards.py index 322759884..7cfa8eb94 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -127,7 +127,7 @@ def buy(v:User, award): @app.post("/award//") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def award_thing(v, thing_type, id): kind = request.values.get("kind", "").strip() diff --git a/files/routes/chat.py b/files/routes/chat.py index 9310642cf..dc64ea15b 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -48,7 +48,7 @@ def chat(v): @socketio.on('speak') @limiter.limit("3/second;10/minute") @admin_level_required(PERMS['CHAT']) -@ratelimit_user("3/second;10/minute") +@limiter.limit("3/second;10/minute", key_func=lambda:f'{SITE}-{g.v.id}') def speak(data, v): limiter.check() if v.is_banned: return '', 403 diff --git a/files/routes/comments.py b/files/routes/comments.py index 40bc130ec..c2fb7c1d1 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -78,9 +78,9 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None): return render_template(template, v=v, p=post, sort=sort, comment_info=comment_info, render_replies=True, sub=post.subr) @app.post("/comment") -@limiter.limit("1/second;20/minute;200/hour;1000/day") +@limiter.limit("1/day") @auth_required -@ratelimit_user("1/second;20/minute;200/hour;1000/day") +@limiter.limit("1/day", key_func=lambda:f'{SITE}-{g.v.id}') def comment(v:User): if v.is_suspended: abort(403, "You can't perform this action while banned.") @@ -363,7 +363,7 @@ def comment(v:User): @app.post("/edit_comment/") @limiter.limit("1/second;10/minute;100/hour;200/day") @is_not_permabanned -@ratelimit_user("1/second;10/minute;100/hour;200/day") +@limiter.limit("1/second;10/minute;100/hour;200/day", key_func=lambda:f'{SITE}-{g.v.id}') def edit_comment(cid, v): c = get_comment(cid, v=v) @@ -438,7 +438,7 @@ def edit_comment(cid, v): @app.post("/delete/comment/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def delete_comment(cid, v): if v.id == 253: abort(403) c = get_comment(cid, v=v) @@ -460,7 +460,7 @@ def delete_comment(cid, v): @app.post("/undelete/comment/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def undelete_comment(cid, v): c = get_comment(cid, v=v) if c.deleted_utc: @@ -524,7 +524,7 @@ def unpin_comment(cid, v): @app.post("/save_comment/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def save_comment(cid, v): comment=get_comment(cid) @@ -541,7 +541,7 @@ def save_comment(cid, v): @app.post("/unsave_comment/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def unsave_comment(cid, v): comment=get_comment(cid) @@ -577,7 +577,7 @@ def diff_words(answer, guess): @app.post("/wordle/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def handle_wordle_action(cid, v): comment = get_comment(cid) diff --git a/files/routes/login.py b/files/routes/login.py index f1a94f509..78982eb70 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -136,7 +136,7 @@ def me(v:User): @app.post("/logout") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def logout(v): loggedin = cache.get(f'{SITE}_loggedin') or {} if session.get("lo_user") in loggedin: del loggedin[session["lo_user"]] diff --git a/files/routes/mail.py b/files/routes/mail.py index 311d68101..6047dd7ed 100644 --- a/files/routes/mail.py +++ b/files/routes/mail.py @@ -11,7 +11,7 @@ from files.__main__ import app, limiter @app.post("/verify_email") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def verify_email(v): send_verification_email(v) return {"message": "Email has been sent (ETA ~5 minutes)"} diff --git a/files/routes/notifications.py b/files/routes/notifications.py index 5fe89b723..0756737a4 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -11,7 +11,7 @@ from files.__main__ import app @app.post("/clear") @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT, key_func=lambda:f'{SITE}-{g.v.id}') def clear(v): notifs = g.db.query(Notification).join(Notification.comment).filter(Notification.read == False, Notification.user_id == v.id).all() for n in notifs: @@ -25,7 +25,7 @@ def clear(v): @app.get("/unread") @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT, key_func=lambda:f'{SITE}-{g.v.id}') def unread(v): listing = g.db.query(Notification, Comment).join(Notification.comment).filter( Notification.read == False, diff --git a/files/routes/oauth.py b/files/routes/oauth.py index 4f4c4fa5a..f9074b6e4 100644 --- a/files/routes/oauth.py +++ b/files/routes/oauth.py @@ -18,7 +18,7 @@ def authorize_prompt(v:User): @app.post("/authorize") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def authorize(v): client_id = request.values.get("client_id") application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none() @@ -38,7 +38,7 @@ def authorize(v): @app.post("/rescind/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def rescind(v, aid): auth = g.db.query(ClientAuth).filter_by(oauth_client = aid, user_id = v.id).one_or_none() @@ -50,7 +50,7 @@ def rescind(v, aid): @app.post("/api_keys") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def request_api_keys(v): new_app = OauthApp( app_name=request.values.get('name').replace('<','').replace('>',''), @@ -89,7 +89,7 @@ def request_api_keys(v): @app.post("/delete_app/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def delete_oauth_app(v, aid): try: aid = int(aid) @@ -112,7 +112,7 @@ def delete_oauth_app(v, aid): @app.post("/edit_app/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def edit_oauth_app(v, aid): try: aid = int(aid) @@ -279,7 +279,7 @@ def admin_apps_list(v): @app.post("/reroll/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def reroll_oauth_tokens(aid, v): aid = aid diff --git a/files/routes/posts.py b/files/routes/posts.py index e3dab05a6..23714d5ec 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -36,7 +36,7 @@ titleheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWe @app.post("/publish/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def publish(pid, v): post = get_post(pid) if not post.private: return {"message": "Post published!"} @@ -261,7 +261,7 @@ def more_comments(v, cid): @app.post("/edit_post/") @limiter.limit("1/second;10/minute;100/hour;200/day") @is_not_permabanned -@ratelimit_user("1/second;10/minute;100/hour;200/day") +@limiter.limit("1/second;10/minute;100/hour;200/day", key_func=lambda:f'{SITE}-{g.v.id}') def edit_post(pid, v): p = get_post(pid) if not v.can_edit(p): abort(403) @@ -836,7 +836,7 @@ def submit_post(v:User, sub=None): @app.post("/delete_post/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def delete_post_pid(pid, v): post = get_post(pid) if post.author_id != v.id: abort(403) @@ -863,7 +863,7 @@ def delete_post_pid(pid, v): @app.post("/undelete_post/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def undelete_post_pid(pid, v): post = get_post(pid) if post.author_id != v.id: abort(403) @@ -953,7 +953,7 @@ def unmark_post_nsfw(pid, v): @app.post("/save_post/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def save_post(pid, v): post=get_post(pid) @@ -969,7 +969,7 @@ def save_post(pid, v): @app.post("/unsave_post/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def unsave_post(pid, v): post=get_post(pid) @@ -1041,7 +1041,7 @@ extensions = IMAGE_FORMATS + VIDEO_FORMATS + AUDIO_FORMATS @app.get("/submit/title") @limiter.limit("3/minute") @auth_required -@ratelimit_user("3/minute") +@limiter.limit("3/minute", key_func=lambda:f'{SITE}-{g.v.id}') def get_post_title(v): POST_TITLE_TIMEOUT = 5 url = request.values.get("url") diff --git a/files/routes/reporting.py b/files/routes/reporting.py index 53efcc33d..3a68ed353 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -14,7 +14,7 @@ from files.__main__ import app, limiter, cache @app.post("/report/post/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def flag_post(pid, v): post = get_post(pid) reason = request.values.get("reason", "").strip() @@ -70,7 +70,7 @@ def flag_post(pid, v): @app.post("/report/comment/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def flag_comment(cid, v): comment = get_comment(cid) diff --git a/files/routes/settings.py b/files/routes/settings.py index 00b145d44..a8efd8d3e 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -37,7 +37,7 @@ def settings_personal(v:User): @app.delete('/settings/background') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def remove_background(v): if v.background: if v.background.startswith('/images/'): @@ -49,7 +49,7 @@ def remove_background(v): @app.post('/settings/custom_background') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def upload_custom_background(v): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") @@ -73,7 +73,7 @@ def upload_custom_background(v): @app.post('/settings/profile_background') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def upload_profile_background(v): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") @@ -94,7 +94,7 @@ def upload_profile_background(v): @app.delete('/settings/profile_background') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def delete_profile_background(v): if v.profile_background: os.remove(v.profile_background) @@ -104,7 +104,7 @@ def delete_profile_background(v): @app.post("/settings/personal") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_personal_post(v): if v.id == 253 and request.values.get("private"): abort(403) @@ -384,21 +384,21 @@ def set_color(v:User, attr:str, color:Optional[str]): @app.post("/settings/namecolor") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def namecolor(v): return set_color(v, "namecolor", request.values.get("namecolor")) @app.post("/settings/themecolor") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def themecolor(v): return set_color(v, "themecolor", request.values.get("themecolor")) @app.post("/settings/gumroad") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def gumroad(v): if GUMROAD_TOKEN == DEFAULT_CONFIG_VALUE: abort(404) if not (v.email and v.is_activated): @@ -435,14 +435,14 @@ def gumroad(v): @app.post("/settings/titlecolor") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def titlecolor(v): return set_color(v, "titlecolor", request.values.get("titlecolor")) @app.post("/settings/verifiedcolor") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def verifiedcolor(v): if not v.verified: abort(403, "You don't have a checkmark") return set_color(v, "verifiedcolor", request.values.get("verifiedcolor")) @@ -450,7 +450,7 @@ def verifiedcolor(v): @app.post("/settings/security") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_security_post(v): if request.values.get("new_password"): if request.values.get("new_password") != request.values.get("cnf_password"): @@ -524,7 +524,7 @@ def settings_security_post(v): @app.post("/settings/log_out_all_others") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_log_out_others(v): submitted_password = request.values.get("password", "").strip() if not v.verifyPass(submitted_password): @@ -539,7 +539,7 @@ def settings_log_out_others(v): @app.post("/settings/images/profile") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_images_profile(v): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") @@ -574,7 +574,7 @@ def settings_images_profile(v): @feature_required('USERS_PROFILE_BANNER') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_images_banner(v): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") @@ -600,7 +600,7 @@ def settings_css_get(v:User): @app.post("/settings/css") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_css(v): if v.agendaposter: abort(400, "Agendapostered users can't edit CSS!") css = request.values.get("css", v.css).strip().replace('\\', '').strip()[:CSS_LENGTH_LIMIT] @@ -612,7 +612,7 @@ def settings_css(v): @app.post("/settings/profilecss") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_profilecss(v): profilecss = request.values.get("profilecss", v.profilecss).strip().replace('\\', '').strip()[:CSS_LENGTH_LIMIT] valid, error = validate_css(profilecss) @@ -634,7 +634,7 @@ def settings_security(v:User): @app.post("/settings/block") @limiter.limit("1/second;20/day") @auth_required -@ratelimit_user("1/second;20/day") +@limiter.limit("1/second;20/day", key_func=lambda:f'{SITE}-{g.v.id}') def settings_block_user(v): user = get_user(request.values.get("username"), graceful=True) if not user: abort(404, "This user doesn't exist.") @@ -661,7 +661,7 @@ def settings_block_user(v): @app.post("/settings/unblock") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_unblock_user(v): user = get_user(request.values.get("username")) x = v.has_blocked(user) @@ -685,7 +685,7 @@ def settings_advanced_get(v:User): @app.post("/settings/name_change") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_name_change(v): new_name=request.values.get("name").strip() @@ -724,7 +724,7 @@ def settings_name_change(v): @feature_required('USERS_PROFILE_SONG') @limiter.limit("1/second;10/day") @auth_required -@ratelimit_user("1/second;10/day") +@limiter.limit("1/second;10/day", key_func=lambda:f'{SITE}-{g.v.id}') def settings_song_change_mp3(v): file = request.files['file'] if file.content_type != 'audio/mpeg': @@ -752,7 +752,7 @@ def settings_song_change_mp3(v): @feature_required('USERS_PROFILE_SONG') @limiter.limit("3/second;10/day") @auth_required -@ratelimit_user("3/second;10/day") +@limiter.limit("3/second;10/day", key_func=lambda:f'{SITE}-{g.v.id}') def settings_song_change(v): song=request.values.get("song").strip() @@ -830,7 +830,7 @@ def settings_song_change(v): @app.post("/settings/title_change") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_title_change(v): if v.flairchanged: abort(403) @@ -859,7 +859,7 @@ def settings_title_change(v): @feature_required('PRONOUNS') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_pronouns_change(v): pronouns = sanitize_settings_text(request.values.get("pronouns")) @@ -885,7 +885,7 @@ def settings_pronouns_change(v): @app.post("/settings/checkmark_text") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def settings_checkmark_text(v): if not v.verified: abort(403) new_name = sanitize_settings_text(request.values.get("checkmark-text"), 100) diff --git a/files/routes/static.py b/files/routes/static.py index f19f2b9fc..f6587ca41 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -202,7 +202,7 @@ def contact(v:Optional[User]): @app.post("/send_admin") @limiter.limit("1/second;1/2 minutes;10/day") @auth_required -@ratelimit_user("1/second;1/2 minutes;10/day") +@limiter.limit("1/second;1/2 minutes;10/day", key_func=lambda:f'{SITE}-{g.v.id}') def submit_contact(v): body = request.values.get("message") if not body: abort(400) diff --git a/files/routes/subs.py b/files/routes/subs.py index e946b4723..e69335dd5 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -226,7 +226,7 @@ def sub_followers(v:User, sub): @app.post("/h//add_mod") @limiter.limit("1/second;30/day") @is_not_permabanned -@ratelimit_user("1/second;30/day") +@limiter.limit("1/second;30/day", key_func=lambda:f'{SITE}-{g.v.id}') def add_mod(v:User, sub): if SITE_NAME == 'WPD': abort(403) sub = get_sub_by_name(sub).name @@ -383,7 +383,7 @@ def sub_settings(v:User, sub): @app.post('/h//sidebar') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def post_sub_sidebar(v:User, sub): sub = get_sub_by_name(sub) if not v.mods(sub.name): abort(403) @@ -408,7 +408,7 @@ def post_sub_sidebar(v:User, sub): @app.post('/h//css') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def post_sub_css(v:User, sub): sub = get_sub_by_name(sub) css = request.values.get('css', '').strip() @@ -448,7 +448,7 @@ def get_sub_css(sub): @app.post("/h//settings/banners/") @limiter.limit("1/second;50/day") @is_not_permabanned -@ratelimit_user("1/second;50/day") +@limiter.limit("1/second;50/day", key_func=lambda:f'{SITE}-{g.v.id}') def upload_sub_banner(v:User, sub:str): if g.is_tor: abort(403, "Image uploads are not allowed through Tor") @@ -478,7 +478,7 @@ def upload_sub_banner(v:User, sub:str): @app.delete("/h//settings/banners/") @limiter.limit("1/2 second;30/day") @is_not_permabanned -@ratelimit_user("1/2 second;30/day") +@limiter.limit("1/2 second;30/day", key_func=lambda:f'{SITE}-{g.v.id}') def delete_sub_banner(v:User, sub:str, index:int): sub = get_sub_by_name(sub) if not v.mods(sub.name): abort(403) @@ -509,7 +509,7 @@ def delete_sub_banner(v:User, sub:str, index:int): @app.delete("/h//settings/banners/") @limiter.limit("1/10 second;30/day") @is_not_permabanned -@ratelimit_user("1/10 second;30/day") +@limiter.limit("1/10 second;30/day", key_func=lambda:f'{SITE}-{g.v.id}') def delete_all_sub_banners(v:User, sub:str): sub = get_sub_by_name(sub) if not v.mods(sub.name): abort(403) @@ -535,7 +535,7 @@ def delete_all_sub_banners(v:User, sub:str): @app.post("/h//sidebar_image") @limiter.limit("1/second;10/day") @is_not_permabanned -@ratelimit_user("1/second;10/day") +@limiter.limit("1/second;10/day", key_func=lambda:f'{SITE}-{g.v.id}') def sub_sidebar(v:User, sub): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") @@ -566,7 +566,7 @@ def sub_sidebar(v:User, sub): @app.post("/h//marsey_image") @limiter.limit("1/second;10/day") @is_not_permabanned -@ratelimit_user("1/second;10/day") +@limiter.limit("1/second;10/day", key_func=lambda:f'{SITE}-{g.v.id}') def sub_marsey(v:User, sub): if g.is_tor: abort(403, "Image uploads are not allowed through TOR.") diff --git a/files/routes/users.py b/files/routes/users.py index 465f35d5f..b4ab28ac9 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -286,7 +286,7 @@ def downvoting(v:User, username:str): @feature_required('USERS_SUICIDE') @limiter.limit("1/second;5/day") @auth_required -@ratelimit_user("1/second;5/day") +@limiter.limit("1/second;5/day", key_func=lambda:f'{SITE}-{g.v.id}') def suicide(v:User, username:str): user = get_user(username) suicide = f"Hi there,\n\nA [concerned user](/id/{v.id}) reached out to us about you.\n\nWhen you're in the middle of something painful, it may feel like you don't have a lot of options. But whatever you're going through, you deserve help and there are people who are here for you.\n\nThere are resources available in your area that are free, confidential, and available 24/7:\n\n- Call, Text, or Chat with Canada's [Crisis Services Canada](https://www.crisisservicescanada.ca/en/)\n- Call, Email, or Visit the UK's [Samaritans](https://www.samaritans.org/)\n- Text CHAT to America's [Crisis Text Line](https://www.crisistextline.org/) at 741741.\nIf you don't see a resource in your area above, the moderators keep a comprehensive list of resources and hotlines for people organized by location. Find Someone Now\n\nIf you think you may be depressed or struggling in another way, don't ignore it or brush it aside. Take yourself and your feelings seriously, and reach out to someone.\n\nIt may not feel like it, but you have options. There are people available to listen to you, and ways to move forward.\n\nYour fellow users care about you and there are people who want to help." @@ -344,7 +344,7 @@ def transfer_currency(v:User, username:str, currency_name:Literal['coins', 'mars @app.post("/@/transfer_coins") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def transfer_coins(v:User, username:str): return transfer_currency(v, username, 'coins', True) @@ -352,7 +352,7 @@ def transfer_coins(v:User, username:str): @feature_required('MARSEYBUX') @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @is_not_permabanned -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def transfer_bux(v:User, username:str): return transfer_currency(v, username, 'marseybux', False) @@ -435,7 +435,7 @@ def usersong(username:str): @app.post("/subscribe/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def subscribe(v, post_id): existing = g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).one_or_none() if not existing: @@ -446,7 +446,7 @@ def subscribe(v, post_id): @app.post("/unsubscribe/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def unsubscribe(v, post_id): existing = g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).one_or_none() if existing: @@ -456,7 +456,7 @@ def unsubscribe(v, post_id): @app.post("/@/message") @limiter.limit("1/second;10/minute;20/hour;50/day") @is_not_permabanned -@ratelimit_user("1/second;10/minute;20/hour;50/day") +@limiter.limit("1/second;10/minute;20/hour;50/day", key_func=lambda:f'{SITE}-{g.v.id}') def message2(v:User, username:str): user = get_user(username, v=v, include_blocks=True, include_shadowbanned=False) @@ -517,7 +517,7 @@ def message2(v:User, username:str): @app.post("/reply") @limiter.limit("1/second;6/minute;50/hour;200/day") @auth_required -@ratelimit_user("1/second;6/minute;50/hour;200/day") +@limiter.limit("1/second;6/minute;50/hour;200/day", key_func=lambda:f'{SITE}-{g.v.id}') def messagereply(v:User): body = sanitize_raw_body(request.values.get("body"), False) if not body and not request.files.get("file"): abort(400, "Message is empty!") @@ -1020,7 +1020,7 @@ def u_user_id_info(id, v=None): @app.post("/follow/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def follow_user(username, v): target = get_user(username, v=v, include_shadowbanned=False) @@ -1047,7 +1047,7 @@ def follow_user(username, v): @app.post("/unfollow/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def unfollow_user(username, v): target = get_user(username) @@ -1075,7 +1075,7 @@ def unfollow_user(username, v): @app.post("/remove_follow/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required -@ratelimit_user() +@limiter.limit(DEFAULT_RATELIMIT_SLOWER, key_func=lambda:f'{SITE}-{g.v.id}') def remove_follow(username, v): target = get_user(username) diff --git a/files/routes/votes.py b/files/routes/votes.py index eef09c1c2..b9a33d339 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -183,7 +183,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): @app.post("/vote/post//") @limiter.limit("5/second;60/minute;1000/hour;2000/day") @is_not_permabanned -@ratelimit_user("5/second;60/minute;1000/hour;2000/day") +@limiter.limit("5/second;60/minute;1000/hour;2000/day", key_func=lambda:f'{SITE}-{g.v.id}') @limiter.limit("1/second", key_func=lambda:f'{g.v.id}-{request.full_path}') def vote_post(post_id, new, v): return vote_post_comment(post_id, new, v, Submission, Vote) @@ -191,7 +191,7 @@ def vote_post(post_id, new, v): @app.post("/vote/comment//") @limiter.limit("5/second;60/minute;1000/hour;2000/day") @is_not_permabanned -@ratelimit_user("5/second;60/minute;1000/hour;2000/day") +@limiter.limit("5/second;60/minute;1000/hour;2000/day", key_func=lambda:f'{SITE}-{g.v.id}') @limiter.limit("1/second", key_func=lambda:f'{g.v.id}-{request.full_path}') def vote_comment(comment_id, new, v): return vote_post_comment(comment_id, new, v, Comment, CommentVote) diff --git a/files/routes/wrappers.py b/files/routes/wrappers.py index 46361de4d..37383435f 100644 --- a/files/routes/wrappers.py +++ b/files/routes/wrappers.py @@ -133,16 +133,3 @@ def feature_required(x): wrapper.__name__ = f.__name__ return wrapper return wrapper_maker - -def ratelimit_user(limit:Union[str, Callable[[], str]]=DEFAULT_RATELIMIT_USER): - ''' - Ratelimits based on a user. This requires at least auth_required (or stronger) to be present, - otherwise logged out users will receive 500s - ''' - def inner(func): - @functools.wraps(func) - @limiter.limit(limit, key_func=lambda:f'{SITE}-{g.v.id}') - def wrapped(*args, **kwargs): - return func(*args, **kwargs) - return wrapped - return inner