feat(paste): support blacklisting MIME types

This commit is contained in:
orhun 2021-08-09 22:48:51 +03:00
parent 03348cb38f
commit e7ad855f4d
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 19 additions and 1 deletions

View file

@ -17,3 +17,8 @@ mime_override = [
{ mime = "application/octet-stream", regex = "^.*\\.bin$" }, { mime = "application/octet-stream", regex = "^.*\\.bin$" },
{ mime = "text/plain", regex = "^.*\\.(log|txt|diff)$" }, { mime = "text/plain", regex = "^.*\\.(log|txt|diff)$" },
] ]
mime_blacklist = [
"application/x-dosexec",
"application/java-archive",
"application/java-vm"
]

View file

@ -35,6 +35,8 @@ pub struct PasteConfig {
pub default_extension: String, pub default_extension: String,
/// Media type override options. /// Media type override options.
pub mime_override: Vec<MimeMatcher>, pub mime_override: Vec<MimeMatcher>,
/// Media type blacklist.
pub mime_blacklist: Vec<String>,
} }
impl Config { impl Config {

View file

@ -48,6 +48,17 @@ impl Paste {
/// [`default_extension`]: crate::config::PasteConfig::default_extension /// [`default_extension`]: crate::config::PasteConfig::default_extension
/// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled /// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled
pub fn store_file(&self, file_name: &str, config: &Config) -> IoResult<String> { pub fn store_file(&self, file_name: &str, config: &Config) -> IoResult<String> {
let file_type = infer::get(&self.data);
if let Some(file_type) = file_type {
for mime_type in &config.paste.mime_blacklist {
if mime_type == file_type.mime_type() {
return Err(IoError::new(
IoErrorKind::Other,
String::from("this file type is not permitted"),
));
}
}
}
let file_name = match PathBuf::from(file_name) let file_name = match PathBuf::from(file_name)
.file_name() .file_name()
.map(|v| v.to_str()) .map(|v| v.to_str())
@ -70,7 +81,7 @@ impl Paste {
path.set_file_name(file_name); path.set_file_name(file_name);
} }
path.set_extension( path.set_extension(
infer::get(&self.data) file_type
.map(|t| t.extension()) .map(|t| t.extension())
.unwrap_or(&config.paste.default_extension), .unwrap_or(&config.paste.default_extension),
); );