fix (security): Sanitize output filenames in ExtractOutputPreprocessor to prevent path traversal - #2285
Open
LinZiyuu wants to merge 2 commits into
Open
fix (security): Sanitize output filenames in ExtractOutputPreprocessor to prevent path traversal#2285LinZiyuu wants to merge 2 commits into
LinZiyuu wants to merge 2 commits into
Conversation
…h traversal ExtractOutputPreprocessor used the notebook-provided output.metadata.filename verbatim, so a crafted notebook could cause extracted output files to be written outside the output directory (both absolute paths and "../" sequences escape) with attacker-controlled contents when written by FilesWriter. Reduce the filename to its basename before use, and fall back to the generated filename template when the basename is empty. Add regression tests covering traversal, absolute-path, and empty-basename cases.
for more information, see https://pre-commit.ci
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
ExtractOutputPreprocessoruses the notebook-providedoutput.metadata.filenameas the on-disk filename for extracted outputs without sanitizing it. A crafted notebook can therefore cause extracted output files to be written outside the conversion output directory — both an absolute path and a../traversal escape — and forimage/svg+xmloutputs the written bytes are fully controlled by the notebook.Details
The default writer then writes each item without checking that the destination stays inside the build directory:
ExtractOutputPreprocessoris a default preprocessor for therst,markdown,asciidoc, andlatex/pdfexporters andFilesWriteris the default writer, so this code path is reached by an ordinaryjupyter nbconvertinvocation. The notebook does not need to be executed — the payload lives in staticoutputs.Reproduction (before this change)
A notebook whose display output carries
metadata.filename = "../../../../../../tmp/x/evil.svg"(or an absolute path) together withdata["image/svg+xml"]:Fix
Reduce the output filename to its basename before use, and fall back to the generated
output_filename_templatewhen the basename is empty, so extracted outputs always stay inside the output directory. A warning is logged when a filename contained path components.Tests
Adds regression tests covering
../traversal, absolute paths, and empty-basename filenames intests/preprocessors/test_extractoutput.py. The existing extractoutput tests continue to pass.