Skip to content
zit Artifact [47c9947b34]
Login

Artifact [47c9947b34]

Artifact 47c9947b34493ed5fe351af2a355f3081529427e187535c03a8a9e773d66a6f2:


const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const checkinHash = support.checkinHash;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const ssh_stub_signer = support.ssh_stub_signer;
const stub_signer = support.stub_signer;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

/// A signer that MANGLES: same armor, but it rewrites a card on the way
/// through, so the wrapped text no longer strips back to what was signed.
const mangling_signer = "sh -c 'printf -- \"-----BEGIN PGP SIGNED MESSAGE-----\\nHash: SHA256\\n\\n\" > \"$0\"; " ++
    "sed \"s/^C /C x/\" \"$1\" >> \"$0\"; printf -- \"-----BEGIN PGP SIGNATURE-----\\nstubsig\\n-----END PGP SIGNATURE-----\\n\" >> \"$0\"'";

test "PGP: commit --sign stores the wrapped manifest, cards still read, a mangling signer is refused" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "f.txt", "hello\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});

    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = stub_signer }, &.{ "commit", "-m", "signed", "--sign" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);
    const hash = try checkinHash(c.stdout);

    // The STORED artifact is the wrapped text — its name hashes the wrapper.
    var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &raw);
    try expectExit(raw.term, 0);
    try std.testing.expect(std.mem.startsWith(u8, raw.stdout, "-----BEGIN PGP SIGNED MESSAGE-----\n"));
    try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "-----BEGIN PGP SIGNATURE-----") != null);

    // And every reader still sees a manifest: the summary classifies it,
    // reports the signature, and the Z card verifies over the stripped body.
    var sum = try runZit(gpa, io, tmp.dir, &.{ "artifact", hash });
    defer freeResult(gpa, &sum);
    try expectExit(sum.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, sum.stdout, "type: check-in") != null or
        std.mem.indexOf(u8, sum.stdout, "type: manifest") != null);
    try std.testing.expect(std.mem.indexOf(u8, sum.stdout, "signature: clearsigned") != null);
    try std.testing.expect(std.mem.indexOf(u8, sum.stdout, "Z: verified") != null);

    // Derived state is unaffected: the check-in appears, resolves, and its
    // file is materializable — the wrapper never reaches the card layer.
    var tl = try runZit(gpa, io, tmp.dir, &.{"timeline"});
    defer freeResult(gpa, &tl);
    try expectExit(tl.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, hash[0..12]) != null);
    var ls = try runZit(gpa, io, tmp.dir, &.{ "ls", hash });
    defer freeResult(gpa, &ls);
    try expectExit(ls.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "f.txt") != null);

    // A signer whose output does not strip back to the manifest it was given
    // is refused: signing bytes the reader will not interpret is worse than
    // not signing.
    try writeFile(io, tmp.dir, "f.txt", "second\n");
    var bad = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = mangling_signer }, &.{ "commit", "-m", "mangled", "--sign" });
    defer freeResult(gpa, &bad);
    try expectExit(bad.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "--sign failed") != null);
    // Refused means nothing landed: the tip is still the signed check-in.
    var tl2 = try runZit(gpa, io, tmp.dir, &.{"timeline"});
    defer freeResult(gpa, &tl2);
    try expectExit(tl2.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, tl2.stdout, "mangled") == null);
}

test "PGP carry: a signed artifact survives pack and a clone byte-exact" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "f.txt", "carry\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = stub_signer }, &.{ "commit", "-m", "signed", "--sign" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);
    const hash = try checkinHash(c.stdout);

    var before = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &before);
    try expectExit(before.term, 0);

    // Pack rewrites the whole generation (re-deltas, recompresses). The
    // artifact's bytes are its identity, so they must come back identical.
    var packed_res = try runZit(gpa, io, tmp.dir, &.{"pack"});
    defer freeResult(gpa, &packed_res);
    try expectExit(packed_res.term, 0);
    var after = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &after);
    try expectExit(after.term, 0);
    try std.testing.expectEqualStrings(before.stdout, after.stdout);
    // Belt and braces: verify re-hashes every artifact against its name, so
    // a wrapper lost or altered anywhere in the pack is a fault here.
    var ver = try runZit(gpa, io, tmp.dir, &.{"verify"});
    defer freeResult(gpa, &ver);
    try expectExit(ver.term, 0);
}

test "PGP carry: a signed manifest crosses the sync wire byte-exact (clone over HTTP)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // Repo A holds a SIGNED check-in. The wrapper is part of the artifact's
    // bytes and therefore of its name, so a wire path that dropped or
    // rewrote it could not even produce the same hash — this test asserts
    // the bytes, which is the stronger statement.
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try writeFile(io, tmp_a.dir, "hello.txt", "signed over http\n");
    try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
    var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "alice", .pgp = stub_signer }, &.{ "commit", "-m", "signed", "--sign" });
    defer freeResult(gpa, &commit);
    try expectExitDiag(commit.term, 0, commit);
    const hash = try checkinHash(commit.stdout);

    var origin = try runZit(gpa, io, tmp_a.dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &origin);
    try expectExit(origin.term, 0);
    try std.testing.expect(std.mem.startsWith(u8, origin.stdout, "-----BEGIN PGP SIGNED MESSAGE-----\n"));

    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, tmp_a.dir, port);
    defer server.kill(io);
    try waitForListen(io, port);

    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var url_buf: [64]u8 = undefined;
    const url = try std.fmt.bufPrint(&url_buf, "http://127.0.0.1:{d}/", .{port});
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);

    var cloned_dir = try tmp_b.dir.openDir(io, "repo", .{});
    defer cloned_dir.close(io);
    var landed = try runZit(gpa, io, cloned_dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &landed);
    try expectExit(landed.term, 0);
    try std.testing.expectEqualStrings(origin.stdout, landed.stdout);
}

test "PGP: the ssh signer family gets fossil's detached handling and assembled armor" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "f.txt", "ssh signed\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});

    // A command whose basename starts with `ssh` is the detached case: zit
    // passes only the input file, the signer writes INFILE.sig, and zit
    // assembles the armor — fossil's layout, since 2025 ssh still cannot
    // produce a non-detached signature itself.
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = ssh_stub_signer }, &.{ "commit", "-m", "ssh-signed", "--sign" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);
    const hash = try checkinHash(c.stdout);

    var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &raw);
    try expectExit(raw.term, 0);
    // Fossil's exact assembly: armor line, ONE blank line (no `Hash:`
    // header, unlike the gpg path), the manifest, then the detached sig.
    try std.testing.expect(std.mem.startsWith(u8, raw.stdout, "-----BEGIN SSH SIGNED MESSAGE-----\n\n"));
    try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "-----BEGIN SSH SIGNATURE-----") != null);

    // And it still reads as a check-in through the stripper.
    var sum = try runZit(gpa, io, tmp.dir, &.{ "artifact", hash });
    defer freeResult(gpa, &sum);
    try expectExit(sum.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, sum.stdout, "signature: clearsigned") != null);
    try std.testing.expect(std.mem.indexOf(u8, sum.stdout, "Z: verified") != null);
}

test "PGP: a false-ish pgp-command means do not sign (fossil's is_false), and the commit is refused rather than silently unsigned" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "f.txt", "x\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});

    // `off` is fossil's disable spelling. Asking for --sign with signing
    // disabled is a contradiction the operator must see: zit refuses rather
    // than committing an unsigned check-in that was asked to be signed.
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = "off" }, &.{ "commit", "-m", "nope", "--sign" });
    defer freeResult(gpa, &c);
    try expectExit(c.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, c.stderr, "--sign failed") != null);
}