Files
codeql/python/ql/test/experimental/query-tests/Security/CWE-208/TimingAttackAgainstSensitiveInfo.py
2022-06-27 12:18:45 -04:00

37 lines
769 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Desc timing attack against Secret
"""
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/bad')
def check_credentials():
if request.method == 'POST':
password = request.form['pwd']
return password == "token"
@app.route('/good')
def check_credentials(password):
if request.method == 'POST':
password = request.form['pwd']
return constant_time_string_compare(password, "token")
def constant_time_string_compare(a, b):
if len(a) != len(b):
return False
result = 0
for x, y in zip(a, b):
result |= ord(x) ^ ord(y)
return result == 0
if __name__ == '__main__':
app.debug = True
app.run()