惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园_首页
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
B
Blog
D
DataBreaches.Net
腾讯CDC
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
月光博客
月光博客
V
V2EX
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
The Cloudflare Blog
博客园 - 叶小钗
Y
Y Combinator Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Our Auditor Asked How We Prove Logs Aren
GrimLabs · 2026-04-27 · via DEV Community

We were 4 months into our SOC 2 audit when the auditor asked a question that stopped us cold: "How do you prove that your audit logs haven't been modified after the fact?"

We looked at each other. Our audit logs were in a PostgreSQL table. Anyone with database access could UPDATE or DELETE rows. We had no mechanism to detect if someone had changed a log entry.

"We trust our engineers" was not the answer the auditor was looking for.

Why Immutability Matters

The whole value of an audit log depends on one thing: trust. If someone can modify the logs, then the logs prove nothing. An employee could delete evidence of unauthorized access. A compromised admin account could alter the record. An insider threat could cover their tracks.

According to NIST SP 800-92, audit log integrity is a fundamental requirement. The guidance specifically states that logs should be protected against unauthorized modification and that integrity checking mechanisms should be employed.

The auditor wasn't being difficult. They were checking for a basic security control that we'd simply never thought about.

The Hash Chain Approach

The standard solution for proving log integrity is a hash chain, sometimes called a blockchain-lite pattern (without the cryptocurrency nonsense). Each log entry includes a hash of its own content plus the hash of the previous entry. This creates a chain where modifying any entry invalidates every subsequent hash.

import { createHash } from 'crypto';

interface AuditEventWithIntegrity {
  id: string;
  eventType: string;
  actor: { id: string; email: string };
  target: { type: string; id: string };
  changes: Record<string, unknown>[];
  timestamp: Date;
  // Integrity fields
  contentHash: string;
  previousHash: string;
  chainHash: string;
}

function computeContentHash(event: Omit<AuditEventWithIntegrity, 'contentHash' | 'previousHash' | 'chainHash'>): string {
  const content = JSON.stringify({
    id: event.id,
    eventType: event.eventType,
    actor: event.actor,
    target: event.target,
    changes: event.changes,
    timestamp: event.timestamp.toISOString(),
  });

  return createHash('sha256').update(content).digest('hex');
}

function computeChainHash(contentHash: string, previousHash: string): string {
  return createHash('sha256')
    .update(contentHash + previousHash)
    .digest('hex');
}

async function appendAuditEvent(
  event: Omit<AuditEventWithIntegrity, 'contentHash' | 'previousHash' | 'chainHash'>
): Promise<AuditEventWithIntegrity> {
  // Get the previous event's chain hash
  const lastEvent = await db.auditEvents.findFirst({
    orderBy: { timestamp: 'desc' },
  });

  const previousHash = lastEvent?.chainHash || 'GENESIS';
  const contentHash = computeContentHash(event);
  const chainHash = computeChainHash(contentHash, previousHash);

  const fullEvent: AuditEventWithIntegrity = {
    ...event,
    contentHash,
    previousHash,
    chainHash,
  };

  await db.auditEvents.create({ data: fullEvent });
  return fullEvent;
}

Enter fullscreen mode Exit fullscreen mode

Now if someone modifies a log entry, recalculating its content hash will produce a different value. And since the next entry's chain hash depends on it, the entire chain after the modified entry becomes invalid.

Verifying the Chain

Having a hash chain is only useful if you actually verify it. This means periodically walking the chain and checking that every hash is consistent.

interface VerificationResult {
  valid: boolean;
  totalEvents: number;
  checkedEvents: number;
  firstInvalidEvent: string | null;
  errors: string[];
}

