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: verify a browser read landed on the requested territory, and honor the page limit (#3318)

[._.]/ Adam Eivy92a043ea34ce
diff --git a/server/integrations/stackerNews/browserReader.js b/server/integrations/stackerNews/browserReader.js
index de811da96f..19b57df4e5 100644
--- a/server/integrations/stackerNews/browserReader.js
+++ b/server/integrations/stackerNews/browserReader.js
@@ -99,16 +99,31 @@ const ITEMS_EXPRESSION = `(() => {
};
})()`;
-// Every read: pinned navigation, public-address check, origin verification, then
-// tear the tab down. Read tabs are scratch — a five-page sync would otherwise
-// leave five tabs open in the user's browser per territory.
+const comparablePath = (url) => new URL(url).pathname.replace(/\/+$/, '').toLowerCase();
+
+// Origin alone is not enough for a read: Stacker News redirects a renamed or
+// merged territory to a DIFFERENT same-origin territory, and the caller would
+// then file that territory's settings, owner ID, and items under the configured
+// slug. Refuse anything that did not land on the page we asked for.
+const verifyFinalLocation = (requestedUrl, finalUrl) => {
+ verifyFinalOrigin(finalUrl);
+ if (comparablePath(requestedUrl) !== comparablePath(finalUrl)) {
+ throw new Error('Stacker News browser navigation landed on a different page than the one requested');
+ }
+};
+
+// Every read: pinned navigation, public-address check, location verification,
+// then tear the tab down. Read tabs are scratch — a five-page sync would
+// otherwise leave five tabs open in the user's browser per territory.
async function readPage(url, evaluateExpression) {
const page = await navigateToUrlPinned(url, {
verifyRemoteIp: (ip) => !isPrivateAddress(ip),
settleMs: SETTLE_MS,
evaluateExpression,
});
- const originError = await Promise.resolve(page.url).then(verifyFinalOrigin).then(() => null, (error) => error);
+ const originError = await Promise.resolve()
+ .then(() => verifyFinalLocation(url, page.url))
+ .then(() => null, (error) => error);
await closeCdpPage(page.id);
if (originError) throw originError;
// Null means the extractor found no server-rendered payload, or `Runtime.evaluate`
@@ -133,10 +148,10 @@ const nullableString = (value, max) => {
// Re-normalizes what the page returned. The evaluate expression runs inside an
// untrusted document, so its output is treated as remote data, not as a trusted
// projection — the shape below is what the rest of PortOS is allowed to see.
-const normalizeItemsPage = (page) => ({
+const normalizeItemsPage = (page, limit) => ({
cursor: nullableString(page?.cursor, MAX_CURSOR_LENGTH),
items: (Array.isArray(page?.items) ? page.items : [])
- .slice(0, MAX_ITEMS_PER_PAGE)
+ .slice(0, Number.isInteger(limit) && limit > 0 ? Math.min(limit, MAX_ITEMS_PER_PAGE) : MAX_ITEMS_PER_PAGE)
.map((item) => ({
// `boundedString` rejects anything that is not a string or finite number,
// so an id of `true` / `{}` normalizes to '' and the item is dropped below
@@ -167,6 +182,12 @@ export async function readSub(slug) {
const { sub } = await readPage(slugPath(slug), SUB_EXPRESSION);
const name = boundedString(sub?.name, 120);
if (!name) return { sub: null };
+ // The stored `userId` becomes this territory's ownership evidence, which gates
+ // the territory-settings handoff — so a payload naming a different territory
+ // is refused rather than filed under the requested slug.
+ if (name.toLowerCase() !== slug.trim().toLowerCase()) {
+ throw new Error(`Stacker News returned territory ~${name} for a different requested territory`);
+ }
return {
sub: {
name,
@@ -181,15 +202,17 @@ export async function readSub(slug) {
};
}
-export async function readItems(slug, cursor = null) {
+// `limit` mirrors the GraphQL transport's page bound so both transports ingest
+// the same number of items per page, even though the SSR page size is fixed.
+export async function readItems(slug, cursor = null, limit = null) {
const { items } = await readPage(itemsUrl(slug, cursor), ITEMS_EXPRESSION);
- return { items: normalizeItemsPage(items) };
+ return { items: normalizeItemsPage(items, limit) };
}
const BROWSER_READS = Object.freeze({
me: () => readMe(),
sub: ({ name }) => readSub(name),
- items: ({ sub, cursor = null }) => readItems(sub, cursor),
+ items: ({ sub, cursor = null, limit = null }) => readItems(sub, cursor, limit),
});
/**
diff --git a/server/integrations/stackerNews/browserReader.test.js b/server/integrations/stackerNews/browserReader.test.js
index 5f86b7375b..b9106c618b 100644
--- a/server/integrations/stackerNews/browserReader.test.js
+++ b/server/integrations/stackerNews/browserReader.test.js
@@ -77,7 +77,8 @@ describe('Stacker News browser read transport', () => {
// A failed extraction must never look like a quiet empty page: sync would
// clear last_error and advance last_sync_at while ingesting nothing.
it('throws when the page yielded no extractable payload instead of reporting an empty page', async () => {
- navigateToUrlPinned.mockResolvedValue(page('https://stacker.news/~example/recent', null));
+ // Echo the requested URL back so only the missing payload can fail the read.
+ navigateToUrlPinned.mockImplementation(async (url) => page(url, null));
await expect(readItems('example')).rejects.toThrow('Could not read Stacker News data');
await expect(readSub('example')).rejects.toThrow('Could not read Stacker News data');
await expect(readMe()).rejects.toThrow('Could not read Stacker News data');
@@ -109,6 +110,35 @@ describe('Stacker News browser read transport', () => {
expect(closeCdpPage).toHaveBeenCalledWith('page-1');
});
+ // Stacker News redirects a renamed/merged territory to a different same-origin
+ // one; filing that page under the configured slug would store another
+ // territory's owner ID as this territory's ownership evidence.
+ it('refuses a same-origin redirect to a different territory', async () => {
+ navigateToUrlPinned.mockResolvedValue(page('https://stacker.news/~other/recent', { items: { cursor: null, items: [] } }));
+ await expect(readItems('example')).rejects.toThrow('different page than the one requested');
+ expect(closeCdpPage).toHaveBeenCalledWith('page-1');
+ });
+
+ it('tolerates a trailing-slash or case difference in the landed URL', async () => {
+ navigateToUrlPinned.mockResolvedValue(page('https://stacker.news/~Example/recent/?nodata=1', { items: { cursor: null, items: [] } }));
+ await expect(readItems('example')).resolves.toEqual({ items: { cursor: null, items: [] } });
+ });
+
+ it('refuses a payload naming a territory other than the one requested', async () => {
+ navigateToUrlPinned.mockResolvedValue(page('https://stacker.news/~example/recent', { sub: { name: 'other', userId: '42' } }));
+ await expect(readSub('example')).rejects.toThrow('for a different requested territory');
+ });
+
+ it('bounds a page to the caller-requested limit, matching the GraphQL transport', async () => {
+ const items = Array.from({ length: 40 }, (_, index) => ({ id: index + 1, user: { name: 'example_artist' } }));
+ navigateToUrlPinned.mockResolvedValue(page('https://stacker.news/~example/recent', { items: { cursor: null, items } }));
+ await expect(executeStackerNewsBrowserRead('items', { sub: 'example', limit: 30 })).resolves.toMatchObject({ items: { items: expect.any(Array) } });
+ const bounded = await executeStackerNewsBrowserRead('items', { sub: 'example', limit: 30 });
+ expect(bounded.items.items).toHaveLength(30);
+ const unbounded = await readItems('example');
+ expect(unbounded.items.items).toHaveLength(40);
+ });
+
it('rejects a territory name or cursor the closed registry does not allow', async () => {
await expect(readSub('../../etc/passwd')).rejects.toThrow('Invalid Stacker News territory name');
await expect(readItems('example', 'x'.repeat(401))).rejects.toThrow('Invalid Stacker News cursor');