DC-059: Joi validation middleware + schemas for destructive routes
CI / Test & Lint (push) Has been cancelled
CI / Security audit (push) Has been cancelled

[grade=B]

- New src/utilities/validate.js: validateBody(schema) middleware + 9 schemas
  (backupConfigUpdate, backupScheduleCreate, backupRestore, backupRestoreFile,
   appDeploy, appRestore, appRevert, assetUpload, logoUpload)
- Uses Joi's authoritative CIDR validator (rejects malformed IPv6 like ::::/64
  that the previous hex/colon regex would have accepted)
- appDeploy.config uses .unknown(true) for forward-compat with template-specific
  fields (sslType, dnsType, plexClaimToken, etc.) — preserves fields the live
  frontend posts, prevents a behavioural regression
- appRestore uses Joi.any().custom() so the empty-body semantics hold under
  middleware stripUnknown (default) — body with extra keys now rejected
- Wired into 8 destructive routes: backups schedule/restore/config, apps
  deploy/restore/revert, assets upload/logo
- Duplicate legacy POST /backups/schedule handler (line 519) marked LEGACY
  with TODO removal note (Express only matches first registration; this
  handler is unreachable under normal routing)
- Removed redundant manual appId check in /backups/schedule (Joi schema
  enforces it)
- Removed unused 'mime' destructure in /assets/favicon (decodeImageData
  validates MIME internally)
- 41 unit tests covering every exported schema + middleware integration
- 1539/1539 Jest tests pass, zero new ESLint warnings
This commit is contained in:
Hermes
2026-08-08 15:39:48 -07:00
parent c1358df0ec
commit a667de7920
8 changed files with 291 additions and 67 deletions
+4 -3
View File
@@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
const { DOCKER } = require('../../src/utilities/constants');
const { ok, validationError, notFound, errorResponse } = require('../../src/utils/responses');
const { validateBody, schemas: valSchemas } = require('../../src/utilities/validate');
const DEFAULT_BACKUP_DIR = process.env.BACKUP_DIR || path.join(__dirname, '..', 'backups');
@@ -36,7 +37,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
* Pulls image, creates container, starts it, recreates Caddy config.
* Skips if container is already running.
*/
router.post('/:appId/restore', asyncHandler(async (req, res) => {
router.post('/:appId/restore', validateBody(valSchemas.appRestore), asyncHandler(async (req, res) => {
const { appId } = req.params;
const services = await servicesStateManager.read();
const service = services.find(s => s.id === appId);
@@ -183,9 +184,9 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
}, 'apps-backup-points'));
// Revert a specific app to a backup file (point-in-time restore)
router.post('/:appId/revert/:filename', asyncHandler(async (req, res) => {
router.post('/:appId/revert/:filename', validateBody(valSchemas.appRevert), asyncHandler(async (req, res) => {
const { appId, filename } = req.params;
const { encryptionKey, restartContainers } = req.body || {};
const { encryptionKey, restartContainers } = req.body;
// Security: prevent path traversal
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {