Upgrade Flask app to Python 3.12: Authlib, Flask 3, SQLAlchemy 2, factory pattern, security hardening#98
Upgrade Flask app to Python 3.12: Authlib, Flask 3, SQLAlchemy 2, factory pattern, security hardening#98
Conversation
…rn, security hardening Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
| response = requests.post(url, json=data) | ||
|
|
||
| logger.debug('Requests Code: %s Response Code: %s', requests.codes["ok"], response.status_code) | ||
| logger.debug('Response: %s', response.text) |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 10 days ago
In general, to fix clear‑text logging of sensitive information you should avoid logging any value that may contain secrets (API keys, tokens, passwords, or responses that could include them). Instead, log only non‑sensitive metadata (e.g., HTTP status code, high‑level result). If detailed body content is ever needed for debugging, it should be gated behind explicit development / debug flags and must be carefully sanitized before logging.
For this specific case, the best minimal fix is to stop logging response.text from the request_sync call, since that response is tainted by the API key used in the request. We already log the response status code on the previous line, which is usually sufficient in production. If keeping some indication of the response is desirable, we can log a short, generic message (e.g., “Response body omitted for security reasons”) instead of the raw text. This preserves existing functionality (the HTTP call and return value are unchanged) while eliminating the sensitive sink.
Concretely:
- In
action_devices.py, insiderequest_sync, replacelogger.debug('Response: %s', response.text)with a non‑sensitive debug line that does not include the body, e.g.logger.debug('Response body omitted from logs for security.')or simply remove that line. - No changes are needed in
app.pyorroutes.pybeyond how they callrequest_sync, because the leak occurs when the response body is logged, not when the key is passed into the function.
No new imports or helper methods are required.
| @@ -313,7 +313,8 @@ | ||
| response = requests.post(url, json=data) | ||
|
|
||
| logger.debug('Requests Code: %s Response Code: %s', requests.codes["ok"], response.status_code) | ||
| logger.debug('Response: %s', response.text) | ||
| # Do not log response.text directly to avoid leaking sensitive information. | ||
| logger.debug('Response body omitted from logs for security.') | ||
|
|
||
| return response.status_code == requests.codes['ok'] | ||
| except Exception as e: |
Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
Modernises the entire application stack from Python 3.8/Flask 1.x to Python 3.12/Flask 3.x, replacing all deprecated libraries and patterns that were blocking the runtime upgrade.
Dependency Overhaul (
requirements.txt,runtime.txt)runtime.txt:python-3.8.9→python-3.12.2OAuth (
my_oauth.py,routes.py)Full rewrite from
OAuth2Providerdecorator pattern to Authlib'sAuthorizationServer+ResourceProtector:Existing DB schema (Client, Grant, Token tables) is preserved; Authlib interface methods are added to models instead.
Application Factory (
app.py)app = Flask(...)replaced withcreate_app(config_class=None)factory@app.before_first_requestremoved;db.create_all()runs inwith app.app_context()during factory init_init_full_features()returnsbool; fallback routes auto-register on failure (keeps test/dev parity)print()replaced withloggingSecurity Hardening (
auth.py)Removed explicit
method='sha256'fromgenerate_password_hash()— Werkzeug 3.x defaults to scrypt.Python 3.12 Deprecation Fixes
datetime.utcnow()→datetime.now(timezone.utc)everywhereReportState.py: removed.decode("utf-8")onjwt.encode()return value — google-auth ≥ 2.x returnsstrmodels.py: removedusernamecolumn fromUser;emailis now the sole identifierIntent Routing (
action_devices.py)Replaced nested
if/elifchain with a dispatch map:Validation
make test)make health)AI-Assisted Review (if applicable)
Risk & Rollback
user.username) so no migration rollback is neededOriginal prompt
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.