Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

6.3 KB
Raw Blame
import { readEnv } from '../../app-config';
import { openDatabase } from '../../db/connection';
import { unknownMigrations } from '../../db/known-migrations';

import fs from 'fs';
import path from 'path';
import unzipper from 'unzipper';

/**
 * Reading a backup archive, without touching the live database.
 *
 * Split out of backup.impl.ts so the restore that runs before the database is
 * opened (boot-restore.ts, #1089) can use the same guards as the one in the
 * admin panel: importing backup.impl.ts opens the database as a side effect,
 * which is exactly what a first-boot restore has to happen before.
 */

// Frozen at import on purpose (legacy timing, see backup.impl.ts).
const backupEnv = readEnv().backup;
export const MAX_BACKUP_UPLOAD_SIZE = backupEnv.uploadLimitMb * 1024 * 1024; // compressed
export const MAX_BACKUP_DECOMPRESSED_SIZE = backupEnv.maxDecompressedMb * 1024 * 1024;

/** A refusal in the restore route's own words and status. */
export interface ArchiveRefusal {
  error: string;
  status: number;
}

/**
 * Unpack an archive into `extractDir`, or say why not.
 *
 * Fast reject on the central-directory's declared size, then extract entry-by-entry
 * enforcing the ACTUAL decompressed bytes. The declared uncompressedSize is
 * attacker-declarable — a zip bomb can under-report it and expand past the cap during
 * extraction — so the real guard counts bytes as they are written and aborts once the
 * running total crosses the cap. Each entry's resolved path is also confined to
 * extractDir (a `../` entry that escaped the root — zip-slip — is refused).
 *
 * A refusal leaves nothing behind. Any other failure is thrown, also with the
 * directory removed.
 */
export async function extractBackupArchive(zipPath: string, extractDir: string): Promise<ArchiveRefusal | null> {
  const directory = await unzipper.Open.file(zipPath);
  const claimedSize = directory.files.reduce((sum, f) => sum + (f.uncompressedSize || 0), 0);
  if (claimedSize > MAX_BACKUP_DECOMPRESSED_SIZE) {
    return { error: 'Backup exceeds the maximum decompressed size.', status: 400 };
  }

  fs.mkdirSync(extractDir, { recursive: true });
  let decompressedBytes = 0;
  for (const entry of directory.files) {
    if (entry.type === 'Directory') continue;
    const dest = path.join(extractDir, entry.path);
    const rel = path.relative(extractDir, dest);
    if (rel.startsWith('..') || path.isAbsolute(rel)) {
      fs.rmSync(extractDir, { recursive: true, force: true });
      return { error: 'Invalid backup: an entry path escapes the archive root.', status: 400 };
    }
    fs.mkdirSync(path.dirname(dest), { recursive: true });
    try {
      await new Promise<void>((resolve, reject) => {
        const source = entry.stream();
        const out = fs.createWriteStream(dest);
        source.on('data', (chunk: Buffer) => {
          decompressedBytes += chunk.length;
          if (decompressedBytes > MAX_BACKUP_DECOMPRESSED_SIZE) {
            source.destroy();
            out.destroy();
            reject(new Error('DECOMPRESSED_CAP_EXCEEDED'));
          }
        });
        source.on('error', reject);
        out.on('error', reject);
        out.on('finish', resolve);
        source.pipe(out);
      });
    } catch (err) {
      fs.rmSync(extractDir, { recursive: true, force: true });
      if (err instanceof Error && err.message === 'DECOMPRESSED_CAP_EXCEEDED') {
        return { error: 'Backup exceeds the maximum decompressed size.', status: 400 };
      }
      throw err;
    }
  }
  return null;
}

/**
 * Whether an unpacked archive holds a TREK database that can be put in place:
 * present, intact, and carrying the tables every TREK has. Read-only; the file
 * is closed again before this returns.
 */
export function checkBackupDatabase(extractDir: string): ArchiveRefusal | null {
  const extractedDb = path.join(extractDir, 'travel.db');
  if (!fs.existsSync(extractedDb)) {
    return { error: 'Invalid backup: travel.db not found', status: 400 };
  }

  let uploadedDb: ReturnType<typeof openDatabase> | null = null;
  try {
    uploadedDb = openDatabase(extractedDb, { readonly: true });

    const integrityResult = uploadedDb.prepare('PRAGMA integrity_check').get() as { integrity_check: string };
    if (integrityResult.integrity_check !== 'ok') {
      return { error: `Uploaded database failed integrity check: ${integrityResult.integrity_check}`, status: 400 };
    }

    const requiredTables = ['users', 'trips', 'trip_members', 'places', 'days'];
    const existingTables = uploadedDb.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as {
      name: string;
    }[];
    const tableNames = new Set(existingTables.map((t) => t.name));
    for (const table of requiredTables) {
      if (!tableNames.has(table)) {
        return {
          error: `Uploaded database is missing required table: ${table}. This does not appear to be a TREK backup.`,
          status: 400,
        };
      }
    }
    // A backup a newer TREK made would be swapped in and then refused at boot
    // (legacy-baseline.ts), leaving the install down. Refuse it here, while the
    // current database is still in place.
    if (tableNames.has('mikro_orm_migrations')) {
      const executed = uploadedDb.prepare('SELECT name FROM mikro_orm_migrations').all() as { name: string }[];
      const unknown = unknownMigrations(executed.map((row) => row.name));
      if (unknown.length > 0) {
        return { error: 'This backup was made by a newer TREK version. Update TREK before restoring it.', status: 400 };
      }
    }
    return null;
  } catch {
    return { error: 'Uploaded file is not a valid SQLite database', status: 400 };
  } finally {
    uploadedDb?.close();
  }
}

/**
 * Leaves `data/.trek-reclaim-reset` (data-paths.ts) before a restore swaps the
 * database in. Storage reclaim removes it once it has dropped the state the
 * swapped database brought (the deletion switch, the confirmations, the marks,
 * the release queue); until then every start does that first. So a restart
 * after a reopen that failed, and a restore at boot, never keep that state,
 * whichever installation the backup came from. Throws when the file cannot be
 * written: the restore then stops before it swaps anything.
 */
export function markReclaimResetPending(file: string): void {
  fs.mkdirSync(path.dirname(file), { recursive: true });
  fs.writeFileSync(file, `${new Date().toISOString()}\n`, { mode: 0o600 });
}