Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions implants/lib/eldritchv2/stdlib/eldritch-libhttp/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::HttpLibrary;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use eldritch_core::Value;
use eldritch_macros::eldritch_library_impl;
use spin::RwLock;
Expand All @@ -12,20 +11,27 @@ use spin::RwLock;
pub struct HttpLibraryFake;

impl HttpLibrary for HttpLibraryFake {
fn download(&self, _url: String, _path: String, _insecure: Option<bool>) -> Result<(), String> {
fn download(
&self,
_uri: String,
_dst: String,
_allow_insecure: Option<bool>,
) -> Result<(), String> {
Ok(())
}

fn get(
&self,
url: String,
uri: String,
_query_params: Option<BTreeMap<String, String>>,
_headers: Option<BTreeMap<String, String>>,
_allow_insecure: Option<bool>,
) -> Result<BTreeMap<String, Value>, String> {
let mut map = BTreeMap::new();
map.insert("status_code".into(), Value::Int(200));
map.insert(
"body".into(),
Value::Bytes(format!("Mock GET response from {}", url).into_bytes()),
Value::Bytes(format!("Mock GET response from {}", uri).into_bytes()),
);

// Mock headers
Expand All @@ -44,9 +50,11 @@ impl HttpLibrary for HttpLibraryFake {

fn post(
&self,
url: String,
body: Option<Vec<u8>>,
uri: String,
body: Option<String>,
_form: Option<BTreeMap<String, String>>,
_headers: Option<BTreeMap<String, String>>,
_allow_insecure: Option<bool>,
) -> Result<BTreeMap<String, Value>, String> {
let mut map = BTreeMap::new();
map.insert("status_code".into(), Value::Int(201));
Expand All @@ -56,7 +64,7 @@ impl HttpLibrary for HttpLibraryFake {
Value::Bytes(
format!(
"Mock POST response from {}, received {} bytes",
url, body_len
uri, body_len
)
.into_bytes(),
),
Expand Down Expand Up @@ -84,7 +92,7 @@ mod tests {
#[test]
fn test_http_fake_get() {
let http = HttpLibraryFake;
let resp = http.get("http://example.com".into(), None).unwrap();
let resp = http.get("http://example.com".into(), None, None, None).unwrap();
assert_eq!(resp.get("status_code").unwrap(), &Value::Int(200));
if let Value::Bytes(b) = resp.get("body").unwrap() {
assert_eq!(
Expand All @@ -100,7 +108,13 @@ mod tests {
fn test_http_fake_post() {
let http = HttpLibraryFake;
let resp = http
.post("http://example.com".into(), Some(vec![1, 2, 3]), None)
.post(
"http://example.com".into(),
Some("abc".into()),
None,
None,
None,
)
.unwrap();
assert_eq!(resp.get("status_code").unwrap(), &Value::Int(201));
if let Value::Bytes(b) = resp.get("body").unwrap() {
Expand Down
34 changes: 20 additions & 14 deletions implants/lib/eldritchv2/stdlib/eldritch-libhttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use eldritch_core::Value;
use eldritch_macros::{eldritch_library, eldritch_method};

Expand All @@ -19,30 +18,31 @@ pub mod std;
/// It supports:
/// - GET and POST requests.
/// - File downloading.
/// - Custom headers.
///
/// **Note**: TLS validation behavior depends on the underlying agent configuration and may not be exposed per-request in this version of the library (unlike v1 which had `allow_insecure` arg).
/// - Custom headers and query parameters.
pub trait HttpLibrary {
#[eldritch_method]
/// Downloads a file from a URL to a local path.
///
/// **Parameters**
/// - `url` (`str`): The URL to download from.
/// - `path` (`str`): The local destination path.
/// - `insecure` (`Option<bool>`): If true, ignore SSL certificate verification (insecure).
/// - `uri` (`str`): The URL to download from.
/// - `dst` (`str`): The local destination path.
/// - `allow_insecure` (`Option<bool>`): If true, ignore SSL certificate verification.
/// **Returns**
/// - `None`
///
/// **Errors**
/// - Returns an error string if the download fails.
fn download(&self, url: String, path: String, insecure: Option<bool>) -> Result<(), String>;
fn download(&self, uri: String, dst: String, allow_insecure: Option<bool>)
-> Result<(), String>;

#[eldritch_method]
/// Performs an HTTP GET request.
///
/// **Parameters**
/// - `url` (`str`): The target URL.
/// - `uri` (`str`): The target URL.
/// - `query_params` (`Option<Dict<str, str>>`): Optional query parameters.
/// - `headers` (`Option<Dict<str, str>>`): Optional custom HTTP headers.
/// - `allow_insecure` (`Option<bool>`): If true, ignore SSL certificate verification.
///
/// **Returns**
/// - `Dict`: A dictionary containing the response:
Expand All @@ -54,17 +54,21 @@ pub trait HttpLibrary {
/// - Returns an error string if the request fails.
fn get(
&self,
url: String,
uri: String,
query_params: Option<BTreeMap<String, String>>,
headers: Option<BTreeMap<String, String>>,
allow_insecure: Option<bool>,
) -> Result<BTreeMap<String, Value>, String>;

#[eldritch_method]
/// Performs an HTTP POST request.
///
/// **Parameters**
/// - `url` (`str`): The target URL.
/// - `body` (`Option<Bytes>`): The request body.
/// - `uri` (`str`): The target URL.
/// - `body` (`Option<str>`): The request body.
/// - `form` (`Option<Dict<str, str>>`): Form data (application/x-www-form-urlencoded).
/// - `headers` (`Option<Dict<str, str>>`): Optional custom HTTP headers.
/// - `allow_insecure` (`Option<bool>`): If true, ignore SSL certificate verification.
///
/// **Returns**
/// - `Dict`: A dictionary containing the response:
Expand All @@ -76,8 +80,10 @@ pub trait HttpLibrary {
/// - Returns an error string if the request fails.
fn post(
&self,
url: String,
body: Option<Vec<u8>>,
uri: String,
body: Option<String>,
form: Option<BTreeMap<String, String>>,
headers: Option<BTreeMap<String, String>>,
allow_insecure: Option<bool>,
) -> Result<BTreeMap<String, Value>, String>;
}
Loading