Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

How I built a tamper-evident audit trail for e-signed waivers in plain PHP (no framework)

When a gym or trampoline park swaps paper waivers for a tablet, the signature itself is the easy part. The hard part is proving, months later, that the person signed this exact text at this exact time. If a claim ever re

When a gym or trampoline park swaps paper waivers for a tablet, the signature itself is the easy part. The hard part is proving, months later, that the person signed this exact text at this exact time. If a claim ever reaches a lawyer, a PNG of a squiggle proves very little on its own.

I run a small waiver SaaS on shared hosting with vanilla PHP and MySQL, no Composer and no framework. Here is the audit-trail design I use, and the traps it avoids.

What the record has to prove

A signed waiver record should answer four questions without anyone trusting my database:

  1. What did they agree to? The exact waiver text and version, not "waiver #12".
  2. Who signed? Name, email or phone, and for minors, the guardian.
  3. When and from where? Server timestamp in UTC, IP address, user agent.
  4. Has anything changed since? Any edit to the record must be detectable.

The first three are columns. The fourth needs hashing.

Step 1: freeze the waiver text

The obvious first design stores a waiver_id and joins to the current template. The day a customer edits that template, every old signature points at text the signer never saw. So store the rendered text with each signature:

$waiverText = renderWaiver($template, $participant); // final HTML the signer saw
$textHash   = hash('sha256', $waiverText);

Storing the full text costs a few KB per row. That is cheap compared with losing the evidence.

Step 2: hash the whole signing event

Everything the signer submitted goes into one canonical JSON string, and that string gets a SHA-256 hash:

$event = [
    'waiver_text_sha256'   => $textHash,
    'signer_name'          => $name,
    'signer_email'         => $email,
    'guardian_name'        => $guardian ?: null,
    'signature_png_sha256' => hash('sha256', $signaturePng),
    'signed_at_utc'        => gmdate('Y-m-d\TH:i:s\Z'),
    'ip'                   => $_SERVER['REMOTE_ADDR'],
    'user_agent'           => substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 255),
];
ksort($event);
$canonical = json_encode($event, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$eventHash = hash('sha256', $canonical);

ksort matters: the same data must always produce the same string, or your hashes won't verify later.

Step 3: chain the records

A single hash only proves a row matches itself. Someone with database access could edit the row and recompute its hash. So each record also includes the previous record's hash for the same account:

$prev = $db->prepare('SELECT chain_hash FROM signatures WHERE account_id = ? ORDER BY id DESC LIMIT 1');
$prev->execute([$accountId]);
$prevHash  = $prev->fetchColumn() ?: str_repeat('0', 64);
$chainHash = hash('sha256', $prevHash . $eventHash);

Now editing an old record breaks every hash after it. A nightly cron job walks the chain and emails me if anything fails to verify.

Step 4: give the signer a copy

The signer gets an email with a PDF of what they signed (I use FPDF), the event hash and the timestamp. That copy lives outside my system, which is the point: if my data and their copy disagree, the hash shows which one changed.

Step 5: make verification boring

A verification page takes a record ID, rebuilds the canonical JSON from stored fields, and compares hashes. Staff can run it in front of a customer. If it ever says "mismatch", that is a real incident, not a UI bug.

Traps to avoid

  • Trusting client time. Front-desk tablets often have the wrong clock or time zone. Only server UTC goes into the hash.
  • Hashing the base64 signature string. Different encoders produce different strings for the same image. Hash the decoded bytes.
  • Mutable templates. Covered above; it is the worst one.
  • No minor flow. A participant under 18 cannot sign alone in most places, so the form needs a guardian step with its own name and signature in the event.

Is a drawn signature legally valid?

In the US, the ESIGN Act and UETA generally give electronic signatures the same weight as ink, provided the signer intended to sign and the record is kept accurately. That second condition is what this audit trail is for. Laws differ by country and state, so this is not legal advice.

If you'd rather not build this yourself, it's what I ended up packaging into WaiverForm, where customers can sign a waiver online from a link, QR code or kiosk. Either way: freeze the text, hash the event, chain the rows, and give the signer a copy.

Disclosure: I built WaiverForm.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.