-
Notifications
You must be signed in to change notification settings - Fork 0
fix: harden SDK validation #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ use tokio::sync::OnceCell; | |
| use url::Url; | ||
|
|
||
| use crate::error::{HonchoError, Result}; | ||
| use crate::http::client::HttpClient; | ||
| use crate::http::client::{HttpClient, normalize_base_url}; | ||
| use crate::http::routes; | ||
| use crate::peer::Peer; | ||
| use crate::session::{PeerSpec, Session}; | ||
|
|
@@ -92,15 +92,12 @@ impl Honcho { | |
| /// # Ok::<(), honcho_ai::error::HonchoError>(()) | ||
| /// ``` | ||
| pub fn new(base_url: &str, workspace_id: &str) -> Result<Self> { | ||
| if workspace_id.is_empty() { | ||
| return Err(HonchoError::Configuration( | ||
| "workspace_id must not be empty".into(), | ||
| )); | ||
| } | ||
| let http = | ||
| HttpClient::from_params(HttpClient::builder().base_url(base_url.to_string()).build())?; | ||
| let url = Url::parse(base_url) | ||
| .map_err(|e| HonchoError::Configuration(format!("invalid base_url: {e}")))?; | ||
| validate_workspace_id(workspace_id)?; | ||
| let url = normalize_base_url(base_url)?; | ||
| let http = HttpClient::from_params_with_base_url( | ||
| HttpClient::builder().base_url(base_url.to_owned()).build(), | ||
| url.clone(), | ||
| )?; | ||
| Ok(Self { | ||
| inner: Arc::new(Inner { | ||
| http, | ||
|
|
@@ -154,16 +151,10 @@ impl Honcho { | |
| .or_else(|| std::env::var("HONCHO_WORKSPACE_ID").ok()) | ||
| .unwrap_or_else(|| "default".to_owned()); | ||
|
|
||
| if resolved_workspace_id.is_empty() { | ||
| return Err(HonchoError::Configuration( | ||
| "workspace_id must not be empty".into(), | ||
| )); | ||
| } | ||
|
|
||
| let base_url = Url::parse(&resolved_base_url) | ||
| .map_err(|e| HonchoError::Configuration(format!("invalid base_url: {e}")))?; | ||
| validate_workspace_id(&resolved_workspace_id)?; | ||
| let base_url = normalize_base_url(&resolved_base_url)?; | ||
|
|
||
| let http = HttpClient::from_params( | ||
| let http = HttpClient::from_params_with_base_url( | ||
| HttpClient::builder() | ||
| .base_url(resolved_base_url) | ||
| .maybe_api_key(resolved_api_key) | ||
|
|
@@ -173,6 +164,7 @@ impl Honcho { | |
| .default_headers(params.default_headers.unwrap_or_default()) | ||
| .default_query(params.default_query.unwrap_or_default()) | ||
| .build(), | ||
| base_url.clone(), | ||
| )?; | ||
|
|
||
| Ok(Self { | ||
|
|
@@ -252,10 +244,15 @@ impl Honcho { | |
|
|
||
| /// Fetch workspace metadata from the server. | ||
| pub async fn get_metadata(&self) -> Result<HashMap<String, Value>> { | ||
| let body = crate::types::workspace::WorkspaceCreate { | ||
| id: self.inner.workspace_id.clone(), | ||
| metadata: None, | ||
| configuration: None, | ||
| }; | ||
| let ws: Workspace = self | ||
| .inner | ||
| .http | ||
| .get(&routes::workspace(self.workspace_id())?, &[]) | ||
| .post(&routes::workspaces(), Some(&body), &[]) | ||
| .await?; | ||
|
Comment on lines
246
to
256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change from If the server is guaranteed to return |
||
| Ok(ws.metadata) | ||
| } | ||
|
|
@@ -282,10 +279,15 @@ impl Honcho { | |
| /// } | ||
| /// ``` | ||
| pub async fn get_configuration(&self) -> Result<WorkspaceConfiguration> { | ||
| let body = crate::types::workspace::WorkspaceCreate { | ||
| id: self.inner.workspace_id.clone(), | ||
| metadata: None, | ||
| configuration: None, | ||
| }; | ||
| let ws: Workspace = self | ||
| .inner | ||
| .http | ||
| .get(&routes::workspace(self.workspace_id())?, &[]) | ||
| .post(&routes::workspaces(), Some(&body), &[]) | ||
| .await?; | ||
| Ok(ws.configuration) | ||
| } | ||
|
|
@@ -320,10 +322,15 @@ impl Honcho { | |
| /// Use this when the server returns fields not yet represented in | ||
| /// [`WorkspaceConfiguration`]. | ||
| pub async fn get_configuration_raw(&self) -> Result<HashMap<String, Value>> { | ||
| let body = crate::types::workspace::WorkspaceCreate { | ||
| id: self.inner.workspace_id.clone(), | ||
| metadata: None, | ||
| configuration: None, | ||
| }; | ||
| let raw: serde_json::Value = self | ||
| .inner | ||
| .http | ||
| .get(&routes::workspace(self.workspace_id())?, &[]) | ||
| .post(&routes::workspaces(), Some(&body), &[]) | ||
| .await?; | ||
| match raw.get("configuration") { | ||
| Some(serde_json::Value::Object(map)) => { | ||
|
|
@@ -557,6 +564,11 @@ impl Honcho { | |
| session_id: Option<&str>, | ||
| observed_peer: Option<&str>, | ||
| ) -> Result<()> { | ||
| if observer.is_empty() { | ||
| return Err(HonchoError::Validation( | ||
| "observer must not be empty".to_string(), | ||
| )); | ||
| } | ||
| self.ensure_workspace().await?; | ||
| let observed_peer = observed_peer.unwrap_or(observer); | ||
| let body = crate::types::dream::ScheduleDreamRequest { | ||
|
|
@@ -623,6 +635,8 @@ impl Honcho { | |
|
|
||
| /// List peers with filters. | ||
| /// | ||
| /// `page` is 1-based. `size` must be in `1..=100`. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```no_run | ||
|
|
@@ -691,6 +705,8 @@ impl Honcho { | |
|
|
||
| /// List sessions with filters. | ||
| /// | ||
| /// `page` is 1-based. `size` must be in `1..=100`. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```no_run | ||
|
|
@@ -756,3 +772,28 @@ impl Honcho { | |
| Ok(page.map(|ws| ws.id)) | ||
| } | ||
| } | ||
|
|
||
| fn validate_workspace_id(workspace_id: &str) -> Result<()> { | ||
| if workspace_id.is_empty() { | ||
| return Err(HonchoError::Configuration( | ||
| "workspace_id must not be empty".into(), | ||
| )); | ||
| } | ||
|
|
||
| if workspace_id.len() > 512 { | ||
| return Err(HonchoError::Configuration( | ||
| "workspace_id must be at most 512 characters".into(), | ||
| )); | ||
| } | ||
|
|
||
| if !workspace_id | ||
| .bytes() | ||
| .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-')) | ||
| { | ||
| return Err(HonchoError::Configuration( | ||
| "workspace_id must match [a-zA-Z0-9_-]+".into(), | ||
| )); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.