BagIt in 2018: detecting same-size corruption in station data deliveries
A small BagIt 1.0 package demonstrates why a byte count can agree while a SHA-256 payload manifest still detects an altered measurement.
Archive retrospective · Topic year 2018
In this article
Why byte counts can agree after corruption
A monitoring station sends its daily readings to a university archive as a CSV file. The receiving team sees the expected file size and the expected number of records. One reading has nevertheless changed from 19 to 18. The alteration leaves both counts untouched. Inspecting the received table cannot reconstruct the bytes the producer originally intended to deliver. The producer can calculate a checksum when it closes the file, and the receiving team can compare that checksum against the actual data after transfer. BagIt defines a directory layout for transporting both the measurement files and a manifest that helps the recipient verify them. The same acceptance procedure can then be shared by teams with different storage services and transport tools.
What the 2018 format puts in the package
RFC 8493 was published in October 2018 and describes the BagIt 1.0 format as an Informational RFC. The sample bag has a bagit.txt declaration at the root, a data directory for the payload and a manifest-sha256.txt file. The declaration identifies the format version and the tag file character encoding. The manifest records a digest and a path for the CSV. The path lets the recipient identify which payload was expected instead of finding an arbitrary file with the same extension. Optional descriptive tag files can carry details about the shipment. A tag manifest, where supplied, covers tag files separately. An implementation needs to know which manifest and algorithm it is validating before it interprets a pass as evidence about any particular file.
Run the same-length modification test
The example makes a temporary directory and writes a 22-byte CSV file. It stores the SHA-256 digest together with the payload path. An initial verification reads the digest from the manifest and recalculates it from the actual file. The program then changes one reading without changing the file length. The second verification fails. Running node verify-bagit.mjs locally on Node.js 24.19.0 produced original true and sameSizeEdit false, with the file still at 22 bytes. The program deletes its temporary fixtures at the end. It validates a single known path and illustrates the checksum comparison. A full BagIt validator also has to check the layout, path representation, coverage and applicable manifests. The historical date is that of the specification, while the runtime used to check the example is modern.
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
const root = await mkdtemp(join(tmpdir(), 'yg-bag-'));
const sha = data => createHash('sha256').update(data).digest('hex');
try {
const payload = Buffer.from('station,reading\n42,19\n');
await mkdir(join(root, 'data'));
await writeFile(join(root, 'bagit.txt'), 'BagIt-Version: 1.0\nTag-File-Character-Encoding: UTF-8\n');
await writeFile(join(root, 'data', 'readings.csv'), payload);
await writeFile(join(root, 'manifest-sha256.txt'), sha(payload)+' data/readings.csv\n');
const verifyFixture = async () => {
const row = (await readFile(join(root, 'manifest-sha256.txt'), 'utf8')).trim();
const match = row.match(/^([a-f0-9]{64}) data\/readings\.csv$/u);
assert(match, 'unexpected fixture manifest');
return match[1] === sha(await readFile(join(root, 'data', 'readings.csv')));
};
assert.equal(await verifyFixture(), true);
const modified = Buffer.from('station,reading\n42,18\n');
assert.equal(modified.length, payload.length);
await writeFile(join(root, 'data', 'readings.csv'), modified);
assert.equal(await verifyFixture(), false);
console.log(process.version, JSON.stringify({bytes: payload.length, original: true, sameSizeEdit: false, sha256: sha(payload)}));
} finally {
await rm(join(root, 'data', 'readings.csv'), { force: true });
await rm(join(root, 'bagit.txt'), { force: true });
await rm(join(root, 'manifest-sha256.txt'), { force: true });
const { rmdir }=await import('node:fs/promises');
await rmdir(join(root,'data'));
await rmdir(root);
}
Verify at the handoff
At the sender, finish writing the CSV before calculating its digest. If the file changes between those operations, the archive could receive a different version from the one described by the manifest. At the receiver, open the package in a staging location and compare the paths and bytes before marking the delivery accepted. Report the concrete failing path and the expected and calculated values. If a file needs to be sent again, retrieve the producer's authoritative copy. Recalculating the manifest from damaged received bytes and treating the result as a repaired delivery defeats the purpose of the check. For a large file a production validator can stream it through the digest calculation. The resulting acceptance decision should still account for each applicable entry in the package.
Integrity, provenance and measurement accuracy
A matching checksum shows that bytes agree with the recorded digest. It does not identify the sender or prove that the station was measuring correctly. An actor able to change both a payload and its manifest could construct a different, internally consistent bag. A receiving process can compare its expected manifest against a copy obtained over a trusted channel, or separately verify a signed delivery receipt. That is a different check from calculating the digest again. A miscalibrated instrument can also produce incorrect values that are transported without any corruption. The receiving record can track file integrity, origin and measurement quality separately so an issue found later can be traced to the appropriate part of the process.
Classify missing and altered payloads separately
A production verifier should distinguish an unreadable manifest, a missing payload and a digest mismatch. Include the delivery and path in each receipt. An unexpected extra file should not become a measurement merely because it has a CSV extension. This gives the field team a clear clue about whether to regenerate the manifest, resend a file from the original package or investigate an unexplained modification.
Acceptance cases for a real archive
Test the delivery process using a short, known station file first. Check the successful case, a missing payload, an additional unlisted file and the same-length modification shown here. Include a retry so that receiving the same delivery twice does not create duplicate archive records. Save the delivery identifier and validation timestamp, then test that a backed-up package can still be opened and verified. Preserve the original bag when normalizing its CSV to a new schema. The normalized result can have its own identifier and manifest, which makes it possible to tell a processing regression from damage to the original delivery. Keep enough evidence to explain what was accepted and how that decision can be repeated.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment