Skip to content

Commit

Permalink
Lowercase domain on db query filters (#3849) (#3873)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Freakazoid182 <>
  • Loading branch information
Freakazoid182 and Freakazoid182 authored Aug 22, 2023
1 parent 28324ad commit 51ccf31
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
18 changes: 18 additions & 0 deletions api_tests/src/community.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
registerUser,
API,
getPosts,
getCommunityByName,
} from "./shared";

beforeAll(async () => {
Expand Down Expand Up @@ -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);
});
11 changes: 11 additions & 0 deletions api_tests/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,17 @@ export async function getCommunity(
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(
api: API,
deleted: boolean,
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
}
Expand All @@ -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))?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>(conn)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/db_schema/src/impls/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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::<Self>(conn)
.await;
match instance {
Expand Down
2 changes: 1 addition & 1 deletion crates/db_schema/src/impls/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>(conn)
.await
Expand Down

0 comments on commit 51ccf31

Please sign in to comment.