Repository navigation
fix: close JDBC resources in Session Management 2, 3 and 5 challenges (part of #857) - #1004
Open
ismisepaul wants to merge 1 commit into
Open
ismisepaul wants to merge 1 commit into
ismisepaul wants to merge 1 commit into
Conversation
… (part of #857) Each challenge schema has its own connection pool of three connections. SessionManagement3ChangePassword and SessionManagement5ChangePassword never closed their connection, so the fourth password change waited for the pool timeout and failed until Tomcat was restarted. The other servlets closed theirs only when no exception was thrown. Open every Connection, PreparedStatement and ResultSet in try-with-resources. Statements that were reassigned to one variable now each get their own resource (commitStmt, authStmt, addressStmt). Only resource handling changes: the queries, parameters, messages and the catch block each exception reaches are the same as before. SessionManagement2To5PoolLeakIT calls each servlet five times and checks every response came from the database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrowly scoped resource cleanup preserves challenge logic, includes targeted regression tests, and has no identified blocking issues.
No open issues 🎉
What changed in this PR
Addresses part of #857 by preventing connection-pool exhaustion in Session Management challenges 2, 3, and 5 while preserving their intentionally vulnerable behavior.
Changes:
- Converts JDBC connections, statements, and result sets to try-with-resources across seven servlets.
- Preserves existing SQL, parameters, commit order, and challenge responses.
- Adds regression tests that call each servlet five times, exceeding the three-connection pool capacity.
| File | Description |
|---|---|
src/main/java/servlets/module/challenge/SessionManagement5SetToken.java |
Closes token-request lookup resources automatically. |
src/main/java/servlets/module/challenge/SessionManagement5ChangePassword.java |
Returns previously leaked password-change connections. |
src/main/java/servlets/module/challenge/SessionManagement5.java |
Closes login lookup and authentication resources. |
src/main/java/servlets/module/challenge/SessionManagement3ChangePassword.java |
Returns previously leaked password-change connections. |
src/main/java/servlets/module/challenge/SessionManagement3.java |
Closes login lookup and authentication resources. |
src/main/java/servlets/module/challenge/SessionManagement2ChangePassword.java |
Closes password-reset resources on all execution paths. |
src/main/java/servlets/module/challenge/SessionManagement2.java |
Closes authentication and address-lookup resources. |
src/it/java/servlets/module/challenge/SessionManagement2To5PoolLeakIT.java |
Adds repeated-request pool-exhaustion regression tests. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-2-3-5Set 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-2-3-5' mvn -Pdocker clean install -DskipTests docker compose down -v docker compose up -d --buildadmin/passwordand set a new password when asked.Test
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 (SQL injection), and touches none of the same files.
What
SessionManagement3ChangePasswordandSessionManagement5ChangePasswordnever closed their connection. Each challenge has a pool of three (#816), so the 4th password change hung and then failed until Tomcat restarted. The other five servlets closed their connection only when no exception was thrown.Every
Connection,PreparedStatementandResultSetin these servlets is now in try-with-resources:SessionManagement2,SessionManagement2ChangePasswordSessionManagement3,SessionManagement3ChangePasswordSessionManagement5,SessionManagement5ChangePassword,SessionManagement5SetTokenThe levels are exactly as vulnerable as before
Only resource handling changed. The servlets reused one
callstmt/resultSetvariable for several statements, and try-with-resources variables can't be reassigned. So each statement now gets its own resource (commitStmt,authStmt,addressStmt/addressResult). The SQL, the parameters, theCOMMITorder, the messages and the catch each exception reaches are all unchanged. Review withgit diff -w origin/dev.Tests
SessionManagement2To5PoolLeakITcalls each servlet 5 times, more than the pool's 3 connections, and checks that every response came from the database. Ondevit fails 3 of 7 with the pool exhausted ("An Error Occurred! You must be getting funky!" from the 4th request). With this change, all 7 pass.Every step of
test.ymlpassed locally (lint, build, PGP check, compose build, Tomcat config check, unit tests, integration tests).🤖 Generated with Claude Code