Skip to content

Commit 991142d

Browse files
Merge pull request #16 from RelationalAI/ag-bypass-path-validation
Bypass Path validation
2 parents d2ad7b8 + 6688ddc commit 991142d

File tree

4 files changed

+27
-52
lines changed

4 files changed

+27
-52
lines changed

src/crud_ops.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CResult, Config, NotifyGuard, SQ, clients, dyn_connect, static_config, Request};
1+
use crate::{CResult, Config, NotifyGuard, SQ, clients, dyn_connect, static_config, Request, util::cstr_to_path};
22

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

@@ -174,13 +174,7 @@ pub extern "C" fn get(
174174
) -> CResult {
175175
let response = unsafe { ResponseGuard::new(response, handle) };
176176
let path = unsafe { std::ffi::CStr::from_ptr(path) };
177-
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
178-
Ok(p) => p,
179-
Err(e) => {
180-
response.into_error(e);
181-
return CResult::Error;
182-
}
183-
};
177+
let path = unsafe{ cstr_to_path(path) };
184178
let slice = unsafe { std::slice::from_raw_parts_mut(buffer, size) };
185179
let config = unsafe { & (*config) };
186180
match SQ.get() {
@@ -216,13 +210,7 @@ pub extern "C" fn put(
216210
) -> CResult {
217211
let response = unsafe { ResponseGuard::new(response, handle) };
218212
let path = unsafe { std::ffi::CStr::from_ptr(path) };
219-
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
220-
Ok(p) => p,
221-
Err(e) => {
222-
response.into_error(e);
223-
return CResult::Error;
224-
}
225-
};
213+
let path = unsafe{ cstr_to_path(path) };
226214
let slice = unsafe { std::slice::from_raw_parts(buffer, size) };
227215
let config = unsafe { & (*config) };
228216
match SQ.get() {
@@ -256,13 +244,7 @@ pub extern "C" fn delete(
256244
) -> CResult {
257245
let response = unsafe { ResponseGuard::new(response, handle) };
258246
let path = unsafe { std::ffi::CStr::from_ptr(path) };
259-
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
260-
Ok(p) => p,
261-
Err(e) => {
262-
response.into_error(e);
263-
return CResult::Error;
264-
}
265-
};
247+
let path = unsafe{ cstr_to_path(path) };
266248
let config = unsafe { & (*config) };
267249
match SQ.get() {
268250
Some(sq) => {

src/list.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, Request};
1+
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, Request, util::cstr_to_path};
22

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

@@ -175,13 +175,7 @@ pub extern "C" fn list(
175175
) -> CResult {
176176
let response = unsafe { ListResponseGuard::new(response, handle) };
177177
let prefix = unsafe { std::ffi::CStr::from_ptr(prefix) };
178-
let prefix: Path = match Path::parse(prefix.to_str().expect("invalid utf8")) {
179-
Ok(p) => p,
180-
Err(e) => {
181-
response.into_error(e);
182-
return CResult::Error;
183-
}
184-
};
178+
let prefix = unsafe{ cstr_to_path(prefix) };
185179
let config = unsafe { & (*config) };
186180
match SQ.get() {
187181
Some(sq) => {
@@ -283,13 +277,7 @@ pub extern "C" fn list_stream(
283277
) -> CResult {
284278
let response = unsafe { ListStreamResponseGuard::new(response, handle) };
285279
let prefix = unsafe { std::ffi::CStr::from_ptr(prefix) };
286-
let prefix: Path = match Path::parse(prefix.to_str().expect("invalid utf8")) {
287-
Ok(p) => p,
288-
Err(e) => {
289-
response.into_error(e);
290-
return CResult::Error;
291-
}
292-
};
280+
let prefix = unsafe{ cstr_to_path(prefix) };
293281
let config = unsafe { & (*config) };
294282
match SQ.get() {
295283
Some(sq) => {

src/stream.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{CResult, Config, NotifyGuard, SQ, RT, clients, dyn_connect, static_config, Request};
2-
use crate::util::{size_to_ranges, Compression, with_decoder, with_encoder};
2+
use crate::util::{size_to_ranges, Compression, with_decoder, with_encoder, cstr_to_path};
33
use crate::error::{should_retry_logic, extract_error_info, backoff_duration_for_retry};
44

55
use object_store::{path::Path, ObjectStore};
@@ -222,13 +222,7 @@ pub extern "C" fn get_stream(
222222
) -> CResult {
223223
let response = unsafe { GetStreamResponseGuard::new(response, handle) };
224224
let path = unsafe { std::ffi::CStr::from_ptr(path) };
225-
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
226-
Ok(p) => p,
227-
Err(e) => {
228-
response.into_error(e);
229-
return CResult::Error;
230-
}
231-
};
225+
let path = unsafe{ cstr_to_path(path) };
232226
let decompress = match Compression::try_from(decompress) {
233227
Ok(c) => c,
234228
Err(e) => {
@@ -536,13 +530,7 @@ pub extern "C" fn put_stream(
536530
) -> CResult {
537531
let response = unsafe { PutStreamResponseGuard::new(response, handle) };
538532
let path = unsafe { std::ffi::CStr::from_ptr(path) };
539-
let path: Path = match Path::parse(path.to_str().expect("invalid utf8")) {
540-
Ok(p) => p,
541-
Err(e) => {
542-
response.into_error(e);
543-
return CResult::Error;
544-
}
545-
};
533+
let path = unsafe{ cstr_to_path(path) };
546534
let compress = match Compression::try_from(compress) {
547535
Ok(c) => c,
548536
Err(e) => {

src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ pub(crate) fn with_encoder(compression: Compression, writer: impl AsyncWrite + U
9898
}
9999
}
100100
}
101+
102+
// Safety: This must match the layout of object_store::path::Path
103+
#[allow(dead_code)]
104+
struct RawPath {
105+
raw: String,
106+
}
107+
108+
// This is a workaround to create an object_store::path::Path from a String while skipping
109+
// validation
110+
pub(crate) unsafe fn cstr_to_path(cstr: &std::ffi::CStr) -> object_store::path::Path {
111+
let raw_path = RawPath {
112+
raw: cstr.to_str().expect("invalid utf8").to_string()
113+
};
114+
115+
let path: object_store::path::Path = std::mem::transmute(raw_path);
116+
return path;
117+
}

0 commit comments

Comments
 (0)