get_results_json() and get_results_csv() crash with AttributeError on default SQLite/APSW database
Description
Under Nettacker's default configuration (Config.db.engine = "sqlite" and Config.settings.use_apsw_for_sqlite = True in nettacker/config.py), create_connection() in nettacker/database/db.py (lines 52–67) returns an (apsw.Connection, apsw.Cursor) tuple instead of a SQLAlchemy Session.
While all database helper functions in nettacker/database/db.py check if isinstance(session, tuple):, both /results/get_json (get_results_json, lines 494–514) and /results/get_csv (get_results_csv, lines 517–547) in nettacker/api/engine.py call create_connection() directly and immediately invoke .query(Report) on the returned tuple:
session = create_connection()
result_id = get_value(flask_request, "id")
if not result_id:
return jsonify(structure(status="error", msg=_("invalid_scan_id"))), 400
scan_details = session.query(Report).filter(Report.id == result_id).first()
This causes every request to /results/get_json?id=<id> or /results/get_csv?id=<id> with the default SQLite/APSW backend to fail with an unhandled AttributeError: 'tuple' object has no attribute 'query' (HTTP 500).
Additionally, even when use_apsw_for_sqlite = False (or on PostgreSQL/MySQL), if result_id does not match an existing Report row, scan_details is None and accessing scan_details.scan_unique_id raises AttributeError: 'NoneType' object has no attribute 'scan_unique_id' instead of returning HTTP 404.
Proof / Reproduction
from unittest.mock import patch
from nettacker.api.engine import app
app.config["OWASP_NETTACKER_CONFIG"] = {"api_access_key": "testkey"}
client = app.test_client()
with patch("nettacker.api.engine.create_connection", return_value=(object(), object())):
resp = client.get("/results/get_json?id=1&key=testkey")
Traceback:
File "nettacker/api/engine.py", line 507, in get_results_json
scan_details = session.query(Report).filter(Report.id == result_id).first()
AttributeError: 'tuple' object has no attribute 'query'
Proposed Fix
Add a database helper in nettacker/database/db.py (handling both the (connection, cursor) APSW tuple and SQLAlchemy Session, and closing the APSW cursor/connection in finally) to look up Report metadata (scan_unique_id, report_path_filename) by id, return HTTP 404 when no report is found, and add unit tests in tests/unit/api/test_engine.py.
Hi @securestep9 @arkid15r, could you please assign this issue to me?
OS: Linux
OS Version: Ubuntu 22.04
Python Version: 3.11
Hi maintainers, I'd like to work on these issues. I have PR #1874 ready with fixes. Could you please assign these issues to me so I can proceed? My commits are now GPG-signed and ready for review.
Thanks!
@arkid15r @securestep9 Hi, I'd like to work on this issue. I have a fix ready with PR #1874 open. Could you please assign this issue to me so I can proceed? My commits are now GPG-signed and ready for review.
Thanks!