Skip to content

Commit 74ace2d

Browse files
gyscosclaude
andcommitted
Reset the borrowed context's session in raw::{Encoder,Decoder}::with_context
with_context stored the borrowed CCtx/DCtx as-is, so a stream abandoned on that context (an encoder dropped before finish(), a decoder not read to the end) carried over into the next one: one job's buffered plaintext became the start of the next job's frame, or of its decoded output. Reset the session (keeping parameters and dictionary) when wrapping a borrowed context, as reinit() already does between frames. Note that this also drops a pledged source size set on the context beforehand; set it on the encoder instead. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BCKFI60E. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 165d162 commit 74ace2d

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎src/stream/raw.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ impl Decoder<'static> {
165165

166166
impl<'a> Decoder<'a> {
167167
/// Creates a new decoder which employs the provided context for deserialization.
168+
///
169+
/// The context's session is reset first: anything left over from a frame
170+
/// that a previous user of the context did not finish is discarded.
171+
/// Parameters and dictionary are kept.
168172
pub fn with_context(context: &'a mut zstd_safe::DCtx<'static>) -> Self {
173+
// Otherwise we would start by handing out what is left of the
174+
// previous decoder's output. Resetting only the session cannot fail.
175+
let _ = context.reset(zstd_safe::ResetDirective::SessionOnly);
176+
169177
Self {
170178
context: MaybeOwnedDCtx::Borrowed(context),
171179
}
@@ -300,7 +308,16 @@ impl Encoder<'static> {
300308

301309
impl<'a> Encoder<'a> {
302310
/// Creates a new encoder that uses the provided context for serialization.
311+
///
312+
/// The context's session is reset first: anything left over from a frame
313+
/// that a previous user of the context did not finish is discarded, and
314+
/// so is the pledged source size (set it on the encoder instead).
315+
/// Parameters and dictionary are kept.
303316
pub fn with_context(context: &'a mut zstd_safe::CCtx<'static>) -> Self {
317+
// Otherwise the input buffered by the previous encoder would become
318+
// the start of our frame. Resetting only the session cannot fail.
319+
let _ = context.reset(zstd_safe::ResetDirective::SessionOnly);
320+
304321
Self {
305322
context: MaybeOwnedCCtx::Borrowed(context),
306323
}

‎src/stream/tests.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,41 @@ fn test_copy_compress_from_missing_file() {
405405
.is_err()
406406
);
407407
}
408+
409+
#[test]
410+
fn test_encoder_with_context_starts_a_new_frame() {
411+
use std::io::Write;
412+
413+
// An encoder is given up before `finish()`...
414+
let mut cctx = zstd_safe::CCtx::create();
415+
Encoder::with_context(Vec::new(), &mut cctx)
416+
.write_all(b"first job")
417+
.unwrap();
418+
419+
// ... and the next one on the same context does not inherit its input.
420+
let mut encoder = Encoder::with_context(Vec::new(), &mut cctx);
421+
encoder.write_all(b"second job").unwrap();
422+
let second = encoder.finish().unwrap();
423+
assert_eq!(decode_all(&second[..]).unwrap(), b"second job");
424+
}
425+
426+
#[test]
427+
fn test_decoder_with_context_starts_a_new_frame() {
428+
use std::io::Read;
429+
430+
let first = encode_all(&b"first job"[..], 1).unwrap();
431+
let second = encode_all(&b"second job"[..], 1).unwrap();
432+
433+
// A decoder is given up before all of its output has been read...
434+
let mut dctx = zstd_safe::DCtx::create();
435+
Decoder::with_context(&first[..], &mut dctx)
436+
.read_exact(&mut [0u8; 5])
437+
.unwrap();
438+
439+
// ... and the next one on the same context does not inherit the rest.
440+
let mut output = Vec::new();
441+
Decoder::with_context(&second[..], &mut dctx)
442+
.read_to_end(&mut output)
443+
.unwrap();
444+
assert_eq!(output, b"second job");
445+
}

0 commit comments

Comments
 (0)