Skip to content

[main] Polaris: Automated PR: Fix Improper Resource Shutdown or Release (CWE-404) in SqlInjection.java:12 - #172

Closed
chuckaude wants to merge 1 commit into
mainfrom
BDPR-main-Polaris-BBB05876B291A6691D69FD6852387E7D-1789055738
Closed

chuckaude wants to merge 1 commit into
mainfrom
BDPR-main-Polaris-BBB05876B291A6691D69FD6852387E7D-1789055738

Conversation

@chuckaude

Copy link
Copy Markdown

This pull request was automatically generated by Polaris to remediate a security issue.

The suggested fix is created by AI. Review the proposed changes carefully - AI-generated results should not be relied upon for any specific purpose without verifying their accuracy and completeness.

Issue

  • Severity: HIGH
  • CWE: CWE-404 (Improper Resource Shutdown or Release)
  • File: src/main/java/SqlInjection.java

Click Here To See More Details On Server

Issue summary

CWE-404: Leak of a system resource due to improper release after use.
Can exhaust handles, memory, or connections, degrading performance or causing failure.

Code analysis

  • Purpose: Executes a database lookup for a user’s account_balance using customerName from an HTTP request.
  • Flow: Reads customerName via request.getParameter, concatenates it into a SQL SELECT, creates a Statement, runs executeQuery, and returns the ResultSet.
  • Significance: Demonstrates servlet-to-database data retrieval with JDBC.
  • Capability analysis: Can fetch balances dynamically per request, but only supports raw string-built queries.
  • Actionable insight: This code is SQL injection–vulnerable and should use PreparedStatement, parameter binding, input validation, and proper resource cleanup (try-with-resources).

Fix summary

What changed

The method now executes the query inside a try-with-resources block so the Statement and JDBC ResultSet are closed automatically. It also copies the query results into a CachedRowSet and returns that detached object instead of returning the live JDBC ResultSet.

Why

This prevents a resource leak by ensuring database resources are released even if an exception occurs. Returning a disconnected CachedRowSet preserves the method’s ability to return query data without keeping the underlying JDBC resources open.

Caveats

This only addresses the resource leak; the SQL query is still built via string concatenation and remains vulnerable to SQL injection. Verify callers can work with a CachedRowSet returned as a ResultSet, and ensure the additional rowset dependency is available in the runtime environment.

Confidence

High

Automated review flagged the following items to verify before merging:

  • The method previously returned a live, cursor-backed ResultSet tied to the open Statement; it now returns a disconnected in-memory CachedRowSet. Callers that relied on streaming large result sets or lazy cursor behavior may see different memory or performance characteristics, though this is an inherent trade-off when fixing the resource leak by closing the Statement before returning.

@chuckaude chuckaude closed this Oct 23, 2026
@chuckaude
chuckaude deleted the BDPR-main-Polaris-BBB05876B291A6691D69FD6852387E7D-1789055738 branch October 23, 2026 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant