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 (claude): tighten Pinterest host gate + bound binary-fetch memory

[._.]/ Adam Eivy63f59e062b3d
diff --git a/server/lib/pinterestFeed.js b/server/lib/pinterestFeed.js
index 91537007c8..041d24b246 100644
--- a/server/lib/pinterestFeed.js
+++ b/server/lib/pinterestFeed.js
@@ -16,8 +16,28 @@
import { ServerError } from './errorHandler.js';
-// pinterest.com, www.pinterest.com, pinterest.co.uk, br.pinterest.com, …
-const PINTEREST_HOST_RE = /(^|\.)pinterest\.[a-z]{2,}(\.[a-z]{2,})?$/i;
+// Two-part public suffixes Pinterest's country domains use, so the registrable
+// label sits one level deeper (pinterest.co.uk → "pinterest" is third-from-last).
+const TWO_PART_TLDS = new Set([
+ 'co.uk', 'com.au', 'co.nz', 'com.br', 'com.mx', 'co.jp', 'co.kr', 'com.tr',
+]);
+
+// `pinterest` must be the REGISTRABLE domain, not merely a subdomain LABEL of an
+// attacker host. A plain `pinterest.<tld>` and `pinterest.<sub>.<tld>` are
+// structurally identical (`pinterest.co.uk` vs `pinterest.evil.com`), so a regex
+// can't tell them apart without knowing the public suffix — hence the explicit
+// two-part-TLD set. Accepts pinterest.com, www.pinterest.com, br.pinterest.com,
+// pinterest.co.uk; rejects pinterest.evil.com, pinterest.com.evil.com,
+// evilpinterest.com.
+function isPinterestHost(host) {
+ const labels = host.toLowerCase().split('.');
+ if (labels.length < 2) return false;
+ const lastTwo = labels.slice(-2).join('.');
+ if (TWO_PART_TLDS.has(lastTwo)) {
+ return labels.length >= 3 && labels[labels.length - 3] === 'pinterest';
+ }
+ return labels[labels.length - 2] === 'pinterest';
+}
const badUrl = (msg) => new ServerError(msg, { status: 400, code: 'INVALID_PINTEREST_URL' });
@@ -37,7 +57,7 @@ export function normalizePinterestFeedUrl(input) {
throw badUrl('Pinterest URL must be http(s)');
}
const host = u.hostname.toLowerCase();
- if (!PINTEREST_HOST_RE.test(host)) {
+ if (!isPinterestHost(host)) {
throw badUrl('That doesn’t look like a Pinterest board URL');
}
// A board lives at /<user>/<board>/ and its feed at /<user>/<board>.rss.
diff --git a/server/lib/pinterestFeed.test.js b/server/lib/pinterestFeed.test.js
index e9e1c97754..9f24502cb0 100644
--- a/server/lib/pinterestFeed.test.js
+++ b/server/lib/pinterestFeed.test.js
@@ -26,16 +26,24 @@ describe('normalizePinterestFeedUrl', () => {
.toBe('https://pinterest.com/jane/board.rss');
});
- it('accepts country-code Pinterest hosts', () => {
+ it('accepts a country subdomain of pinterest.com', () => {
expect(normalizePinterestFeedUrl('https://br.pinterest.com/jane/board/').feedUrl)
.toBe('https://br.pinterest.com/jane/board.rss');
});
+ it('accepts a two-part-TLD Pinterest domain (pinterest.co.uk)', () => {
+ expect(normalizePinterestFeedUrl('https://pinterest.co.uk/jane/board/').feedUrl)
+ .toBe('https://pinterest.co.uk/jane/board.rss');
+ });
+
it.each([
['empty', ''],
['not a url', 'not a url'],
['non-pinterest host', 'https://example.com/jane/board'],
- ['pinterest look-alike', 'https://pinterest.com.evil.com/jane/board'],
+ ['pinterest look-alike (suffix)', 'https://pinterest.com.evil.com/jane/board'],
+ ['pinterest as attacker subdomain label', 'https://pinterest.evil.com/jane/board'],
+ ['pinterest as attacker subdomain under ccTLD', 'https://pinterest.evil.co.uk/jane/board'],
+ ['embedded brand name', 'https://evilpinterest.com/jane/board'],
['ftp scheme', 'ftp://pinterest.com/jane/board'],
['root path only', 'https://www.pinterest.com/'],
])('rejects %s with a 400', (_label, input) => {
diff --git a/server/lib/safeUrlFetch.js b/server/lib/safeUrlFetch.js
index 3f89f0b745..cd60b13d20 100644
--- a/server/lib/safeUrlFetch.js
+++ b/server/lib/safeUrlFetch.js
@@ -84,16 +84,44 @@ export async function fetchPublicText(url, { timeoutMs, headers } = {}) {
/**
* Fetch a URL's body as a Buffer. Returns `{ buffer, contentType }` on a 2xx
- * within the size cap, or null on any failure. The size cap is enforced first
- * via Content-Length (cheap early-out) and again after the body is read (a
- * server can lie about / omit the header).
+ * within the size cap, or null on any failure. The cap is enforced first via
+ * Content-Length (cheap early-out) and then bounds PEAK MEMORY by streaming the
+ * body and aborting the moment accumulated bytes exceed it — so a server that
+ * omits or lies about Content-Length can't stream gigabytes into memory before a
+ * post-read check fires. Falls back to a buffered read only when the response
+ * exposes no stream (e.g. a test double).
*/
export async function fetchPublicBinary(url, { timeoutMs, headers, maxBytes = DEFAULT_MAX_BYTES } = {}) {
const res = await fetchGuarded(url, { timeoutMs, headers });
if (!res?.ok) return null;
+ const contentType = res.headers.get('content-type') || '';
const declared = Number(res.headers.get('content-length'));
if (maxBytes && Number.isFinite(declared) && declared > maxBytes) return null;
- const buffer = Buffer.from(await res.arrayBuffer());
- if (maxBytes && buffer.byteLength > maxBytes) return null;
- return { buffer, contentType: res.headers.get('content-type') || '' };
+
+ if (!res.body || typeof res.body.getReader !== 'function') {
+ // No readable stream (some runtimes / test doubles) — fall back to a
+ // buffered read, still capped post-read.
+ const buffer = Buffer.from(await res.arrayBuffer());
+ if (maxBytes && buffer.byteLength > maxBytes) return null;
+ return { buffer, contentType };
+ }
+
+ const reader = res.body.getReader();
+ const chunks = [];
+ let total = 0;
+ try {
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ total += value.byteLength;
+ if (maxBytes && total > maxBytes) {
+ await reader.cancel();
+ return null;
+ }
+ chunks.push(Buffer.from(value));
+ }
+ } finally {
+ try { reader.releaseLock(); } catch { /* already released by cancel() */ }
+ }
+ return { buffer: Buffer.concat(chunks), contentType };
}
diff --git a/server/lib/safeUrlFetch.test.js b/server/lib/safeUrlFetch.test.js
index 7112fa3b61..31a8965f51 100644
--- a/server/lib/safeUrlFetch.test.js
+++ b/server/lib/safeUrlFetch.test.js
@@ -86,8 +86,28 @@ describe('fetchPublicText', () => {
});
});
+// Streaming response double — getReader() yields the given Uint8Array chunks.
+const streamRes = (chunks, { headers = {}, ok = true, status = 200 } = {}) => {
+ let i = 0;
+ const cancel = vi.fn(async () => {});
+ return {
+ ok,
+ status,
+ headers: new Map(Object.entries(headers)),
+ arrayBuffer: async () => new ArrayBuffer(0),
+ body: {
+ getReader: () => ({
+ read: async () => (i < chunks.length ? { done: false, value: chunks[i++] } : { done: true, value: undefined }),
+ cancel,
+ releaseLock: () => {},
+ }),
+ _cancel: cancel,
+ },
+ };
+};
+
describe('fetchPublicBinary', () => {
- it('returns the buffer + content-type within the cap', async () => {
+ it('returns the buffer + content-type from a non-streaming response (fallback)', async () => {
const bytes = new Uint8Array([1, 2, 3]).buffer;
fetchMock.mockResolvedValue(res({ body: bytes, headers: { 'content-type': 'image/jpeg' } }));
const out = await fetchPublicBinary('https://i.pinimg.com/736x/x.jpg');
@@ -99,4 +119,17 @@ describe('fetchPublicBinary', () => {
fetchMock.mockResolvedValue(res({ headers: { 'content-length': String(99 * 1024 * 1024) } }));
expect(await fetchPublicBinary('https://i.pinimg.com/x.jpg', { maxBytes: 1024 })).toBeNull();
});
+ it('streams a chunked body and concatenates within the cap', async () => {
+ const r = streamRes([new Uint8Array([1, 2]), new Uint8Array([3, 4])], { headers: { 'content-type': 'image/png' } });
+ fetchMock.mockResolvedValue(r);
+ const out = await fetchPublicBinary('https://i.pinimg.com/x.png', { maxBytes: 1024 });
+ expect(out.buffer.length).toBe(4);
+ expect(out.contentType).toBe('image/png');
+ });
+ it('aborts a no-Content-Length body that exceeds the cap (bounds peak memory)', async () => {
+ const r = streamRes([new Uint8Array(600), new Uint8Array(600)]); // 1200 > cap, no content-length
+ fetchMock.mockResolvedValue(r);
+ expect(await fetchPublicBinary('https://evil.example.com/x', { maxBytes: 1024 })).toBeNull();
+ expect(r.body._cancel).toHaveBeenCalled(); // stream was cancelled rather than fully read
+ });
});