Bazel does not provide any security boundaries against malicious workspaces. If you want to guard against Bazel executing malicious code, either sandbox it or control the source code it has access to.
All of the non-test code under this source tree that is packaged into or compiled into Bazel itself, namely: the client and server implementations, Starlark engine, external services interfaces (e.g. clients for RBE and remote caching), process and sandbox wrappers, and existing security mitigations for flags and select tools.
Out of scope exceptions:
Bazel is an open-source build system used to efficiently build and test software. Its user interface is the bazel CLI tool. Builds are configured through BUILD files with compilation/execution rules defined in “Starlark” .bzl files. Bazel exposes many flags to configure build/runtime behavior that can be specified on the command line or in .bazelrc files.
Bazel offers some protections for select assets.
Bazel generally runs in a “trust-first” mode. There are only minimal checksum verifications on metadata and blobs downloaded from remote services (e.g. RBE and remote dep hosts).
All boundaries described in this section hold true provided that the host machine is not compromised. See “Attacker-controlled workspaces” below for more details.
Bazel‘s server socket permissions are set to 700 to prevent cross-user build contamination. Users should not be able to access or control another user’s Bazel server.
Bazel may use authentication credentials to communicate with remote services (e.g. fetching external dependencies, accessing remote caches, or streaming BEP events). Those credentials and tokens should not be exposed to build actions (particularly remote actions) or other services outside of the intended service, or recorded in test logs, Java server logs, or the BEP.
Bazel must always reject downloaded content whose hash does not match the expected hash assuming the host system is not compromised. Because this threat model presumes that the workspace visible to Bazel is already trusted, the remote dependency locations (i.e. websites hosting archives), their contents, and hashes are all therefore assumed to be trusted.
There are several categories of alleged vulnerabilities often reported to Bazel maintainers that are not considered security issues.
In general, achieving arbitrary code execution is expected behavior, to a certain extent (and indeed the whole point of a build tool). It is working-as-intended if a build initiated from Bazel executes arbitrary code either on the host machine or in a remote executor.
This is constrained in several ways:
Attacks that are predicated on an attacker already having admin access to the host machine are out of scope. Bazel does not need administrator permissions to perform its work, and so should not be considered a vector for privilege escalation.
Bazel's sandbox is described here. The relevant text is copied below:
Bazel’s file system sandbox runs processes in a working directory that only contains known inputs, such that compilers and other tools don‘t see source files they should not access, unless they know the absolute paths to them. Sandboxing doesn’t hide the host environment in any way. Processes can freely access all files on the file system. However, on platforms that support user namespaces, processes can’t modify any files outside their working directory. This ensures that the build graph doesn’t have hidden dependencies that could affect the reproducibility of the build.
The local sandboxes Bazel offers guard against bugs, not malice. Their goal is to ensure that the build has declared its dependencies properly and actions don't access files not in their dependencies. By design, there are no guarantees that the sandbox has any kind of isolation across file system, network, user, or process boundaries. Therefore, the sandbox should not be considered a security boundary, or a truly hermetic environment in the usual sense.
A corollary to the minimal sandbox abstraction is that path traversal exploits are unfortunate, yet unavoidable. Build actions as well as repository rules require wide access to the file system, and cannot be meaningfully restricted.
Bazel trusts the source code the user runs it on and does not offer a security boundary of any kind around code it executes from it. There is limited validation of boundedness in some places in the product, e.g. extracting remote archives.
The implication for attacker-controlled workspaces is that if an attacker already has control over a user‘s Bazel client and local environment, Bazel generally cannot worsen the attacker’s position.
Bazel calls upon many subtools, which exist under its source tree, as part of compilation workflows. These are tools such as singlejar, ijar, and buildjar. Exploits that abuse vulnerabilities in these tools, or in the underlying Java runtime, are considered to be outside of Bazel's security scope.
Some subtools are part of Bazel's execution model, such as the process-wrapper and linux-sandbox. Vulnerabilities in these tools should be considered in-scope.
Note that this is not an exhaustive list, but should cover the most highly anticipated attack vectors.
Bazel's client and server communicate over a local network socket. This channel can theoretically be abused to inject malicious commands or code and should remain intact.
Path traversal attacks cannot be meaningfully defended against in Bazel. As discussed earlier, Bazel's sandbox is only a weak layer meant to enforce declared dependency boundaries, not system resource access.
There are a few notable exceptions to this rule where Bazel does have some built-in protections. These subsystems and APIs are mostly restricted to path validation for interacting with and extracting external repositories and dependencies. Path validation in this context is limited to disallowing out-of-bounds symlinks, upwards-relative paths (i.e. ..), and absolute paths. Bypassing these existing validations is a vulnerability.
http_archive (used by Bazel in repo rules and bzlmod)rctx.execute() and impossible for Bazel to defend against.Note that in most cases, the above exceptions are intended to prevent unintentional path traversal. Bazel cannot protect against generalized path traversal exploits.
Bazel accepts many inputs via parsed command-line flags and files. These inputs should be escaped as a first-order protection against arbitrary code injection in the Bazel process.
As a build tool, Bazel is a potential vector for supply chain attacks, both on the receiving and giving end.
Bazel projects have many remote dependencies: external rule repositories, binaries (e.g. Java toolchains). An attacker could aim to intercept incoming downloads of these remote assets to inject malicious code.
In addition to remote dependencies, Bazel also can read cached intermediate artifacts from a remote cache. This cache, or the network connection to it, could be compromised to inject malicious code.
Bazel is also used to produce binaries, scripts, and other artifacts that are consumed by downstream users or systems. These can include shell scripts, container images, and more. An attacker could aim to compromise Bazel to inject malicious content into these outputs, which could then be used in a downstream system.
Bazel can interact with user credentials in several ways, with each access type presenting a potential attack vector.
Bazel supports many credential specification methods, e.g. flags, environment variables, .netrc, credential helpers, and cloud default credentials.:
SafeRequestLogging class). The subsystems responsible for scrubbing these values should not be circumvented, and doing so is a vulnerability.If the answer to 1 + 2 and either 3 or 4 is “yes”, then the issue should be considered a vulnerability. All other cases should be considered “working as intended” from a security perspective, but may be considered bugs.