Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deny invalid config fields #5370

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions crates/utils/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{error::LemmyResult, location_info};
use anyhow::{anyhow, Context};
use deser_hjson::from_str;
use regex::Regex;
use std::{env, fs, io::Error, sync::LazyLock};
use std::{env, fs, sync::LazyLock};
use structs::{PictrsConfig, Settings};
use url::Url;

Expand Down Expand Up @@ -39,7 +39,10 @@ impl Settings {
/// `lemmy_db_schema/src/lib.rs::get_database_url_from_env()`
/// Warning: Only call this once.
pub(crate) fn init() -> LemmyResult<Self> {
let config = from_str::<Settings>(&Self::read_config_file()?)?;
let path =
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string());
let plain = fs::read_to_string(path)?;
let config = from_str::<Settings>(&plain)?;
if config.hostname == "unset" {
Err(anyhow!("Hostname variable is not set!").into())
} else {
Expand All @@ -55,14 +58,6 @@ impl Settings {
}
}

fn get_config_location() -> String {
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string())
}

fn read_config_file() -> Result<String, Error> {
fs::read_to_string(Self::get_config_location())
}

/// Returns either "http" or "https", depending on tls_enabled setting
pub fn get_protocol_string(&self) -> &'static str {
if self.tls_enabled {
Expand Down Expand Up @@ -109,3 +104,15 @@ impl Settings {
fn pictrs_placeholder_url() -> Url {
Url::parse("http://localhost:8080").expect("parse pictrs url")
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_load_config() -> LemmyResult<()> {
Settings::init()?;
Ok(())
}
}
13 changes: 6 additions & 7 deletions crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use url::Url;

#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)]
#[serde(default, deny_unknown_fields)]
pub struct Settings {
/// settings related to the postgresql database
pub database: DatabaseConfig,
Expand Down Expand Up @@ -109,7 +109,6 @@ pub struct PictrsConfig {
}

#[derive(Debug, Deserialize, Serialize, Clone, Default, Document, PartialEq)]
#[serde(deny_unknown_fields)]
pub enum PictrsImageMode {
/// Leave images unchanged, don't generate any local thumbnails for post urls. Instead the
/// Opengraph image is directly returned as thumbnail
Expand All @@ -132,7 +131,7 @@ pub enum PictrsImageMode {
}

#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)]
#[serde(default, deny_unknown_fields)]
pub struct DatabaseConfig {
/// Configure the database by specifying URI pointing to a postgres instance
///
Expand All @@ -153,7 +152,7 @@ pub struct DatabaseConfig {
}

#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
#[serde(deny_unknown_fields)]
#[serde(default, deny_unknown_fields)]
pub struct EmailConfig {
/// Hostname and port of the smtp server
#[doku(example = "localhost:25")]
Expand All @@ -180,7 +179,7 @@ impl EmailConfig {
}

#[derive(Debug, Deserialize, Serialize, Clone, Default, Document)]
#[serde(deny_unknown_fields)]
#[serde(default, deny_unknown_fields)]
pub struct SetupConfig {
/// Username for the admin user
#[doku(example = "admin")]
Expand All @@ -197,7 +196,7 @@ pub struct SetupConfig {
}

#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(deny_unknown_fields)]
#[serde(default, deny_unknown_fields)]
pub struct PrometheusConfig {
// Address that the Prometheus metrics will be served on.
#[default(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))]
Expand All @@ -210,7 +209,7 @@ pub struct PrometheusConfig {
}

#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)]
#[serde(default, deny_unknown_fields)]
// named federation"worker"config to disambiguate from the activitypub library configuration
pub struct FederationWorkerConfig {
/// Limit to the number of concurrent outgoing federation requests per target instance.
Expand Down