feat: Enhance status endpoint with path validation #347
+19
−2
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.
Added path normalization and validation to prevent directory traversal attacks in the status endpoint to fix uncontrolled path usage you must ensure that any user-controlled component of a file path is validated and/or that the final path is constrained to a safe directory after normalization. Options here include: (1) constraining
uidto match a strict pattern (for example, UUID or a limited character set), or (2) computing the full path underSAVE_DIR, normalizing it withos.path.normpathoros.path.realpath, and then enforcing that it still resides insideSAVE_DIRbefore opening it.The least invasive and most robust change here is to keep using
uidas-is for lookup, but protect the final path: buildsave_file_pathunderSAVE_DIR, normalize it, and check that it is still withinSAVE_DIR. If the check fails, treat the status as an error or as “not found/processing” without reading any file. Concretely, in@app.get("/status/{uid}")we should: (1) computerequested_path = os.path.join(SAVE_DIR, f'{uid}.glb'), (2) normalize bothSAVE_DIRandrequested_path(e.g. withos.path.abspath+os.path.normpath), (3) verify thatnormalized_pathstarts with the normalizedSAVE_DIRprefix (using a proper boundary check), and (4) only then check existence and read the file. If the path is invalid, immediately return an error JSON response. This change is local to thestatushandler and does not alter the external API or how legitimate UIDs behave.We already import
osat the top of the file, so no new imports are required. All changes are withinapi_server.py, in thestatusfunction body around lines 287–297.References
werkzeug.utils.secure_filename