From 51ccf318e8129f1bea9aa725fc2081257f583ab6 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <5238563+Freakazoid182@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:10:21 +0200 Subject: [PATCH] Lowercase domain on db query filters (#3849) (#3873) * Lowercase domain on db query filters (#3849) * Add test to get a community on different cased domain (#3849) * Lowercase the identity for webfinger (#3849) * Lowercase both sides of the domain comparison (#3849) * Format api_tests (#3849) * Lowercase domain lookup on Instance and Person (#3849) --------- Co-authored-by: Freek van Zee Co-authored-by: Freakazoid182 <> --- api_tests/src/community.spec.ts | 18 ++++++++++++++++++ api_tests/src/shared.ts | 11 +++++++++++ crates/apub/src/fetcher/mod.rs | 2 +- crates/apub/src/lib.rs | 4 ++-- crates/db_schema/src/impls/community.rs | 2 +- crates/db_schema/src/impls/instance.rs | 4 ++-- crates/db_schema/src/impls/person.rs | 2 +- 7 files changed, 36 insertions(+), 7 deletions(-) diff --git a/api_tests/src/community.spec.ts b/api_tests/src/community.spec.ts index fd09f8f56..274721d94 100644 --- a/api_tests/src/community.spec.ts +++ b/api_tests/src/community.spec.ts @@ -21,6 +21,7 @@ import { registerUser, API, getPosts, + getCommunityByName, } from "./shared"; beforeAll(async () => { @@ -279,3 +280,20 @@ test("moderator view", async () => { expect(postIds).toContain(alphaPost.post.id); expect(postIds).toContain(otherAlphaPost.post.id); }); + +test("Get community for different casing on domain", async () => { + let communityRes = await createCommunity(alpha); + expect(communityRes.community_view.community.name).toBeDefined(); + + // A dupe check + let prevName = communityRes.community_view.community.name; + await expect(createCommunity(alpha, prevName)).rejects.toBe( + "community_already_exists", + ); + + // Cache the community on beta, make sure it has the other fields + let communityName = `${communityRes.community_view.community.name}@LEMMY-ALPHA:8541`; + let betaCommunity = (await getCommunityByName(beta, communityName)) + .community_view; + assertCommunityFederation(betaCommunity, communityRes.community_view); +}); diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index 9460d896a..6110f1e34 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -557,6 +557,17 @@ export async function getCommunity( return api.client.getCommunity(form); } +export async function getCommunityByName( + api: API, + name: string, +): Promise { + let form: GetCommunity = { + name, + auth: api.auth, + }; + return api.client.getCommunity(form); +} + export async function deleteCommunity( api: API, deleted: boolean, diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index 701d10383..f56f1ccad 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -46,7 +46,7 @@ where Ok(actor?.into()) } else if local_user_view.is_some() { // Fetch the actor from its home instance using webfinger - let actor: ActorType = webfinger_resolve_actor(identifier, context).await?; + let actor: ActorType = webfinger_resolve_actor(&identifier.to_lowercase(), context).await?; Ok(actor) } else { Err(NotFound.into()) diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index d4e34f9e7..31febd7ff 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -84,7 +84,7 @@ fn check_apub_id_valid(apub_id: &Url, local_site_data: &LocalSiteData) -> Result if local_site_data .blocked_instances .iter() - .any(|i| domain.eq(&i.domain)) + .any(|i| domain.to_lowercase().eq(&i.domain.to_lowercase())) { Err(LemmyErrorType::DomainBlocked(domain.clone()))?; } @@ -94,7 +94,7 @@ fn check_apub_id_valid(apub_id: &Url, local_site_data: &LocalSiteData) -> Result && !local_site_data .allowed_instances .iter() - .any(|i| domain.eq(&i.domain)) + .any(|i| domain.to_lowercase().eq(&i.domain.to_lowercase())) { Err(LemmyErrorType::DomainNotInAllowList(domain))?; } diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index bedb9820e..82d2847f4 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -334,7 +334,7 @@ impl ApubActor for Community { community::table .inner_join(instance::table) .filter(lower(community::name).eq(community_name.to_lowercase())) - .filter(instance::domain.eq(for_domain)) + .filter(lower(instance::domain).eq(for_domain.to_lowercase())) .select(community::all_columns) .first::(conn) .await diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index f92d261b5..9bf3c28f5 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -3,7 +3,7 @@ use crate::{ newtypes::InstanceId, schema::{federation_allowlist, federation_blocklist, instance, local_site, site}, source::instance::{Instance, InstanceForm}, - utils::{get_conn, naive_now, DbPool}, + utils::{functions::lower, get_conn, naive_now, DbPool}, }; use diesel::{ dsl::{insert_into, now}, @@ -23,7 +23,7 @@ impl Instance { // First try to read the instance row and return directly if found let instance = instance::table - .filter(domain.eq(&domain_)) + .filter(lower(domain).eq(&domain_.to_lowercase())) .first::(conn) .await; match instance { diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index 43bdf5c3b..d8a80596f 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -141,7 +141,7 @@ impl ApubActor for Person { person::table .inner_join(instance::table) .filter(lower(person::name).eq(person_name.to_lowercase())) - .filter(instance::domain.eq(for_domain)) + .filter(lower(instance::domain).eq(for_domain.to_lowercase())) .select(person::all_columns) .first::(conn) .await