|
| 1 | +--- |
| 2 | +layout: blog-detail |
| 3 | +post-type: blog |
| 4 | +by: Adrien Piquerez, Scala Center |
| 5 | +title: Secure the Dependencies of your Scala Project on Github |
| 6 | +--- |
| 7 | + |
| 8 | +We released [sbt-dependency-submission][sbt-dependency-submission], a GitHub action that submits the dependencies of sbt builds to the [GitHub Dependency submission API](https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/using-the-dependency-submission-api) (currently in beta). |
| 9 | +This action, once installed in a Github workflow, allows you to view the [Dependency graph](https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-the-dependency-graph) in Github and to receive regular security reports from [Dependabot](https://docs.github.com/en/code-security/dependabot/dependabot-alerts/about-dependabot-alerts). |
| 10 | + |
| 11 | +A similar action for the Mill build tool, the [mill-dependency-submission][mill-dependency-submission] action, developed by Chris Kipp, is also available. |
| 12 | +Other solutions are available for Maven and Gradle as well. |
| 13 | + |
| 14 | +The next sections explain the motivation behind this project, show how to get started, compare Dependabot and Scala Steward, and list similar solutions for other build tools. |
| 15 | + |
| 16 | +## GitHub Dependabot and the Scala ecosystem |
| 17 | + |
| 18 | +Last year, the [Log4j CVE](https://scala-lang.org/blog-detail/2021/12/16/state-of-log4j-in-scala-ecosystem.html) reminded us of the importance of staying alert to security vulnerabilities. |
| 19 | +Fixing all known vulnerabilities once does not make your project secure, since critical vulnerabilities can always be found and exploited in open source libraries, even the most widely used ones. |
| 20 | +Being able to identify new vulnerabilities and to respond to them quickly reduces the risk of exposure to its minimum. |
| 21 | +But the task can be time consuming, unless we automate it. |
| 22 | + |
| 23 | +GitHub Dependabot is an automation tool that helps us secure our projects. |
| 24 | +Its initial approach is to scan static configuration files such as POM files or package-lock.json files to extract the dependencies of a repository and cross reference them with a database of vulnerabilities. |
| 25 | +But some build definitions, such as build.sbt files, are programs that cannot be statically analyzed. |
| 26 | +A few weeks ago, GitHub released the beta version of the Dependency submission API, a REST API that we can use in CI workflows to submit complete snapshots of dependencies when they are resolved at build-time. |
| 27 | + |
| 28 | +As a large part of the Scala community uses GitHub to host their projects, we developed sbt-dependency-submission, a GitHub action that can extract the graphs of dependencies of sbt projects, for Dependabot to analyze them and report any vulnerability. |
| 29 | +With this tool, we hope to contribute to building a more secure Scala open source ecosystem. |
| 30 | + |
| 31 | +## Setting up the workflow |
| 32 | + |
| 33 | +Before installing the dependency submission action, you need to ensure that the `Dependency graph` view is enabled in the `Settings > Code security and analysis` tab of your GitHub repository. |
| 34 | +You should also consider enabling `Dependabot alerts`. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +To install the action, add a new workflow in the `.github/workflows` folder, with the following definition: |
| 39 | + |
| 40 | +```yaml |
| 41 | +# .github/workflows/dependency-graph.yml |
| 42 | +name: Update Dependency Graph |
| 43 | + |
| 44 | +on: |
| 45 | + push: |
| 46 | + branches: |
| 47 | + - main # default branch of the project |
| 48 | + |
| 49 | +jobs: |
| 50 | + update-graph: |
| 51 | + name: Update Dependency Graph |
| 52 | + runs-on: ubuntu-latest |
| 53 | + steps: |
| 54 | + - uses: actions/checkout@v3 |
| 55 | + - uses: scalacenter/sbt-dependency-submission@v2 |
| 56 | + with: |
| 57 | + ## Optional: Define the working directory of your build. |
| 58 | + ## It should contain the build.sbt file. |
| 59 | + working-directory: ‘./’ |
| 60 | +``` |
| 61 | +
|
| 62 | +This default definition should work in many sbt builds out-of-the-box. |
| 63 | +Given a valid sbt build, the action installs an sbt plugin that will extract all transitive library dependencies from all subprojects on all Scala versions. |
| 64 | +
|
| 65 | +For troubleshooting, please refer to the [documentation](https://github.com/marketplace/actions/sbt-dependency-submission) of the action. |
| 66 | +
|
| 67 | +## Scala Steward and GitHub Dependabot |
| 68 | +
|
| 69 | +[Scala Steward](https://github.com/scala-steward-org/scala-steward) is a tool that helps you keep the dependencies of your project up-to-date by opening pull requests on GitHub (and other hosting services). |
| 70 | +It is used in more than a thousand open-source repositories and many proprietary ones and contributes to a large extent to the security of the Scala ecosystem. |
| 71 | +
|
| 72 | +Dependabot and Scala Steward can be used as complementary tools. |
| 73 | +Scala Steward, as a preventive tool, can help you keep your dependencies up-to-date, which reduces the risk of security vulnerabilities. |
| 74 | +Dependabot, as a monitoring tool, can notify you when a vulnerability is found, so that you can act quickly. |
| 75 | +
|
| 76 | +Dependabot can also send PRs to update dependencies, but in static configuration files only. |
| 77 | +It can update the actions in your Github workflows, or the Maven dependencies in your POM files, but not the dependencies in the `build.sbt` or `build.sc` files. |
| 78 | + |
| 79 | +## Other supported build tools |
| 80 | + |
| 81 | +You can use Dependabot with other Scala-compatible build tools. |
| 82 | + |
| 83 | +### Maven |
| 84 | + |
| 85 | +Dependabot supports Maven through static analysis of POM files. |
| 86 | +You can enable the Dependency graph view and Dependabot in the `Settings > Code security and analysis` page. |
| 87 | + |
| 88 | +### Mill |
| 89 | +The [mill-dependency-submission][mill-dependency-submssion] action, developed by Chris Kipp, can extract the dependencies of a Mill build. |
| 90 | + |
| 91 | +To install it: |
| 92 | + - Enable the `Dependency graph` view, and optionally the `Dependabot alerts`, in the `Settings > Code security and analysis` page of your repository. |
| 93 | + - Add this workflow in you repository: |
| 94 | + |
| 95 | +```yaml |
| 96 | +# .github/workflows/dependency-graph.yml |
| 97 | +name: Update Dependency Graph |
| 98 | +
|
| 99 | +on: |
| 100 | + push: |
| 101 | + branches: |
| 102 | + - main # default branch of the project |
| 103 | +
|
| 104 | +jobs: |
| 105 | + update-graph: |
| 106 | + name: Update Dependency Graph |
| 107 | + runs-on: ubuntu-latest |
| 108 | + steps: |
| 109 | + - uses: actions/checkout@v3 |
| 110 | + - uses: coursier/cache-action@v6 |
| 111 | + - uses: actions/setup-java@v3 |
| 112 | + with: |
| 113 | + distribution: 'temurin' |
| 114 | + java-version: '17' |
| 115 | + - uses: ckipp01/mill-dependency-submission@v1 |
| 116 | +``` |
| 117 | +### Gradle |
| 118 | + |
| 119 | +Gradle users can use the [gradle-dependency-submission][gradle-dependency-submission] action. |
| 120 | + |
| 121 | +## Summary |
| 122 | + |
| 123 | +In Github, it is now possible to use pre-made actions, like [sbt-dependency-submission][sbt-dependency-submission] or [mill-dependency-submission][mill-dependency-submission], to extract full graphs of dependencies at build time. |
| 124 | +This allows Dependabot to analyze the dependencies and publish regular reports of vulnerabilities. |
| 125 | +Only project administrators, organization owners and users with explicit access can see the Dependabot reports. |
| 126 | + |
| 127 | +The sbt-dependency-submission action is a fresh tool that relies on the beta Dependency submission API, but the cost of trying it is low and it will help you secure your sbt builds. |
| 128 | +Give it a try and report any bug at [scalacenter/sbt-dependency-submission](https://github.com/scalacenter/sbt-dependency-submission). |
| 129 | + |
| 130 | +[sbt-dependency-submission]: https://github.com/marketplace/actions/sbt-dependency-submission |
| 131 | +[mill-dependency-submission]: https://github.com/marketplace/actions/mill-dependency-submission |
| 132 | +[gradle-dependency-submission]: https://github.com/marketplace/actions/gradle-dependency-submission |
0 commit comments