Repository navigation
feat(id-store): validate numeric registry write keys #12899
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| //! Numeric registry write validation (slice A9122-numeric-registry, issue | ||
| //! #10277; design A-9122.1, S4.64-S4.73). | ||
| //! | ||
| //! A numeric registry is a derived namespace over the array-address core. Its | ||
| //! write boundary admits only the numeric kinds named by its policy and uses | ||
| //! the numeric comparator's operational budget. The domain remains unbounded: | ||
| //! a large value is accepted whenever its encoded parts fit the configured | ||
| //! budget. | ||
| //! | ||
| //! This module deliberately does not own namespace generations or | ||
| //! authorization. Callers first admit a key here, then perform the namespace | ||
| //! mutation through [`crate::namespace_registry::RegistryState`] with its | ||
| //! existing [`crate::namespace_registry::RegistryAuthorizer`] seam. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| use id_core::numeric_order; | ||
| use id_core::numeric_value::{NumericError, NumericLimits, NumericValue}; | ||
|
|
||
| /// The version of this numeric write-policy contract. | ||
| pub const POLICY_VERSION: u64 = 1; | ||
|
|
||
| /// The three stable [`NumericValue`] kind tags. | ||
| pub const ALL_KINDS: u8 = (1 << id_core::numeric_value::KIND_INTEGER) | ||
| | (1 << id_core::numeric_value::KIND_DECIMAL) | ||
| | (1 << id_core::numeric_value::KIND_BINARY_FLOAT); | ||
|
|
||
| /// Namespace-specific numeric write policy. | ||
| /// | ||
| /// `allowed_kinds` is a bit mask over the stable numeric kind tags. The | ||
| /// `limits` field is operational only; it is never a semantic magnitude or | ||
| /// precision maximum. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub struct NumericNamespacePolicy { | ||
| /// Version of the policy contract. | ||
| pub version: u64, | ||
| /// Allowed [`NumericValue`] kinds, represented as `1 << kind_tag`. | ||
| pub allowed_kinds: u8, | ||
| /// Parser/comparator resource budget. | ||
| pub limits: NumericLimits, | ||
| } | ||
|
|
||
| impl NumericNamespacePolicy { | ||
| /// A V3 ordered numeric policy admitting all three kinds. | ||
| #[must_use] | ||
| pub const fn ordered() -> Self { | ||
| Self { | ||
| version: POLICY_VERSION, | ||
| allowed_kinds: ALL_KINDS, | ||
| limits: NumericLimits { | ||
| max_magnitude_bytes: id_core::numeric_value::DEFAULT_MAX_MAGNITUDE_BYTES, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Creates a policy with an explicit kind mask and operational budget. | ||
| #[must_use] | ||
| pub const fn with_limits(allowed_kinds: u8, limits: NumericLimits) -> Self { | ||
| Self { | ||
| version: POLICY_VERSION, | ||
| allowed_kinds, | ||
| limits, | ||
| } | ||
| } | ||
|
|
||
| fn allows(&self, value: &NumericValue) -> bool { | ||
| self.allowed_kinds & (1 << value.kind_tag()) != 0 | ||
| } | ||
| } | ||
|
|
||
| /// A validated numeric key ready for an array-core write. | ||
| /// | ||
| /// The inner value is private so callers cannot construct a key without | ||
| /// passing through [`NumericRegistry::admit`]. Namespace generation and | ||
| /// authorization still belong to `RegistryState`. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct NumericKey(NumericValue); | ||
|
|
||
| impl NumericKey { | ||
| /// The admitted numeric value. | ||
| #[must_use] | ||
| pub const fn value(&self) -> &NumericValue { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| /// Why a numeric namespace write was refused. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum NumericRegistryError { | ||
| /// The value's kind is not enabled by the namespace policy. | ||
| KindNotAllowed { | ||
| /// The stable kind tag that was presented. | ||
| kind: u8, | ||
| }, | ||
| /// The value exceeded an operational parser/comparator budget. | ||
| Numeric(NumericError), | ||
| } | ||
|
|
||
| impl NumericRegistryError { | ||
| /// Stable error-envelope class. | ||
| #[must_use] | ||
| pub const fn code(&self) -> &'static str { | ||
| match self { | ||
| Self::KindNotAllowed { .. } => "kind_not_allowed", | ||
| Self::Numeric(error) => error.code(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for NumericRegistryError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Self::KindNotAllowed { kind } => write!(f, "numeric kind {kind} is not allowed"), | ||
| Self::Numeric(error) => write!(f, "numeric key rejected: {error}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for NumericRegistryError {} | ||
|
|
||
| impl From<NumericError> for NumericRegistryError { | ||
| fn from(error: NumericError) -> Self { | ||
| Self::Numeric(error) | ||
| } | ||
| } | ||
|
|
||
| /// The numeric write boundary for one namespace policy. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub struct NumericRegistry { | ||
| policy: NumericNamespacePolicy, | ||
| } | ||
|
|
||
| impl NumericRegistry { | ||
| /// Creates a validator for a namespace policy. | ||
| #[must_use] | ||
| pub const fn new(policy: NumericNamespacePolicy) -> Self { | ||
| Self { policy } | ||
| } | ||
|
|
||
| /// The policy this validator applies. | ||
| #[must_use] | ||
| pub const fn policy(&self) -> NumericNamespacePolicy { | ||
| self.policy | ||
| } | ||
|
|
||
| /// Admits a key for a namespace write. | ||
| /// | ||
| /// Kind validation happens before comparator work. The self-comparison | ||
| /// then exercises the exact comparator's budget checks without imposing a | ||
| /// semantic maximum on the numeric value. The caller must pass the | ||
| /// returned key to its array write and use `RegistryState` for the | ||
| /// authorized, versioned namespace mutation. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// [`NumericRegistryError::KindNotAllowed`] when the namespace policy | ||
| /// rejects the stable kind, or [`NumericRegistryError::Numeric`] when an | ||
| /// operational budget is exceeded. | ||
| pub fn admit(&self, value: NumericValue) -> Result<NumericKey, NumericRegistryError> { | ||
| if !self.policy.allows(&value) { | ||
| return Err(NumericRegistryError::KindNotAllowed { | ||
| kind: value.kind_tag(), | ||
| }); | ||
| } | ||
| numeric_order::compare_with(&value, &value, self.policy.limits)?; | ||
| Ok(NumericKey(value)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| //! Integration tests for the A9122 numeric registry write boundary. | ||
| //! | ||
| //! Oracle: a small independent policy procedure checks the allowed kind mask, | ||
| //! while the existing numeric comparator is the operational-budget oracle. | ||
| //! Registry generation and authorization remain exercised through the | ||
| //! `RegistryState` seam rather than being reimplemented in this test. | ||
|
|
||
| #![allow(clippy::expect_used, clippy::unwrap_used)] | ||
|
|
||
| use id_core::namespace_definition::{ | ||
| CoreKind, NamespaceDefinition, NamespaceId, RegistryId, Token, | ||
| }; | ||
| use id_core::numeric_value::{BigInt, NumericLimits, NumericValue}; | ||
| use id_store::namespace_registry::{AdminScopeAuthorizer, MutationContext, RegistryState}; | ||
| use id_store::numeric_registry::{NumericNamespacePolicy, NumericRegistry, NumericRegistryError}; | ||
|
|
||
| fn definition(name: &str) -> NamespaceDefinition { | ||
| NamespaceDefinition { | ||
| namespace: NamespaceId::new(name).unwrap(), | ||
| core: CoreKind::Array, | ||
| handler: Token::new("handler", "numeric").unwrap(), | ||
| encoding_profile: Token::new("encoding", "numeric-v1").unwrap(), | ||
| read_policy: Token::new("read", "anyone").unwrap(), | ||
| write_policy: Token::new("write", "owner").unwrap(), | ||
| shorthand_policy: Token::new("shorthand", "none").unwrap(), | ||
| schema_version: 1, | ||
| } | ||
| } | ||
|
|
||
| fn admin() -> MutationContext { | ||
| MutationContext::new("operator", "tenant-a", "registry-admin").unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_a_kind_not_allowed_by_the_namespace_policy_at_admission() { | ||
|
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new admission boundary is covered only by four fixed examples, leaving its invariants across kind masks, numeric kinds and budget boundaries without the property/model-based coverage required by the root AGENTS.md reference: AGENTS.md:L37-L39 Useful? React with 👍 / 👎. |
||
| let policy = NumericNamespacePolicy::with_limits( | ||
| 1 << id_core::numeric_value::KIND_INTEGER, | ||
| NumericLimits::default(), | ||
| ); | ||
| let registry = NumericRegistry::new(policy); | ||
| let error = registry | ||
| .admit(NumericValue::decimal(&BigInt::from_i64(3), &BigInt::zero())) | ||
| .unwrap_err(); | ||
| assert_eq!(error, NumericRegistryError::KindNotAllowed { kind: 1 }); | ||
| assert_eq!(error.code(), "kind_not_allowed"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn configured_budget_is_resource_limit_not_numeric_semantics() { | ||
| let registry = NumericRegistry::new(NumericNamespacePolicy::with_limits( | ||
| id_store::numeric_registry::ALL_KINDS, | ||
| NumericLimits { | ||
| max_magnitude_bytes: 8, | ||
| }, | ||
| )); | ||
| let value = NumericValue::integer(BigInt::from_be_magnitude(false, &[0xff; 9])); | ||
| let error = registry.admit(value).unwrap_err(); | ||
| assert_eq!(error.code(), "resource_limit"); | ||
| assert!(matches!(error, NumericRegistryError::Numeric(_))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn large_valid_value_is_admitted_within_operational_budget() { | ||
| let registry = NumericRegistry::new(NumericNamespacePolicy::with_limits( | ||
| id_store::numeric_registry::ALL_KINDS, | ||
| NumericLimits { | ||
| max_magnitude_bytes: 8 * 1024, | ||
| }, | ||
| )); | ||
| let digits = "9".repeat(5_000); | ||
| let value = NumericValue::decimal( | ||
| &BigInt::parse(&digits).unwrap(), | ||
| &BigInt::parse("-400").unwrap(), | ||
| ); | ||
| let key = registry.admit(value.clone()).unwrap(); | ||
| assert_eq!(key.value(), &value); | ||
| } | ||
|
|
||
| #[test] | ||
| fn admitted_key_does_not_bypass_versioned_registry_authorization() { | ||
| let validator = NumericRegistry::new(NumericNamespacePolicy::ordered()); | ||
| let key = validator | ||
| .admit(NumericValue::integer(BigInt::from_i64(7))) | ||
| .unwrap(); | ||
| assert_eq!(key.value().kind_tag(), id_core::numeric_value::KIND_INTEGER); | ||
|
|
||
| let mut state = RegistryState::new(RegistryId::server("numeric").unwrap()); | ||
| let authorizer = AdminScopeAuthorizer::new("registry-admin"); | ||
| assert_eq!( | ||
| state.create(&authorizer, &admin(), 0, definition("numbers")), | ||
| Ok(1) | ||
| ); | ||
| assert_eq!(state.generation(), 1); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For large nonzero decimal keys within the configured budget, admission performs two quadratic decimal conversions merely to validate magnitude lengths:
numeric_order::compare_withcallscmp_decimal_magnitude, which converts both coefficients throughBigInt::to_decimal_string. With the default 1 MiB coefficient budget, this adds substantial CPU work to each admission despite the comparison result being unused. Expose or reuse a budget-only validator instead, so admitting an already-canonicalNumericValuedoes not require decimal conversion.Useful? React with 👍 / 👎.