Bazel
Applicability
Snyk supports Bazel only for Snyk Open Source.
Snyk for Bazel provides support for using the Bazel build and test tool with Snyk Open Source. The instructions in this documentation apply to Bazel v 7 only.
Snyk supports testing Projects whose dependencies are managed by Bazel. Snyk recommends testing and monitoring using the Dep Graph API.
Unlike npm, Bazel does not rely on dependency manifest files or lock files. Instead, you manage build configurations in BUILD files using Starlark, a domain-specific language based on Python 3.
You manually specify all dependencies (package name, location, and version), including transitive dependencies. Bazel fetches these dependencies during builds.
Bazel has limited native integration with package registries, such as npmjs.org or Maven Central. You can add Bazel rules to help install dependencies from external registries.
Because Bazel dependencies are specified as code in BUILD files using Starlark, Snyk cannot easily discover the dependencies from a Project.
Dep Graph API
To secure Bazel Projects, you must use the Snyk Dep Graph API. This API accepts a generic dependency graph and returns a report containing any relevant vulnerabilities for those dependencies.
Requirements and considerations
The Dep Graph API requires specific permissions. If you do not have access, contact Snyk Support.
You can test Bazel dependencies across any supported ecosystem, except C++, which is not supported by these endpoints.
Use the Snyk Dep Graph API endpoints Test Dep Graph and Monitor Dep Graph to test and monitor dependencies managed by Bazel. The monitor capability allows you to submit a tree for Snyk to monitor for vulnerabilities.
Test and monitor dependencies
To integrate Snyk into your Bazel workflow, follow these steps to manually generate and submit a dependency graph to the Snyk API:
Create a Dep Graph JSON object listing all the dependency packages and versions for each type of dependency (for example, Maven or CocoaPods).
Send the Dep Graph JSON object as a POST request to the Test Dep Graph endpoint, along with your auth token, as part of a Bazel test rule.
Check the API response for pass or fail status and any resulting vulnerabilities.
For example:
Dep Graph JSON syntax
The Test Dep Graph API accepts a Snyk Dep Graph JSON object. This object describes the root application and the graph of direct and transitive dependencies.
The schema for this format is:
Specific components in the Dep Graph object include:
schemaVersion- the version of the Dep Graph schema. Set this to1.2.0.pkgManager.name- can be one ofdeb,gomodules,gradle,maven,npm,nuget,paket,pip,rpm,rubygems, orcocoapods.pkgs- an array of objects containingid,nameandversionof all packages in the Dep Graph. Theidmust be in the formname@version. List each of your dependencies in this array, including an item representing the Project itself.graph.nodes- an array of objects describing the relationships between entries inpkgs. This is typically the Project node with all other packages defined as a flat array of direct dependencies indeps.graph.rootNodeId- specifies theidof the entry ingraph.nodesto use as the root node of the graph. Set this to thenodeIdof the Project node.
Dep Graph Test API response
The Test Dep Graph API returns a JSON object describing any issues (vulnerabilities and licenses) found in the Dep Graph dependencies.
An example response with a single vulnerability:
Specific components in the response object include:
ok- Boolean value summarizing whether Snyk found any vulnerabilities in the supplied dependencies. You can use this for a quick pass or fail test.issuesData- a hash of each unique vulnerability found. Each vulnerability contains useful properties, such astitle,description,identifiers,publicationTime,severity, and so on.issues- an array of mappings from vulnerabilities inissuesDatato package. This mapping shortens the response length because a vulnerability can apply to multiple packages.
Example of dependency mapping for a Bazel Project
For a Bazel Project with a single dependency on a Maven package, you can specify the dependency as follows:
Use the provided template to construct the following Dep Graph JSON object:
This package (ch.qos.logback:[email protected]) contains a vulnerability described in detail in the resulting JSON response object.
Last updated
Was this helpful?

