Skip to content
This repository was archived by the owner on Jul 8, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ You can configure `buf-setup-action` with these parameters:
| Parameter | Description | Default |
|:---------------|:---------------------------------------------------|:-------------------|
| `version` | The version of the [`buf` CLI][buf-cli] to install | [`v1.50.0`][version] |
| `checksum` | Expected SHA256 checksum of the downloaded buf archive or executable | |
| `github_token` | The GitHub token to use when making API requests | |
| `buf_user` | The username to use for logging into Buf Schema registry. | |
| `buf_api_token` | The API token to use for logging into Buf Schema registry. | |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
github_token:
description: The GitHub token to use when making API requests.
required: false
checksum:
description: Expected SHA256 checksum of the downloaded buf archive or executable.
required: false
buf_user:
description: The username to use for logging into Buf Schema registry.
required: false
Expand Down
66 changes: 33 additions & 33 deletions dist/main.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/buf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import * as os from "os";
import * as path from "path";
import * as crypto from "crypto";
import * as fs from "fs";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { Octokit } from "@octokit/core";
Expand All @@ -27,6 +29,7 @@ const versionPrefix = "v";
export async function getBuf(
version: string,
githubToken: string,
checksum?: string,
): Promise<string | Error> {
const binaryPath = tc.find("buf", version, os.arch());
if (binaryPath !== "") {
Expand All @@ -44,6 +47,7 @@ export async function getBuf(
core.info(`Downloading buf version "${version}" from ${downloadURL}`);
if (downloadURL.endsWith(".tar.gz")) {
const downloadPath = await tc.downloadTool(downloadURL);
verifyChecksum(downloadPath, checksum);
core.info(
`Successfully downloaded buf version "${version}" from ${downloadURL}`,
);
Expand All @@ -66,6 +70,7 @@ export async function getBuf(
downloadURL,
"C:\\Users\\runneradmin\\buf-download\\buf.exe",
);
verifyChecksum(downloadPath, checksum);
core.info(
`Successfully downloaded buf version "${version}" from ${downloadURL} to ${downloadPath}`,
);
Expand All @@ -82,6 +87,29 @@ export async function getBuf(
return cacheDir;
}

function verifyChecksum(path: string, expectedChecksum?: string): void {
if (!expectedChecksum) {
return;
}

const normalizedExpected = expectedChecksum.trim().toLowerCase();
if (!/^[a-f0-9]{64}$/.test(normalizedExpected)) {
throw new globalThis.Error(
"The checksum input must be a SHA256 hex digest.",
);
}

const actualChecksum = crypto
.createHash("sha256")
.update(fs.readFileSync(path))
.digest("hex");
if (actualChecksum !== normalizedExpected) {
throw new globalThis.Error(
`Downloaded buf archive checksum mismatch. Expected ${normalizedExpected}, got ${actualChecksum}.`,
);
}
}

// getDownloadURL resolves Buf's Github download URL for the
// current architecture and platform.
async function getDownloadURL(
Expand Down
3 changes: 2 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ async function runSetup(): Promise<null | Error> {
"No github_token supplied, API requests will be subject to stricter rate limiting",
);
}
const checksum = core.getInput("checksum");

core.info(`Setting up buf version "${version}"`);
const installDir = await getBuf(version, githubToken);
const installDir = await getBuf(version, githubToken, checksum || undefined);
if (isError(installDir)) {
return installDir;
}
Expand Down
Loading