TocompletetheImmersiveLabsJavaPathTraversalexercise,followthesestep-by-stepinstructions.Step1:WalkThroughtheDemoExploitSwitchtothePreviewtaborclickDemoExploitVulnerabilityonthepreview/testingUI.Observehowprovidingapathlike../../../../etc/passwdallowsreadingfilesoutsidethedesignatedavatardirectory.Step2:UnderstandtheVulnerabilityinAvatarServlet.javaInlines33–38:JavaStringbasePath=req.getServletContext().getRealPath("avatar");Pathpath=Paths.get(basePath,filename);Filefile=path.toAbsolutePath().toFile();Paths.get(basePath,filename)resolvesrelativepathsequenceslike../.Withoutcanonicalizationchecks,anattackercantraverseoutsidebasePath.Step3:RemediatetheVulnerabilityTofixpathtraversal,verifythatthenormalized/canonicaltargetpathstillstartswiththebasepathprefixbeforeservingthefile.OpenCodeEditor$\rightarrow$src/main/java/com/immersivelabs/fitness/servlet/AvatarServlet.java.LocatethedoGetmethodaroundline33.Replacelines33–45withthefollowingsecurepathvalidationlogic:Java// Get the base path where all avatars liveStringbasePath=req.getServletContext().getRealPath("avatar");PathbaseDirPath=Paths.get(basePath).toAbsolutePath().normalize();// Join with the provided filename and normalize path traversal sequencesPathtargetPath=Paths.get(basePath,filename).toAbsolutePath().normalize();// Verify target path remains inside baseDirPathif(!targetPath.startsWith(baseDirPath)){resp.setStatus(HttpServletResponse.SC_FORBIDDEN);return;}Filefile=targetPath.toFile();if(!file.exists()||file.isDirectory()){resp.setStatus(HttpServletResponse.SC_NOT_FOUND);return;}Step4:TestandVerifyClicktheblueTestmycodebuttoninthetoprightheader.TheplatformwillrecompileAvatarServlet.javaandexecutesecuritychecks.BothWorkingandSecureindicatorsshouldturngreentocompletethelab.
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)