Repository navigation
fix: close JDBC resources in Session Management 6 and 7 challenges (part of #857) - #1005
ismisepaul wants to merge 1 commit into
Conversation
…art of #857) Each challenge schema has its own connection pool of three connections. These servlets closed their connection only when no exception was thrown. The Session Management 6 secret-question lookup is injectable, so three malformed email addresses took the level offline until Tomcat was restarted. Open every Connection, PreparedStatement and ResultSet in try-with-resources. Statements that were reassigned to one variable now each get their own resource (commitStmt, addressStmt). Only resource handling changes: the queries, the password filter, the messages (including the SQL error the secret-question lookup shows) and the catch block each exception reaches are the same as before. SessionManagement6And7PoolLeakIT calls each servlet five times, five malformed lookups included, and checks every well-formed request still reaches the database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The scoped resource-lifecycle changes preserve challenge behavior, include regression coverage, and have no identified blocking issues.
No open issues 🎉
What changed in this PR
Closes JDBC resources in the Session Management 6 and 7 challenges as part of #857’s connection-leak cleanup, while preserving the intentionally vulnerable training behavior.
Changes:
- Converts connections, statements, and result sets in four servlets to try-with-resources.
- Preserves existing queries, validation, and player-facing SQL error messages.
- Adds integration tests for repeated malformed lookups and repeated login and secret-answer requests.
| File | Description |
|---|---|
src/main/java/servlets/module/challenge/SessionManagement7SecretQuestion.java |
Automatically closes secret-answer query resources. |
src/main/java/servlets/module/challenge/SessionManagement7.java |
Automatically closes login and address-lookup resources. |
src/main/java/servlets/module/challenge/SessionManagement6SecretQuestion.java |
Closes lookup and answer-query resources, including on SQL errors. |
src/main/java/servlets/module/challenge/SessionManagement6.java |
Automatically closes login and address-lookup resources. |
src/it/java/servlets/module/challenge/SessionManagement6And7PoolLeakIT.java |
Adds five repeated-request tests for challenge-pool availability. |
Review effort: Balanced
| new String( | ||
| "<h2 class='title'>" | ||
| + bundle.getString("question.badAnswer") | ||
| + "</h2><p>" | ||
| + bundle.getString("question.whoAreYou")); |
There was a problem hiding this comment.
This line is unchanged on dev. This PR only re-indents it inside the new try-with-resources block, and in these training servlets the PR changes resource handling only. Tracked for a separate clean-up in #999.
| new String( | ||
| "<h2 class='title'>" | ||
| + bundle.getString("question.badAnswer") | ||
| + "</h2><p>" | ||
| + bundle.getString("question.whoAreYou") | ||
| + "</p>"); |
There was a problem hiding this comment.
This line is unchanged on dev. This PR only re-indents it inside the new try-with-resources block, and in these training servlets the PR changes resource handling only. Tracked for a separate clean-up in #999.
| // rs.getString(1) contains the question for the user to answer. This question | ||
| // is asked in English as it must be answered in English to successfully pass | ||
| // the level | ||
| htmlOutput = new String(Encode.forHtml(rs.getString(1))); |
There was a problem hiding this comment.
This line is unchanged on dev. This PR only re-indents it inside the new try-with-resources block, and in these training servlets the PR changes resource handling only. Tracked for a separate clean-up in #999.
The automated tests prove these levels no longer exhaust their connection pools. They don't prove each level can still be solved the intended way, and these are deliberately vulnerable training levels.
Please run this branch, follow the steps, and comment on this PR with the result: pass, or the step that failed and what you saw.
Branch:
dev#857-session-management-6-7Set up. Use a machine where the Shepherd Docker volumes can be thrown away:
down -vdeletes their data. JDK 17 and Maven 3.9.2+ are needed.git fetch origin git checkout 'dev#857-session-management-6-7' mvn -Pdocker clean install -DskipTests docker compose down -v docker compose up -d --buildadmin/passwordand set a new password when asked.Test
'five times in a row. Each should show the SQL error, as before. Then look up a valid address: it should show its question straight away.It's broken if: a level's cheat-sheet solution no longer returns the result key; or a level answers An Error Occurred! You must be getting funky!, hangs for about 5 seconds, or mentions Connection is not available after the repeated inputs, and keeps doing so until Tomcat restarts.
Part of #857. Follows #998, and touches none of the same files.
What
SessionManagement6SecretQuestion's GET is the injectable lookup. It leaked its connection on a malformed email, so 3 bad lookups took the level offline until Tomcat restarted.SessionManagement6,SessionManagement7andSessionManagement7SecretQuestionclosed their connection only when no exception was thrown.Every
Connection,PreparedStatementandResultSetis now in try-with-resources.The levels are exactly as vulnerable as before
Only resource handling changed. The conversion is the same as in the session management 2/3/5 PR, with one variable per statement (
commitStmt,addressStmt/addressResult). The secret-question lookup still shows the player the raw SQL exception, which is part of the level. Review withgit diff -w origin/dev.Tests
SessionManagement6And7PoolLeakITsends 5 malformed secret-question lookups, checking for "SQL syntax", then a valid one. It also makes 5 calls each to the SM6 and SM7 login and secret-answer servlets. Ondevit fails 3 of 5, for example:With this change, all 5 pass.
Every step of
test.ymlpassed locally (lint, build, PGP check, compose build, Tomcat config check, unit tests, integration tests).🤖 Generated with Claude Code