下面我通过一个充值业务来剖析高并发漏洞
1、用户提交充值码
2、数据库查询该码,如果查到库里有这个码并且码未被消费过,则走到第3步的逻辑里,否则返回code not useful
3、更新码的状态,更新消费该码的用户,
4、更新用户的余额,比如兑换的充值码为20元,用户money字段增加20
@user_blue.route('/exchange',methods=['POST'])
@user_auth
def exchange():
data = request.json
code = data.get('code') #1.用户提交充值码
try:
flag = Flag.query.filter_by(code=code).first() #数据库查询该码
if not flag or flag.isused is True: #表示无该充值码或该码已被消费过
return {"code":40001,"msg":"code not useful"}
else:
flag.used_id = session.get('uid') #设置消费该码的用户
flag.isused = True #将该码的状态更新为已使用
db.session.add(flag)
db.session.commit()
return jsonify({"msg":"exchange success"})
except Exception as e:
db.session.rollback()
raise e