Skip to content

contain gemini batch-job results file within results_folder - #71832

Merged
potiuk merged 5 commits into
apache:mainfrom
Samin061:gemini-batch-results-path
Sep 20, 2026
Merged

potiuk merged 5 commits into
apache:mainfrom
Samin061:gemini-batch-results-path

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

Path traversal via unsanitized Gemini batch-job display_name

GenAIGeminiCreateBatchJobOperator and GenAIGeminiCreateEmbeddingsBatchJobOperator name the local results file after job.display_name, a field returned in the batch-job metadata. job.name is already sanitized against / at that spot, but display_name (used first) is not, so a .. in it lands the .jsonl outside results_folder on the worker. The check now resolves the join and refuses anything escaping results_folder, applied at both sites, matching the sync_to_local_dir guards already in the GCS and S3 hooks.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

@Samin061
Samin061 requested a review from shahar1 as a code owner August 19, 2026 10:34
@boring-cyborg boring-cyborg Bot added area:providers provider:google Google (including GCP) related issues labels Aug 19, 2026
@fat-catTW

Copy link
Copy Markdown
Contributor

Thanks for the fix!

Could we use Path.resolve() for the containment check here, matching the existing GCS/S3 sync_to_local_dir guards? abspath() catches .. traversal, but it does not resolve symlinks.
For example, if results_folder/link is a symlink to a directory outside results_folder, a display_name like link/evil would still pass the commonpath() check while writing outside the intended folder.

Using Path(results_folder).resolve() and checking the resolved target path is relative to that resolved base would make the containment guarantee stronger and align this with the existing guards.

@Samin061

Copy link
Copy Markdown
Contributor Author

Good point on the symlink gap. Switched the check to Path(results_folder).resolve() and resolved_target.is_relative_to(base), so a symlinked subdir pointing outside now gets rejected too. That matches the GCS/S3 sync_to_local_dir guards. Tests still pass.

@fat-catTW

Copy link
Copy Markdown
Contributor

Thanks, the change addresses the symlink gap!

Could you also add a regression test for the symlink case? The current tests cover ../evil, but not a path like link/evil where results_folder/link is a symlink to a directory outside results_folder. That would lock in the behavior this change is intended to protect.

@Samin061

Copy link
Copy Markdown
Contributor Author

Added a symlink regression test for each operator: results_folder/link is a symlink to a dir outside results_folder, and a display_name of link/evil now raises ValueError before any write, with nothing landing in the outside dir. Both pass on the resolved-path check and would slip through the old abspath()/commonpath() version.

@fat-catTW

Copy link
Copy Markdown
Contributor

Nice!!
LGTM

@potiuk
potiuk force-pushed the gemini-batch-results-path branch from 6d19f3f to cb809ba Compare September 20, 2026 20:30
Rename _results_file_path to _build_results_file_path per the action-verb
naming rule, and annotate the job parameter as BatchJob (already imported).

Add one happy-path test per operator. The four existing tests are all
rejection cases, and the file-writing branch had no other coverage, so a
helper that raised unconditionally would have passed the whole suite.
Verified: sabotaging the helper to always raise fails exactly these two.

Generated-by: Claude Opus 5

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The containment guard is correct at both call sites, and the four new tests genuinely fail without it — on the old os.path.abspath() path the traversal case writes tmp_path/evil.jsonl and the symlink case writes outside/evil.jsonl, neither raising. Real regression tests rather than tests written around the new code.

I rebased the branch on main (it was 699 commits behind, which the base-branch policy would have refused) and pushed two fixups:

  • Renamed _results_file_path to _build_results_file_path per the action-verb naming rule in AGENTS.md, and annotated the parameter as job: BatchJob — already imported at module level, so no new import.
  • Added one happy-path test per operator. All four of your tests are rejection cases, and I checked the rest of the file: the file-writing branch of _prepare_results_for_xcom had no other coverage, so a helper that raised unconditionally would have passed the entire suite. I verified the new tests close that hole by sabotaging the helper to always raise — exactly the two new tests fail, the four rejection tests still pass.

Smaller observations

  • str(path_to_file) is now a fully resolved path where it used to be os.path.abspath(...), which does not follow symlinks. That string is the task's XCom value, so a deployment whose results_folder sits under a symlink will see a different return value than before. Almost certainly an improvement, and needed for the containment check to mean anything, but it is user-visible and worth a line in the description.
  • Path(f"{results_folder}") turns a None folder into a literal directory named None under the worker's cwd. That is unchanged from the line you replaced, so not on you — but the new helper is the tidiest place to make it an explicit error if you ever feel like it.
  • On framing: display_name reaches the worker from a template field, so the value is Dag-author-supplied, and Dag authors already execute arbitrary code on the worker under Airflow's security model. The guard earns its keep when a Dag templates that value from dag_run.conf or a Variable. Good hardening either way — just not a CWE-22 in the vulnerability sense, so I would not frame it as one.

This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Airflow handles maintainer review:
contributing-docs/05_pull_requests.rst.

Both fields are optional on BatchJob, so job.name.replace(...) could raise
AttributeError when neither is set. Annotating the parameter as BatchJob made
mypy surface it; fail with a clear message instead.

Generated-by: Claude Opus 5
@potiuk

potiuk commented Sep 20, 2026

Copy link
Copy Markdown
Member

Heads-up: MyPy providers checks went red on my previous fixup, and it turned out to be a real latent bug rather than a spurious annotation error.

BatchJob.name is str | None, so job.name.replace("/", "-") could raise AttributeError when a job had neither display_name nor name. While job was unannotated mypy never looked at it; adding job: BatchJob is what surfaced it. I have pushed a follow-up that derives the name None-safely and raises a clear ValueError when there is nothing to name the file after. Tests and mypy are both clean locally.

So the annotation earned its keep — thanks for the patience on the extra round-trip.


This message was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. If you think
something here is mistaken, please reply on the PR and a
maintainer will weigh in.

@potiuk
potiuk merged commit 1a9091e into apache:main Sep 20, 2026
86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:google Google (including GCP) related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants