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 (codex): explicit Pinterest domain allowlist + mid-sync link guard

[._.]/ Adam Eivy674642b03807
diff --git a/server/lib/pinterestFeed.js b/server/lib/pinterestFeed.js
index 041d24b246..b68ecdee0d 100644
--- a/server/lib/pinterestFeed.js
+++ b/server/lib/pinterestFeed.js
@@ -16,27 +16,30 @@
import { ServerError } from './errorHandler.js';
-// 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',
+// Explicit allow-list of Pinterest's REGISTRABLE domains. A registrable-label
+// check ("second-to-last label is pinterest") is NOT enough: an attacker can
+// register `pinterest.zip`, `pinterest.app`, etc. (real TLDs), and the importer
+// would then fetch/download from an arbitrary host the SSRF guard otherwise
+// permits (it allows public hosts by design). Pinterest's RSS feeds all live on
+// `pinterest.com`; the country domains redirect to it (and safeUrlFetch follows
+// one validated redirect), but accept them so a pasted ccTLD board URL still
+// works. A host matches when it equals one of these or is a subdomain of one.
+const PINTEREST_DOMAINS = Object.freeze([
+ 'pinterest.com',
+ 'pinterest.co.uk', 'pinterest.ca', 'pinterest.fr', 'pinterest.de',
+ 'pinterest.es', 'pinterest.it', 'pinterest.pt', 'pinterest.nl',
+ 'pinterest.se', 'pinterest.dk', 'pinterest.at', 'pinterest.ch',
+ 'pinterest.ie', 'pinterest.nz', 'pinterest.co.nz', 'pinterest.ph',
+ 'pinterest.jp', 'pinterest.co.jp', 'pinterest.co.kr', 'pinterest.cl',
+ 'pinterest.com.au', 'pinterest.com.mx',
]);
-// `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,
+// Accepts pinterest.com, www.pinterest.com, br.pinterest.com, pinterest.co.uk;
+// rejects pinterest.zip, 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 h = host.toLowerCase();
+ return PINTEREST_DOMAINS.some((d) => h === d || h.endsWith(`.${d}`));
}
const badUrl = (msg) => new ServerError(msg, { status: 400, code: 'INVALID_PINTEREST_URL' });
diff --git a/server/lib/pinterestFeed.test.js b/server/lib/pinterestFeed.test.js
index 9f24502cb0..4e82d51f17 100644
--- a/server/lib/pinterestFeed.test.js
+++ b/server/lib/pinterestFeed.test.js
@@ -43,6 +43,7 @@ describe('normalizePinterestFeedUrl', () => {
['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'],
+ ['attacker-registered pinterest.<tld> (not a real Pinterest domain)', 'https://pinterest.zip/jane/board'],
['embedded brand name', 'https://evilpinterest.com/jane/board'],
['ftp scheme', 'ftp://pinterest.com/jane/board'],
['root path only', 'https://www.pinterest.com/'],
diff --git a/server/services/moodBoard/db.js b/server/services/moodBoard/db.js
index 32e880c7f3..7ac55a8083 100644
--- a/server/services/moodBoard/db.js
+++ b/server/services/moodBoard/db.js
@@ -251,12 +251,14 @@ export async function clearPinterestLink(id) {
}
// Append the freshly-downloaded pins in ONE locked write (rather than N
-// addBoardItem round-trips) and stamp lastSyncedAt. Always persists so a
-// zero-new sync still records the check. Returns { board, added }.
+// addBoardItem round-trips) and stamp lastSyncedAt. Persists on every real sync
+// so a zero-new sync still records the check — but skips the write entirely when
+// appendPinterestPins aborts (the link changed mid-sync, see opts.expectedFeedUrl).
+// Returns { board, added, aborted }.
export async function appendPinterestItems(id, imported, opts = {}) {
const { board, result } = await withLockedBoard(id, (b) => {
- const { board: next, added } = appendPinterestPins(b, imported, opts);
- return { board: next, result: { added } };
+ const { board: next, added, aborted } = appendPinterestPins(b, imported, opts);
+ return { board: next, result: { added, aborted }, skipPersist: aborted };
});
return { board, ...result };
}
diff --git a/server/services/moodBoard/logic.js b/server/services/moodBoard/logic.js
index ca3ba78969..60b875e680 100644
--- a/server/services/moodBoard/logic.js
+++ b/server/services/moodBoard/logic.js
@@ -247,16 +247,19 @@ export function applyBoardRestore(board, patch) {
// schema-version bump: full-record LWW, and an older peer preserves the unknown
// key). `lastSyncedAt` tracks the most recent manual "Sync now".
-// Set (or replace) the Pinterest link, preserving any prior sync timestamp so
-// re-linking the SAME board doesn't lose "last synced" context.
+// Set (or replace) the Pinterest link. Preserve the prior sync timestamp only
+// when re-linking the SAME feed; pointing the board at a DIFFERENT Pinterest
+// board clears lastSyncedAt, otherwise the UI would claim the new board was
+// "last synced" at the old board's time before any sync has run.
export function applyPinterestLink(board, { feedUrl, boardUrl }) {
const prior = board.pinterest || {};
+ const sameFeed = prior.feedUrl === feedUrl;
return {
...board,
pinterest: {
feedUrl,
boardUrl: boardUrl ?? null,
- lastSyncedAt: prior.lastSyncedAt ?? null,
+ lastSyncedAt: sameFeed ? (prior.lastSyncedAt ?? null) : null,
},
updatedAt: nowIso(),
};
@@ -280,9 +283,18 @@ export function clearPinterestLinkRecord(board) {
* before downloading) and respects MAX_ITEMS_PER_BOARD by truncating to the
* remaining capacity rather than throwing — a partial import beats a hard
* mid-sync failure. Always re-stamps `pinterest.lastSyncedAt` (even on zero new)
- * so the UI reflects the check. Returns `{ board, added }`.
+ * so the UI reflects the check. Returns `{ board, added, aborted }`.
+ *
+ * `expectedFeedUrl` guards a single-user re-entrancy race: the feed fetch +
+ * image downloads run OUTSIDE the row lock, so the user can unlink or re-link
+ * the board mid-sync. When the persisted feed no longer matches the one this
+ * sync fetched, abort — append no pins and DON'T stamp lastSyncedAt — so stale
+ * pins never land on a board that was unlinked/repointed.
*/
-export function appendPinterestPins(board, imported, { syncedAt = nowIso() } = {}) {
+export function appendPinterestPins(board, imported, { syncedAt = nowIso(), expectedFeedUrl = null } = {}) {
+ if (expectedFeedUrl && board.pinterest?.feedUrl !== expectedFeedUrl) {
+ return { board, added: 0, aborted: true };
+ }
const items = Array.isArray(board.items) ? board.items : [];
const seen = new Set(items.map((it) => it && it.source).filter(Boolean));
const capacity = Math.max(0, MAX_ITEMS_PER_BOARD - items.length);
@@ -302,5 +314,5 @@ export function appendPinterestPins(board, imported, { syncedAt = nowIso() } = {
pinterest: { ...(board.pinterest || {}), lastSyncedAt: syncedAt },
updatedAt: nowIso(),
};
- return { board: next, added: fresh.length };
+ return { board: next, added: fresh.length, aborted: false };
}
diff --git a/server/services/moodBoard/logic.test.js b/server/services/moodBoard/logic.test.js
index 4381877205..3ef74db5ef 100644
--- a/server/services/moodBoard/logic.test.js
+++ b/server/services/moodBoard/logic.test.js
@@ -248,10 +248,16 @@ describe('applyPinterestLink', () => {
expect(next.updatedAt).not.toBe('t0');
});
- it('preserves the prior sync timestamp when re-linking', () => {
- const linked = { ...base(), pinterest: { feedUrl: 'old', boardUrl: 'oldb', lastSyncedAt: 's1' } };
- const next = applyPinterestLink(linked, { feedUrl: 'new', boardUrl: 'newb' });
- expect(next.pinterest).toMatchObject({ feedUrl: 'new', lastSyncedAt: 's1' });
+ it('preserves the prior sync timestamp when re-linking the SAME feed', () => {
+ const linked = { ...base(), pinterest: { feedUrl: 'f.rss', boardUrl: 'oldb', lastSyncedAt: 's1' } };
+ const next = applyPinterestLink(linked, { feedUrl: 'f.rss', boardUrl: 'newb' });
+ expect(next.pinterest).toMatchObject({ feedUrl: 'f.rss', lastSyncedAt: 's1' });
+ });
+
+ it('clears the sync timestamp when re-linking to a DIFFERENT feed', () => {
+ const linked = { ...base(), pinterest: { feedUrl: 'old.rss', boardUrl: 'oldb', lastSyncedAt: 's1' } };
+ const next = applyPinterestLink(linked, { feedUrl: 'new.rss', boardUrl: 'newb' });
+ expect(next.pinterest).toMatchObject({ feedUrl: 'new.rss', lastSyncedAt: null });
});
});
@@ -305,4 +311,19 @@ describe('appendPinterestPins', () => {
const { added } = appendPinterestPins(full, [imp(1), imp(2), imp(3)], { syncedAt: 's1' });
expect(added).toBe(1);
});
+
+ it('aborts (no append, no stamp) when the link changed mid-sync', () => {
+ const b = { ...linked(), pinterest: { feedUrl: 'now.rss', lastSyncedAt: 'prev' } };
+ const { board, added, aborted } = appendPinterestPins(b, [imp(1)], { syncedAt: 's9', expectedFeedUrl: 'stale.rss' });
+ expect(aborted).toBe(true);
+ expect(added).toBe(0);
+ expect(board).toBe(b); // unchanged — no items appended, lastSyncedAt not bumped
+ });
+
+ it('proceeds when expectedFeedUrl matches the current link', () => {
+ const b = { ...linked(), pinterest: { feedUrl: 'now.rss', lastSyncedAt: 'prev' } };
+ const { added, aborted } = appendPinterestPins(b, [imp(1)], { syncedAt: 's9', expectedFeedUrl: 'now.rss' });
+ expect(aborted).toBe(false);
+ expect(added).toBe(1);
+ });
});
diff --git a/server/services/moodBoard/pinterest.js b/server/services/moodBoard/pinterest.js
index 7e81bfde2e..c8b185eec6 100644
--- a/server/services/moodBoard/pinterest.js
+++ b/server/services/moodBoard/pinterest.js
@@ -115,8 +115,14 @@ export async function syncPinterestBoard(boardId) {
for (const r of results) if (r) imported.push(r);
}
+ // Pass the feed we actually fetched so the locked append aborts if the user
+ // unlinked / repointed the board while downloads ran (fetch is outside the lock).
const syncedAt = new Date().toISOString();
- const { board: nextBoard, added } = await store.appendPinterestItems(boardId, imported, { syncedAt });
+ const { board: nextBoard, added, aborted } = await store.appendPinterestItems(boardId, imported, { syncedAt, expectedFeedUrl: feedUrl });
+ if (aborted) {
+ console.log(`📌 Pinterest sync: board ${boardId} aborted — link changed mid-sync`);
+ return { board: nextBoard, added: 0, feedCount: pins.length, aborted: true };
+ }
emitRecordUpdated('moodBoard', boardId);
console.log(`📌 Pinterest sync: board ${boardId} +${added} new (${pins.length} pins in feed)`);
return { board: nextBoard, added, feedCount: pins.length };
diff --git a/server/services/moodBoard/pinterest.test.js b/server/services/moodBoard/pinterest.test.js
index 9dab055e6a..5528e29af9 100644
--- a/server/services/moodBoard/pinterest.test.js
+++ b/server/services/moodBoard/pinterest.test.js
@@ -117,6 +117,28 @@ describe('syncPinterestBoard', () => {
expect(emitRecordUpdated).toHaveBeenCalledWith('moodBoard', 'mb-1');
});
+ it('reports aborted (and does not emit) when the link changed mid-sync', async () => {
+ store.getBoard.mockResolvedValue({ id: 'mb-1', items: [], pinterest: { feedUrl: 'https://www.pinterest.com/j/b.rss' } });
+ net.fetchPublicText.mockResolvedValue(feedXml(PIN1));
+ net.fetchPublicBinary.mockResolvedValue({ buffer: Buffer.from([0xff, 0xd8, 0xff, 0x00]), contentType: 'image/jpeg' });
+ // store reports the locked append aborted (board was unlinked/repointed)
+ store.appendPinterestItems.mockResolvedValue({ board: { id: 'mb-1' }, added: 0, aborted: true });
+
+ const result = await syncPinterestBoard('mb-1');
+ expect(result).toMatchObject({ added: 0, aborted: true });
+ expect(emitRecordUpdated).not.toHaveBeenCalled();
+ });
+
+ it('forwards the fetched feed as expectedFeedUrl to the locked append', async () => {
+ store.getBoard.mockResolvedValue({ id: 'mb-1', items: [], pinterest: { feedUrl: 'https://www.pinterest.com/j/b.rss' } });
+ net.fetchPublicText.mockResolvedValue(feedXml(PIN1));
+ net.fetchPublicBinary.mockResolvedValue({ buffer: Buffer.from([0xff, 0xd8, 0xff, 0x00]), contentType: 'image/jpeg' });
+ store.appendPinterestItems.mockImplementation(async (_id, imported) => ({ board: { id: 'mb-1' }, added: imported.length }));
+
+ await syncPinterestBoard('mb-1');
+ expect(store.appendPinterestItems.mock.calls[0][2]).toMatchObject({ expectedFeedUrl: 'https://www.pinterest.com/j/b.rss' });
+ });
+
it('skips a pin whose download returns a non-image body', async () => {
store.getBoard.mockResolvedValue({ id: 'mb-1', items: [], pinterest: { feedUrl: 'https://www.pinterest.com/j/b.rss' } });
net.fetchPublicText.mockResolvedValue(feedXml(PIN1));