Skip to content

Commit

Permalink
Merge pull request #16 from RelationalAI/ag-bypass-path-validation
Browse files Browse the repository at this point in the history
Bypass Path validation
  • Loading branch information
andrebsguedes authored Feb 29, 2024
2 parents d2ad7b8 + 6688ddc commit 991142d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 52 deletions.
26 changes: 4 additions & 22 deletions src/crud_ops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CResult, Config, NotifyGuard, SQ, clients, dyn_connect, static_config, Request};
use crate::{CResult, Config, NotifyGuard, SQ, clients, dyn_connect, static_config, Request, util::cstr_to_path};

use object_store::{path::Path, ObjectStore};

Expand Down Expand Up @@ -174,13 +174,7 @@ pub extern "C" fn get(
) -> CResult {
let response = unsafe { ResponseGuard::new(response, handle) };
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let path = unsafe{ cstr_to_path(path) };
let slice = unsafe { std::slice::from_raw_parts_mut(buffer, size) };
let config = unsafe { & (*config) };
match SQ.get() {
Expand Down Expand Up @@ -216,13 +210,7 @@ pub extern "C" fn put(
) -> CResult {
let response = unsafe { ResponseGuard::new(response, handle) };
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let path = unsafe{ cstr_to_path(path) };
let slice = unsafe { std::slice::from_raw_parts(buffer, size) };
let config = unsafe { & (*config) };
match SQ.get() {
Expand Down Expand Up @@ -256,13 +244,7 @@ pub extern "C" fn delete(
) -> CResult {
let response = unsafe { ResponseGuard::new(response, handle) };
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let path = unsafe{ cstr_to_path(path) };
let config = unsafe { & (*config) };
match SQ.get() {
Some(sq) => {
Expand Down
18 changes: 3 additions & 15 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, Request};
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, Request, util::cstr_to_path};

use object_store::{path::Path, ObjectStore, ObjectMeta};

Expand Down Expand Up @@ -175,13 +175,7 @@ pub extern "C" fn list(
) -> CResult {
let response = unsafe { ListResponseGuard::new(response, handle) };
let prefix = unsafe { std::ffi::CStr::from_ptr(prefix) };
let prefix: Path = match Path::parse(prefix.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let prefix = unsafe{ cstr_to_path(prefix) };
let config = unsafe { & (*config) };
match SQ.get() {
Some(sq) => {
Expand Down Expand Up @@ -283,13 +277,7 @@ pub extern "C" fn list_stream(
) -> CResult {
let response = unsafe { ListStreamResponseGuard::new(response, handle) };
let prefix = unsafe { std::ffi::CStr::from_ptr(prefix) };
let prefix: Path = match Path::parse(prefix.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let prefix = unsafe{ cstr_to_path(prefix) };
let config = unsafe { & (*config) };
match SQ.get() {
Some(sq) => {
Expand Down
18 changes: 3 additions & 15 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, static_config, Request};
use crate::util::{size_to_ranges, Compression, with_decoder, with_encoder};
use crate::util::{size_to_ranges, Compression, with_decoder, with_encoder, cstr_to_path};
use crate::error::{should_retry_logic, extract_error_info, backoff_duration_for_retry};

use object_store::{path::Path, ObjectStore};
Expand Down Expand Up @@ -222,13 +222,7 @@ pub extern "C" fn get_stream(
) -> CResult {
let response = unsafe { GetStreamResponseGuard::new(response, handle) };
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let path = unsafe{ cstr_to_path(path) };
let decompress = match Compression::try_from(decompress) {
Ok(c) => c,
Err(e) => {
Expand Down Expand Up @@ -536,13 +530,7 @@ pub extern "C" fn put_stream(
) -> CResult {
let response = unsafe { PutStreamResponseGuard::new(response, handle) };
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
Ok(p) => p,
Err(e) => {
response.into_error(e);
return CResult::Error;
}
};
let path = unsafe{ cstr_to_path(path) };
let compress = match Compression::try_from(compress) {
Ok(c) => c,
Err(e) => {
Expand Down
17 changes: 17 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,20 @@ pub(crate) fn with_encoder(compression: Compression, writer: impl AsyncWrite + U
}
}
}

// Safety: This must match the layout of object_store::path::Path
#[allow(dead_code)]
struct RawPath {
raw: String,
}

// This is a workaround to create an object_store::path::Path from a String while skipping
// validation
pub(crate) unsafe fn cstr_to_path(cstr: &std::ffi::CStr) -> object_store::path::Path {
let raw_path = RawPath {
raw: cstr.to_str().expect("invalid utf8").to_string()
};

let path: object_store::path::Path = std::mem::transmute(raw_path);
return path;
}

0 comments on commit 991142d

Please sign in to comment.