Revert "Adding a URL max length lemmy error. (#4960)"

This reverts commit 33fd31754a.
This commit is contained in:
Felix Ableitner 2024-10-21 10:18:13 +02:00
parent 1b0d02fd80
commit 770e030982
9 changed files with 19 additions and 48 deletions

View file

@ -37,11 +37,11 @@ use lemmy_utils::{
utils::{ utils::{
slurs::check_slurs, slurs::check_slurs,
validation::{ validation::{
check_url_scheme,
is_url_blocked, is_url_blocked,
is_valid_alt_text_field, is_valid_alt_text_field,
is_valid_body_field, is_valid_body_field,
is_valid_post_title, is_valid_post_title,
is_valid_url,
}, },
}, },
}; };
@ -71,11 +71,11 @@ pub async fn create_post(
if let Some(url) = &url { if let Some(url) = &url {
is_url_blocked(url, &url_blocklist)?; is_url_blocked(url, &url_blocklist)?;
is_valid_url(url)?; check_url_scheme(url)?;
} }
if let Some(custom_thumbnail) = &custom_thumbnail { if let Some(custom_thumbnail) = &custom_thumbnail {
is_valid_url(custom_thumbnail)?; check_url_scheme(custom_thumbnail)?;
} }
if let Some(alt_text) = &data.alt_text { if let Some(alt_text) = &data.alt_text {

View file

@ -28,11 +28,11 @@ use lemmy_utils::{
utils::{ utils::{
slurs::check_slurs, slurs::check_slurs,
validation::{ validation::{
check_url_scheme,
is_url_blocked, is_url_blocked,
is_valid_alt_text_field, is_valid_alt_text_field,
is_valid_body_field, is_valid_body_field,
is_valid_post_title, is_valid_post_title,
is_valid_url,
}, },
}, },
}; };
@ -77,11 +77,11 @@ pub async fn update_post(
if let Some(Some(url)) = &url { if let Some(Some(url)) = &url {
is_url_blocked(url, &url_blocklist)?; is_url_blocked(url, &url_blocklist)?;
is_valid_url(url)?; check_url_scheme(url)?;
} }
if let Some(Some(custom_thumbnail)) = &custom_thumbnail { if let Some(Some(custom_thumbnail)) = &custom_thumbnail {
is_valid_url(custom_thumbnail)?; check_url_scheme(custom_thumbnail)?;
} }
let post_id = data.post_id; let post_id = data.post_id;

View file

@ -44,7 +44,7 @@ use lemmy_utils::{
utils::{ utils::{
markdown::markdown_to_html, markdown::markdown_to_html,
slurs::check_slurs_opt, slurs::check_slurs_opt,
validation::{is_url_blocked, is_valid_url}, validation::{check_url_scheme, is_url_blocked},
}, },
}; };
use std::ops::Deref; use std::ops::Deref;
@ -235,7 +235,7 @@ impl Object for ApubPost {
if let Some(url) = &url { if let Some(url) = &url {
is_url_blocked(url, &url_blocklist)?; is_url_blocked(url, &url_blocklist)?;
is_valid_url(url)?; check_url_scheme(url)?;
} }
let alt_text = first_attachment.cloned().and_then(Attachment::alt_text); let alt_text = first_attachment.cloned().and_then(Attachment::alt_text);

View file

@ -711,7 +711,7 @@ diesel::table! {
id -> Int4, id -> Int4,
#[max_length = 200] #[max_length = 200]
name -> Varchar, name -> Varchar,
#[max_length = 2000] #[max_length = 512]
url -> Nullable<Varchar>, url -> Nullable<Varchar>,
body -> Nullable<Text>, body -> Nullable<Text>,
creator_id -> Int4, creator_id -> Int4,

View file

@ -180,7 +180,6 @@ pub enum LemmyErrorType {
InboxTimeout, InboxTimeout,
Unknown(String), Unknown(String),
CantDeleteSite, CantDeleteSite,
UrlLengthOverflow,
} }
cfg_if! { cfg_if! {

View file

@ -18,7 +18,6 @@ const ALLOWED_POST_URL_SCHEMES: [&str; 3] = ["http", "https", "magnet"];
const BODY_MAX_LENGTH: usize = 10000; const BODY_MAX_LENGTH: usize = 10000;
const POST_BODY_MAX_LENGTH: usize = 50000; const POST_BODY_MAX_LENGTH: usize = 50000;
const BIO_MAX_LENGTH: usize = 1000; const BIO_MAX_LENGTH: usize = 1000;
const URL_MAX_LENGTH: usize = 2000;
const ALT_TEXT_MAX_LENGTH: usize = 1500; const ALT_TEXT_MAX_LENGTH: usize = 1500;
const SITE_NAME_MAX_LENGTH: usize = 20; const SITE_NAME_MAX_LENGTH: usize = 20;
const SITE_NAME_MIN_LENGTH: usize = 1; const SITE_NAME_MIN_LENGTH: usize = 1;
@ -288,17 +287,11 @@ pub fn check_site_visibility_valid(
} }
} }
pub fn is_valid_url(url: &Url) -> LemmyResult<()> { pub fn check_url_scheme(url: &Url) -> LemmyResult<()> {
if !ALLOWED_POST_URL_SCHEMES.contains(&url.scheme()) { if !ALLOWED_POST_URL_SCHEMES.contains(&url.scheme()) {
Err(LemmyErrorType::InvalidUrlScheme)? Err(LemmyErrorType::InvalidUrlScheme)?
} }
max_length_check(
url.as_str(),
URL_MAX_LENGTH,
LemmyErrorType::UrlLengthOverflow,
)?;
Ok(()) Ok(())
} }
@ -359,6 +352,7 @@ mod tests {
utils::validation::{ utils::validation::{
build_and_check_regex, build_and_check_regex,
check_site_visibility_valid, check_site_visibility_valid,
check_url_scheme,
check_urls_are_valid, check_urls_are_valid,
clean_url, clean_url,
clean_urls_in_text, clean_urls_in_text,
@ -368,13 +362,11 @@ mod tests {
is_valid_display_name, is_valid_display_name,
is_valid_matrix_id, is_valid_matrix_id,
is_valid_post_title, is_valid_post_title,
is_valid_url,
site_description_length_check, site_description_length_check,
site_name_length_check, site_name_length_check,
BIO_MAX_LENGTH, BIO_MAX_LENGTH,
SITE_DESCRIPTION_MAX_LENGTH, SITE_DESCRIPTION_MAX_LENGTH,
SITE_NAME_MAX_LENGTH, SITE_NAME_MAX_LENGTH,
URL_MAX_LENGTH,
}, },
}; };
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -606,27 +598,15 @@ mod tests {
} }
#[test] #[test]
fn test_check_url_valid() -> LemmyResult<()> { fn test_check_url_scheme() -> LemmyResult<()> {
assert!(is_valid_url(&Url::parse("http://example.com")?).is_ok()); assert!(check_url_scheme(&Url::parse("http://example.com")?).is_ok());
assert!(is_valid_url(&Url::parse("https://example.com")?).is_ok()); assert!(check_url_scheme(&Url::parse("https://example.com")?).is_ok());
assert!(is_valid_url(&Url::parse("https://example.com")?).is_ok()); assert!(check_url_scheme(&Url::parse("https://example.com")?).is_ok());
assert!(is_valid_url(&Url::parse("ftp://example.com")?) assert!(check_url_scheme(&Url::parse("ftp://example.com")?).is_err());
.is_err_and(|e| e.error_type.eq(&LemmyErrorType::InvalidUrlScheme))); assert!(check_url_scheme(&Url::parse("javascript:void")?).is_err());
assert!(is_valid_url(&Url::parse("javascript:void")?)
.is_err_and(|e| e.error_type.eq(&LemmyErrorType::InvalidUrlScheme)));
let magnet_link="magnet:?xt=urn:btih:4b390af3891e323778959d5abfff4b726510f14c&dn=Ravel%20Complete%20Piano%20Sheet%20Music%20-%20Public%20Domain&tr=udp%3A%2F%2Fopen.tracker.cl%3A1337%2Fannounce"; let magnet_link="magnet:?xt=urn:btih:4b390af3891e323778959d5abfff4b726510f14c&dn=Ravel%20Complete%20Piano%20Sheet%20Music%20-%20Public%20Domain&tr=udp%3A%2F%2Fopen.tracker.cl%3A1337%2Fannounce";
assert!(is_valid_url(&Url::parse(magnet_link)?).is_ok()); assert!(check_url_scheme(&Url::parse(magnet_link)?).is_ok());
// Also make sure the length overflow hits an error
let mut long_str = "http://example.com/test=".to_string();
for _ in 1..URL_MAX_LENGTH {
long_str.push('X');
}
let long_url = Url::parse(&long_str)?;
assert!(
is_valid_url(&long_url).is_err_and(|e| e.error_type.eq(&LemmyErrorType::UrlLengthOverflow))
);
Ok(()) Ok(())
} }

@ -1 +1 @@
Subproject commit 6b0bb99efb6b13c412715ea3a96019895885bab0 Subproject commit 05eac2f761a02363020bb15b0de5839d6240d642

View file

@ -1,3 +0,0 @@
ALTER TABLE post
ALTER COLUMN url TYPE varchar(512);

View file

@ -1,5 +0,0 @@
-- Change the post url max limit to 2000
-- From here: https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers#417184
ALTER TABLE post
ALTER COLUMN url TYPE varchar(2000);