fix: support nested starter code paths and block path traversal#435
Open
anshul23102 wants to merge 1 commit into
Open
fix: support nested starter code paths and block path traversal#435anshul23102 wants to merge 1 commit into
anshul23102 wants to merge 1 commit into
Conversation
…lharshita#407) resolve_starter_file only extracted the basename, so any project whose starter_code value contained a subdirectory (e.g. survey_form/index.html) always resolved to the wrong flat path and returned 404. Strip the leading "starter_code/" prefix, join the remainder with STARTER_CODE_DIR, normalise with os.path.normpath, and verify the result stays inside STARTER_CODE_DIR before accepting it. This allows any depth of nesting while preventing path traversal attacks. Also fix the download route: send_from_directory was called with the root starter_code directory but only the basename, so nested files were never served. Changed to derive the directory from the resolved full_path. Three new tests cover: /code for a nested project, /download for a nested project, and rejection of a path traversal payload.
|
@anshul23102 is attempting to deploy a commit to the komalsony234-1530's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary [required]
Projects whose
starter_codevalue contains a subdirectory (e.g.starter_code/survey_form/index.html) were returning 404 for both the View Code and Download endpoints. The root cause was a two-part bug:resolve_starter_fileinutils/file_server.pycalledos.path.basenameon the raw path, stripping the subdirectory completely.survey_form/index.htmlbecame justindex.html, which does not exist at the root of thestarter_codedirectory.Even after fixing (1), the download route in
routes/main_routes.pypassedget_starter_code_dir()(the rootstarter_codefolder) tosend_from_directorytogether with only the basename, so Flask still could not locate nested files.This PR fixes both layers.
resolve_starter_filenow strips any leadingstarter_code/prefix, resolves the relative subpath insideSTARTER_CODE_DIR, and usesos.path.normpathplus a prefix check to block path traversal attempts. The download route derives the serving directory fromos.path.dirname(full_path)so Flask serves the file from its actual location regardless of nesting depth.Related Issue [required]
Closes #407
Type of Change [required]
What Was Changed [required]
utils/file_server.pyresolve_starter_fileto support nested paths with a path traversal guardroutes/main_routes.pydownload_codeto serve from the file's actual directory, not the starter_code roottests/test_basic.py/code, nested/download, and path traversal rejectionHow to Test This PR [required]
git checkout fix/nested-starter-code-resolutionpip install -r requirements.txtpython app.pyindex.htmlfile should download successfully.python tests/test_basic.pyExpected test output:
Test Results [required]
Self-Review Checklist [required]
feat/,fix/,docs/,data/,style/,test/python tests/test_basic.pyand all tests passprint()orconsole.log()debug statementsNotes for Reviewer
The path traversal guard works by normalising the joined path with
os.path.normpathand confirming it starts withSTARTER_CODE_DIR + os.sep. Without the trailing separator the check could be bypassed by a directory name that shares the same prefix (e.g.starter_code_evil/), so the separator is included intentionally.The download route change is minimal: the resolved
full_pathfromresolve_starter_fileis already validated, so derivingfile_dirfrom it adds no new trust surface.