SQLAlchemy ORM
# SECURE: Using SQLAlchemy ORM
@app.route('/user/<int:user_id>')
def get_user_secure(user_id):
user = User.query.filter_by(id=user_id).first()
if user:
return jsonify({
'id': user.id,
'username': user.username
})
return jsonify({'error': 'User not found'}), 404
NoSQL Injection Prevention
# SECURE: NoSQL injection prevention
def authenticate_user_secure(username, password):
if not isinstance(username, str):
return None
username = re.escape(username)
user = db.users.find_one({
'username': username,
'password': hash_password(password)
})
return user