From 6a6108ac552d859c6a088d2996768cf79dbfca26 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 23 May 2024 11:11:25 -0400 Subject: [PATCH] Fixing proxied images for federated posts. (#4737) * Fixing proxied images for federated posts. - Also added test. - Fixes #4736 * Address PR comments. --- Cargo.toml | 10 ++-- api_tests/src/image.spec.ts | 81 +++++++++++++++++++++++++-------- api_tests/src/shared.ts | 25 +++++----- crates/apub/src/objects/post.rs | 9 +--- src/root_span_builder.rs | 2 +- 5 files changed, 81 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ce329dbb5..aec0e8373 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,8 +67,8 @@ members = [ [workspace.lints.clippy] cast_lossless = "deny" -complexity = "deny" -correctness = "deny" +complexity = { level = "deny", priority = -1 } +correctness = { level = "deny", priority = -1 } dbg_macro = "deny" explicit_into_iter_loop = "deny" explicit_iter_loop = "deny" @@ -79,10 +79,10 @@ inefficient_to_string = "deny" items-after-statements = "deny" manual_string_new = "deny" needless_collect = "deny" -perf = "deny" +perf = { level = "deny", priority = -1 } redundant_closure_for_method_calls = "deny" -style = "deny" -suspicious = "deny" +style = { level = "deny", priority = -1 } +suspicious = { level = "deny", priority = -1 } uninlined_format_args = "allow" unused_self = "deny" unwrap_used = "deny" diff --git a/api_tests/src/image.spec.ts b/api_tests/src/image.spec.ts index 806c5b313..123982e85 100644 --- a/api_tests/src/image.spec.ts +++ b/api_tests/src/image.spec.ts @@ -31,6 +31,7 @@ import { waitUntil, createPostWithThumbnail, sampleImage, + sampleSite, } from "./shared"; const downloadFileSync = require("download-file-sync"); @@ -172,54 +173,94 @@ test("Purge post, linked image removed", async () => { expect(content2).toBe(""); }); -test("Images in remote post are proxied if setting enabled", async () => { - let user = await registerUser(beta, betaUrl); +test("Images in remote image post are proxied if setting enabled", async () => { let community = await createCommunity(gamma); - - const upload_form: UploadImage = { - image: Buffer.from("test"), - }; - const upload = await user.uploadImage(upload_form); - let post = await createPost( + let postRes = await createPost( gamma, community.community_view.community.id, - upload.url, + sampleImage, `![](${sampleImage})`, ); - expect(post.post_view.post).toBeDefined(); + const post = postRes.post_view.post; + expect(post).toBeDefined(); // remote image gets proxied after upload expect( - post.post_view.post.thumbnail_url?.startsWith( + post.thumbnail_url?.startsWith( "http://lemmy-gamma:8561/api/v3/image_proxy?url", ), ).toBeTruthy(); expect( - post.post_view.post.body?.startsWith( - "![](http://lemmy-gamma:8561/api/v3/image_proxy?url", - ), + post.body?.startsWith("![](http://lemmy-gamma:8561/api/v3/image_proxy?url"), ).toBeTruthy(); - let epsilonPost = await resolvePost(epsilon, post.post_view.post); - expect(epsilonPost.post).toBeDefined(); + // Make sure that it ends with jpg, to be sure its an image + expect(post.thumbnail_url?.endsWith(".jpg")).toBeTruthy(); + + let epsilonPostRes = await resolvePost(epsilon, postRes.post_view.post); + expect(epsilonPostRes.post).toBeDefined(); // Fetch the post again, the metadata should be backgrounded now // Wait for the metadata to get fetched, since this is backgrounded now - let epsilonPost2 = await waitUntil( - () => getPost(epsilon, epsilonPost.post!.post.id), + let epsilonPostRes2 = await waitUntil( + () => getPost(epsilon, epsilonPostRes.post!.post.id), p => p.post_view.post.thumbnail_url != undefined, ); + const epsilonPost = epsilonPostRes2.post_view.post; expect( - epsilonPost2.post_view.post.thumbnail_url?.startsWith( + epsilonPost.thumbnail_url?.startsWith( "http://lemmy-epsilon:8581/api/v3/image_proxy?url", ), ).toBeTruthy(); expect( - epsilonPost2.post_view.post.body?.startsWith( + epsilonPost.body?.startsWith( "![](http://lemmy-epsilon:8581/api/v3/image_proxy?url", ), ).toBeTruthy(); + + // Make sure that it ends with jpg, to be sure its an image + expect(epsilonPost.thumbnail_url?.endsWith(".jpg")).toBeTruthy(); +}); + +test("Thumbnail of remote image link is proxied if setting enabled", async () => { + let community = await createCommunity(gamma); + let postRes = await createPost( + gamma, + community.community_view.community.id, + // The sample site metadata thumbnail ends in png + sampleSite, + ); + const post = postRes.post_view.post; + expect(post).toBeDefined(); + + // remote image gets proxied after upload + expect( + post.thumbnail_url?.startsWith( + "http://lemmy-gamma:8561/api/v3/image_proxy?url", + ), + ).toBeTruthy(); + + // Make sure that it ends with png, to be sure its an image + expect(post.thumbnail_url?.endsWith(".png")).toBeTruthy(); + + let epsilonPostRes = await resolvePost(epsilon, postRes.post_view.post); + expect(epsilonPostRes.post).toBeDefined(); + + let epsilonPostRes2 = await waitUntil( + () => getPost(epsilon, epsilonPostRes.post!.post.id), + p => p.post_view.post.thumbnail_url != undefined, + ); + const epsilonPost = epsilonPostRes2.post_view.post; + + expect( + epsilonPost.thumbnail_url?.startsWith( + "http://lemmy-epsilon:8581/api/v3/image_proxy?url", + ), + ).toBeTruthy(); + + // Make sure that it ends with png, to be sure its an image + expect(epsilonPost.thumbnail_url?.endsWith(".png")).toBeTruthy(); }); test("No image proxying if setting is disabled", async () => { diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index 6cc8d5aca..056f25538 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -83,21 +83,22 @@ export const fetchFunction = fetch; export const imageFetchLimit = 50; export const sampleImage = "https://i.pinimg.com/originals/df/5f/5b/df5f5b1b174a2b4b6026cc6c8f9395c1.jpg"; +export const sampleSite = "https://yahoo.com"; -export let alphaUrl = "http://127.0.0.1:8541"; -export let betaUrl = "http://127.0.0.1:8551"; -export let gammaUrl = "http://127.0.0.1:8561"; -export let deltaUrl = "http://127.0.0.1:8571"; -export let epsilonUrl = "http://127.0.0.1:8581"; +export const alphaUrl = "http://127.0.0.1:8541"; +export const betaUrl = "http://127.0.0.1:8551"; +export const gammaUrl = "http://127.0.0.1:8561"; +export const deltaUrl = "http://127.0.0.1:8571"; +export const epsilonUrl = "http://127.0.0.1:8581"; -export let alpha = new LemmyHttp(alphaUrl, { fetchFunction }); -export let alphaImage = new LemmyHttp(alphaUrl); -export let beta = new LemmyHttp(betaUrl, { fetchFunction }); -export let gamma = new LemmyHttp(gammaUrl, { fetchFunction }); -export let delta = new LemmyHttp(deltaUrl, { fetchFunction }); -export let epsilon = new LemmyHttp(epsilonUrl, { fetchFunction }); +export const alpha = new LemmyHttp(alphaUrl, { fetchFunction }); +export const alphaImage = new LemmyHttp(alphaUrl); +export const beta = new LemmyHttp(betaUrl, { fetchFunction }); +export const gamma = new LemmyHttp(gammaUrl, { fetchFunction }); +export const delta = new LemmyHttp(deltaUrl, { fetchFunction }); +export const epsilon = new LemmyHttp(epsilonUrl, { fetchFunction }); -export let betaAllowedInstances = [ +export const betaAllowedInstances = [ "lemmy-alpha", "lemmy-gamma", "lemmy-delta", diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 3f69fa0c8..59f4920d3 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -25,12 +25,7 @@ use html2text::{from_read_with_decorator, render::text_renderer::TrivialDecorato use lemmy_api_common::{ context::LemmyContext, request::generate_post_link_metadata, - utils::{ - get_url_blocklist, - local_site_opt_to_slur_regex, - process_markdown_opt, - proxy_image_link_opt_apub, - }, + utils::{get_url_blocklist, local_site_opt_to_slur_regex, process_markdown_opt}, }; use lemmy_db_schema::{ source::{ @@ -228,8 +223,6 @@ impl Object for ApubPost { let alt_text = first_attachment.cloned().and_then(Attachment::alt_text); - let url = proxy_image_link_opt_apub(url, context).await?; - let slur_regex = &local_site_opt_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(context).await?; diff --git a/src/root_span_builder.rs b/src/root_span_builder.rs index e062ade21..09ce2544e 100644 --- a/src/root_span_builder.rs +++ b/src/root_span_builder.rs @@ -79,5 +79,5 @@ fn handle_error(span: Span, status_code: StatusCode, response_error: &dyn Respon } }); - span.record("exception.message", &tracing::field::display(display_error)); + span.record("exception.message", tracing::field::display(display_error)); }