JavaBuilder setUpSourceJars resolves .srcjar entry names without containment check (same pattern as #28120)

Open Beginner friendly
#33,068 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
build-system

Research direction

Start with SimpleJavaLibraryBuilder.setUpSourceJars and compare its extraction logic with the GenClass fix for #28120. Check both the normal and collision-renamed destination paths for entries containing ..; the issue proposes rejecting any normalized path outside sourcesDir. Done means escaping entries fail with a clear error while entries inside the extraction directory still work.

Written by the indexing model from the issue text.

Description

team-Rules-Java type: bug untriaged
Description of the bug:

SimpleJavaLibraryBuilder.setUpSourceJars extracts .java entries from each --source_jars input into the build's temp directory:

String fileName = entry.getName();
if (fileName.endsWith(".java")) {
  if (fileName.charAt(0) == '/') {
    fileName = CharMatcher.is('/').trimLeadingFrom(fileName);
  }
  Path to = sourcesDir.resolve(fileName);
  ...
  Files.createDirectories(to.getParent());
  Files.copy(jarFile.getInputStream(entry), to);

Leading / is stripped, but .. segments are not, so an entry like ../../x/Foo.java is written outside sourcesDir. The collision path (sourcesDir.resolve(Integer.toString(root++)).resolve(fileName)) has the same issue.

This is the same pattern fixed in GenClass for #28120 (559326a301). Following the discussion there, we're not claiming a security boundary, since srcjars come from build inputs, which can already run code. The point is hermeticity: a buggy srcjar producer (genrule, codegen tool, third-party srcjar) should fail with a clear error rather than write outside the extraction root.

CodeQL's java/zipslip query flags this location.

Proposed fix

After computing to (including the collision-renamed path), reject entries that don't stay under sourcesDir:

Path root = sourcesDir.normalize();
...
if (!to.normalize().startsWith(root)) {
  throw new IOException(
      String.format("%s: entry %s escapes the source extraction directory", sourceJar, entry.getName()));
}

This is a lexical check; it doesn't address symlinks already present in sourcesDir (which cleanupDirectory empties first).

Which operating system are you running Bazel on?

Any (code path is platform-independent).

What is the output of bazel info release?

Present at 9.3.1 and on master as of 7849f5e0d3.

Dominant language
Java
Stars
26.2k
Forks
4.7k
Avg merge
3d 11h
Merged PRs (30d)
125

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/bazel

All issues in bazelbuild/bazel

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.