v1.13.4: Standardize all route responses to use response helpers
CI / Test & Lint (push) Has been cancelled
CI / Security audit (push) Has been cancelled

Convert ~160 raw res.json()/res.status().json() calls across 32+ files
to use centralized helpers from src/utils/responses.js (ok, errorResponse,
successMessage, notFound, validationError, forbidden, unauthorized, conflict).

No behavior changes — response shapes are identical. Future schema changes
(e.g., requestId envelope) only need to update one module.

Fix error vs errorResponse signature mismatch in routes/health.js CA cert
endpoint where error(res, message, statusCode) was being called with
errorResponse(res, statusCode, message, extras) argument order.

Files changed: middleware.js, csrf-protection.js, error-handler.js,
license-manager.js, src/app.js, and 27 route files.

Test suite: 755 pass / 4 pre-existing failures (services credential tests).
This commit is contained in:
Hermes
2026-06-11 00:48:13 -07:00
parent 2d394d882d
commit 53680c4c74
40 changed files with 251 additions and 275 deletions
+4 -3
View File
@@ -3,6 +3,7 @@ const yaml = require('js-yaml');
const { DOCKER, REGEX } = require('../../constants');
const { ValidationError } = require('../../errors');
const platformPaths = require('../../platform-paths');
const { ok } = require('../../src/utils/responses');
/**
* Docker Compose import routes
@@ -162,7 +163,7 @@ module.exports = function({ docker, caddy, servicesStateManager, portLockManager
}
const name = (stackName || 'stack').replace(/[^a-zA-Z0-9_-]/g, '').substring(0, 32) || 'stack';
const result = parseCompose(yamlStr, name);
res.json({ success: true, ...result });
ok(res, { ...result });
}, 'compose-import'));
// POST /deploy-compose — deploy parsed services
@@ -300,7 +301,7 @@ module.exports = function({ docker, caddy, servicesStateManager, portLockManager
results.push({ type: 'container', name: svc.name, status: 'skipped', reason: svc.reason });
}
res.json({ success: true, results, stackName: stackName || prefix });
ok(res, { results, stackName: stackName || prefix });
}, 'compose-deploy'));
// DELETE /compose-stack/:stackName — remove an entire stack
@@ -329,7 +330,7 @@ module.exports = function({ docker, caddy, servicesStateManager, portLockManager
});
await servicesStateManager.update(data => { data.services = updated; });
res.json({ success: true, removed, count: removed.length });
ok(res, { removed, count: removed.length });
}, 'compose-stack-delete'));
return router;
+3 -2
View File
@@ -8,6 +8,7 @@ const { exists } = require('../../fs-helpers');
const platformPaths = require('../../platform-paths');
const { ValidationError } = require('../../errors');
const { logError } = require('../../src/utils/logging');
const { ok } = require('../../src/utils/responses');
/**
* Apps deployment routes factory
* @param {Object} deps - Explicit dependencies
@@ -243,9 +244,9 @@ module.exports = function({ docker, caddy, credentialManager, servicesStateManag
if (!template) throw new ValidationError('Invalid app template');
const existingContainer = await helpers.findExistingContainerByImage(template);
if (existingContainer) {
res.json({ success: true, exists: true, container: existingContainer, message: `Found existing ${template.name} container: ${existingContainer.name}` });
ok(res, { exists: true, container: existingContainer, message: `Found existing ${template.name} container: ${existingContainer.name}` });
} else {
res.json({ success: true, exists: false, message: `No existing ${template.name} container found` });
ok(res, { exists: false, message: `No existing ${template.name} container found` });
}
}, 'check-existing'));
+2 -1
View File
@@ -1,6 +1,7 @@
const express = require('express');
const { exists } = require('../../fs-helpers');
const { logError } = require('../../src/utils/logging');
const { ok } = require('../../src/utils/responses');
module.exports = function({
docker, caddy, servicesStateManager, asyncHandler, log, helpers,
@@ -135,7 +136,7 @@ module.exports = function({
results.service = error.message;
}
res.json({ success: true, message: `App ${appId} removal completed`, results });
ok(res, { message: `App ${appId} removal completed`, results });
} catch (error) {
await logError('app-removal', error);
errorResponse(res, 500, ctx.safeErrorMessage(error), { results });
+12 -16
View File
@@ -2,6 +2,7 @@ const express = require('express');
const path = require('path');
const fs = require('fs');
const { DOCKER } = require('../../constants');
const { ok, validationError, notFound, errorResponse } = require('../../src/utils/responses');
const DEFAULT_BACKUP_DIR = process.env.BACKUP_DIR || path.join(__dirname, '..', 'backups');
@@ -47,7 +48,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
}
const result = await restoreService(service);
res.json({ success: true, result });
ok(res, { result });
}, 'apps-restore'));
/**
@@ -59,8 +60,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
const restoreable = services.filter(s => s.deploymentManifest);
if (restoreable.length === 0) {
return res.json({
success: true,
return ok(res, {
message: 'No services have deployment manifests to restore',
results: []
});
@@ -85,8 +85,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
const skipped = results.filter(r => r.status === 'skipped').length;
const failed = results.filter(r => r.status === 'failed').length;
res.json({
success: true,
ok(res, {
message: `Restore complete: ${succeeded} restored, ${skipped} skipped, ${failed} failed`,
results
});
@@ -123,7 +122,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
status.push(entry);
}
res.json({ success: true, services: status });
ok(res, { services: status });
}, 'apps-restore-status'));
// ==================== POINT-IN-TIME RESTORE (BACKUP FILE BASED) ====================
@@ -174,8 +173,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
// Sort by timestamp descending (newest first)
files.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
res.json({
success: true,
ok(res, {
appId,
isBackupFile: true,
files,
@@ -190,12 +188,12 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
// Security: prevent path traversal
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
return res.status(400).json({ success: false, error: 'Invalid filename' });
return validationError(res, 'Invalid filename');
}
const filepath = path.join(DEFAULT_BACKUP_DIR, filename);
if (!fs.existsSync(filepath)) {
return res.status(404).json({ success: false, error: `Backup file not found: ${filename}` });
return notFound(res, `Backup file not found: ${filename}`);
}
try {
@@ -207,7 +205,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
try {
fileData = await backupManager.decryptBackup(fileData, encryptionKey);
} catch (err) {
return res.status(400).json({ success: false, error: 'Failed to decrypt backup: ' + err.message });
return validationError(res, 'Failed to decrypt backup: ' + err.message);
}
}
@@ -264,8 +262,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
// Cleanup temp dir
fs.rmSync(tempDir, { recursive: true, force: true });
res.json({
success: true,
ok(res, {
isBackupFile: true,
restored: {
services: !!restoreData.services,
@@ -277,8 +274,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
} else {
// Preview mode
fs.rmSync(tempDir, { recursive: true, force: true });
res.json({
success: true,
ok(res, {
isBackupFile: true,
preview: true,
filename,
@@ -296,7 +292,7 @@ module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, e
throw err;
}
} catch (err) {
res.status(500).json({ success: false, error: err.message });
errorResponse(res, 500, err.message);
}
}, 'apps-revert'));
+5 -6
View File
@@ -20,6 +20,7 @@ const { exists } = require('../../fs-helpers');
* @returns {express.Router}
*/
const { REGEX } = require('../../constants');
const { ok } = require('../../src/utils/responses');
module.exports = function({
servicesStateManager, asyncHandler, helpers,
@@ -42,8 +43,7 @@ module.exports = function({
// Get available app templates
router.get('/templates', asyncHandler(async (req, res) => {
res.json({
success: true,
ok(res, {
templates: ctx.APP_TEMPLATES,
categories: ctx.TEMPLATE_CATEGORIES,
difficultyLevels: ctx.DIFFICULTY_LEVELS
@@ -58,7 +58,7 @@ module.exports = function({
const { NotFoundError } = require('../../errors');
throw new NotFoundError('App template');
}
res.json({ success: true, template });
ok(res, { template });
}, 'apps-template-detail'));
// Check port availability
@@ -80,7 +80,7 @@ module.exports = function({
const usedPorts = await docker.getUsedPorts();
for (let port = basePort; port < basePort + maxAttempts; port++) {
if (!usedPorts.has(port)) {
res.json({ success: true, suggestedPort: port, basePort });
ok(res, { suggestedPort: port, basePort });
return;
}
}
@@ -170,8 +170,7 @@ module.exports = function({
log.warn('deploy', 'Service update warning', { error: error.message || String(error) });
}
res.json({
success: true,
ok(res, {
message: `Subdomain updated: ${oldSubdomain} -> ${newSubdomain}`,
newUrl: `https://${ctx.buildDomain(newSubdomain)}`,
results