From 368ee5e7ba1ef43c651667d9b1ca7fabc60984d8 Mon Sep 17 00:00:00 2001 From: Saloni Garg <26901250+saloni-garg@users.noreply.github.com> Date: Sun, 25 Oct 2026 13:28:23 -0700 Subject: [PATCH 1/2] descriptor: reject an editions file whose edition is outside the supported range --- .../unreleased/fixed-20261025-140000.yaml | 4 + buffa-descriptor/src/features.rs | 3 +- buffa-descriptor/src/pool.rs | 65 ++++++--- buffa-descriptor/tests/pool_e2e.rs | 24 ++-- .../tests/pool_unsupported_edition.rs | 136 ++++++++++++++++++ 5 files changed, 204 insertions(+), 28 deletions(-) create mode 100644 .changes/unreleased/fixed-20261025-140000.yaml create mode 100644 buffa-descriptor/tests/pool_unsupported_edition.rs diff --git a/.changes/unreleased/fixed-20261025-140000.yaml b/.changes/unreleased/fixed-20261025-140000.yaml new file mode 100644 index 000000000..f0264facc --- /dev/null +++ b/.changes/unreleased/fixed-20261025-140000.yaml @@ -0,0 +1,4 @@ +kind: Fixed +body: |- + **An editions file whose edition is outside `EDITION_PROTO2` through `EDITION_2024` no longer links** (#634). `DescriptorPool` accepted any other edition (a defined one such as `EDITION_UNSTABLE` or the test-only editions, or a number this version of buffa does not define at all, which decoding keeps as an unknown field) and silently resolved its features with the edition 2023 defaults, which are only right while the newer edition changes no runtime feature default. Such a file is now rejected with the new `PoolError::UnsupportedEdition`, which carries the file name and the edition number as written, matching protoc and protobuf-go. An unset or `EDITION_UNKNOWN` edition is still `PoolError::MissingEdition`. +time: 2026-10-25T14:00:00.000000000-07:00 diff --git a/buffa-descriptor/src/features.rs b/buffa-descriptor/src/features.rs index edbf4e555..c930f3221 100644 --- a/buffa-descriptor/src/features.rs +++ b/buffa-descriptor/src/features.rs @@ -32,7 +32,8 @@ use crate::generated::descriptor::{ /// A file with syntax `editions` and no edition that this crate defines /// resolves to the edition 2023 defaults. `DescriptorPool` (feature /// `reflect`) rejects such a file when its `edition` is unset or -/// `EDITION_UNKNOWN`, before it calls this function. +/// `EDITION_UNKNOWN`, or outside the supported range, before it calls +/// this function; the fallback here serves direct callers. #[must_use] pub fn for_file(file: &FileDescriptorProto) -> ResolvedFeatures { match file.syntax.as_deref() { diff --git a/buffa-descriptor/src/pool.rs b/buffa-descriptor/src/pool.rs index 3990d97cb..bdf2713b8 100644 --- a/buffa-descriptor/src/pool.rs +++ b/buffa-descriptor/src/pool.rs @@ -43,7 +43,7 @@ use crate::generated::descriptor::{ use buffa::editions::{ EnumType, FieldPresence, JsonFormat, MessageEncoding, RepeatedFieldEncoding, }; -use buffa::MessageField; +use buffa::{MessageField, UnknownFieldData}; /// Clone a descriptor's raw `*Options` into a boxed `Option`, the form the /// linked descriptors store. `None` for the common no-options case; one @@ -269,10 +269,17 @@ pub enum PoolError { /// A file declares syntax `editions` and its `edition` is unset or /// `EDITION_UNKNOWN`, so its feature defaults are undefined. /// - /// An edition number that this version of buffa does not define is a - /// different case: decoding keeps it as an unknown field, and the file - /// links with the edition 2023 defaults. + /// An edition that is set but outside the supported range is a + /// different case: see [`PoolError::UnsupportedEdition`]. MissingEdition { file: String }, + /// A file declares syntax `editions` with an edition outside the range + /// this version of buffa supports, `EDITION_PROTO2` through + /// `EDITION_2024`. Carries the file name and the edition number as + /// written, which may be one this crate does not define (decoding keeps + /// such a number as an unknown field). A later edition's feature + /// defaults are unknowable here, so the file is rejected rather than + /// linked with the edition 2023 defaults. + UnsupportedEdition { file: String, edition: i32 }, /// A file's `public_dependency` names an index outside its `dependency` /// list. The indices are positions in that list, so an out-of-range one /// names no import at all. @@ -705,6 +712,12 @@ impl core::fmt::Display for PoolError { "file {file} has syntax \"editions\" but its edition is unset or EDITION_UNKNOWN" ) } + Self::UnsupportedEdition { file, edition } => { + write!( + f, + "file {file} has syntax \"editions\" with unsupported edition {edition}" + ) + } Self::InvalidPublicDependencyIndex { file, index, @@ -1123,22 +1136,28 @@ impl std::error::Error for PoolError { } } -/// Whether `file` carries an edition other than `EDITION_UNKNOWN`. +/// The edition number `file` declares, as written, or `None` if it +/// declares none. /// /// `Edition` is a closed enum, so decoding leaves a number that this crate -/// does not define in the file's unknown fields with `edition` unset. Such a -/// file has an edition, which [`features::for_file`] resolves to the edition -/// 2023 defaults. -fn has_edition(file: &FileDescriptorProto) -> bool { +/// does not define in the file's unknown fields with `edition` unset; the +/// number is recovered from there so an unsupported edition can be reported +/// (and rejected) with the value the descriptor actually carries. +fn edition_number(file: &FileDescriptorProto) -> Option { /// Field number of `FileDescriptorProto.edition`. const EDITION_FIELD_NUMBER: u32 = 14; - match file.edition { - Some(edition) => edition != Edition::EDITION_UNKNOWN, - None => file - .__buffa_unknown_fields - .iter() - .any(|field| field.number == EDITION_FIELD_NUMBER), + if let Some(edition) = file.edition { + return Some(edition as i32); } + file.__buffa_unknown_fields.iter().find_map(|field| { + if field.number != EDITION_FIELD_NUMBER { + return None; + } + match field.data { + UnknownFieldData::Varint(value) => i32::try_from(value).ok(), + _ => None, + } + }) } /// What a fully-qualified name resolves to within the pool. @@ -1523,13 +1542,23 @@ impl DescriptorPool { } match file.syntax.as_deref() { None | Some("" | "proto2" | "proto3") => {} - Some("editions") => { - if !has_edition(file) { + Some("editions") => match edition_number(file) { + None | Some(0) => { return Err(PoolError::MissingEdition { file: file.name.clone().unwrap_or_default(), }); } - } + Some(edition) + if !(Edition::EDITION_PROTO2 as i32..=Edition::EDITION_2024 as i32) + .contains(&edition) => + { + return Err(PoolError::UnsupportedEdition { + file: file.name.clone().unwrap_or_default(), + edition, + }); + } + _ => {} + }, Some(syntax) => { return Err(PoolError::UnrecognizedSyntax { file: file.name.clone().unwrap_or_default(), diff --git a/buffa-descriptor/tests/pool_e2e.rs b/buffa-descriptor/tests/pool_e2e.rs index 1821362dc..cb0c63a26 100644 --- a/buffa-descriptor/tests/pool_e2e.rs +++ b/buffa-descriptor/tests/pool_e2e.rs @@ -719,7 +719,7 @@ fn editions_file_without_edition_is_rejected_without_mutating_pool() { } #[test] -fn editions_file_with_an_undefined_edition_number_links_with_2023_defaults() { +fn editions_file_with_an_undefined_edition_number_is_rejected() { use buffa::Message; use buffa_descriptor::generated::descriptor::field_descriptor_proto::Type; use buffa_descriptor::generated::descriptor::{ @@ -754,15 +754,21 @@ fn editions_file_with_an_undefined_edition_number_links_with_2023_defaults() { .encode_to_vec(); // The wire form carries the number, and decoding keeps it as an unknown - // field with `edition` unset. - let p = DescriptorPool::decode(&bytes).unwrap(); + // field with `edition` unset. The pool cannot know that edition's + // feature defaults, so it rejects the file with the number it carried + // instead of linking it with the edition 2023 defaults. + let err = match DescriptorPool::decode(&bytes) { + Ok(_) => panic!("an undefined edition number links"), + Err(err) => err, + }; + assert!(matches!( + err, + PoolError::UnsupportedEdition { ref file, edition: UNDEFINED_EDITION } + if file == "future-edition.proto" + )); assert_eq!( - p.message_by_name("FutureEdition") - .unwrap() - .field(1) - .unwrap() - .presence(), - FieldPresence::Explicit + err.to_string(), + "file future-edition.proto has syntax \"editions\" with unsupported edition 5000" ); } diff --git a/buffa-descriptor/tests/pool_unsupported_edition.rs b/buffa-descriptor/tests/pool_unsupported_edition.rs new file mode 100644 index 000000000..84d2eec7e --- /dev/null +++ b/buffa-descriptor/tests/pool_unsupported_edition.rs @@ -0,0 +1,136 @@ +//! An editions file whose edition is outside the range the pool defines +//! (`EDITION_PROTO2` through `EDITION_2024`) does not link: the pool cannot +//! know a later edition's feature defaults, so it rejects the file with +//! `PoolError::UnsupportedEdition` instead of silently using the edition +//! 2023 defaults. + +#![cfg(feature = "reflect")] + +use buffa::Message; +use buffa_descriptor::generated::descriptor::{Edition, FileDescriptorProto, FileDescriptorSet}; +use buffa_descriptor::{DescriptorPool, PoolError}; + +fn editions_file(name: &str, edition: Option) -> FileDescriptorProto { + FileDescriptorProto { + name: Some(name.into()), + syntax: Some("editions".into()), + edition, + ..Default::default() + } +} + +fn link(file: FileDescriptorProto) -> Result { + DescriptorPool::new(FileDescriptorSet { + file: vec![file], + ..Default::default() + }) +} + +fn assert_unsupported(file: FileDescriptorProto, name: &str, edition: i32) { + match link(file) { + Ok(_) => panic!("edition {edition} links, expected UnsupportedEdition"), + Err(err) => assert!( + matches!(&err, PoolError::UnsupportedEdition { file, edition: got } + if file == name && *got == edition), + "got {err:?}", + ), + } +} + +#[test] +fn defined_editions_outside_the_supported_range_are_rejected() { + for edition in [ + Edition::EDITION_LEGACY, + Edition::EDITION_UNSTABLE, + Edition::EDITION_1_TEST_ONLY, + Edition::EDITION_2_TEST_ONLY, + Edition::EDITION_99997_TEST_ONLY, + Edition::EDITION_99998_TEST_ONLY, + Edition::EDITION_99999_TEST_ONLY, + Edition::EDITION_MAX, + ] { + assert_unsupported( + editions_file("future.proto", Some(edition)), + "future.proto", + edition as i32, + ); + } +} + +#[test] +fn an_edition_number_the_pool_does_not_define_is_rejected_with_its_number() { + // Edition 1002 (a future EDITION_2026) cannot be written through the + // typed field: `Edition` is a closed enum, so decoding a real descriptor + // keeps the number as an unknown field. Hand-encode the set instead: + // FileDescriptorSet { file { name: "future.proto", syntax: "editions", + // edition: 1002 } }. + fn varint(mut value: u64, out: &mut Vec) { + loop { + let byte = (value & 0x7f) as u8; + value >>= 7; + if value == 0 { + out.push(byte); + return; + } + out.push(byte | 0x80); + } + } + let mut proto = Vec::new(); + proto.push(0x0a); // FileDescriptorProto.name (field 1, length-delimited) + varint(12, &mut proto); + proto.extend_from_slice(b"future.proto"); + proto.push(0x62); // FileDescriptorProto.syntax (field 12, length-delimited) + varint(8, &mut proto); + proto.extend_from_slice(b"editions"); + proto.push(0x70); // FileDescriptorProto.edition (field 14, varint) + varint(1002, &mut proto); + let mut set = Vec::new(); + set.push(0x0a); // FileDescriptorSet.file (field 1, length-delimited) + varint(proto.len() as u64, &mut set); + set.extend_from_slice(&proto); + + let decoded = FileDescriptorSet::decode(&mut &set[..]).expect("set decodes"); + match DescriptorPool::new(decoded) { + Ok(_) => panic!("edition 1002 links, expected UnsupportedEdition"), + Err(err) => assert!( + matches!(&err, PoolError::UnsupportedEdition { file, edition: 1002 } + if file == "future.proto"), + "got {err:?}", + ), + } +} + +#[test] +fn supported_editions_still_link() { + for edition in [ + Edition::EDITION_PROTO2, + Edition::EDITION_PROTO3, + Edition::EDITION_2023, + Edition::EDITION_2024, + ] { + link(editions_file("ok.proto", Some(edition))) + .unwrap_or_else(|err| panic!("edition {edition:?} should link: {err:?}")); + } +} + +#[test] +fn unset_and_unknown_editions_are_still_missing_edition() { + for edition in [None, Some(Edition::EDITION_UNKNOWN)] { + match link(editions_file("plain.proto", edition)) { + Ok(_) => panic!("edition {edition:?} links, expected MissingEdition"), + Err(err) => assert!( + matches!(&err, PoolError::MissingEdition { file } if file == "plain.proto"), + "got {err:?}", + ), + } + } +} + +#[test] +fn the_edition_gate_only_applies_to_editions_files() { + // A proto3 file that happens to carry an edition value is judged by its + // syntax, as before; the edition field means nothing there. + let mut file = editions_file("legacy.proto", Some(Edition::EDITION_UNSTABLE)); + file.syntax = Some("proto3".into()); + link(file).expect("a proto3 file links regardless of its edition field"); +} From 06fda69ecf4c186026118a554d8f0c022cf37df1 Mon Sep 17 00:00:00 2001 From: Saloni Garg Date: Tue, 24 Nov 2026 14:11:37 -0800 Subject: [PATCH 2/2] rerun ci