error::LemmyError,
utils::{
slurs::{check_slurs, check_slurs_opt},
- validation::{clean_url_params, is_valid_body_field, is_valid_post_title},
+ validation::{check_url_scheme, clean_url_params, is_valid_body_field, is_valid_post_title},
},
};
use tracing::{warn, Instrument};
is_valid_post_title(&data.name)?;
is_valid_body_field(&data.body, true)?;
+ check_url_scheme(&data.url)?;
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
check_community_deleted_or_removed(data.community_id, context.pool()).await?;
error::LemmyError,
utils::{
slurs::check_slurs_opt,
- validation::{clean_url_params, is_valid_body_field, is_valid_post_title},
+ validation::{check_url_scheme, clean_url_params, is_valid_body_field, is_valid_post_title},
},
};
}
is_valid_body_field(&data.body, true)?;
+ check_url_scheme(&data.url)?;
let post_id = data.post_id;
let orig_post = Post::read(context.pool(), post_id).await?;
markdown::markdown_to_html,
slurs::{check_slurs_opt, remove_slurs},
time::convert_datetime,
+ validation::check_url_scheme,
},
};
use std::ops::Deref;
} else {
None
};
+ check_url_scheme(&url)?;
let local_site = LocalSite::read(context.pool()).await.ok();
let allow_sensitive = local_site_opt_to_sensitive(&local_site);
Ok(())
}
+pub fn check_url_scheme(url: &Option<Url>) -> LemmyResult<()> {
+ if let Some(url) = url {
+ if url.scheme() != "http" && url.scheme() != "https" {
+ return Err(LemmyError::from_message("invalid_url_scheme"));
+ }
+ }
+ Ok(())
+}
+
#[cfg(test)]
mod tests {
use super::build_totp_2fa;
use crate::utils::validation::{
build_and_check_regex,
check_site_visibility_valid,
+ check_url_scheme,
clean_url_params,
generate_totp_2fa_secret,
is_valid_actor_name,
assert!(check_site_visibility_valid(false, false, &Some(true), &None).is_ok());
assert!(check_site_visibility_valid(false, false, &None, &Some(true)).is_ok());
}
+
+ #[test]
+ fn test_check_url_scheme() {
+ assert!(check_url_scheme(&None).is_ok());
+ assert!(check_url_scheme(&Some(Url::parse("http://example.com").unwrap())).is_ok());
+ assert!(check_url_scheme(&Some(Url::parse("https://example.com").unwrap())).is_ok());
+ assert!(check_url_scheme(&Some(Url::parse("ftp://example.com").unwrap())).is_err());
+ assert!(check_url_scheme(&Some(Url::parse("javascript:void").unwrap())).is_err());
+ }
}