fix(api): 🐛 Fix account lookup address parsing (again)

This commit is contained in:
Jesse Wierzbinski 2024-12-02 15:40:20 +01:00
parent 91da99c934
commit e4768620e2
No known key found for this signature in database
3 changed files with 12 additions and 22 deletions

View file

@ -1,4 +1,4 @@
import { apiRoute, applyConfig, auth, userAddressValidator } from "@/api"; import { apiRoute, applyConfig, auth, parseUserAddress } from "@/api";
import { createRoute } from "@hono/zod-openapi"; import { createRoute } from "@hono/zod-openapi";
import { Instance, User } from "@versia/kit/db"; import { Instance, User } from "@versia/kit/db";
import { RolePermissions, Users } from "@versia/kit/tables"; import { RolePermissions, Users } from "@versia/kit/tables";
@ -71,23 +71,14 @@ export default apiRoute((app) =>
const { user } = context.get("auth"); const { user } = context.get("auth");
// Check if acct is matching format username@domain.com or @username@domain.com // Check if acct is matching format username@domain.com or @username@domain.com
const accountMatches = [...acct.trim().matchAll(userAddressValidator)]; const { username, domain } = parseUserAddress(acct);
if (accountMatches.length === 0) {
return context.json({ error: 'Invalid parameter "acct"' }, 422);
}
const [, username, instanceHost] = accountMatches[0];
if (!username) { if (!username) {
throw new Error("Invalid username"); throw new Error("Invalid username");
} }
// User is local // User is local
if ( if (!domain || domain === new URL(config.http.base_url).host) {
!instanceHost ||
instanceHost === new URL(config.http.base_url).host
) {
const account = await User.fromSql( const account = await User.fromSql(
and(eq(Users.username, username), isNull(Users.instanceId)), and(eq(Users.username, username), isNull(Users.instanceId)),
); );
@ -104,13 +95,10 @@ export default apiRoute((app) =>
// User is remote // User is remote
// Try to fetch it from database // Try to fetch it from database
const instance = await Instance.resolveFromHost(instanceHost); const instance = await Instance.resolveFromHost(domain);
if (!instance) { if (!instance) {
return context.json( return context.json({ error: `Instance ${domain} not found` }, 404);
{ error: `Instance ${instanceHost} not found` },
404,
);
} }
const account = await User.fromSql( const account = await User.fromSql(
@ -127,7 +115,7 @@ export default apiRoute((app) =>
// Fetch from remote instance // Fetch from remote instance
const manager = await (user ?? User).getFederationRequester(); const manager = await (user ?? User).getFederationRequester();
const uri = await User.webFinger(manager, username, instanceHost); const uri = await User.webFinger(manager, username, domain);
const foundAccount = await User.resolve(uri); const foundAccount = await User.resolve(uri);

View file

@ -8,7 +8,7 @@ import {
import { createRoute } from "@hono/zod-openapi"; import { createRoute } from "@hono/zod-openapi";
import { Note, User, db } from "@versia/kit/db"; import { Note, User, db } from "@versia/kit/db";
import { Instances, Notes, RolePermissions, Users } from "@versia/kit/tables"; import { Instances, Notes, RolePermissions, Users } from "@versia/kit/tables";
import { and, eq, inArray, sql } from "drizzle-orm"; import { and, eq, inArray, isNull, sql } from "drizzle-orm";
import { z } from "zod"; import { z } from "zod";
import { searchManager } from "~/classes/search/search-manager"; import { searchManager } from "~/classes/search/search-manager";
import { config } from "~/packages/config-manager"; import { config } from "~/packages/config-manager";
@ -136,7 +136,9 @@ export default apiRoute((app) =>
.where( .where(
and( and(
eq(Users.username, username), eq(Users.username, username),
eq(Instances.baseUrl, domain), domain
? eq(Instances.baseUrl, domain)
: isNull(Users.instanceId),
), ),
) )
)[0]?.id; )[0]?.id;
@ -154,7 +156,7 @@ export default apiRoute((app) =>
); );
} }
if (resolve) { if (resolve && domain) {
const manager = await ( const manager = await (
self ?? User self ?? User
).getFederationRequester(); ).getFederationRequester();

View file

@ -130,7 +130,7 @@ export const parseUserAddress = (
address: string, address: string,
): { ): {
username: string; username: string;
domain: string; domain?: string;
} => { } => {
let output = address; let output = address;
// Remove leading @ if it exists // Remove leading @ if it exists