Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat:(image_proxy_service): use macro authorization image proxy service (#4910)

Will Hutchinson5fa3b116c5b5
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index c103d06fc4..b8297796f6 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -2856,6 +2856,7 @@
"crates/entity_access_db_utils",
"crates/ip_extractor",
"crates/macro_auth",
+ "crates/macro_authorization",
"crates/macro_aws_config",
"crates/macro_config",
"crates/macro_config_derive",
diff --git a/Cargo.lock b/Cargo.lock
index d36552feee..8edc26f88d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7043,7 +7043,9 @@ dependencies = [
"doppler-config",
"futures",
"http-body-util",
+ "jsonwebtoken 10.3.0",
"macro_auth",
+ "macro_authorization",
"macro_aws_config",
"macro_config",
"macro_cors",
diff --git a/services/image_proxy_service/Cargo.toml b/services/image_proxy_service/Cargo.toml
index a5d29fa989..84d19c16fc 100644
--- a/services/image_proxy_service/Cargo.toml
+++ b/services/image_proxy_service/Cargo.toml
@@ -23,6 +23,7 @@ aws-sdk-secretsmanager = { workspace = true }
axum = { workspace = true, features = ["macros"] }
doppler-config = { workspace = true }
macro_auth = { path = "../../crates/macro_auth" }
+macro_authorization = { path = "../../crates/macro_authorization" }
macro_aws_config = { path = "../../crates/macro_aws_config" }
macro_config = { path = "../../crates/macro_config" }
macro_cors = { path = "../../crates/macro_cors" }
@@ -50,3 +51,5 @@ workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }
[dev-dependencies]
http-body-util = { workspace = true }
+jsonwebtoken = { workspace = true }
+macro_auth = { path = "../../crates/macro_auth", features = ["testing"] }
diff --git a/services/image_proxy_service/src/api/context.rs b/services/image_proxy_service/src/api/context.rs
index a93d913171..3f0e14465e 100644
--- a/services/image_proxy_service/src/api/context.rs
+++ b/services/image_proxy_service/src/api/context.rs
@@ -1,10 +1,14 @@
use axum::extract::FromRef;
-use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use macro_authorization::{
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
use macro_env::Environment;
+pub type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+
#[derive(Clone, FromRef)]
pub struct ApiContext {
- pub jwt_args: JwtValidationArgs,
+ pub authorization_state: MacroAuthorizationState<AuthorizationService>,
pub environment: Environment,
pub http_client: reqwest::Client,
}
diff --git a/services/image_proxy_service/src/api/mod.rs b/services/image_proxy_service/src/api/mod.rs
index 18784ae2ee..012bdab372 100644
--- a/services/image_proxy_service/src/api/mod.rs
+++ b/services/image_proxy_service/src/api/mod.rs
@@ -11,16 +11,12 @@ mod health;
pub(crate) mod proxy;
pub(crate) mod swagger;
-pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<()> {
- let cors = macro_cors::cors_layer();
+#[cfg(test)]
+mod test;
+pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<()> {
let env = state.environment;
- let app = api_router(state.clone())
- .with_state(state)
- .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
- .merge(health::router())
- .layer(cors)
- .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()));
+ let app = app(state);
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
.await
@@ -37,14 +33,17 @@ pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<(
.context("error starting service")
}
-fn api_router(state: ApiContext) -> Router<ApiContext> {
- Router::new().nest(
- "/proxy",
- proxy::router().layer(
- ServiceBuilder::new().layer(axum::middleware::from_fn_with_state(
- state.jwt_args.clone(),
- macro_middleware::auth::decode_jwt::handler,
- )),
- ),
- )
+fn app(state: ApiContext) -> Router {
+ let cors = macro_cors::cors_layer();
+
+ api_router()
+ .with_state(state)
+ .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
+ .merge(health::router())
+ .layer(cors)
+ .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))
+}
+
+fn api_router() -> Router<ApiContext> {
+ Router::new().nest("/proxy", proxy::router())
}
diff --git a/services/image_proxy_service/src/api/proxy/mod.rs b/services/image_proxy_service/src/api/proxy/mod.rs
index ca70fb0834..93deead657 100644
--- a/services/image_proxy_service/src/api/proxy/mod.rs
+++ b/services/image_proxy_service/src/api/proxy/mod.rs
@@ -1,12 +1,16 @@
mod resolver;
+use crate::api::context::AuthorizationService;
+
use axum::Router;
use axum::body::Body;
use axum::extract::{Query, State};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use futures::StreamExt;
+use macro_authorization::MacroAuthorizationExtractor;
use macro_middleware::tracking::ClientIp;
+use model::response::ErrorResponse;
use reqwest::StatusCode;
use serde::Deserialize;
use std::error::Error;
@@ -152,9 +156,17 @@ pub struct ProxyParams {
get,
path = "/proxy",
params(("url" = String, Query, description = "The image url to proxy")),
+ responses(
+ (status = 401, description = "Unauthorized", body = ErrorResponse),
+ ),
+)]
+#[tracing::instrument(
+ err(Debug),
+ skip(user, http_client),
+ fields(user_id = ?user.macro_user_id)
)]
-#[tracing::instrument(err(Debug), skip(http_client))]
pub async fn proxy_request_handler(
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Query(params): Query<ProxyParams>,
State(http_client): State<reqwest::Client>,
_ip: ClientIp,
diff --git a/services/image_proxy_service/src/api/test.rs b/services/image_proxy_service/src/api/test.rs
new file mode 100644
index 0000000000..35fc4b6f34
--- /dev/null
+++ b/services/image_proxy_service/src/api/test.rs
@@ -0,0 +1,191 @@
+use std::sync::Arc;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+use axum::body::{Body, Bytes};
+use axum::http::header::{AUTHORIZATION, COOKIE};
+use axum::http::{Request, StatusCode};
+use http_body_util::BodyExt;
+use jsonwebtoken::{Algorithm, EncodingKey, Header};
+use macro_auth::middleware::decode_jwt::{JwtValidationArgs, MacroAccessToken};
+use macro_authorization::{
+ InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationServiceImpl,
+ MacroAuthorizationState,
+};
+use macro_env::Environment;
+use serde_json::{Value, json};
+use tower::ServiceExt;
+
+use super::*;
+
+const ACCESS_TOKEN_COOKIE: &str = "macro-access-token";
+const INTERNAL_API_KEY_HEADER: &str = "x-internal-auth-key";
+const TEST_INTERNAL_API_KEY: &str = "test-internal-key";
+const TEST_USER_ID: &str = "macro|[email protected]";
+
+fn test_context() -> ApiContext {
+ let authorization_service = MacroAuthorizationServiceImpl::new(
+ MacroAuthJwtValidator::new(JwtValidationArgs::new_testing()),
+ InternalAuthConfig {
+ api_key: TEST_INTERNAL_API_KEY.to_string(),
+ default_user_id: None,
+ },
+ );
+
+ ApiContext {
+ authorization_state: MacroAuthorizationState::new(Arc::new(authorization_service)),
+ environment: Environment::Production,
+ http_client: proxy::build_http_client().expect("test HTTP client should build"),
+ }
+}
+
+fn access_token(expiration: usize) -> String {
+ let claims = MacroAccessToken {
+ aud: String::new(),
+ exp: expiration,
+ tid: "test-tenant".to_string(),
+ iss: String::new(),
+ email: "[email protected]".to_string(),
+ fusion_user_id: "test-fusion-user".to_string(),
+ macro_user_id: TEST_USER_ID.to_string(),
+ macro_organization_id: None,
+ root_macro_id: None,
+ };
+ let mut header = Header::new(Algorithm::HS256);
+ header.kid = Some("test-access-token".to_string());
+
+ jsonwebtoken::encode(&header, &claims, &EncodingKey::from_secret(b""))
+ .expect("test access token should encode")
+}
+
+fn valid_expiration() -> usize {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .expect("system clock should be after the Unix epoch")
+ .as_secs() as usize
+ + 3_600
+}
+
+fn proxy_request(uri: &str) -> axum::http::request::Builder {
+ Request::get(uri).header("x-forwarded-for", "203.0.113.1")
+}
+
+async fn send(request: Request<Body>) -> (StatusCode, Bytes) {
+ let response = app(test_context())
+ .oneshot(request)
+ .await
+ .expect("application should return a response");
+ let status = response.status();
+ let body = response
+ .into_body()
+ .collect()
+ .await
+ .expect("response body should collect")
+ .to_bytes();
+
+ (status, body)
+}
+
+fn assert_json_response(
+ response: (StatusCode, Bytes),
+ expected_status: StatusCode,
+ expected_body: Value,
+) {
+ assert_eq!(response.0, expected_status);
+ assert_eq!(
+ serde_json::from_slice::<Value>(&response.1).expect("response should contain JSON"),
+ expected_body
+ );
+}
+
+#[tokio::test]
+async fn proxy_rejects_missing_credentials() {
+ let request = proxy_request("/proxy?url=http://127.0.0.1/image.png")
+ .body(Body::empty())
+ .unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "unauthorized" }),
+ );
+}
+
+#[tokio::test]
+async fn authentication_precedes_proxy_query_validation() {
+ for uri in ["/proxy", "/proxy?url=not-a-url"] {
+ let request = proxy_request(uri).body(Body::empty()).unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "unauthorized" }),
+ );
+ }
+}
+
+#[tokio::test]
+async fn proxy_rejects_malformed_bearer_credentials() {
+ let request = proxy_request("/proxy?url=http://127.0.0.1/image.png")
+ .header(AUTHORIZATION, "Bearer not-a-token")
+ .body(Body::empty())
+ .unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "unauthorized" }),
+ );
+}
+
+#[tokio::test]
+async fn proxy_rejects_expired_credentials() {
+ let token = access_token(1);
+ let request = proxy_request("/proxy?url=http://127.0.0.1/image.png")
+ .header(AUTHORIZATION, format!("Bearer {token}"))
+ .body(Body::empty())
+ .unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "jwt expired" }),
+ );
+}
+
+#[tokio::test]
+async fn valid_cookie_credentials_reach_loopback_ssrf_rejection() {
+ let token = access_token(valid_expiration());
+ let request = proxy_request("/proxy?url=http://127.0.0.1/image.png")
+ .header(COOKIE, format!("{ACCESS_TOKEN_COOKIE}={token}"))
+ .body(Body::empty())
+ .unwrap();
+ let (status, body) = send(request).await;
+
+ assert_eq!(status, StatusCode::FORBIDDEN);
+ assert_eq!(
+ body,
+ "requests to private/internal IPs are not allowed".as_bytes()
+ );
+}
+
+#[tokio::test]
+async fn proxy_rejects_internal_credentials_without_an_acting_user() {
+ let request = proxy_request("/proxy?url=http://127.0.0.1/image.png")
+ .header(INTERNAL_API_KEY_HEADER, TEST_INTERNAL_API_KEY)
+ .body(Body::empty())
+ .unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "unauthorized" }),
+ );
+}
+
+#[tokio::test]
+async fn health_remains_public() {
+ let request = Request::get("/health").body(Body::empty()).unwrap();
+ let (status, _) = send(request).await;
+
+ assert_eq!(status, StatusCode::OK);
+}
diff --git a/services/image_proxy_service/src/config.rs b/services/image_proxy_service/src/config.rs
index 5313d3b21e..2c3530aa69 100644
--- a/services/image_proxy_service/src/config.rs
+++ b/services/image_proxy_service/src/config.rs
@@ -1,9 +1,10 @@
use anyhow::Context;
+use macro_middleware::auth::internal_access::InternalApiKey;
pub use macro_env::Environment;
/// The configuration parameters for the application.
-#[derive(Debug, macro_config::MacroConfig)]
+#[derive(macro_config::MacroConfig)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Config {
/// The port to listen on.
@@ -12,6 +13,8 @@ pub struct Config {
/// The environment we are in.
#[macro_config_default(Environment::new_or_prod())]
pub environment: Environment,
+ /// The internal API key used to authorize service requests.
+ pub internal_api_key: InternalApiKey,
}
impl Config {
diff --git a/services/image_proxy_service/src/main.rs b/services/image_proxy_service/src/main.rs
index cec6c98a36..a3c1b485a8 100644
--- a/services/image_proxy_service/src/main.rs
+++ b/services/image_proxy_service/src/main.rs
@@ -2,9 +2,15 @@
mod api;
mod config;
+use std::sync::Arc;
+
use anyhow::Context;
use config::Config;
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use macro_authorization::{
+ InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationServiceImpl,
+ MacroAuthorizationState,
+};
use macro_entrypoint::MacroEntrypoint;
#[tokio::main]
@@ -22,11 +28,19 @@ async fn main() -> anyhow::Result<()> {
let jwt_args =
JwtValidationArgs::new_with_secret_manager(config.environment, &secretsmanager_client)
.await?;
+ let authorization_service = MacroAuthorizationServiceImpl::new(
+ MacroAuthJwtValidator::new(jwt_args),
+ InternalAuthConfig {
+ api_key: config.internal_api_key.to_string(),
+ default_user_id: None,
+ },
+ );
+ let authorization_state = MacroAuthorizationState::new(Arc::new(authorization_service));
let http_client = api::proxy::build_http_client().context("failed to build http client")?;
let state = api::context::ApiContext {
- jwt_args,
+ authorization_state,
environment: config.environment,
http_client,
};