reject overlapping media and database paths

This commit is contained in:
Charles Hall 2025-02-28 11:14:52 -08:00
parent 81a449d1d2
commit b01b70fc20
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
20 changed files with 146 additions and 4 deletions

View file

@ -538,5 +538,27 @@ where
return Err(Error::RegistrationTokenEmpty);
}
match &config.media.backend {
MediaBackendConfig::Filesystem(x) => {
let media_path = x
.path
.canonicalize()
.map_err(|e| Error::Canonicalize(e, x.path.clone()))?;
let database_path =
config.database.path.canonicalize().map_err(|e| {
Error::Canonicalize(e, config.database.path.clone())
})?;
let overlap = media_path == database_path
|| media_path.starts_with(&database_path)
|| database_path.starts_with(&media_path);
if overlap {
return Err(Error::DatabaseMediaOverlap);
}
}
}
Ok(config)
}

View file

@ -134,8 +134,14 @@ pub(crate) enum Config {
#[error("failed to parse configuration file {1:?}")]
Parse(#[source] toml::de::Error, PathBuf),
#[error("failed to canonicalize path {}", .1.display())]
Canonicalize(#[source] std::io::Error, PathBuf),
#[error("registration token must not be empty")]
RegistrationTokenEmpty,
#[error("database and media paths overlap")]
DatabaseMediaOverlap,
}
/// Errors that can occur while searching for a config file

View file

@ -93,3 +93,21 @@ make_snapshot_test!(
"A config with invalid values fails",
"invalid-values.toml",
);
make_snapshot_test!(
overlapping_paths_equal,
"A config with equal paths fails",
"equal-paths.toml",
);
make_snapshot_test!(
overlapping_paths_media,
"A config with the media path inside the database path fails",
"media-in-database.toml",
);
make_snapshot_test!(
overlapping_paths_database,
"A config with the database path inside the media path fails",
"database-in-media.toml",
);

View file

@ -0,0 +1,12 @@
server_name = "example.com"
[server_discovery]
client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "tests/integrations/fixtures/check_config/dirs/c/a"
[media.backend]
type = "filesystem"
path = "tests/integrations/fixtures/check_config/dirs/c"

View file

@ -0,0 +1,2 @@
*
!.gitignore

View file

@ -0,0 +1,2 @@
*
!.gitignore

View file

@ -0,0 +1,2 @@
*
!.gitignore

View file

@ -0,0 +1,12 @@
server_name = "example.com"
[server_discovery]
client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "tests/integrations/fixtures/check_config/dirs/a"
[media.backend]
type = "filesystem"
path = "tests/integrations/fixtures/check_config/dirs/a"

View file

@ -0,0 +1,12 @@
server_name = "example.com"
[server_discovery]
client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "tests/integrations/fixtures/check_config/dirs/c"
[media.backend]
type = "filesystem"
path = "tests/integrations/fixtures/check_config/dirs/c/a"

View file

@ -5,8 +5,8 @@ client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "/var/lib/grapevine/database"
path = "tests/integrations/fixtures/check_config/dirs/a"
[media.backend]
type = "filesystem"
path = "/var/lib/grapevine/media"
path = "tests/integrations/fixtures/check_config/dirs/b"

View file

@ -10,11 +10,11 @@ client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "/var/lib/grapevine/database"
path = "tests/integrations/fixtures/check_config/dirs/a"
[media.backend]
type = "filesystem"
path = "/var/lib/grapevine/media"
path = "tests/integrations/fixtures/check_config/dirs/b"
[federation]
enable = true

View file

@ -0,0 +1,7 @@
---
source: tests/integrations/check_config.rs
description: A config with the database path inside the media path fails
---
Some(
1,
)

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A config with the database path inside the media path fails
---
Error: failed to validate configuration
Caused by: database and media paths overlap

View file

@ -0,0 +1,5 @@
---
source: tests/integrations/check_config.rs
description: A config with the database path inside the media path fails
---

View file

@ -0,0 +1,7 @@
---
source: tests/integrations/check_config.rs
description: A config with equal paths fails
---
Some(
1,
)

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A config with equal paths fails
---
Error: failed to validate configuration
Caused by: database and media paths overlap

View file

@ -0,0 +1,5 @@
---
source: tests/integrations/check_config.rs
description: A config with equal paths fails
---

View file

@ -0,0 +1,7 @@
---
source: tests/integrations/check_config.rs
description: A config with the media path inside the database path fails
---
Some(
1,
)

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A config with the media path inside the database path fails
---
Error: failed to validate configuration
Caused by: database and media paths overlap

View file

@ -0,0 +1,5 @@
---
source: tests/integrations/check_config.rs
description: A config with the media path inside the database path fails
---