SQLAlchemy host pagination leaves recently updated targets on older pages
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
Research direction
The bug is in the SQLAlchemy backend's last_host_logs() and search_logs() queries, which group by HostsLog.target but order by HostsLog.id. Read both queries and reproduce with an in-memory SQLite database holding 12 targets plus one newer event for host-0. Done means both functions return host-0 first on page 1 and that pagination still applies after grouping and ordering, with the existing search filters intact. Confirm with a maintainer that the newest-event ordering is intended before starting.
Written by the indexing model from the issue text.
Description
Bug Description
The SQLAlchemy backend's last_host_logs() and search_logs() functions can return targets in an incorrect order when a previously scanned target receives a newer event.
The queries group records by HostsLog.target but order by the non-aggregated HostsLog.id. As a result, a target with the newest scan event can remain on an older page.
Steps to Reproduce
- Use Nettacker commit
02275b6bwith Python 3.12 and SQLAlchemy 2.0.48. - Create an in-memory SQLite database using the existing
HostsLogmodel. - Insert 12 distinct targets (
host-0throughhost-11). - Insert an additional, newer event for
host-0. - Query pages 1 and 2 using
last_host_logs()andsearch_logs().
Expected Behavior
Targets should be ordered by their most recent matching event.
Page 1 should start with host-0, followed by host-11, host-10, and so on.
Actual Behavior
Both functions return host-0 on page 2, despite it having the newest event.
The observed results were:
Page 1: host-11, host-10, ..., host-2
Page 2: host-1, host-0
Root Cause
The SQLAlchemy queries use the following pattern:
.group_by(HostsLog.target)
.order_by(HostsLog.id.desc())
Ordering grouped records by a non-aggregated ID does not guarantee ordering by the newest record within each target group.
A possible fix is to order grouped targets using:
func.max(HostsLog.id).desc()
The query should apply pagination after grouping and ordering, while preserving existing search filters and response structures.
Environment
- OS: Windows 11
- Python: 3.12.14
- Nettacker: 0.4.2
- Commit:
02275b6b - SQLAlchemy: 2.0.48
- SQLite: 3.53.1
- Backend: SQLAlchemy SQLite (
use_apsw_for_sqlite=False)
Verification
The behavior was reproduced using synthetic records in an isolated in-memory SQLite database.
- Both affected SQLAlchemy functions failed the expected-order tests.
- Equivalent control queries returned the expected ordering.
- 67 existing focused tests passed.
- No external network targets were scanned.
The default APSW backend was not demonstrated to have this issue.
Related Work
I checked existing pagination and database discussions, including:
- #1294 – Pagination-related changes
- #1076 – APSW migration
- #1145 – Database writer queue
I did not find an existing report covering this specific grouped-ordering behavior within the issues and PR discussions reviewed.
Contribution
Could a maintainer confirm whether ordering targets by their newest matching event is the intended behavior?
If this is a valid issue and is available, I would be happy to work on a focused fix and add regression tests after approval and assignment.
Thank you!
- Dominant language
- Python
- Stars
- 5.7k
- Forks
- 1.2k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 12
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from OWASP/Nettacker
-
SslLibrary crashes with unhandled OSError: port/proto not found on non-standard SSL portsPossibly taken @himanchalkaushale claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
OWASP/Nettacker#1884 · 3 comments ·
Maintainers usually reply within 1 day
-
Hindi (hi.yaml) is missing 29 translation keys, can I add them?Possibly taken @shivanshuagrwl claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
OWASP/Nettacker#1876 · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
-
Use time.perf_counter() for HTTP response-time measurementsPossibly taken @29rdx claimed this 3 days ago. Openlow priority
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
OWASP/Nettacker#1859 · 2 comments ·
Maintainers usually reply within 1 day
-
Fix remove_old_logs() failing to delete stale scan events when scan_compare_id is NonePossibly taken @himanchalkaushale claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
OWASP/Nettacker#1856 · 5 comments ·
Maintainers usually reply within 1 day
-
admin_scan reports the homepage as an admin pagePossibly taken @Alfa-Dev404 claimed this 7 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
OWASP/Nettacker#1850 · 1 comment · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
splunk/security_content#4418 ·
Maintainers usually reply within 1 day
-
[Bug / Enhancement]: Improve teleop_robot CLI with configurable timeout, robust input validation, and clean ROS shutdownPossibly taken @muskan-creates352 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
-
bug(pipelines): shortcode stripping deletes text between a {{< >}} shortcode and the next {{% %}}Possibly taken @Ashborne-lab claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
kubeflow/docs-agent#293 ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Imbad0202/academic-research-skills#1093 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pierreaubert/gpui-toolkit#114 ·
Maintainers usually reply within 1 day