Skip to content
Merged
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
16 changes: 15 additions & 1 deletion src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,26 @@ export function parseRawCommit(oid: string, raw: string): Commit {
);
}

if (headers.some((line) => line.startsWith("gpgsig "))) {
if (
headers.some(
(line) => line.startsWith("gpgsig ") || line.startsWith("gpgsig-sha256 "),
)
) {
throw new Error(
`Commit ${oid} is signed. bstack cannot add an identity trailer without replacing its signature`,
);
}

const encoding = headers
.find((line) => line.startsWith("encoding "))
?.slice("encoding ".length);

if (encoding && !/^utf-?8$/i.test(encoding)) {
throw new Error(
`Commit ${oid} uses the ${encoding} message encoding; bstack only supports UTF-8 commit messages`,
);
}

const message = raw.slice(boundary + 2);

return {
Expand Down
36 changes: 36 additions & 0 deletions test/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ describe("commit identity", () => {
});
});

test.each([
["gpgsig -----BEGIN PGP SIGNATURE-----", "is signed"],
["gpgsig-sha256 -----BEGIN PGP SIGNATURE-----", "is signed"],
["encoding ISO-8859-1", "uses the ISO-8859-1 message encoding"],
])("rejects commits with the %s header", (header, message) => {
const raw = [
"tree aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"parent bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"author Ada <ada@example.com> 1 +0000",
"committer Ada <ada@example.com> 1 +0000",
header,
"",
"Original message",
"",
].join("\n");

expect(() =>
parseRawCommit("cccccccccccccccccccccccccccccccccccccccc", raw),
).toThrow(message);
});

test("accepts an explicit UTF-8 encoding header", () => {
const raw = [
"tree aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"parent bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"encoding UTF-8",
"",
"Original message",
"",
].join("\n");

expect(
parseRawCommit("cccccccccccccccccccccccccccccccccccccccc", raw).message,
).toBe("Original message\n");
});

test("rewrites only the parent and message of a raw commit", () => {
const raw = [
"tree aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
Expand Down
Loading