diff --git a/Cargo.lock b/Cargo.lock index 67715bf423..d582ee65a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -565,26 +565,63 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" dependencies = [ "async-trait", - "axum-core", + "axum-core 0.4.5", "bytes", "futures-util", "http 1.2.0", "http-body 1.0.1", "http-body-util", "itoa", - "matchit", + "matchit 0.7.3", "memchr", "mime", "percent-encoding", "pin-project-lite", "rustversion", "serde", - "sync_wrapper 1.0.2", - "tower 0.5.1", + "sync_wrapper", + "tower 0.5.2", "tower-layer", "tower-service", ] +[[package]] +name = "axum" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d6fd624c75e18b3b4c6b9caf42b1afe24437daaee904069137d8bab077be8b8" +dependencies = [ + "axum-core 0.5.0", + "base64 0.22.1", + "bytes", + "form_urlencoded", + "futures-util", + "http 1.2.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.5.2", + "hyper-util", + "itoa", + "matchit 0.8.4", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sha1", + "sync_wrapper", + "tokio", + "tokio-tungstenite", + "tower 0.5.2", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "axum-core" version = "0.4.5" @@ -600,7 +637,49 @@ dependencies = [ "mime", "pin-project-lite", "rustversion", - "sync_wrapper 1.0.2", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1362f362fd16024ae199c1970ce98f9661bf5ef94b9808fee734bc3698b733" +dependencies = [ + "bytes", + "futures-util", + "http 1.2.0", + "http-body 1.0.1", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-extra" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fc6f625a1f7705c6cf62d0d070794e94668988b1c38111baeec177c715f7b" +dependencies = [ + "axum 0.8.1", + "axum-core 0.5.0", + "bytes", + "futures-util", + "headers", + "http 1.2.0", + "http-body 1.0.1", + "http-body-util", + "mime", + "pin-project-lite", + "serde", + "tower 0.5.2", "tower-layer", "tower-service", ] @@ -2105,14 +2184,14 @@ dependencies = [ [[package]] name = "headers" -version = "0.3.9" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" +checksum = "322106e6bd0cba2d5ead589ddb8150a13d7c4217cf80d7c4f682ca994ccc6aa9" dependencies = [ "base64 0.21.7", "bytes", "headers-core", - "http 0.2.12", + "http 1.2.0", "httpdate", "mime", "sha1", @@ -2120,11 +2199,11 @@ dependencies = [ [[package]] name = "headers-core" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +checksum = "54b4a22553d4242c49fddb9ba998a99962b5cc6f22cb5a3482bec22522403ce4" dependencies = [ - "http 0.2.12", + "http 1.2.0", ] [[package]] @@ -3527,6 +3606,12 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "mdbx-sys" version = "12.12.0" @@ -3559,16 +3644,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "mime_guess" -version = "2.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "minicov" version = "0.3.7" @@ -3605,24 +3680,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "multer" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" -dependencies = [ - "bytes", - "encoding_rs", - "futures-util", - "http 0.2.12", - "httparse", - "log", - "memchr", - "mime", - "spin 0.9.8", - "version_check", -] - [[package]] name = "multiaddr" version = "0.18.2" @@ -4150,43 +4207,43 @@ dependencies = [ [[package]] name = "nimiq-jsonrpc-client" -version = "0.1.2" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e42f658452741799b7c5ac8852729d05f952ba925f3524752f22b43491ad43e" +checksum = "f251102c037c9357604098a821e6de70a91aa446284836595c6446ffc9ee54dd" dependencies = [ "async-stream", "async-trait", "base64 0.22.1", "futures", - "http 0.2.12", + "http 1.2.0", "log", "nimiq-jsonrpc-core", "reqwest", "serde", "serde_json", - "thiserror 1.0.69", + "thiserror 2.0.11", "tokio", - "tokio-tungstenite 0.20.1", + "tokio-tungstenite", "url", ] [[package]] name = "nimiq-jsonrpc-core" -version = "0.1.2" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b55415115dbedde87b282b3359269bc6d4f231778e5912ff7516661beaebc3ca" +checksum = "844ff8c10decca1ba306e650d3fca09e15e184d07735cd9a1980cb9f1d86f1cf" dependencies = [ "log", "serde", "serde_json", - "thiserror 1.0.69", + "thiserror 2.0.11", ] [[package]] name = "nimiq-jsonrpc-derive" -version = "0.1.2" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c80d9a4e435131b2eb1223e4d0e6f0e7d541a74fe24cf503170ccd9676846978" +checksum = "a58a9567ff158ce2420f78a97bf3ce9811c7c921280d8ee4e2aaea19024f6606" dependencies = [ "darling", "heck", @@ -4200,24 +4257,23 @@ dependencies = [ [[package]] name = "nimiq-jsonrpc-server" -version = "0.1.2" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74a83610134208ccf343e28462b77029429a1d9ca4e5b4f0b3f0052bdffc58e1" +checksum = "fa0e7b400e3f552cb649342c40007263ed59e811459cea721c1144cef0ace89d" dependencies = [ "async-trait", + "axum 0.8.1", + "axum-extra", "blake2", - "bytes", "futures", - "headers", - "http 0.2.12", "log", "nimiq-jsonrpc-core", "serde", "serde_json", "subtle", - "thiserror 1.0.69", + "thiserror 2.0.11", "tokio", - "warp", + "tower-http", ] [[package]] @@ -6284,7 +6340,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sync_wrapper 1.0.2", + "sync_wrapper", "system-configuration", "tokio", "tokio-native-tls", @@ -6645,12 +6701,6 @@ dependencies = [ "syn 2.0.96", ] -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "scopeguard" version = "1.2.0" @@ -6827,6 +6877,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.19" @@ -7125,12 +7185,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" - [[package]] name = "sync_wrapper" version = "1.0.2" @@ -7395,26 +7449,14 @@ dependencies = [ [[package]] name = "tokio-tungstenite" -version = "0.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "212d5dcb2a1ce06d81107c3d0ffa3121fe974b73f068c8282cb1c32328113b6c" -dependencies = [ - "futures-util", - "log", - "tokio", - "tungstenite 0.20.1", -] - -[[package]] -name = "tokio-tungstenite" -version = "0.21.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" +checksum = "be4bf6fecd69fcdede0ec680aaf474cdab988f9de6bc73d3758f0160e3b7025a" dependencies = [ "futures-util", "log", "tokio", - "tungstenite 0.21.0", + "tungstenite", ] [[package]] @@ -7472,7 +7514,7 @@ checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ "async-stream", "async-trait", - "axum", + "axum 0.7.9", "base64 0.22.1", "bytes", "h2 0.4.7", @@ -7516,14 +7558,32 @@ dependencies = [ [[package]] name = "tower" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ "futures-core", "futures-util", "pin-project-lite", - "sync_wrapper 0.1.2", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403fa3b783d4b626a8ad51d766ab03cb6d2dbfc46b1c5d4448395e6628dc9697" +dependencies = [ + "base64 0.22.1", + "bitflags 2.8.0", + "bytes", + "http 1.2.0", + "mime", + "pin-project-lite", "tower-layer", "tower-service", ] @@ -7687,28 +7747,9 @@ dependencies = [ [[package]] name = "tungstenite" -version = "0.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" -dependencies = [ - "byteorder", - "bytes", - "data-encoding", - "http 0.2.12", - "httparse", - "log", - "rand", - "sha1", - "thiserror 1.0.69", - "url", - "utf-8", -] - -[[package]] -name = "tungstenite" -version = "0.21.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" +checksum = "413083a99c579593656008130e29255e54dcaae495be556cc26888f211648c24" dependencies = [ "byteorder", "bytes", @@ -7718,8 +7759,7 @@ dependencies = [ "log", "rand", "sha1", - "thiserror 1.0.69", - "url", + "thiserror 2.0.11", "utf-8", ] @@ -7741,12 +7781,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "unicase" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" - [[package]] name = "unicode-ident" version = "1.0.14" @@ -7893,35 +7927,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "warp" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4378d202ff965b011c64817db11d5829506d3404edeadb61f190d111da3f231c" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "headers", - "http 0.2.12", - "hyper 0.14.31", - "log", - "mime", - "mime_guess", - "multer", - "percent-encoding", - "pin-project", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-tungstenite 0.21.0", - "tokio-util", - "tower-service", - "tracing", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 1967dc6378..56d252f9b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -196,10 +196,10 @@ nimiq-genesis-builder = { path = "genesis-builder", default-features = false } nimiq-handel = { path = "handel", default-features = false } nimiq-hash = { path = "hash", default-features = false } nimiq-hash_derive = { path = "hash/hash_derive", default-features = false } -nimiq-jsonrpc-client = { version = "0.1.2", default-features = false } -nimiq-jsonrpc-core = { version = "0.1.2", default-features = false } -nimiq-jsonrpc-derive = { version = "0.1.2", default-features = false } -nimiq-jsonrpc-server = { version = "0.1.2", default-features = false } +nimiq-jsonrpc-client = { version = "0.2.2", default-features = false } +nimiq-jsonrpc-core = { version = "0.2.2", default-features = false } +nimiq-jsonrpc-derive = { version = "0.2.2", default-features = false } +nimiq-jsonrpc-server = { version = "0.2.2", default-features = false } nimiq-key-derivation = { path = "key-derivation", default-features = false } nimiq-keys = { path = "keys", default-features = false } nimiq-light-blockchain = { path = "light-blockchain", default-features = false } diff --git a/lib/src/config/config.rs b/lib/src/config/config.rs index 7e220165c9..8e9d4bcbe3 100644 --- a/lib/src/config/config.rs +++ b/lib/src/config/config.rs @@ -651,9 +651,8 @@ pub struct RpcServerConfig { #[builder(default = "consts::RPC_DEFAULT_PORT")] pub port: u16, - /// TODO: Use this config setting and add it to the config.example.toml file #[builder(setter(strip_option))] - pub corsdomain: Option>, + pub cors_domains: Option>, /// If specified, only allow connections from these IP addresses /// @@ -987,7 +986,7 @@ impl ClientConfigBuilder { self.rpc_server = Some(Some(RpcServerConfig { bind_to, port: rpc_config.port.unwrap_or(consts::RPC_DEFAULT_PORT), - corsdomain: Some(rpc_config.corsdomain.clone()), + cors_domains: Some(rpc_config.cors_domains.clone()), allow_ips, allowed_methods: Some(rpc_config.methods.clone()), credentials, diff --git a/lib/src/config/config_file/client.example.toml b/lib/src/config/config_file/client.example.toml index a4395fe9d1..919bbf9507 100644 --- a/lib/src/config/config_file/client.example.toml +++ b/lib/src/config/config_file/client.example.toml @@ -205,6 +205,12 @@ sync_mode = "full" # Default: none #password = "secret" +# Configure Cross-Origin Resource Sharing for the JSON-RPC server. +# Example: ["*"] +# Example: ["https://nimiq.com", "https://github.com"] +# Default: [] +# cors_domains = [] + ############################################################################## # Metrics-server configuration. # diff --git a/lib/src/config/config_file/mod.rs b/lib/src/config/config_file/mod.rs index 41b3198be7..db8e828f8e 100644 --- a/lib/src/config/config_file/mod.rs +++ b/lib/src/config/config_file/mod.rs @@ -308,7 +308,7 @@ pub struct RpcServerSettings { pub bind: Option, pub port: Option, #[serde(default)] - pub corsdomain: Vec, + pub cors_domains: Vec, #[serde(default)] pub allowip: Vec, #[serde(default)] diff --git a/lib/src/extras/rpc_server.rs b/lib/src/extras/rpc_server.rs index 01331db6c8..3e49e91262 100644 --- a/lib/src/extras/rpc_server.rs +++ b/lib/src/extras/rpc_server.rs @@ -1,7 +1,7 @@ use std::{collections::HashSet, iter::FromIterator, sync::Arc}; use nimiq_jsonrpc_server::{ - AllowListDispatcher, Config, Credentials, ModularDispatcher, Server as _Server, + AllowListDispatcher, Config, Cors, Credentials, ModularDispatcher, Server as _Server, }; use nimiq_rpc_server::dispatchers::*; use nimiq_wallet::WalletStore; @@ -33,8 +33,13 @@ pub fn initialize_rpc_server( Some(HashSet::from_iter(allowed_methods)) }; - // TODO: Pass this to the rpc server config - let _corsdomain = config.corsdomain.unwrap_or_default(); + let cors_domains = config.cors_domains.unwrap_or_default(); + let is_cors_wildcard = cors_domains.iter().any(|origin| origin.trim() == "*"); + let cors_config = if is_cors_wildcard { + Cors::new().with_any_origin() + } else { + Cors::new().with_origins(cors_domains) + }; let mut dispatcher = ModularDispatcher::default(); @@ -68,6 +73,7 @@ pub fn initialize_rpc_server( enable_websocket: false, ip_whitelist: None, basic_auth, + cors: Some(cors_config), }, AllowListDispatcher::new(dispatcher, allowed_methods), ))