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 <freek.van.zee@mediamonks.com>
Co-authored-by: Freakazoid182 <>
move-admin-flag-to-local-user^2
Freakazoid182 9 months ago committed by GitHub
parent 28324ad2c8
commit 51ccf318e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ import {
registerUser, registerUser,
API, API,
getPosts, getPosts,
getCommunityByName,
} from "./shared"; } from "./shared";
beforeAll(async () => { beforeAll(async () => {
@ -279,3 +280,20 @@ test("moderator view", async () => {
expect(postIds).toContain(alphaPost.post.id); expect(postIds).toContain(alphaPost.post.id);
expect(postIds).toContain(otherAlphaPost.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);
});

@ -557,6 +557,17 @@ export async function getCommunity(
return api.client.getCommunity(form); return api.client.getCommunity(form);
} }
export async function getCommunityByName(
api: API,
name: string,
): Promise<CommunityResponse> {
let form: GetCommunity = {
name,
auth: api.auth,
};
return api.client.getCommunity(form);
}
export async function deleteCommunity( export async function deleteCommunity(
api: API, api: API,
deleted: boolean, deleted: boolean,

@ -46,7 +46,7 @@ where
Ok(actor?.into()) Ok(actor?.into())
} else if local_user_view.is_some() { } else if local_user_view.is_some() {
// Fetch the actor from its home instance using webfinger // 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) Ok(actor)
} else { } else {
Err(NotFound.into()) Err(NotFound.into())

@ -84,7 +84,7 @@ fn check_apub_id_valid(apub_id: &Url, local_site_data: &LocalSiteData) -> Result
if local_site_data if local_site_data
.blocked_instances .blocked_instances
.iter() .iter()
.any(|i| domain.eq(&i.domain)) .any(|i| domain.to_lowercase().eq(&i.domain.to_lowercase()))
{ {
Err(LemmyErrorType::DomainBlocked(domain.clone()))?; 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 && !local_site_data
.allowed_instances .allowed_instances
.iter() .iter()
.any(|i| domain.eq(&i.domain)) .any(|i| domain.to_lowercase().eq(&i.domain.to_lowercase()))
{ {
Err(LemmyErrorType::DomainNotInAllowList(domain))?; Err(LemmyErrorType::DomainNotInAllowList(domain))?;
} }

@ -334,7 +334,7 @@ impl ApubActor for Community {
community::table community::table
.inner_join(instance::table) .inner_join(instance::table)
.filter(lower(community::name).eq(community_name.to_lowercase())) .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) .select(community::all_columns)
.first::<Self>(conn) .first::<Self>(conn)
.await .await

@ -3,7 +3,7 @@ use crate::{
newtypes::InstanceId, newtypes::InstanceId,
schema::{federation_allowlist, federation_blocklist, instance, local_site, site}, schema::{federation_allowlist, federation_blocklist, instance, local_site, site},
source::instance::{Instance, InstanceForm}, source::instance::{Instance, InstanceForm},
utils::{get_conn, naive_now, DbPool}, utils::{functions::lower, get_conn, naive_now, DbPool},
}; };
use diesel::{ use diesel::{
dsl::{insert_into, now}, dsl::{insert_into, now},
@ -23,7 +23,7 @@ impl Instance {
// First try to read the instance row and return directly if found // First try to read the instance row and return directly if found
let instance = instance::table let instance = instance::table
.filter(domain.eq(&domain_)) .filter(lower(domain).eq(&domain_.to_lowercase()))
.first::<Self>(conn) .first::<Self>(conn)
.await; .await;
match instance { match instance {

@ -141,7 +141,7 @@ impl ApubActor for Person {
person::table person::table
.inner_join(instance::table) .inner_join(instance::table)
.filter(lower(person::name).eq(person_name.to_lowercase())) .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) .select(person::all_columns)
.first::<Self>(conn) .first::<Self>(conn)
.await .await

Loading…
Cancel
Save