Skip to content

fix: close JDBC resources in Session Management 6 and 7 challenges (part of #857) - #1005

Open
ismisepaul wants to merge 1 commit into
devfrom
dev#857-session-management-6-7
Open

ismisepaul wants to merge 1 commit into
devfrom
dev#857-session-management-6-7

Conversation

@ismisepaul

@ismisepaul ismisepaul commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

⚠️ Manual testing required before merging

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-7

Set up. Use a machine where the Shepherd Docker volumes can be thrown away: down -v deletes 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 --build
  1. Open https://localhost, accept the self-signed certificate warning, log in as admin / password and set a new password when asked.
  2. On the Get Started page, tick Enable Unsafe Levels and click Open All Levels.
  3. Admin → Module Management → Change Module Layout → Open Floor, then reload the page, so every level is listed on the left.
  4. Admin → Configuration → Cheat Sheet Management → Enable Cheat Sheets for Administrators. With a level open, the Cheat button in the left-hand menu then shows its solution.

Test

  1. Solve Session Management Challenge 6 and 7 with the solutions from their Cheat buttons and check you get the result keys.
  2. In Session Management Challenge 6, look up the secret question with a broken email such as ' 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.
  3. In both levels, try a wrong login five times, then a wrong secret answer five times. Every attempt should get the level's normal reply.

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, SessionManagement7 and SessionManagement7SecretQuestion closed their connection only when no exception was thrown.

Every Connection, PreparedStatement and ResultSet is 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 with git diff -w origin/dev.

Tests

SessionManagement6And7PoolLeakIT sends 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. On dev it fails 3 of 5, for example:

Malformed email 4 did not fail (expected "SQL syntax"): java.sql.SQLTransientConnectionException: ChallengePool-… - Connection is not available, request timed out after 5…

With this change, all 5 pass.

Every step of test.yml passed locally (lint, build, PGP check, compose build, Tomcat config check, unit tests, integration tests).

🤖 Generated with Claude Code

…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>
@ismisepaul ismisepaul added this to the 4.0.0 milestone Oct 25, 2026
Copilot AI balanced review requested due to automatic review settings October 25, 2026 00:59
@ismisepaul ismisepaul moved this from Backlog to Next Release Backlog in Security Shepherd Oct 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Comment on lines +129 to +133
new String(
"<h2 class='title'>"
+ bundle.getString("question.badAnswer")
+ "</h2><p>"
+ bundle.getString("question.whoAreYou"));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +142 to +147
new String(
"<h2 class='title'>"
+ bundle.getString("question.badAnswer")
+ "</h2><p>"
+ bundle.getString("question.whoAreYou")
+ "</p>");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ismisepaul ismisepaul added the Needs Manual Testing Must be tested by hand from the PR's branch (steps in the PR) before merging label Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Levels Needs Manual Testing Must be tested by hand from the PR's branch (steps in the PR) before merging

Projects

Status: Next Release Backlog

Development

Successfully merging this pull request may close these issues.

2 participants