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

address review: fix 6 Copilot review findings (iteration 4)

Adam Eivy1ef01d62c975
diff --git a/server/routes/dataManager.js b/server/routes/dataManager.js
index 7d190b8ef5..318d47b018 100644
--- a/server/routes/dataManager.js
+++ b/server/routes/dataManager.js
@@ -41,6 +41,12 @@ router.post('/:category/archive', asyncHandler(async (req, res) => {
res.json(result);
}));
+// DELETE /api/data/backups/:filename — delete a backup file (must precede /:category)
+router.delete('/backups/:filename', asyncHandler(async (req, res) => {
+ const result = await deleteBackup(req.params.filename);
+ res.json(result);
+}));
+
// DELETE /api/data/:category — purge a category's contents
router.delete('/:category', asyncHandler(async (req, res) => {
const { subPath } = req.body || {};
@@ -48,10 +54,4 @@ router.delete('/:category', asyncHandler(async (req, res) => {
res.json(result);
}));
-// DELETE /api/data/backups/:filename — delete a backup file
-router.delete('/backups/:filename', asyncHandler(async (req, res) => {
- const result = await deleteBackup(req.params.filename);
- res.json(result);
-}));
-
export default router;
diff --git a/server/services/autobiography.js b/server/services/autobiography.js
index 265c15c221..c37edcac85 100644
--- a/server/services/autobiography.js
+++ b/server/services/autobiography.js
@@ -458,7 +458,8 @@ Return a JSON array of exactly 3 strings, nothing else. Example:
if (result.error) return { error: result.error };
- const followUps = parseLLMJSON(result.text);
+ let followUps;
+ try { followUps = parseLLMJSON(result.text); } catch { /* invalid JSON */ }
if (!Array.isArray(followUps) || followUps.length === 0) {
return { error: 'Failed to parse follow-up questions from AI response' };
}
diff --git a/server/services/feeds.js b/server/services/feeds.js
index 5f26b49d4b..ecde0af4ea 100644
--- a/server/services/feeds.js
+++ b/server/services/feeds.js
@@ -10,6 +10,8 @@
import { randomUUID } from 'crypto';
import { join } from 'path';
+import dns from 'dns/promises';
+import net from 'net';
import { PATHS, createCachedStore } from '../lib/fileUtils.js';
const FEEDS_FILE = join(PATHS.data, 'feeds.json');
@@ -277,38 +279,70 @@ export async function getFeedStats() {
// ─── Internal Helpers ───────────────────────────────────────────────────────
-// Check if a hostname resolves to a private/loopback/link-local IP
-function isPrivateHost(hostname) {
- // Block common private/loopback/link-local patterns (pre-DNS resolution)
- const blocked = ['localhost', '127.0.0.1', '::1', '0.0.0.0', '169.254.169.254'];
- if (blocked.includes(hostname)) return true;
- // Block numeric private ranges: 10.x, 172.16-31.x, 192.168.x, 169.254.x
- const parts = hostname.split('.').map(Number);
- if (parts.length === 4 && parts.every(p => !isNaN(p))) {
+// Check if an IP address is private/loopback/link-local
+function isPrivateIP(ip) {
+ if (!ip) return true;
+ // IPv6 loopback
+ if (ip === '::1' || ip === '::') return true;
+ // IPv4
+ const parts = ip.split('.').map(Number);
+ if (parts.length === 4 && parts.every(p => !isNaN(p) && p >= 0 && p <= 255)) {
if (parts[0] === 10) return true;
if (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) return true;
if (parts[0] === 192 && parts[1] === 168) return true;
if (parts[0] === 169 && parts[1] === 254) return true;
if (parts[0] === 127) return true;
+ if (parts[0] === 0) return true;
}
return false;
}
+// Resolve hostname and verify it doesn't point to a private IP
+async function isHostSafe(hostname) {
+ // Numeric IPs: check directly
+ if (net.isIP(hostname)) return !isPrivateIP(hostname);
+ // DNS resolution: check all resolved addresses
+ const addresses = await dns.resolve4(hostname).catch(() => []);
+ if (addresses.length === 0) return false;
+ return addresses.every(addr => !isPrivateIP(addr));
+}
+
async function fetchFeedXml(url) {
// Restrict to http/https to prevent SSRF via file://, data://, etc.
const parsed = new URL(url);
if (!['http:', 'https:'].includes(parsed.protocol)) return null;
- // Block private/loopback/link-local hosts
- if (isPrivateHost(parsed.hostname)) return null;
+ // Resolve DNS and block private/loopback/link-local IPs (prevents rebinding attacks)
+ if (!await isHostSafe(parsed.hostname)) return null;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
const res = await fetch(url, {
signal: controller.signal,
- redirect: 'follow',
+ redirect: 'manual',
headers: { 'User-Agent': 'PortOS Feed Reader/1.0', Accept: 'application/rss+xml, application/atom+xml, application/xml, text/xml' }
}).catch(() => null);
clearTimeout(timeout);
+
+ // Handle redirects manually to validate each redirect target
+ if (res?.status >= 300 && res?.status < 400) {
+ const location = res.headers.get('location');
+ if (!location) return null;
+ const redirectUrl = new URL(location, url);
+ if (!['http:', 'https:'].includes(redirectUrl.protocol)) return null;
+ if (!await isHostSafe(redirectUrl.hostname)) return null;
+ // Follow the validated redirect
+ const controller2 = new AbortController();
+ const timeout2 = setTimeout(() => controller2.abort(), FETCH_TIMEOUT_MS);
+ const res2 = await fetch(redirectUrl.href, {
+ signal: controller2.signal,
+ redirect: 'error',
+ headers: { 'User-Agent': 'PortOS Feed Reader/1.0', Accept: 'application/rss+xml, application/atom+xml, application/xml, text/xml' }
+ }).catch(() => null);
+ clearTimeout(timeout2);
+ if (!res2?.ok) return null;
+ return res2.text();
+ }
+
if (!res?.ok) return null;
return res.text();
}
diff --git a/server/services/instances.js b/server/services/instances.js
index a701a2591a..8a29568abc 100644
--- a/server/services/instances.js
+++ b/server/services/instances.js
@@ -7,6 +7,7 @@
import { writeFile } from 'fs/promises';
import os from 'os';
+import net from 'net';
import crypto from 'crypto';
import { dataPath, readJSONFile, ensureDir, PATHS } from '../lib/fileUtils.js';
import { createMutex } from '../lib/asyncMutex.js';
@@ -115,7 +116,7 @@ function validName(name, fallback) {
}
function isIPAddress(str) {
- return /^\d+\.\d+\.\d+\.\d+$/.test(str);
+ return net.isIP(str) !== 0;
}
// Default sync categories — all disabled until explicitly enabled per-peer
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 4b074a64f3..6aaa17eee4 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -11,7 +11,7 @@
import { spawn } from 'child_process';
import { existsSync } from 'fs';
-import { readdir, readFile, rm } from 'fs/promises';
+import { readdir, readFile, rm, stat } from 'fs/promises';
import { join } from 'path';
import { ensureDir, PATHS } from '../lib/fileUtils.js';
@@ -322,11 +322,12 @@ export async function cleanupOrphanedWorktrees(sourceWorkspace, activeAgentIds)
const branchName = wt.branch?.replace('refs/heads/', '') || '';
// Attempt merge so committed work from preserved worktrees (e.g., PR/push failures) isn't lost.
// If merge fails, the branch is preserved for manual recovery.
- await removeWorktree(agentId, sourceWorkspace, branchName, { merge: true })
+ const result = await removeWorktree(agentId, sourceWorkspace, branchName, { merge: true })
.catch(err => {
console.log(`⚠️ Failed to clean orphaned worktree ${agentId}: ${err.message}`);
+ return { removed: false };
});
- cleaned++;
+ if (result?.removed) cleaned++;
}
}