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: backticks, quoted keys, strict port coercion

[._.]/ Adam Eivy020b8ddcabb1
diff --git a/server/services/apps.js b/server/services/apps.js
index 69f76d9047..c21e4b5581 100644
--- a/server/services/apps.js
+++ b/server/services/apps.js
@@ -437,8 +437,11 @@ export async function getReservedPorts() {
const ports = new Set();
const addPort = (p) => {
- const n = typeof p === 'number' ? p : parseInt(p, 10);
- if (Number.isInteger(n) && n > 0) ports.add(n);
+ let n = null;
+ if (typeof p === 'number' && Number.isInteger(p)) n = p;
+ // Strict /^\d+$/ rather than parseInt — '5565abc' should not coerce to 5565.
+ else if (typeof p === 'string' && /^\d+$/.test(p)) n = Number(p);
+ if (n !== null && n >= 1 && n <= 65535) ports.add(n);
};
for (const app of apps) {
diff --git a/server/services/apps.test.js b/server/services/apps.test.js
index a426966253..e2515e1128 100644
--- a/server/services/apps.test.js
+++ b/server/services/apps.test.js
@@ -87,4 +87,31 @@ describe('getReservedPorts', () => {
expect(reserved).not.toContain(0);
expect(reserved.every(p => Number.isInteger(p) && p > 0)).toBe(true);
});
+
+ it('rejects garbage strings (e.g. "5565abc") and out-of-range integers', async () => {
+ // parseInt-style coercion would silently accept `'5565abc'` as 5565.
+ // Strict /^\\d+$/ + range check is what keeps a hand-edited apps.json from
+ // smuggling a bogus reservation.
+ readJSONFile.mockResolvedValue({
+ apps: {
+ [PORTOS_APP_ID]: { name: 'PortOS' },
+ 'sketchy': {
+ name: 'sketchy',
+ apiPort: '5565abc', // partially-numeric string
+ uiPort: 99999, // above 65535
+ processes: [
+ { name: 'a', ports: { api: 5571, weird: '12.5', neg: -1, big: 70000 } },
+ ],
+ },
+ },
+ });
+
+ const reserved = await getReservedPorts();
+ expect(reserved).toContain(5571);
+ expect(reserved).not.toContain(5565);
+ expect(reserved).not.toContain(99999);
+ expect(reserved).not.toContain(70000);
+ expect(reserved).not.toContain(-1);
+ expect(reserved.every(p => Number.isInteger(p) && p >= 1 && p <= 65535)).toBe(true);
+ });
});
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index ea6575770b..7f48d7b217 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -14,7 +14,9 @@ export const usesPm2 = (type) => !NON_PM2_TYPES.has(type);
/**
* Find the index of the `}` that matches the `{` at `openBraceIdx`, ignoring
- * braces inside strings and comments. Returns -1 if no match.
+ * braces inside strings (single/double/backtick) and JS comments. Returns -1
+ * if no match. Backticks are treated as opaque so `${...}` interpolations
+ * don't perturb the depth count of the surrounding object.
*/
function findMatchingBrace(content, openBraceIdx) {
let depth = 0;
@@ -34,7 +36,7 @@ function findMatchingBrace(content, openBraceIdx) {
if (inBlockComment && char === '*' && nextChar === '/') { inBlockComment = false; i++; continue; }
if (inLineComment || inBlockComment) continue;
- if ((char === '"' || char === "'") && prevChar !== '\\') {
+ if ((char === '"' || char === "'" || char === '`') && prevChar !== '\\') {
if (!inString) { inString = true; stringChar = char; }
else if (char === stringChar) { inString = false; stringChar = null; }
}
@@ -265,7 +267,9 @@ export function parseEcosystemConfig(content) {
const closeIdx = findMatchingBrace(appBlock, openIdx);
if (closeIdx < 0) continue;
const envContent = appBlock.substring(openIdx + 1, closeIdx);
- const portKeyRegex = /\b(PORT|[A-Z][A-Z0-9_]*_PORT)\b\s*:\s*([^,}\n]+)/g;
+ // `['"]?` lets quoted keys like `'PORT': 3000` or `"COINBASE_IPC_PORT": 5565`
+ // match — common in JSON-style ecosystem configs. `\b` still anchors the key.
+ const portKeyRegex = /['"]?\b(PORT|[A-Z][A-Z0-9_]*_PORT)\b['"]?\s*:\s*([^,}\n]+)/g;
let m;
while ((m = portKeyRegex.exec(envContent)) !== null) {
const key = m[1];
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index f88d9be968..1fc6dd9d4c 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -129,6 +129,54 @@ describe('parseEcosystemConfig', () => {
expect(proc.ports).toEqual({ api: 5580 });
});
+ it('captures *_PORT keys when surrounding code uses backtick template literals', () => {
+ // PM2 configs commonly use template literals (script paths, CLI args). The
+ // brace-counter must treat backticks as string delimiters so `${...}` braces
+ // don't perturb depth and miscount the env block close.
+ const content = `
+ module.exports = {
+ apps: [
+ {
+ name: 'tmpl-app',
+ script: \`\${__dirname}/server.js\`,
+ args: \`--port \${5602} --extra \${{x: 1}}\`,
+ env: {
+ PORT: 5602,
+ IPC_PORT: 5603,
+ },
+ },
+ ],
+ };
+ `;
+
+ const { processes } = parseEcosystemConfig(content);
+ const proc = processes.find(p => p.name === 'tmpl-app');
+ expect(proc.ports.api).toBe(5602);
+ expect(proc.ports.ipc).toBe(5603);
+ });
+
+ it('captures *_PORT keys when written with quoted key syntax', () => {
+ // JSON-style ecosystem configs quote env keys.
+ const content = `
+ module.exports = {
+ apps: [
+ {
+ name: 'json-style',
+ env: {
+ 'PORT': 5610,
+ "COINBASE_IPC_PORT": 5611,
+ },
+ },
+ ],
+ };
+ `;
+
+ const { processes } = parseEcosystemConfig(content);
+ const proc = processes.find(p => p.name === 'json-style');
+ expect(proc.ports.api).toBe(5610);
+ expect(proc.ports.coinbaseIpc).toBe(5611);
+ });
+
it('captures *_PORT keys after a nested brace inside the env block', () => {
// Env values can contain object spreads/ternaries that introduce nested `}`.
// A naive `\\{[^}]*\\}` env-block regex would truncate at the inner `}` and