Repository navigation
fix: close JDBC resources in CSRF, crypto, URL access and misconfiguration challenges (part of #857) - #1016
Open
ismisepaul wants to merge 1 commit into
Open
fix: close JDBC resources in CSRF, crypto, URL access and misconfiguration challenges (part of #857)#1016ismisepaul wants to merge 1 commit into
ismisepaul wants to merge 1 commit into
Conversation
…ation challenges (part of #857) Each challenge schema has its own connection pool of three connections. UrlAccess3UserList never closed its connection, so the fourth request 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, statement and ResultSet in try-with-resources, and do the same in ModuleServletTemplate so new levels start from the safe pattern. Only resource handling changes: the queries, messages and the catch block each exception reaches are the same as before. MiscChallengesPoolLeakIT calls each servlet five times, five malformed UrlAccess3UserList cookies included, and checks every well-formed request still reaches the database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The changes are narrowly scoped resource-lifecycle improvements with regression coverage and no identified blocking defects.
No open issues 🎉
What changed in this PR
This PR advances #857’s JDBC cleanup by closing resources reliably in miscellaneous challenge servlets, preventing challenge-specific connection pool exhaustion while preserving intentionally vulnerable challenge logic.
Changes:
- Converts connections, statements, and result sets to try-with-resources in six challenge servlets.
- Updates the module template to demonstrate automatic resource cleanup.
- Adds integration tests covering repeated requests and recovery after malformed URL-access cookies.
| File | Description |
|---|---|
src/main/java/servlets/module/ModuleServletTemplate.java |
Demonstrates try-with-resources for new modules. |
src/main/java/servlets/module/challenge/UrlAccess3UserList.java |
Closes user-list query resources on success and failure. |
src/main/java/servlets/module/challenge/SecurityMisconfigStealTokens.java |
Closes token lookup and validation resources. |
src/main/java/servlets/module/challenge/CsrfChallengeTargetFour.java |
Closes CSRF token-check resources. |
src/main/java/servlets/module/challenge/CsrfChallengeSixGetToken.java |
Adds automatic cleanup to token retrieval. |
src/main/java/servlets/module/challenge/CsrfChallengeSevenGetToken.java |
Adds automatic cleanup to token enumeration. |
src/main/java/servlets/module/challenge/BrokenCrypto4.java |
Closes coupon-query resources. |
src/it/java/servlets/module/challenge/MiscChallengesPoolLeakIT.java |
Adds repeated-request and pool-exhaustion regression coverage. |
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-miscSet 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-misc' mvn -Pdocker clean install -DskipTests docker compose down -v docker compose up -d --buildadmin/passwordand set a new password when asked.Test
currentPersoncookie in a proxy). It should keep answering.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. With #998, #1004 and #1005, this covers every JDBC lesson and challenge servlet, except those in #919 (DirectObject) and #932 (SqlInjection1, 3, 4, 5).
What
UrlAccess3UserListnever closed its connection, so every request leaked one. It's also injectable through thecurrentPersoncookie, so malformed cookies took the level offline after three requests.BrokenCrypto4,CsrfChallengeSevenGetToken,CsrfChallengeSixGetToken,CsrfChallengeTargetFourandSecurityMisconfigStealTokensclosed their connection only when no exception was thrown.ModuleServletTemplate, the starting point for new levels, now uses the same pattern too.The levels are exactly as vulnerable as before
Only resource handling changed. Exception routing is kept exactly:
error.noToken;SecurityMisconfigStealTokensstill logs and rethrowsSQLException.The explicit
tokenRs.close()calls are gone because try-with-resources closes the result set. Review withgit diff -w origin/dev.Tests
MiscChallengesPoolLeakIT:UrlAccess3cookies, then 5 valid requests. Ondevthis fails ("Request 1 failed: An Error Occurred! You must be getting funky!").BrokenCrypto4, the CSRF 7 token list (GET, as the browser sends it), andSecurityMisconfigStealTokens'getUserToken/validToken.All 4 tests pass with this change. Every step of
test.ymlalso passed locally.Two servlets have no test:
CsrfChallengeTargetFour's privatevalidCsrfTokenonly fails on database errors, which a player can't trigger.CsrfChallengeSixGetTokenisn't mapped inweb.xml, and its database user doesn't exist: CsrfChallengeSixGetToken is unreachable and its database user and schema don't exist #1006.🤖 Generated with Claude Code