Skip to content

wasi-http: Ensure buffered body reads aren't dropped when guests pass a host-provided body - #15027

Merged
rvolosatovs merged 6 commits into
bytecodealliance:mainfrom
adamrk:abk/http-body-peek-bug
Oct 28, 2026
Merged

rvolosatovs merged 6 commits into
bytecodealliance:mainfrom
adamrk:abk/http-body-peek-bug

Conversation

@adamrk

@adamrk adamrk commented Oct 27, 2026

Copy link
Copy Markdown
Member

When a guest passes an http request/response to the host and the body contains a host provided StreamProducer, we take the fast path of downcasting the stream to the underlying http::Body. But if the guest read from the stream before passing it on, there may be a buffer of data that was read out of the StreamProducer, but not handled by the guest.

This change ensures that that buffered data is included when downcasting to an http::Body. Otherwise it will be silently dropped.

Resolves #15020

@adamrk
adamrk requested review from a team as code owners October 27, 2026 12:52
@adamrk
adamrk requested review from pchickey and rvolosatovs and removed request for a team October 27, 2026 12:52
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Oct 27, 2026

@rvolosatovs rvolosatovs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, few minor comments

Comment thread crates/wasi-http/src/p3/body.rs Outdated

/// A wrapper around [http_body::Body], which yields `prefix` before `body`
struct BodyWithPrefix<T> {
prefix: Option<Bytes>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about storing Bytes directly and checking for is_empty() instead of None?

Comment thread crates/wasi-http/src/p3/body.rs Outdated
Comment on lines +655 to +657
res.set_lower(hint.lower() + n);
if let Some(upper) = hint.upper() {
res.set_upper(upper + n);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; could we use saturating_add?

Comment thread crates/wasi-http/tests/all/p3/mod.rs Outdated
.body(http_body_util::Full::new(Bytes::from_static(
b"hello world",
)))?;
let res = run_http(P3_HTTP_FORWARD_BODY_COMPONENT, req, oneshot::channel().0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could oneshot::channel().0 be split into a separate assignment with a let, e.g. let (tx, _) = oneshot::channel()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was also thinking we should refactor run_http since that channel is really only used for some of the tests. What do you think about this additionally: #15041 ?

Comment thread crates/wasi-http/tests/all/p3/mod.rs Outdated
Comment on lines +1291 to +1293
/// Enable when https://github.com/bytecodealliance/wasmtime/issues/15026
/// has been fixed.
#[ignore]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this from a comment to a reason with ignore =? See https://doc.rust-lang.org/reference/attributes/testing.html#the-ignore-attribute

Comment thread crates/wasi-http/tests/all/p3/mod.rs Outdated
Comment on lines +1270 to +1272
.body(http_body_util::Full::new(Bytes::from_static(
b"hello world",
)))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we split this in at least two frames instead to test that the BodyWithPrefix actually forwards to the wrapped body correctly?

In the current test BodyWithPrefix never reads anything from the inner body, because it's read in full by HostBodyStreamProducer.

We could also consider adding trailers here

@adamrk
adamrk requested a review from rvolosatovs October 28, 2026 01:22
Comment thread crates/wasi-http/src/p3/body.rs
@rvolosatovs
rvolosatovs added this pull request to the merge queue Oct 28, 2026
Merged via the queue into bytecodealliance:main with commit c2f7a91 Oct 28, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasi-http p3: forwarding a partly read host body silently drops buffered bytes

2 participants