From e9771e9cbd3ed9ef55d1a5c8ebb32b388d08682b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonah=20Br=C3=BCchert?= Date: Tue, 20 Feb 2024 18:46:41 +0100 Subject: [PATCH] Allow to configure the list allowed endpoints --- motis-proxy/src/main.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/motis-proxy/src/main.rs b/motis-proxy/src/main.rs index 5b3b5e30..2c204705 100644 --- a/motis-proxy/src/main.rs +++ b/motis-proxy/src/main.rs @@ -34,6 +34,9 @@ fn default_motis_address() -> String { fn default_proxy_assets() -> bool { false } +fn default_allowed_endpoints() -> Option> { + None +} #[derive(Deserialize, DefaultFromSerde)] struct Config { @@ -47,11 +50,16 @@ struct Config { /// Proxy endpoints other than `/`. This should only ever be used for debugging. /// It is slow and incomplete. #[serde(default = "default_proxy_assets" )] - proxy_assets: bool + proxy_assets: bool, + + /// List of endpoints (by path) that should be allowed through the proxy. + /// If this option is not set, all known endpoints will be allowed. + #[serde(default = "default_allowed_endpoints")] + allowed_endpoints: Option> } -#[derive(Deserialize, Serialize)] -enum AllowedEndpoints { +#[derive(Deserialize, Serialize, PartialEq, Eq)] +enum Endpoint { #[serde(rename = "/intermodal")] Intermodal, #[serde(rename = "/guesser")] @@ -75,7 +83,7 @@ enum AllowedEndpoints { #[derive(Deserialize, Serialize)] #[serde(tag = "type")] enum RequestDestination { - Module { target: AllowedEndpoints }, + Module { target: Endpoint }, } #[derive(Deserialize, Serialize)] @@ -287,6 +295,17 @@ async fn proxy_api( ) -> ResultResponse>> { let request = request.into_inner(); + // Check if the requested endpoint is allowed + match &request.destination { + RequestDestination::Module { target } => { + if let Some(allowed_endpoints) = &config.allowed_endpoints { + if !allowed_endpoints.contains(&target) { + return Err(Custom(Status::UnprocessableEntity, ())) + } + } + } + } + trace!("MOTIS Request <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); trace!("{}", serde_json::to_string_pretty(&request).unwrap());