unbounded query fix #2400
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!2400
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "refs/pull/2400/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: #1920
Original author: @mfreeman451
Original URL: https://github.com/carverauto/serviceradar/pull/1920
Original created: 2025-11-03T06:51:20Z
Original updated: 2025-11-03T07:50:40Z
Original head: carverauto/serviceradar:1919-bugcore
Original base: main
Original merged: 2025-11-03T07:50:10Z 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 unbounded query vulnerability in
GetUnifiedDeviceby escaping deviceID parameterImprove
joinValueTuplesto handle empty values and prevent malformed SQL tuplesAdd defensive checks for empty string values in tuple construction
Diagram Walkthrough
File Walkthrough
unified_devices.go
Secure SQL queries and improve tuple handlingpkg/db/unified_devices.go
escapeLiteral()todeviceIDparameter inGetUnifiedDevicequeryto prevent SQL injection
joinValueTuplesto filter out empty string values beforebuilding tuples
before joining
Imported GitHub PR comment.
Original author: @qodo-code-review[bot]
Original URL: https://github.com/carverauto/serviceradar/pull/1920#issuecomment-3479140434
Original created: 2025-11-03T06:51:48Z
PR Compliance Guide 🔍
(Compliance updated until commit
github.com/carverauto/serviceradar@360221de44)Below is a summary of compliance checks for this PR:
Potential heavy query
Description: Using ORDER BY _tp_time DESC NULLS LAST LIMIT 1 without an index or proper query plan may
still allow expensive scans on large tables; however, the primary injection risk is
mitigated by parameterization and escaping—this is a possible performance-related concern
requiring verification rather than a security exploit.
unified_devices.go [42-49]
Referred Code
🎫 #1919
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: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation
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: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities
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 new query paths for fetching unified devices and batch querying do not add or
reference any audit logging for these read operations, making it unclear if critical
access to device records is logged.
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 360221d
No security concerns identified
No security vulnerabilities detected by AI analysis. Human verification advised for critical 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: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation
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: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities
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 new query paths for fetching unified devices and batch lookups do not indicate any
audit logging of access to potentially sensitive device data, and the diff provides no
evidence that such access is logged elsewhere.
Referred Code
Compliance check up to commit baaae70
Non-parameterized SQL
Description: Query is built via string formatting with an interpolated literal even though
escapeLiteral is used; lack of parameterized queries means any missed escaping edge case
could enable SQL injection or query breakage.
unified_devices.go [41-49]
Referred Code
Unsafe default tuple
Description: Function joinValueTuples returns "('')" for empty inputs which may be interpreted as a
valid value and could lead to unintended query logic or data leakage if used to build
IN-like clauses.
unified_devices.go [382-400]
Referred Code
🎫 #1919
and not causing timeouts or failures.
during unified device queries and registrations after this change.
regressions under load.
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: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities
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 audit logs: The new query handling and input sanitization do not add or reference any auditing or
logging of critical actions, and it's unclear whether querying sensitive device data
is captured in audit trails elsewhere.
Referred Code
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation
Status:
Edge case return: The function
joinValueTuplesreturns "('')" for empty or all-emptyinputs, which may hide an upstream validation issue or produce ambiguous SQL semantics
without explicit error handling or logging.
Referred Code
Imported GitHub PR comment.
Original author: @qodo-code-review[bot]
Original URL: https://github.com/carverauto/serviceradar/pull/1920#issuecomment-3479142142
Original created: 2025-11-03T06:52:46Z
PR Code Suggestions ✨
Latest suggestions up to
c3388b5Return empty slice, not nil
*In
queryUnifiedDeviceBatch, return an empty slice[]models.UnifiedDevice{}instead of
nilwhen there are no conditions to provide a clearer "no results"signal to callers.
pkg/db/unified_devices.go [304-306]
Suggestion importance[1-10]: 7
__
Why: The suggestion improves the code by adhering to Go's best practice of returning an empty slice instead of a nil slice for "no results", which enhances caller safety and code clarity.
✅
Make ordering deterministically latestSuggestion Impact:
The query was updated to "ORDER BY _tp_time DESC NULLS LAST" as suggested, ensuring deterministic ordering.code diff:
Add
NULLS LASTto theORDER BY _tp_time DESCclause in theGetUnifiedDevicequery to ensure consistent ordering if
_tp_timecontainsNULLvalues.pkg/db/unified_devices.go [41-48]
[Suggestion processed]Suggestion importance[1-10]: 5
__
Why: The suggestion correctly identifies a potential robustness issue in the SQL query's
ORDER BYclause and proposes a standard fix, which improves query determinism if_tp_timecan beNULL.✅
Error on empty filterSuggestion Impact:
The code was changed to return an error (fmt.Errorf("no deviceIDs or ips provided")) when len(conditions) == 0 instead of returning nil, nil.code diff:
Modify the function to return an error instead of
nil, nilwhen no validdeviceIDsoripsare provided, making invalid usage explicit to the caller.pkg/db/unified_devices.go [304-306]
[Suggestion processed]Suggestion importance[1-10]: 6
__
Why: The suggestion correctly identifies that returning
nil, nilfor no provided filters can mask incorrect usage. Returning an error would create a more robust and explicit API contract, which is a good practice.Previous suggestions
✅ Suggestions up to commit
baaae70Use parameterized queries to prevent SQL injection
Instead of manually escaping single quotes to prevent SQL injection, the code
should be refactored to use parameterized queries. This is a more secure and
robust industry-standard practice.
Examples:
pkg/db/unified_devices.go [41-48]
Solution Walkthrough:
Before:
After:
Suggestion importance[1-10]: 9
__
Why: The suggestion correctly identifies that manual string escaping is an inadequate fix for SQL injection and proposes the industry-standard best practice of using parameterized queries, which is a critical security improvement.
✅
Avoid returning incorrect query resultsSuggestion Impact:
The commit removed the fallback returns of "('')" in joinValueTuples and additionally trimmed whitespace from values. Call sites were updated to skip adding clauses when joinValueTuples returns an empty string, aligning with the suggestion's intent.code diff:
Modify
joinValueTuplesto return an empty string instead of('')when the inputlist is empty or contains only empty strings. This prevents queries from
returning incorrect results by matching on empty string values.
pkg/db/unified_devices.go [382-400]
[Suggestion processed]Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a logical bug where returning
('')for an empty list would cause a query to incorrectly search for empty strings instead of returning no results. This is a significant correctness issue.