Performance: Why security is critical
Security impacts user trust and application availability, indirectly affecting user experience and perceived performance.
from flask import Flask, request from werkzeug.security import check_password_hash app = Flask(__name__) users = {'admin': 'pbkdf2:sha256:150000$abc$hashedpassword'} @app.route('/login', methods=['POST']) def login(): username = request.form.get('username') password = request.form.get('password') if username in users and check_password_hash(users[username], password): return 'Logged in' return 'Failed login'
from flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['POST']) def login(): username = request.form['username'] password = request.form['password'] # No input validation or protection if username == 'admin' and password == 'password123': return 'Logged in' return 'Failed login'
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No security validation | Minimal | 0 | 0 | [X] Bad |
| Proper input validation and hashing | Minimal | 0 | 0 | [OK] Good |
abort() function immediately stops processing and returns an HTTP error code, useful for unauthorized access.abort() fits the need to stop a request with an error code.from flask import abort, session, Flask
app = Flask(__name__)
@app.route('/dashboard')
def dashboard():
if not session.get('user'):
abort(401)
return 'Welcome to your dashboard'abort(401), which sends a 401 Unauthorized error.@app.route('/profile')
def profile():
user_id = request.args.get('user_id')
user = get_user_from_db(user_id)
return f'Profile page of {user.name}'if not session.get('user'): with abort(401) prevents unauthorized access.@app.route('/settings')
def settings():
# What should be added here?
return 'User settings page'abort(401) to stop access and send an error.Pass the test and this pattern moves unknown