Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

76 lines
2.8 KiB
TypeScript

/**
* Tests for `verifyChecksum`: accepts hex SHA-256 + multibase-multihash
* (base32, sha2-256), rejects mismatches and malformed values. Backfills
* coverage deferred from PR #1011.
*/
import { createHash } from "node:crypto";
import { toBase32 } from "@atcute/multibase";
import { describe, expect, it } from "vitest";
import { verifyChecksum } from "../../../src/api/handlers/registry.js";
function sha256Hex(bytes: Uint8Array): string {
return createHash("sha256").update(bytes).digest("hex");
}
/**
* Compute the multibase-multihash form atcute uses on the wire: a
* `b`-prefixed base32 string of `[0x12, 0x20, ...sha2-256(bytes)]`.
*/
function sha256Multibase(bytes: Uint8Array): string {
const digest = createHash("sha256").update(bytes).digest();
const multihash = new Uint8Array(2 + digest.length);
multihash[0] = 0x12; // sha2-256 code
multihash[1] = 0x20; // length (32 bytes)
multihash.set(digest, 2);
return `b${toBase32(multihash)}`;
}
describe("verifyChecksum", () => {
const bytes = new TextEncoder().encode("hello, registry");
it("accepts the correct hex SHA-256 of the bytes", async () => {
expect(await verifyChecksum(bytes, sha256Hex(bytes))).toBe(true);
});
it("accepts the hex SHA-256 case-insensitively", async () => {
expect(await verifyChecksum(bytes, sha256Hex(bytes).toUpperCase())).toBe(true);
});
it("rejects an incorrect hex SHA-256", async () => {
expect(await verifyChecksum(bytes, "0".repeat(64))).toBe(false);
});
it("accepts the multibase-multihash (sha2-256, base32) form", async () => {
expect(await verifyChecksum(bytes, sha256Multibase(bytes))).toBe(true);
});
it("rejects multibase encoded over the wrong bytes", async () => {
const wrong = new TextEncoder().encode("hello, different");
expect(await verifyChecksum(bytes, sha256Multibase(wrong))).toBe(false);
});
it("rejects multibase wrapped around a non-sha2-256 algorithm", async () => {
// Forge a multihash header for sha2-512 (code 0x13, length 0x40)
// and check that verifyChecksum refuses it as the wrong family.
const digest = createHash("sha512").update(bytes).digest();
const multihash = new Uint8Array(2 + digest.length);
multihash[0] = 0x13;
multihash[1] = 0x40;
multihash.set(digest, 2);
// Wrap as multibase but with the wrong inner hash family. The
// outer string length differs (sha2-512 yields a longer multihash)
// so it never passes verifyChecksum's strict 56-char shape check;
// document that as the failure path here.
expect(await verifyChecksum(bytes, `b${toBase32(multihash)}`)).toBe(false);
});
it("rejects strings that are neither hex nor valid multibase", async () => {
expect(await verifyChecksum(bytes, "")).toBe(false);
expect(await verifyChecksum(bytes, "not-a-checksum")).toBe(false);
expect(await verifyChecksum(bytes, "0xdeadbeef")).toBe(false);
});
});