Repository navigation
fix(datasource): store Excel uploads under a sanitised name #1448 - #1449
Merged
XiaJunjie2020 merged 4 commits intoNov 9, 2026
Merged
Conversation
…aease#1448 Adds safe_upload_name: a client-supplied upload name is reduced to a single path component (both separators count) and names that would still address another location are rejected.
…1448 parseExcel and the deprecated uploadExcel now drop every path component from the client name, refuse drive-relative names, match dotted extensions and resolve the write path with realpath plus commonpath.
Unit contract for safe_upload_name across POSIX, Windows and drive-relative names.
Reuses the offline harness of the import-traversal guard and posts multipart bodies with hostile file names.
This was referenced Nov 9, 2026
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.
Cause
/datasource/parseExceland the deprecated/datasource/uploadExcelderive the stored file name from the multipartfile.filename:parse_excelsplit the name on/and dropped everything before the first dot, so a Windows-style name kept its directory part (..\..\evil.xlsx->..\evil_<hash>.xlsx) and a drive-relative one kept its drive (C:evil.xlsx->C:evil_<hash>.xlsx);upload_excelused the raw name and took the extension fromsplit('.')[1], which also raisedIndexErrorfor names without a dot;settings.EXCEL_PATH;upload_excelmatched bare suffixes ({"xlsx", "xls", "csv"}), soevil.txlsxpassed as well.This is the write side of the same class that #1434 / #1435 fixed for
importToDb;apps/terminology/api/terminology.pyandapps/data_training/api/data_training.pyalready strip the client path before writing.Fix
common.utils.utils.safe_upload_name: keeps the last path component of a client-supplied name after treating both/and\as separators, and rejects names that would still address another location (empty,.,.., a drive-relative or alternate-stream:, an embedded NUL);os.path.splitext(soa.b.xlsxkeeps its stem) and resolve it through one shared helper that requires the result to stay insideEXCEL_PATH(os.path.realpath+os.path.commonpath);upload_excelnow matches dotted suffixes, which removes theIndexErrorpath for dot-less names.Tests
backend/tests/test_upload_file_name.py: unit contract ofsafe_upload_namefor POSIX, Windows, mixed and drive-relative names, plus the set that must be rejected;backend/tests/test_datasource_upload_path_traversal.py: reuses the offline harness of the existing import-traversal guard, posts real multipart bodies to both routes and asserts that the stored name is a single component inside the upload directory (or that the request is refused) and that nothing is written outside it.Verification
python -m pytest -q tests/test_upload_file_name.py-> 31 passed, run inbackend/on a checkout ofmainat a0b6145;nested\..\..\evil.xlsxresolved outside the upload directory andC:\Users\me\orders.xlsxto an absolute path, after the change every accepted name is a single component inside the directory andC:evil.xlsxis refused with 400;ruff checkandruff format --checkpass on the touched files.Not covered here
backend/tests/test_datasource_upload_path_traversal.pyimports the publishedsqlbot-xpackwheel and the database drivers, so it runs in CI rather than in the sandbox where the change was prepared;uv run pytest -qsuite was not run in that sandbox;Fixes #1448