1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-07-02 16:38:36 +00:00

Drop mode and add special allowlist entries '*' and 'default'

This commit is contained in:
Reiner Herrmann 2023-07-29 23:15:16 +02:00
parent 61fd9166f6
commit 8f147379ea
5 changed files with 31 additions and 53 deletions

View file

@ -66,12 +66,7 @@ trusted_servers = ["matrix.org"]
address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy
#address = "0.0.0.0" # If Conduit is running in a container, make sure the reverse proxy (ie. Traefik) can reach it. #address = "0.0.0.0" # If Conduit is running in a container, make sure the reverse proxy (ie. Traefik) can reach it.
# possible URL preview modes: url_preview_allowlist = []
# None: previews disabled
# All: previews for any URL allowed
# Allowlist: only domains in `url_preview_allowlist` are allowed
url_preview_mode = "None"
url_preview_allowlist = ["google.com", "youtube.com", "www.youtube.com"]
[global.well_known] [global.well_known]
# Conduit handles the /.well-known/matrix/* endpoints, making both clients and servers try to access conduit with the host # Conduit handles the /.well-known/matrix/* endpoints, making both clients and servers try to access conduit with the host

7
debian/postinst vendored
View file

@ -97,12 +97,7 @@ trusted_servers = ["matrix.org"]
# [0]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives # [0]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives
#log = "..." #log = "..."
# possible URL preview modes: url_preview_allowlist = []
# None: previews disabled
# All: previews for any URL allowed
# Allowlist: only domains in \`url_preview_allowlist\` are allowed
url_preview_mode = "None"
url_preview_allowlist = ["google.com", "youtube.com", "www.youtube.com"]
EOF EOF
fi fi
;; ;;

View file

