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: address review (codex): follow symlinks in stray-file containment and keep tag scans offset-safe

Adam Eivy966b13e4dbb9
diff --git a/server/lib/htmlToText.js b/server/lib/htmlToText.js
index cecc3dc48d..1bd5afaf80 100644
--- a/server/lib/htmlToText.js
+++ b/server/lib/htmlToText.js
@@ -43,8 +43,12 @@ const CONTAINER_OPEN_RE = /<(script|style|head|noscript)\b/gi;
// any sender reaches this): 200 KB of bare `<head>` stalled the loop for
// seconds. Index scanning keeps the old semantics — an unclosed container is
// left in place — in linear time.
+// ASCII-only fold: tag names are ASCII, and a full `toLowerCase()` can change
+// the string length (U+0130 expands), which would skew every index below.
+const asciiLower = (text) => text.replace(/[A-Z]+/g, (run) => run.toLowerCase());
+
function stripContainerElements(html) {
- const lower = html.toLowerCase();
+ const lower = asciiLower(html);
// Next close position per tag, so a run of unclosed opens does not rescan
// to the end for each one (-1 = no close anywhere after the cached search).
const nextClose = new Map();
diff --git a/server/lib/htmlToText.test.js b/server/lib/htmlToText.test.js
index 4b657591d5..62fe689480 100644
--- a/server/lib/htmlToText.test.js
+++ b/server/lib/htmlToText.test.js
@@ -56,6 +56,8 @@ describe('htmlToText on hostile container tags', () => {
expect(htmlToText('<head>a<head>b</head>c')).toBe('c');
expect(htmlToText('<p>before</p><style>unclosed body text')).toBe('before\nunclosed body text');
expect(htmlToText('<SCRIPT>x</script>kept<Style>y</STYLE>')).toBe('kept');
+ // A character whose lowercase form is longer must not shift the offsets.
+ expect(htmlToText('<script>\u0130</script>x')).toBe('x');
});
it('stays linear on a document made of unclosed container tags', () => {
diff --git a/server/services/feeds.js b/server/services/feeds.js
index fca82c8266..6269879e49 100644
--- a/server/services/feeds.js
+++ b/server/services/feeds.js
@@ -74,10 +74,14 @@ async function ensureItemsSorted() {
* never closed it (a 2 KB `<title>` plus spaces took 13 s), and feeds refresh
* on a schedule from servers PortOS does not control.
*/
+// ASCII-only fold keeps `lower` the same length as `xml`; a full toLowerCase()
+// expands characters such as U+0130 and would skew the offsets below.
+const asciiLower = (text) => text.replace(/[A-Z]+/g, (run) => run.toLowerCase());
+
const extractTag = (xml, tag) => {
- const lower = xml.toLowerCase();
- const open = `<${tag.toLowerCase()}`;
- const close = `</${tag.toLowerCase()}>`;
+ const lower = asciiLower(xml);
+ const open = `<${asciiLower(tag)}`;
+ const close = `</${asciiLower(tag)}>`;
for (let from = 0; ; ) {
const start = lower.indexOf(open, from);
if (start === -1) return '';
diff --git a/server/services/feeds.test.js b/server/services/feeds.test.js
index ee4169e774..9d450a0a58 100644
--- a/server/services/feeds.test.js
+++ b/server/services/feeds.test.js
@@ -513,6 +513,7 @@ describe('extractTag', () => {
expect(extractTag('<titles>no</titles><title>yes</title>', 'title')).toBe('yes');
expect(extractTag('<dc:creator>Ann</dc:creator>', 'dc:creator')).toBe('Ann');
expect(extractTag('<title>never closed', 'title')).toBe('');
+ expect(extractTag('<title>\u0130</title>', 'title')).toBe('\u0130');
});
it('does not backtrack on an unclosed tag padded with whitespace', () => {
diff --git a/server/services/pm2Standardizer.js b/server/services/pm2Standardizer.js
index 7039c69a4b..a2b9d72bc3 100644
--- a/server/services/pm2Standardizer.js
+++ b/server/services/pm2Standardizer.js
@@ -1,4 +1,4 @@
-import { readFile, writeFile, readdir } from 'fs/promises';
+import { readFile, writeFile, readdir, realpath } from 'fs/promises';
import { existsSync } from 'fs';
import { join, basename, relative } from 'path';
import { exec, spawn } from '../lib/childProcess.js';
@@ -652,14 +652,16 @@ export async function applyStandardization(
if (stray.action !== 'remove') continue;
// Contain both model-chosen fields at the sink: the file must resolve
- // inside the repository and the variable must be a bare identifier before
- // it becomes a RegExp.
+ // inside the repository (after following symlinks, so a planted `.env`
+ // link cannot aim the write at a host file) and the variable must be a
+ // bare identifier before it becomes a RegExp.
const filePath = join(repoPath, String(stray.file ?? ''));
- if (!isPathInsideDir(repoPath, filePath) || !STRAY_VARIABLE_RE.test(String(stray.variable ?? ''))) {
+ if (!existsSync(filePath)) continue;
+ const [realFile, realRepo] = await Promise.all([realpath(filePath).catch(() => null), realpath(repoPath).catch(() => null)]);
+ if (!realFile || !realRepo || !isPathInsideDir(realRepo, realFile) || !STRAY_VARIABLE_RE.test(String(stray.variable ?? ''))) {
console.warn(`⚠️ PM2 standardizer skipped a stray-port edit outside the plan contract: ${stray.file} / ${stray.variable}`);
continue;
}
- if (!existsSync(filePath)) continue;
const content = await tryReadFile(filePath);
if (!content) continue;
diff --git a/server/services/pm2Standardizer.test.js b/server/services/pm2Standardizer.test.js
index c52e9bb474..846d37cd05 100644
--- a/server/services/pm2Standardizer.test.js
+++ b/server/services/pm2Standardizer.test.js
@@ -1,6 +1,7 @@
import { __testing as standardizerInternals } from './pm2Standardizer.js';
import { mkdtempSync as makeTempDir, writeFileSync as writeFixture, readFileSync as readFixture, rmSync as removeFixture } from 'fs';
import { join as joinPath, relative as relativePath } from 'path';
+import { symlinkSync } from 'fs';
import { tmpdir as osTmpDir } from 'os';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { mkdtempSync, writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
@@ -414,6 +415,8 @@ describe('model-authored plan fields at the apply sink', () => {
try {
writeFixture(joinPath(repo, '.env'), 'PORT=3000\nKEEP=1\n');
writeFixture(joinPath(outside, '.env'), 'PORT=3000\nSECRET=1\n');
+ writeFixture(joinPath(outside, 'linked.env'), 'PORT=3000\nLINKED=1\n');
+ symlinkSync(joinPath(outside, 'linked.env'), joinPath(repo, 'planted.env'));
const escape = relativePath(repo, joinPath(outside, '.env'));
const plan = {
currentState: { hasGit: false },
@@ -422,6 +425,7 @@ describe('model-authored plan fields at the apply sink', () => {
createEcosystem: true,
strayPorts: [
{ file: escape, variable: 'PORT', action: 'remove' },
+ { file: 'planted.env', variable: 'PORT', action: 'remove' },
{ file: '.env', variable: '|', action: 'remove' },
{ file: '.env', variable: 'PORT', action: 'remove' },
],
@@ -429,6 +433,7 @@ describe('model-authored plan fields at the apply sink', () => {
};
const result = await applyStandardization(repo, plan, { skipBackup: true });
expect(readFixture(joinPath(outside, '.env'), 'utf-8')).toBe('PORT=3000\nSECRET=1\n');
+ expect(readFixture(joinPath(outside, 'linked.env'), 'utf-8')).toBe('PORT=3000\nLINKED=1\n');
expect(readFixture(joinPath(repo, '.env'), 'utf-8')).toBe('KEEP=1\n');
expect(result.filesModified).toEqual(['ecosystem.config.cjs', '.env (removed PORT)']);
} finally {