Skip to content

fix(documentai-toolbox): contain split_pdf output to output_path - #18063

Open
Samin061 wants to merge 1 commit into
googleapis:mainfrom
Samin061:documentai-split-pdf-path-traversal
Open

fix(documentai-toolbox): contain split_pdf output to output_path#18063
Samin061 wants to merge 1 commit into
googleapis:mainfrom
Samin061:documentai-split-pdf-path-traversal

Conversation

@Samin061

Copy link
Copy Markdown

split_pdf builds each output filename from entity.type_, which is read straight from the parsed Document (loaded via from_gcs or from_document_path). Document AI entity types can contain "/" (the fixtures already carry "vat/tax_amount"), and a document whose type_ is set to something like ../../../../tmp/pwned makes os.path.join(output_path, output_filename) write the split PDF outside output_path. The entity type is now flattened before it goes into the filename, so the write stays inside output_path.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@Samin061
Samin061 requested a review from a team as a code owner August 11, 2026 12:52

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces sanitization for entity types in split_pdf by replacing path separators (/ and \) with underscores to prevent path traversal vulnerabilities, and adds a corresponding unit test. The reviewer suggested also replacing : with _ to ensure cross-platform compatibility on Windows, where : is an invalid filename character.

Comment on lines +830 to +832
subdoc_type = (
(entity.type_ or "subdoc").replace("/", "_").replace("\\", "_")
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While replacing / and \ successfully prevents directory traversal, entity types can also contain other characters like : (especially in custom processors or namespaces, e.g., custom:entity_type). On Windows, : is an invalid filename character (or denotes an alternate data stream), which can cause split_pdf to fail with an OSError or behave unexpectedly.

Consider also replacing : with _ to ensure robust cross-platform compatibility.

                subdoc_type = (
                    (entity.type_ or "subdoc")
                    .replace("/", "_")
                    .replace("\\", "_")
                    .replace(":", "_")
                )

@parthea parthea self-assigned this Aug 11, 2026
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants