cleaning up broken sql queries #2399
No reviewers
Labels
No labels
1week
2weeks
Failed compliance check
IP cameras
NATS
Possible security concern
Review effort 1/5
Review effort 2/5
Review effort 3/5
Review effort 4/5
Review effort 5/5
UI
aardvark
accessibility
amd64
api
arm64
auth
back-end
bgp
blog
bug
build
checkers
ci-cd
cleanup
cnpg
codex
core
dependencies
device-management
documentation
duplicate
dusk
ebpf
enhancement
eta 1d
eta 1hr
eta 3d
eta 3hr
feature
fieldsurvey
github_actions
go
good first issue
help wanted
invalid
javascript
k8s
log-collector
mapper
mtr
needs-triage
netflow
network-sweep
observability
oracle
otel
plug-in
proton
python
question
reddit
redhat
research
rperf
rperf-checker
rust
sdk
security
serviceradar-agent
serviceradar-agent-gateway
serviceradar-web
serviceradar-web-ng
siem
snmp
sysmon
topology
ubiquiti
wasm
wontfix
zen-engine
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
carverauto/serviceradar!2399
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "refs/pull/2399/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from GitHub pull request.
Original GitHub pull request: #1918
Original author: @mfreeman451
Original URL: https://github.com/carverauto/serviceradar/pull/1918
Original created: 2025-11-03T05:30:48Z
Original updated: 2025-11-03T05:56:40Z
Original head: carverauto/serviceradar:bug/proton_max_query_size
Original base: main
Original merged: 2025-11-03T05:56:16Z by @mfreeman451
User description
IMPORTANT: Please sign the Developer Certificate of Origin
Thank you for your contribution to ServiceRadar. Please note, when contributing, the developer must include
a DCO sign-off statement indicating the DCO acceptance in one commit message. Here
is an example DCO Signed-off-by line in a commit message:
Describe your changes
Issue ticket number and link
Code checklist before requesting a review
PR Type
Bug fix
Description
Fix SQL query construction to handle large parameter sets
Replace parameterized queries with string interpolation for batch operations
Implement batching with 200-item limit to prevent query size overflow
Add deduplication and sorting utilities for device queries
Diagram Walkthrough
File Walkthrough
unified_devices.go
Implement batching and deduplication for device queriespkg/db/unified_devices.go
unifiedDeviceBatchLimitconstant (200) to prevent query sizeoverflow
GetUnifiedDevicesByIPsOrIDsto batch queries and deduplicateinputs
WITHclauses usingVALUESsyntax
chunkStrings,dedupeStrings,buildWithClause,joinValueTuples,escapeLiteralqueryUnifiedDeviceBatchmethodLastSeentimestamp in descending orderservice_registry_queries.go
Convert service registry queries to string interpolationpkg/registry/service_registry_queries.go
fmt.Sprintf?placeholders with escaped string literals inGetPoller,GetAgent,GetCheckerListPollersto build filter conditions with inline stringformatting
ListAgentsByPollerandListCheckersByAgentto use stringinterpolation
IsKnownPollercache query to use string interpolationescapeLiteralandquoteStringSlicehelper functions for SQLescaping
Querymethods
Imported GitHub PR comment.
Original author: @qodo-code-review[bot]
Original URL: https://github.com/carverauto/serviceradar/pull/1918#issuecomment-3478960513
Original created: 2025-11-03T05:31:24Z
PR Compliance Guide 🔍
(Compliance updated until commit
github.com/carverauto/serviceradar@5ea76c20c8)Below is a summary of compliance checks for this PR:
SQL injection risk
Description: SQL queries are constructed via string interpolation with user-controlled identifiers
(e.g., poller_id) instead of parameterized queries, risking SQL injection despite basic
single-quote escaping that may be insufficient for all SQL dialect edge cases.
service_registry_queries.go [19-29]
Referred Code
SQL injection risk
Description: Agent lookup builds SQL with interpolated agent_id using custom escaping rather than
parameters, exposing potential injection vectors if escaping is bypassed or incomplete.
service_registry_queries.go [75-86]
Referred Code
SQL injection risk
Description: Checker lookup constructs SQL via fmt.Sprintf with checker_id embedded directly, relying
on ad-hoc escaping instead of prepared statements.
service_registry_queries.go [133-143]
Referred Code
SQL injection risk
Description: ListAgentsByPoller interpolates poller_id into the WHERE clause; although escaped, direct
string construction is vulnerable across dialect differences and may allow injection in
complex inputs.
service_registry_queries.go [288-298]
Referred Code
SQL injection risk
Description: ListCheckersByAgent interpolates agent_id into SQL using custom escaping rather than
parameterization, creating an injection risk surface.
service_registry_queries.go [358-368]
Referred Code
SQL injection risk
Description: Batch device query builds WITH and WHERE clauses via string concatenation of values
(device IDs and IPs) with only quote-doubling, which may be bypassed or unsafe in certain
SQL engines and prevents the DB from optimizing via parameters.
unified_devices.go [280-309]
Referred Code
🎫 No ticket provided
Codebase context is not defined
Follow the guide to enable codebase context checks.
Generic: Meaningful Naming and Self-Documenting Code
Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting
Status: Passed
Generic: Secure Error Handling
Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.
Status: Passed
Generic: Secure Logging Practices
Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.
Status: Passed
Generic: Comprehensive Audit Trails
Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.
Status:
Missing Auditing: The newly added query-building and batch retrieval functions perform database reads and
updates without adding any audit logging for critical actions, and the diff does not show
existing logging around these paths.
Referred Code
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation
Status:
Partial Errors: Batch querying and collection paths return underlying errors but do not log or annotate
which IDs/IPs failed, potentially hindering debugging for partial failures and edge cases.
Referred Code
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities
Status:
Unsafe SQL Build: New code replaces parameterized queries with string interpolation and manual escaping
(e.g., WHERE clauses using fmt.Sprintf), which reduces defense-in-depth and may still risk
SQL injection depending on the database engine and context.
Referred Code
Compliance status legend
🟢 - Fully Compliant🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label
Previous compliance checks
Compliance check up to commit c828026
SQL injection
Description: Unparameterized SQL query builds WHERE clause with interpolated poller ID using
fmt.Sprintf, allowing SQL injection via crafted pollerID despite basic quote-escaping.
service_registry_queries.go [19-29]
Referred Code
SQL injection
Description: Unparameterized SQL in GetAgent uses string interpolation of agentID into the query,
risking SQL injection even with manual quote escaping.
service_registry_queries.go [76-86]
Referred Code
SQL injection
Description: GetChecker constructs SQL by interpolating checkerID directly, enabling SQL injection.
service_registry_queries.go [133-143]
Referred Code
SQL injection
Description: ListAgentsByPoller interpolates pollerID into SQL without parameters, enabling SQL
injection.
service_registry_queries.go [288-298]
Referred Code
SQL injection
Description: ListCheckersByAgent interpolates agentID into SQL query, creating SQL injection risk.
service_registry_queries.go [358-368]
Referred Code
SQL injection
Description: IsKnownPoller builds SQL with interpolated pollerID; COUNT query is susceptible to SQL
injection.
service_registry_queries.go [557-564]
Referred Code
SQL injection
Description: Batch query builder composes SQL with VALUES lists from user-controlled IDs/IPs using
manual escaping, allowing SQL injection or syntax manipulation if escaping is
insufficient.
unified_devices.go [284-308]
Referred Code
🎫 No ticket provided
Codebase context is not defined
Follow the guide to enable codebase context checks.
Generic: Meaningful Naming and Self-Documenting Code
Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting
Status: Passed
Generic: Secure Error Handling
Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.
Status: Passed
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities
Status:
SQL injection risk: Parameterized queries were replaced with string interpolation across multiple methods,
introducing SQL injection risk despite basic quote escaping.
Referred Code
Generic: Comprehensive Audit Trails
Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.
Status:
Missing auditing: New database read operations and batch queries are added without any explicit audit
logging of access to potentially sensitive device or registry data.
Referred Code
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation
Status:
Edge cases partial: While batching and deduplication are added, database query errors are wrapped generically
and there is no handling for overly large input sets beyond chunking nor contextual
logging for failures per batch.
Referred Code
Generic: Secure Logging Practices
Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.
Status:
No logging shown: The new code paths add batching and queries but do not show structured or redacted logging
for failures or key actions, which may be acceptable if logging exists elsewhere.
Referred Code
Imported GitHub PR comment.
Original author: @qodo-code-review[bot]
Original URL: https://github.com/carverauto/serviceradar/pull/1918#issuecomment-3478963183
Original created: 2025-11-03T05:32:40Z
PR Code Suggestions ✨
Explore these optional code suggestions:
Revert to using parameterized queries
Revert to using secure parameterized queries instead of manual string formatting
with
fmt.SprintfandescapeLiteralinGetPollerand similar functions to preventSQL injection vulnerabilities.
pkg/registry/service_registry_queries.go [18-32]
[To ensure code accuracy, apply this suggestion manually]Suggestion importance[1-10]: 10
__
Why: The suggestion correctly identifies a critical SQL injection vulnerability introduced by replacing secure parameterized queries with manual string formatting, which is a major security regression.
Use parameterized queries for batch lookups
In
queryUnifiedDeviceBatch, replace manual query construction and escaping withsecure parameterized queries for
INclauses to prevent SQL injection andsimplify the code.
pkg/db/unified_devices.go [280-311]
[To ensure code accuracy, apply this suggestion manually]Suggestion importance[1-10]: 10
__
Why: The suggestion correctly identifies a critical SQL injection vulnerability introduced by using manual string formatting and escaping, and proposes a much safer and simpler implementation using parameterized queries.