@ -11,7 +11,6 @@ use ruma::api::client::{
#[cfg(feature = "url_preview")] #[cfg(feature = "url_preview")]
use { use {
crate::config::UrlPreviewMode,
crate::service::media::UrlPreviewData, crate::service::media::UrlPreviewData,
webpage::HTML, webpage::HTML,
reqwest::Url, reqwest::Url,
@ -190,7 +189,7 @@ async fn get_url_preview(url: &str) -> Result<UrlPreviewData> {
.url_preview_requests .url_preview_requests
.write() .write()
.unwrap() .unwrap()
.insert(url.to_string(), notifier.clone()); .insert(url.to_owned(), notifier.clone());
} }
let data = request_url_preview(url).await; let data = request_url_preview(url).await;
@ -222,6 +221,13 @@ async fn get_url_preview(url: &str) -> Result<UrlPreviewData> {
#[cfg(feature = "url_preview")] #[cfg(feature = "url_preview")]
fn url_preview_allowed(url_str: &str) -> bool { fn url_preview_allowed(url_str: &str) -> bool {
const DEFAULT_ALLOWLIST: &[&str] = &[
"matrix.org",
"mastodon.social",
"youtube.com",
"wikipedia.org",
];
let url = match Url::parse(url_str) { let url = match Url::parse(url_str) {
Ok(u) => u, Ok(u) => u,
Err(_) => return false, Err(_) => return false,
@ -229,18 +235,29 @@ fn url_preview_allowed(url_str: &str) -> bool {
if ["http", "https"].iter().all(|&scheme| scheme != url.scheme().to_lowercase()) { if ["http", "https"].iter().all(|&scheme| scheme != url.scheme().to_lowercase()) {
return false; return false;
} }
match services().globals.url_preview_mode() { let mut host = match url.host_str() {
UrlPreviewMode::All => true, None => return false,
UrlPreviewMode::None => false, Some(h) => h.to_lowercase(),
UrlPreviewMode::Allowlist => { };
match url.host_str() {
None => false, let allowlist = services().globals.url_preview_allowlist();
Some(host) => { if allowlist.contains(&"*".to_owned()) {
services().globals.url_preview_allowlist().contains(&host.to_string()) return true;
} }
} while !host.is_empty() {
if allowlist.contains(&host) {
return true;
}
if allowlist.contains(&"default".to_owned()) && DEFAULT_ALLOWLIST.contains(&host.as_str()) {
return true;
}
/* also check higher level domains, so that e.g. `en.m.wikipedia.org` is matched by `wikipedia.org` on allowlist. */
host = match host.split_once('.') {
None => return false,
Some((_, domain)) => domain.to_owned(),
} }
} }
false
} }
/// # `GET /_matrix/media/r0/preview_url` /// # `GET /_matrix/media/r0/preview_url`

View file

@ -13,23 +13,6 @@ mod proxy;
use self::proxy::ProxyConfig; use self::proxy::ProxyConfig;
#[derive(Clone, Copy, Debug, Deserialize)]
pub enum UrlPreviewMode {
All,
None,
Allowlist,
}
impl ToString for UrlPreviewMode {
fn to_string(&self) -> String {
match *self {
UrlPreviewMode::All => "All".to_string(),
UrlPreviewMode::None => "None".to_string(),
UrlPreviewMode::Allowlist => "Allowlist".to_string(),
}
}
}
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub struct Config { pub struct Config {
#[serde(default = "default_address")] #[serde(default = "default_address")]
@ -102,8 +85,6 @@ pub struct Config {
pub emergency_password: Option<String>, pub emergency_password: Option<String>,
#[serde(default = "default_url_preview_mode")]
pub url_preview_mode: UrlPreviewMode,
#[serde(default = "Vec::new")] #[serde(default = "Vec::new")]
pub url_preview_allowlist: Vec<String>, pub url_preview_allowlist: Vec<String>,
@ -254,7 +235,6 @@ impl fmt::Display for Config {
}), }),
("Well-known server name", well_known_server.as_str()), ("Well-known server name", well_known_server.as_str()),
("Well-known client URL", &self.well_known_client()), ("Well-known client URL", &self.well_known_client()),
("URL preview mode", &self.url_preview_mode.to_string()),
("URL preview allowlist", &self.url_preview_allowlist.join(", ")), ("URL preview allowlist", &self.url_preview_allowlist.join(", ")),
]; ];
@ -336,7 +316,3 @@ fn default_openid_token_ttl() -> u64 {
pub fn default_default_room_version() -> RoomVersionId { pub fn default_default_room_version() -> RoomVersionId {
RoomVersionId::V10 RoomVersionId::V10
} }
pub fn default_url_preview_mode() -> UrlPreviewMode {
UrlPreviewMode::None
}

View file

@ -12,7 +12,6 @@ use futures_util::FutureExt;
use hickory_resolver::TokioAsyncResolver; use hickory_resolver::TokioAsyncResolver;
use hyper_util::client::legacy::connect::dns::{GaiResolver, Name as HyperName}; use hyper_util::client::legacy::connect::dns::{GaiResolver, Name as HyperName};
use reqwest::dns::{Addrs, Name, Resolve, Resolving}; use reqwest::dns::{Addrs, Name, Resolve, Resolving};
use crate::config::UrlPreviewMode;
use ruma::{ use ruma::{
api::{client::sync::sync_events, federation::discovery::ServerSigningKeys}, api::{client::sync::sync_events, federation::discovery::ServerSigningKeys},
DeviceId, RoomVersionId, ServerName, UserId, DeviceId, RoomVersionId, ServerName, UserId,
@ -325,10 +324,6 @@ impl Service {
self.config.allow_federation self.config.allow_federation
} }
pub fn url_preview_mode(&self) -> UrlPreviewMode {
self.config.url_preview_mode
}
pub fn url_preview_allowlist(&self) -> &Vec<String> { pub fn url_preview_allowlist(&self) -> &Vec<String> {
&self.config.url_preview_allowlist &self.config.url_preview_allowlist
} }