Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(friends): reject a foreign thread cursor and answer 404 for muting an unknown user

nirholas087e4f471697
diff --git a/api/_lib/friends-store.js b/api/_lib/friends-store.js
index f3191b00a..8769ff4ac 100644
--- a/api/_lib/friends-store.js
+++ b/api/_lib/friends-store.js
@@ -203,13 +203,35 @@ export async function listGraph(meId) {
// `beforeId` paginates older history (cursor = the oldest id currently shown).
export async function getThread(meId, otherId, { limit = 50, beforeId = null } = {}) {
const cap = Math.min(Math.max(limit, 1), 100);
- const rows = beforeId
+
+ // Resolve the cursor inside THIS thread before paging on it. Reading
+ // created_at for any id in the table let a caller page against the timestamp
+ // of a message they cannot see, and an id that resolved to no row made the
+ // comparison `created_at < null`, which matches nothing: an unknown, deleted,
+ // or foreign cursor answered "no older messages" instead of saying the cursor
+ // was bad, and the client stopped paginating on a lie.
+ let before = null;
+ if (beforeId) {
+ const [cursor] = await sql`
+ select created_at from direct_messages
+ where id = ${beforeId}
+ and ((sender_id = ${meId} and recipient_id = ${otherId})
+ or (sender_id = ${otherId} and recipient_id = ${meId}))
+ limit 1
+ `;
+ if (!cursor) {
+ throw Object.assign(new Error('That cursor is not part of this thread.'), { status: 400, code: 'bad_cursor' });
+ }
+ before = cursor.created_at;
+ }
+
+ const rows = before
? await sql`
select id, sender_id, recipient_id, body, created_at, read_at
from direct_messages
where ((sender_id = ${meId} and recipient_id = ${otherId})
or (sender_id = ${otherId} and recipient_id = ${meId}))
- and created_at < (select created_at from direct_messages where id = ${beforeId})
+ and created_at < ${before}
order by created_at desc
limit ${cap}
`
@@ -288,6 +310,13 @@ export async function muteUser(meId, targetId) {
if (targetId === meId) {
throw Object.assign(new Error('You cannot mute yourself.'), { status: 400, code: 'self_mute' });
}
+ // user_mutes.muted_id is a foreign key to users(id). Inserting a well-formed
+ // but unknown id therefore raised a 23503 with no `status` on it, which the
+ // endpoint could only report as a 500 (plus a Sentry capture and an ops alert)
+ // for what is really a caller mistake. Resolve the target first and answer 404,
+ // the same contract sendRequest() already gives an unknown target.
+ const [target] = await sql`select id from users where id = ${targetId} and deleted_at is null`;
+ if (!target) throw Object.assign(new Error('User not found.'), { status: 404, code: 'not_found' });
await sql`
insert into user_mutes (muter_id, muted_id) values (${meId}, ${targetId})
on conflict (muter_id, muted_id) do nothing
diff --git a/api/friends/search.js b/api/friends/search.js
index 6cab3b4d9..587360247 100644
--- a/api/friends/search.js
+++ b/api/friends/search.js
@@ -8,6 +8,11 @@ import { clientIp, limits } from '../_lib/rate-limit.js';
import { resolveAccount } from '../_lib/account-auth.js';
import { searchUsers } from '../_lib/friends-store.js';
+// Display names and usernames top out far below this. A term longer than the cap
+// is not a search, it is a large ILIKE pattern pointed at a sequential scan of
+// every account, so it is rejected here rather than handed to Postgres unbounded.
+const MAX_QUERY_LEN = 200;
+
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
@@ -20,6 +25,9 @@ export default wrap(async (req, res) => {
const url = new URL(req.url, 'http://x');
const q = url.searchParams.get('q') || '';
+ if (q.length > MAX_QUERY_LEN) {
+ return error(res, 400, 'bad_query', `keep the search term under ${MAX_QUERY_LEN} characters`);
+ }
const results = await searchUsers(auth.userId, q);
return json(res, 200, { data: { results } });
});
diff --git a/tests/api/friends-messages.test.js b/tests/api/friends-messages.test.js
new file mode 100644
index 000000000..9efc2ce35
Binary files /dev/null and b/tests/api/friends-messages.test.js differ