async function verifyAuditChain(
  tenantId: string,
  startDate?: Date,
  endDate?: Date
): Promise<VerificationResult> {
  const events = await db.auditEvents.findMany({
    where: {
      tenantId,
      timestamp: {
        gte: startDate,
        lte: endDate,
      },
    },
    orderBy: { timestamp: 'asc' },
  });

  const result: VerificationResult = {
    valid: true,
    totalEvents: events.length,
    checkedEvents: 0,
    firstInvalidEvent: null,
    errors: [],
  };

  for (let i = 0; i < events.length; i++) {
    const event = events[i];
    result.checkedEvents++;

    // Verify content hash
    const expectedContentHash = computeContentHash({
      id: event.id,
      eventType: event.eventType,
      actor: event.actor,
      target: event.target,
      changes: event.changes,
      timestamp: event.timestamp,
    });

    if (expectedContentHash !== event.contentHash) {
      result.valid = false;
      result.firstInvalidEvent = event.id;
      result.errors.push(
        `Event ${event.id}: content hash mismatch (content was modified)`
      );
      break;
    }

    // Verify chain hash
    const expectedPreviousHash = i === 0 ? 'GENESIS' : events[i - 1].chainHash;
    if (event.previousHash !== expectedPreviousHash) {
      result.valid = false;
      result.firstInvalidEvent = event.id;
      result.errors.push(
        `Event ${event.id}: chain hash mismatch (event was inserted or removed)`
      );
      break;
    }

    const expectedChainHash = computeChainHash(event.contentHash, event.previousHash);
    if (expectedChainHash !== event.chainHash) {
      result.valid = false;
      result.firstInvalidEvent = event.id;
      result.errors.push(
        `Event ${event.id}: chain hash invalid`
      );
      break;
    }
  }

  return result;
}

Enter fullscreen mode Exit fullscreen mode

Run this daily or weekly. If it ever reports an invalid chain, something has been tampered with and you need to investigate immediately.

Beyond Hash Chains: External Attestation

A hash chain proves that logs haven't been modified since the chain was created. But what if someone replaces the ENTIRE chain? If an attacker has full database access, they could recalculate all hashes from scratch with modified data.

The solution is external attestation. Periodically publish the current chain hash to an external, immutable store:

// Periodic chain state attestation
async function attestChainState(tenantId: string) {
  const lastEvent = await db.auditEvents.findFirst({
    where: { tenantId },
    orderBy: { timestamp: 'desc' },
  });

  if (!lastEvent) return;

  const attestation = {
    tenantId,
    timestamp: new Date().toISOString(),
    lastEventId: lastEvent.id,
    lastChainHash: lastEvent.chainHash,
    eventCount: await db.auditEvents.count({ where: { tenantId } }),
  };

  // Store in an external immutable ledger
  // Options: AWS QLDB, Azure Immutable Blob Storage,
  // or even just a signed email to a compliance address
  await externalLedger.append(attestation);

  // Also useful: publish to a transparency log
  // Similar concept to Certificate Transparency
  await transparencyLog.publish(attestation);
}

Enter fullscreen mode Exit fullscreen mode

AWS QLDB (Quantum Ledger Database) was designed exactly for this use case, though Amazon announced its deprecation in 2025. Azure Immutable Blob Storage and Google Cloud's various immutable storage options are alternatives.

The Simple Version

If all of this feels like overkill, here's the minimum viable integrity setup that most auditors will accept:

  1. Append-only table with no UPDATE/DELETE permissions (remove those from the application's database role)
  2. Content hashes on each event (proves individual events werent modified)
  3. Daily hash chain verification (automated, alerts on failure)
  4. Weekly database backups that can be compared against live data
-- Remove UPDATE and DELETE permissions on the audit table
REVOKE UPDATE, DELETE ON audit_events FROM app_user;
GRANT INSERT, SELECT ON audit_events TO app_user;

-- Only the backup/admin role can access with full permissions
-- And that role should NOT be used by the application

Enter fullscreen mode Exit fullscreen mode

This wont stop a determined attacker with root database access. But it prevents accidental modification, casual tampering, and application-level bugs from corrupting your audit trail. And its usually enough to satisfy a SOC 2 auditor.

What We Did

After the uncomfortable auditor conversation, we implemented the hash chain approach with daily verification and weekly external attestation. It took about two weeks of engineering time.

The auditor was satisfied. More importantly, we now have actual proof that our audit logs haven't been tampered with, not just trust.

The Broader Point

Most developers think about audit logging as a data storage problem. Capture events, store them, query them. But the integrity question is what separates a debug log from a real audit trail.

If you cant prove your logs are unmodified, they're just text files with timestamps. And any auditor, attorney, or regulator worth their salt will point that out.

Build integrity in from the start. Its much easier than retrofitting it after an auditor asks the question you dont have an answer to.