Skip to content
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

fix: correct ArrayBuffer validation in arrayBufferToBase64 #122

Merged
merged 3 commits into from
Jan 21, 2025
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ endif
echo "Got: $$error_msg"; \
exit 1; \
fi
@extism call examples/base64.wasm greet --wasi --input="Benjamin" --log-level=debug

compile-examples: cli
cd examples/react && npm install && npm run build && cd ../..
Expand All @@ -81,6 +82,7 @@ compile-examples: cli
./target/release/extism-js examples/host_funcs/script.js -i examples/host_funcs/script.d.ts -o examples/host_funcs.wasm
./target/release/extism-js examples/exports/script.js -i examples/exports/script.d.ts -o examples/exports.wasm
./target/release/extism-js examples/exception/script.js -i examples/exception/script.d.ts -o examples/exception.wasm
./target/release/extism-js examples/base64/script.js -i examples/base64/script.d.ts -o examples/base64.wasm

kitchen:
cd examples/kitchen-sink && npm install && npm run build && cd ../..
Expand Down
35 changes: 11 additions & 24 deletions crates/core/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,37 +187,24 @@ fn build_host_object<'js>(this: Ctx<'js>) -> anyhow::Result<Object<'js>> {
this.clone(),
MutFn::new(move |cx: Ctx<'js>, args: Rest<Value<'js>>| {
let data = args.first().unwrap();
if !data.is_array() {
return Err::<_, rquickjs::Error>(to_js_error(
cx.clone(),
anyhow!("Expect an array buffer"),
));
}

let Some(data) = data.as_array() else {
return Err::<_, rquickjs::Error>(to_js_error(
cx.clone(),
anyhow!("Could not convert arg to array buffer"),
));
};
let data = data
.as_object()
.ok_or_else(|| to_js_error(cx.clone(), anyhow!("Expected an ArrayBuffer")))?;

if !data.is_array_buffer() {
return Err::<_, rquickjs::Error>(to_js_error(
cx.clone(),
anyhow!("Expected an array buffer"),
));
return Err(to_js_error(cx.clone(), anyhow!("Expected an ArrayBuffer")));
}

let Some(data) = data.as_array_buffer() else {
return Err::<_, rquickjs::Error>(to_js_error(
cx.clone(),
anyhow!("Could not convert the array to an arrayBuffer"),
));
};
use base64::prelude::*;
let bytes = data
.as_array_buffer()
.expect("Should be able to cast data as an array buffer")
.as_bytes()
.ok_or_else(|| to_js_error(cx.clone(), anyhow!("Could not convert to bytes")))?;
.ok_or_else(|| {
to_js_error(cx.clone(), anyhow!("Could not get bytes from ArrayBuffer"))
})?;

use base64::prelude::*;
let as_string = BASE64_STANDARD.encode(bytes);

rquickjs::String::from_str(cx.clone(), &as_string)
Expand Down
8 changes: 8 additions & 0 deletions examples/base64/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"lib": [],
"types": [
"../../crates/core/src/prelude"
]
}
}
3 changes: 3 additions & 0 deletions examples/base64/script.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "main" {
export function greet(): I32;
}
22 changes: 22 additions & 0 deletions examples/base64/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function greet() {
try {
const base64String = "SGVsbG8g8J+MjSBXb3JsZCHwn4yN";

console.log('decoding string:', base64String);

const decodedBuffer = Host.base64ToArrayBuffer(base64String);
const decodedString = new TextDecoder().decode(decodedBuffer);

console.log('decoded string:', decodedString);

const encodedBuffer = Host.arrayBufferToBase64(decodedBuffer);

console.log('encoded string:', encodedBuffer);

Host.outputString(`Hello, ${Host.inputString()}`)
} catch (error) {
console.error('Base64 test failed, Error:', JSON.stringify(error));
}
}

module.exports = { greet };
2 changes: 1 addition & 1 deletion examples/kitchen-sink/package-lock.json

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

1 change: 1 addition & 0 deletions examples/kitchen-sink/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export function greet() {
const mem = Memory.fromString("Hello, " + body.data + " " + configLastName);
Host.outputString(mem.readString()); // TODO: ideally have a way to output memory directly
mem.free();

return 0;
}
Loading