You've already forked Mailu
mirror of
https://github.com/Mailu/Mailu.git
synced 2025-11-25 22:12:28 +02:00
Use hmac.compare_digest to prevent timing attacks.
This commit is contained in:
committed by
Alexander Graf
parent
5c9cdfe1de
commit
7a36f6bbb9
@@ -2,6 +2,7 @@ from .. import models, utils
|
||||
from . import v1
|
||||
from flask import request
|
||||
import flask
|
||||
import hmac
|
||||
from functools import wraps
|
||||
from flask_restx import abort
|
||||
|
||||
@@ -19,10 +20,13 @@ def api_token_authorization(func):
|
||||
client_ip = flask.request.headers.get('X-Real-IP', flask.request.remote_addr)
|
||||
if utils.limiter.should_rate_limit_ip(client_ip):
|
||||
abort(429, 'Too many attempts from your IP (rate-limit)' )
|
||||
if request.args.get('api_token') != v1.api_token:
|
||||
if (request.args.get('api_token') == '' or
|
||||
request.args.get('api_token') == None):
|
||||
abort(401, 'A valid API token is expected as query string parameter')
|
||||
if not hmac.compare_digest(request.args.get('api_token'), v1.api_token):
|
||||
utils.limiter.rate_limit_ip(client_ip)
|
||||
flask.current_app.logger.warn(f'Invalid API token provided by {client_ip}.')
|
||||
abort(401, 'A valid API token is expected as query string parameter')
|
||||
abort(403, 'A valid API token is expected as query string parameter')
|
||||
else:
|
||||
flask.current_app.logger.info(f'Valid API token provided by {client_ip}.')
|
||||
return func(*args, **kwds)
|
||||
|
||||
Reference in New Issue
Block a user