Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: bound app PM2 log line queries (#9312)

[._.]/ Adam Eivy8219c5682297
diff --git a/server/routes/apps/lifecycle.js b/server/routes/apps/lifecycle.js
index d558f397bf..70110bcac2 100644
--- a/server/routes/apps/lifecycle.js
+++ b/server/routes/apps/lifecycle.js
@@ -15,7 +15,7 @@
*/
import { Router } from 'express';
-import { pm2ProcessNameSchema } from '../../lib/validation.js';
+import { logsQuerySchema, pm2ProcessNameSchema, validateRequest } from '../../lib/validation.js';
import { join, extname } from 'path';
import { tryReadFile, safeJSONParse } from '../../lib/fileUtils.js';
import * as appsService from '../../services/apps.js';
@@ -287,7 +287,7 @@ router.get('/:id/status', loadApp, asyncHandler(async (req, res) => {
// GET /api/apps/:id/logs - Get logs
router.get('/:id/logs', loadApp, asyncHandler(async (req, res) => {
const app = req.loadedApp;
- const lines = parseInt(req.query.lines, 10) || 100;
+ const { lines } = validateRequest(logsQuerySchema.pick({ lines: true }), req.query);
const processName = req.query.process ?? app.pm2ProcessNames?.[0];
if (processName === undefined) {
diff --git a/server/routes/apps/lifecycle.test.js b/server/routes/apps/lifecycle.test.js
index e516e3845d..059cee639a 100644
--- a/server/routes/apps/lifecycle.test.js
+++ b/server/routes/apps/lifecycle.test.js
@@ -512,6 +512,14 @@ describe('Apps Lifecycle Routes', () => {
expect(pm2Service.getLogs).toHaveBeenCalledWith('_worker', 100, undefined);
});
+ it.each(['5000000', '-5'])('rejects out-of-range lines=%s before PM2 reads', async (lines) => {
+ appsService.getAppById.mockResolvedValue({ id: 'app-001', pm2ProcessNames: ['test-app'] });
+ const response = await request(app).get(`/api/apps/app-001/logs?lines=${lines}`);
+ expect(response.status).toBe(400);
+ expect(response.body.code).toBe('VALIDATION_ERROR');
+ expect(pm2Service.getLogs).not.toHaveBeenCalled();
+ });
+
it('should return logs for app process', async () => {
const mockApp = {
id: 'app-001',
diff --git a/server/routes/pm2LogsValidation.test.js b/server/routes/pm2LogsValidation.test.js
new file mode 100644
index 0000000000..1fde2266c9
--- /dev/null
+++ b/server/routes/pm2LogsValidation.test.js
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+import { readdirSync, readFileSync } from 'node:fs';
+import { join } from 'node:path';
+
+const routeFiles = (directory) => readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
+ const filepath = join(directory, entry.name);
+ if (entry.isDirectory()) return routeFiles(filepath);
+ return entry.isFile() && entry.name.endsWith('.js') && !entry.name.endsWith('.test.js') ? [filepath] : [];
+});
+
+describe('PM2 log route validation', () => {
+ it('validates the bounded lines query in every route handler that reads PM2 logs', () => {
+ const routesDirectory = new URL('.', import.meta.url);
+ const files = routeFiles(routesDirectory.pathname);
+ const callSites = files.flatMap((filepath) => {
+ const source = readFileSync(filepath, 'utf8');
+ return [...source.matchAll(/pm2Service\.getLogs\s*\(/g)].map((match) => {
+ const routeStart = source.lastIndexOf('router.get(', match.index);
+ const handlerSource = source.slice(routeStart, match.index);
+ return {
+ file: filepath.slice(routesDirectory.pathname.length),
+ validated: routeStart >= 0 && handlerSource.includes('validateRequest(logsQuerySchema'),
+ };
+ });
+ });
+
+ expect(callSites.length).toBeGreaterThan(0);
+ expect(callSites.filter(({ validated }) => !validated)).toEqual([]);
+ });
+});