Skip to content

Commit

Permalink
Merge pull request #9 from ybensacq/feature/dev-321-protect-set-addre…
Browse files Browse the repository at this point in the history
…ss-rpc

feat(rpc): protect set address rpc
  • Loading branch information
ybensacq authored Apr 5, 2024
2 parents ae2a76c + e65903a commit cbf08d5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/katana/core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,3 @@ fn filter_events_by_params(
}
(filtered_events, index)
}

4 changes: 2 additions & 2 deletions crates/katana/rpc/rpc-api/src/katana.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use katana_core::accounts::Account;
use katana_primitives::FieldElement;
use katana_core::hooker::HookerAddresses;
use katana_primitives::FieldElement;

#[cfg_attr(not(feature = "client"), rpc(server, namespace = "katana"))]
#[cfg_attr(feature = "client", rpc(client, server, namespace = "katana"))]
pub trait KatanaApi {
#[method(name = "setSolisAddresses")]
async fn set_addresses(&self, addresses: HookerAddresses) -> RpcResult<()>;
async fn set_addresses(&self, addresses: HookerAddresses, basic_auth: String) -> RpcResult<()>;

#[method(name = "generateBlock")]
async fn generate_block(&self) -> RpcResult<()>;
Expand Down
2 changes: 2 additions & 0 deletions crates/katana/rpc/rpc-types/src/error/katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum KatanaApiError {
FailedToDumpState = 2,
#[error("Failed to update storage.")]
FailedToUpdateStorage = 3,
#[error("Authentication failed.")]
AuthenticationFailed = 4,
}

impl From<KatanaApiError> for Error {
Expand Down
1 change: 1 addition & 0 deletions crates/katana/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ katana-rpc-types-builder = { path = "../rpc-types-builder" }
katana-tasks = { path = "../../tasks" }

anyhow.workspace = true
base64 = "0.13.0"
cairo-lang-starknet = "2.3.1"
flate2.workspace = true
futures.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/katana/rpc/rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub struct ServerConfig {
pub host: String,
pub max_connections: u32,
pub apis: Vec<ApiKind>,
pub rpc_user: String,
pub rpc_password: String,
}

impl ServerConfig {
Expand Down
34 changes: 29 additions & 5 deletions crates/katana/rpc/rpc/src/katana.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
use std::sync::Arc;

use crate::config::ServerConfig;
use base64::decode;
use jsonrpsee::core::{async_trait, Error};
use katana_core::accounts::Account;
use katana_core::hooker::HookerAddresses;
use katana_core::sequencer::KatanaSequencer;
use katana_primitives::FieldElement;
use katana_rpc_api::katana::KatanaApiServer;
use katana_rpc_types::error::katana::KatanaApiError;
use std::sync::Arc;

pub struct KatanaApi {
sequencer: Arc<KatanaSequencer>,
config: ServerConfig,
}

impl KatanaApi {
pub fn new(sequencer: Arc<KatanaSequencer>) -> Self {
Self { sequencer }
pub fn new(sequencer: Arc<KatanaSequencer>, config: ServerConfig) -> Self {
Self { sequencer, config }
}

fn verify_basic_auth(&self, encoded_credentials: &str) -> bool {
if let Ok(credentials) = decode(encoded_credentials) {
if let Ok(credentials_str) = String::from_utf8(credentials) {
let parts: Vec<&str> = credentials_str.split(':').collect();
if parts.len() == 2 {
let (username, password) = (parts[0], parts[1]);
return username == self.config.rpc_user
&& password == self.config.rpc_password;
}
}
}
false
}
}

#[async_trait]
impl KatanaApiServer for KatanaApi {
async fn set_addresses(&self, addresses: HookerAddresses) -> Result<(), Error> {
async fn set_addresses(
&self,
addresses: HookerAddresses,
basic_auth: String,
) -> Result<(), Error> {
if !self.verify_basic_auth(&basic_auth) {
panic!("authentication failed");
}

self.sequencer.set_addresses(addresses).await;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/rpc/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub async fn spawn(sequencer: Arc<KatanaSequencer>, config: ServerConfig) -> Res
methods.merge(StarknetApi::new(sequencer.clone()).into_rpc())?;
}
ApiKind::Katana => {
methods.merge(KatanaApi::new(sequencer.clone()).into_rpc())?;
methods.merge(KatanaApi::new(sequencer.clone(), config.clone()).into_rpc())?;
}
}
}
Expand Down

0 comments on commit cbf08d5

Please sign in to comment.