diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ec3d5d4..95bf070c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ and the project aims to follow [Semantic Versioning](https://semver.org/spec/v2. ## [Unreleased] +### Upgrade notes +- **Upgrade udmd before nefd** (#2154). nefd refreshes its UDM Nudm_EE legs with + `add /reportingOptions/expiry` (#2133), which a udmd from before #2133 answers 400. nefd falls + back once to the whole-object form, which keeps legs alive across a rolling upgrade but replaces + the UDM's other `reportingOptions` members, so upgrade udmd first. + ### Added - udmd's durable state file (#1836) gets a volume on Kubernetes (#1885). The Helm chart's `udm.durableState` (off by default) mounts a ReadWriteOnce claim, or an emptyDir, at diff --git a/docs-book/src/configuration/nef.md b/docs-book/src/configuration/nef.md index d4173b8a..37d2e7f3 100644 --- a/docs-book/src/configuration/nef.md +++ b/docs-book/src/configuration/nef.md @@ -4,7 +4,7 @@ The NEF (Network Exposure Function, `nextgcore-nefd`, TS 23.501 §6.2.5) exposes - **Monitoring Event** (TS 29.122 §5.3): `POST /3gpp-monitoring-event/v1/{scsAsId}/subscriptions` and `DELETE .../subscriptions/{subscriptionId}`. A create becomes a southbound subscription toward the AMF (`Namf_EventExposure`, TS 29.518) or the UDM (`Nudm_EE`, TS 29.503). - **Device Triggering** (TS 29.122 §5.10): `POST /3gpp-device-triggering/v1/{scsAsId}/transactions`. -- **`Nnef_EventExposure`** (TS 29.591): `POST /nnef-eventexposure/v1/subscriptions`, `DELETE .../subscriptions/{subId}`, and the producer notification sink `POST /nnef-eventexposure/v1/notify/{subId}`, which forwards AMF/UDM notifications to the AF's `notificationDestination` as a TS 29.122 MonitoringNotification. +- **`Nnef_EventExposure`** (TS 29.591): `POST /nnef-eventexposure/v1/subscriptions`, `DELETE .../subscriptions/{subId}`, and the producer notification sink `POST /nnef-eventexposure/v1/notify/{subId}`, which forwards AMF/UDM notifications to the subscriber's notification URI in the shape of the API it subscribed on (#2154): a TS 29.122 MonitoringNotification for a T8 subscription, a TS 29.591 `NefEventExposureNotif` (`notifId`, and one `eventNotifs` entry per report with the subscribed `event` and its `timeStamp`) for a Nnef_EventExposure one. **Ceiling:** TS 29.591 defines no member for a location, reachability or loss-of-connectivity report, so a Nnef_EventExposure notification says which event occurred and when, and no more; the T8 API carries the report itself. It registers with the NRF as `nfType: NEF` advertising `nnef-eventexposure`. PFD management, traffic influence and parameter provision are out of scope (`src/bins/nextgcore-nefd/src/main.rs` module docs). @@ -61,5 +61,5 @@ Everything else is configured by flags. - **Southbound is best-effort.** With no producer URI, or an unreachable producer, the northbound subscription is still created and the southbound leg is retried by the reconciler until `NEF_PENDING_LEG_DEADLINE_SECS`. - **Cancellation-safe create and delete (#1877).** A monitoring-subscription create runs on its own task, so an AF that goes away while the producer's subscribe is out cannot leave a producer subscription recorded nowhere. A 201 the AF never read is removed together with its producer leg. A delete removes the record first, then sends the producer DELETE on its own task. - **Producer unsubscribes are retried (#1877).** A southbound DELETE that fails (no answer, a non-2xx other than 404) is parked and retried every 30 s at the producer the subscription was made at (the URI configured at the time, for a subscription recorded before #1877), until the producer answers 2xx or 404. Within a pass, a producer that fails once is not tried again, and a tried unsubscribe goes to the back of the queue, so a blackholed producer cannot starve the others. Across passes, a failing producer backs off: 30 s after its first failure, doubled after each further one up to 10 min, and cleared by a delivery. A producer with nothing left parked is forgotten, so one that never comes back does not stay tracked (#1994). So a dead producer costs one request timeout per backoff period rather than one per pass. One parked for 24 h is given up and counted `expired`. At most 4096 are kept, and the oldest by park time, wherever rotation has put it, is evicted and counted `abandoned`. With a state file (#2086), the unsubscribes still owed are written to the same snapshot as the subscriptions. One is recorded before the removal of its subscription is persisted and before its DELETE is sent, and it leaves the snapshot once delivered. So a restart retries every unsubscribe the NEF owes, and its 24 h counts from the restart. Without a state file, nothing is kept across a restart. Metrics `nef_southbound_unsubscribes_pending`, `nef_southbound_unsubscribes_parked_total{producer}` and `nef_southbound_unsubscribes_reconciled_total{result}` are served on `/metrics` in a demo build, and are otherwise read in-process. -- **Southbound subscriptions expire unless refreshed (#2102).** Every AMF Namf_EventExposure or UDM Nudm_EE subscribe asks for an expiry `NEF_SOUTHBOUND_LIFETIME_SECS` ahead: `subscription.options.expiry`, or `reportingOptions.expiry` for the UDM. The NEF records the expiry the producer confirms. On every reconcile tick, on a task of its own, it refreshes a leg once half its lifetime or less is left. Legs are refreshed per producer, up to 8 producers at once. A producer that fails a refresh is not tried again that pass, so a dead producer costs one request timeout per tick and other producers' legs are still refreshed (#2128). The modifications are `replace /options/expiry` at the AMF, and `add /reportingOptions/expiry` at the UDM: the member, so the UDM's other `reportingOptions` members are kept (RFC 6902 `add` on `/reportingOptions` replaces the object). A leg made before #2102 has no `reportingOptions` and gets the whole object (#2133). If a refresh is answered 404, or the expiry passes before a refresh gets through, the subscription is removed. In the second case its DELETE is also sent. **The AF is told (#2133):** whenever the NEF removes a subscription on its own initiative (here, or a deferred leg past `NEF_PENDING_LEG_DEADLINE_SECS`), it sends the AF a MonitoringNotification naming the subscription with `cancelInd: true` and no reports (TS 29.122). Best effort, like every AF notification. A DELETE the AF asked for is not notified. So a subscription the NEF holds no record of, because its create timed out after the producer took it, is never refreshed and lapses at the producer. nextgcore's `udmd` enforces the EE expiry and grants a refresh (#2125). +- **Southbound subscriptions expire unless refreshed (#2102).** Every AMF Namf_EventExposure or UDM Nudm_EE subscribe asks for an expiry `NEF_SOUTHBOUND_LIFETIME_SECS` ahead: `subscription.options.expiry`, or `reportingOptions.expiry` for the UDM. The NEF records the expiry the producer confirms. On every reconcile tick, on a task of its own, it refreshes a leg once half its lifetime or less is left. Legs are refreshed per producer, up to 8 producers at once. A producer that fails a refresh is not tried again that pass, so a dead producer costs one request timeout per tick and other producers' legs are still refreshed (#2128). The modifications are `replace /options/expiry` at the AMF, and `add /reportingOptions/expiry` at the UDM: the member, so the UDM's other `reportingOptions` members are kept (RFC 6902 `add` on `/reportingOptions` replaces the object). A leg made before #2102 has no `reportingOptions` and gets the whole object (#2133). If a refresh is answered 404, or the expiry passes before a refresh gets through, the subscription is removed. In the second case its DELETE is also sent. **The AF is told (#2133):** whenever the NEF removes a subscription on its own initiative (here, or a deferred leg past `NEF_PENDING_LEG_DEADLINE_SECS`), it sends the AF a MonitoringNotification naming the subscription with `cancelInd: true` and no reports (TS 29.122). Best effort, like every AF notification. A DELETE the AF asked for is not notified. TS 29.591 defines no such indication for a Nnef_EventExposure consumer, so one is sent nothing (#2154). **Upgrade order (#2154): upgrade udmd before nefd.** A udmd from before #2133 accepts only top-level patch paths and answers `add /reportingOptions/expiry` 400. nefd then retries that refresh once with the whole `reportingOptions` object, so legs are still refreshed during a rolling upgrade, but that form replaces the UDM's other `reportingOptions` members. So a subscription the NEF holds no record of, because its create timed out after the producer took it, is never refreshed and lapses at the producer. nextgcore's `udmd` enforces the EE expiry and grants a refresh (#2125). - **GPSI resolution needs the UDM.** nextgcore's own `udmd` serves `id-translation-result`, which resolves a GPSI through the UDR's `identity-data`. diff --git a/src/bins/nextgcore-nefd/src/context.rs b/src/bins/nextgcore-nefd/src/context.rs index 5252ac94..33b7b5f3 100644 --- a/src/bins/nextgcore-nefd/src/context.rs +++ b/src/bins/nextgcore-nefd/src/context.rs @@ -191,6 +191,11 @@ pub struct NefMonitoringSubscription { /// first refresh sweep bounds. #[serde(default)] pub southbound_expiry: Option, + /// #2154: the northbound API the subscription was made on, which decides + /// the shape of what its consumer is sent (TS 29.122 MonitoringNotification + /// or TS 29.591 NefEventExposureNotif). T8 for a record from before #2154. + #[serde(default)] + pub northbound_api: crate::NorthboundApi, } impl NefMonitoringSubscription { @@ -232,9 +237,16 @@ impl NefMonitoringSubscription { raw: raw.into(), notify_token: Some(nextgcore_sbi::callback_auth::CallbackToken::mint()), southbound_expiry: None, + northbound_api: crate::NorthboundApi::default(), } } + /// Record the northbound API it was made on (#2154). + pub fn with_northbound_api(mut self, api: crate::NorthboundApi) -> Self { + self.northbound_api = api; + self + } + /// Record the external identity the AF used (issue #110). pub fn with_af_target(mut self, target: Option) -> Self { self.af_target = target; diff --git a/src/bins/nextgcore-nefd/src/main.rs b/src/bins/nextgcore-nefd/src/main.rs index 0a4fcabc..5e261ead 100644 --- a/src/bins/nextgcore-nefd/src/main.rs +++ b/src/bins/nextgcore-nefd/src/main.rs @@ -813,9 +813,12 @@ async fn nef_sbi_request_handler(request: SbiRequest) -> SbiResponse { /// request and the resource path of the created subscription. Parameterising the /// one handler is what keeps them from drifting: a fix to the failure semantics or /// the target resolution applies to both by construction. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum NorthboundApi { - /// TS 29.122 §5.3 T8 Monitoring Event. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)] +pub enum NorthboundApi { + /// TS 29.122 §5.3 T8 Monitoring Event. The default, so a subscription + /// recorded before #2154 (every one of them made on either API) reads as + /// T8, the shape it was notified in. + #[default] T8MonitoringEvent, /// TS 29.591 `Nnef_EventExposure`, the service the NF profile advertises. NnefEventExposure, @@ -1278,7 +1281,9 @@ async fn handle_monitoring_subscription_create_inner( body.clone(), ) .with_af_target(af_target) - .with_owner_id(owner_id); + .with_owner_id(owner_id) + // #2154: notifications go out in the shape of the API it was made on. + .with_northbound_api(api); let notify_base = notify_base.unwrap_or_else(|| "http://127.0.0.1:7817".to_string()); let nf_instance_id = nf_instance_id.unwrap_or_else(|| "nef-unregistered".to_string()); @@ -1946,6 +1951,17 @@ fn remove_and_tell_af(sub_id: &str) { let Some(sub) = removed else { return; }; + // #2154: TS 29.591 defines no cancellation indication for a + // Nnef_EventExposure subscriber (NefEventExposureNotif carries notifId and + // eventNotifs only), so none is invented: the ceiling is in nef.md. + if sub.northbound_api == NorthboundApi::NnefEventExposure { + log::info!( + "Nnef_EventExposure subscription {} removed; TS 29.591 has no cancellation \ + indication to send its consumer", + sub.id + ); + return; + } let notification = cancellation_notification(&sub); if tokio::runtime::Handle::try_current().is_err() { log::warn!( @@ -2116,7 +2132,24 @@ async fn refresh_producer_legs( requested, sub.southbound_expiry.is_some(), ); - match client.patch_json(&southbound.delete_path, &patch).await { + let mut answer = client.patch_json(&southbound.delete_path, &patch).await; + // #2154 (R42 on #2152): a UDM from before #2133 accepts only top-level + // patch paths and answers the member form 400. Fall back, once, to the + // whole-object form it does accept, so its legs are refreshed rather than + // left to lapse during a rolling upgrade (nef.md: upgrade udmd first). + if southbound.producer == SouthboundProducer::Udm + && sub.southbound_expiry.is_some() + && matches!(&answer, Ok(resp) if resp.status == 400) + { + log::warn!( + "monitoring subscription {}: the UDM refused `add /reportingOptions/expiry` \ + (400; a udmd older than #2133?); retrying with the whole reportingOptions", + sub.id + ); + let whole = refresh_patch(southbound.producer, requested, false); + answer = client.patch_json(&southbound.delete_path, &whole).await; + } + match answer { Ok(resp) if resp.is_success() => { let expiry = confirmed_expiry(southbound.producer, &resp).unwrap_or(requested); let recorded = nef_self() @@ -2727,6 +2760,8 @@ fn build_monitoring_notification( .map(|t| (t.member(), serde_json::json!(t.value()))); let mut reports = Vec::new(); + // #2154: each forwarded report's producer timestamp, for the TS 29.591 shape. + let mut times = Vec::new(); let report_list = producer_body .get("reportList") .and_then(|v| v.as_array()) @@ -2809,16 +2844,59 @@ fn build_monitoring_notification( out[*member] = value.clone(); } reports.push(out); + times.push( + report + .get("timeStamp") + .and_then(|t| t.as_str()) + .map(str::to_string), + ); } if reports.is_empty() { return None; } + if sub.northbound_api == NorthboundApi::NnefEventExposure { + return Some(nnef_event_exposure_notif(sub, ×)); + } Some(serde_json::json!({ "subscription": self_link, "monitoringEventReports": reports, })) } +/// #2154 (R42 on #2152): the TS 29.591 `NefEventExposureNotif` for a +/// Nnef_EventExposure subscriber: `notifId` (the consumer's, stored as the +/// subscription's `scs_as_id`) and one `NefEventNotification` per forwarded +/// report, each its `event` and `timeStamp` (both mandatory). The `event` is +/// the eventId the consumer subscribed to, the T8 MonitoringType mapped back +/// ([`NNEF_EVENT_TO_MONITORING_TYPE`]); `NefEvent` admits it through its +/// free-form string branch. +/// +/// Ceiling, documented in nef.md: TS 29.591 defines no member for a location, +/// a reachability or a loss-of-connectivity report, so the notification says +/// which event occurred and when, and nothing more. The T8 API carries the +/// report itself. +fn nnef_event_exposure_notif( + sub: &NefMonitoringSubscription, + times: &[Option], +) -> serde_json::Value { + let event = NNEF_EVENT_TO_MONITORING_TYPE + .iter() + .find(|(_, t8)| *t8 == sub.monitoring_type) + .map(|(id, _)| *id) + .unwrap_or(sub.monitoring_type.as_str()); + let now = nextgcore_sbi::datetime::epoch_to_rfc3339(now_unix_secs()); + let notifs: Vec = times + .iter() + .map(|t| { + serde_json::json!({ + "event": event, + "timeStamp": t.clone().unwrap_or_else(|| now.clone()), + }) + }) + .collect(); + serde_json::json!({ "notifId": sub.scs_as_id, "eventNotifs": notifs }) +} + /// POST a MonitoringNotification to the AF's notificationDestination /// (best-effort, bounded timeouts; failures are logged, no retry). async fn forward_notification_to_af( @@ -4174,6 +4252,165 @@ mod tests { reset_context(); } + /// **#2154** (R42 on #2152): TS 29.591 defines no cancellation indication, + /// so a Nnef_EventExposure subscriber whose subscription the NEF removes is + /// sent nothing, while a T8 one in the same sweep gets its `cancelInd`. + /// Then the upgrade fallback: a UDM answering the member-form refresh 400 (a + /// udmd from before #2133) is sent the whole-object form once, and the leg + /// is refreshed. + /// + /// REVERT-VERIFIED: with the Nnef branch of `remove_and_tell_af` removed, + /// the Nnef consumer is POSTed a T8 cancellation and "nothing for the Nnef + /// consumer" fails; with the 400 fallback removed, the old UDM's leg is not + /// refreshed and "the old UDM's leg is refreshed" fails. + #[test] + fn nnef_removals_send_nothing_and_an_old_udm_is_refreshed_2154() { + let _guard = lock_globals(); + // Loopback plaintext stand-ins (the default profile is production). + nextgcore_sbi::security::set_sbi_profile_override(nextgcore_sbi::security::SbiProfile::Dev); + southbound_reconcile::clear_for_test(); + reset_context(); + block_on(async { + let af_seen: Seen2102 = Arc::new(std::sync::Mutex::new(Vec::new())); + let sink = af_seen.clone(); + let (af, af_addr) = + nextgcore_sbi::test_support::sbi_server_on_free_port(move |req: SbiRequest| { + let sink = sink.clone(); + async move { + let body = req + .http + .content + .as_deref() + .and_then(|c| serde_json::from_str(c).ok()) + .unwrap_or_default(); + sink.lock().unwrap().push(( + req.header.method.clone(), + req.header.uri.clone(), + body, + )); + SbiResponse::with_status(204) + } + }) + .await; + // A producer that answers every PATCH 404 at `/gone/...`, and at + // `/old/...` behaves as a pre-#2133 udmd: 400 for a nested path, + // 204 for the whole object. + let patches: Seen2102 = Arc::new(std::sync::Mutex::new(Vec::new())); + let psink = patches.clone(); + let (producer, paddr) = + nextgcore_sbi::test_support::sbi_server_on_free_port(move |req: SbiRequest| { + let psink = psink.clone(); + async move { + let body: serde_json::Value = req + .http + .content + .as_deref() + .and_then(|c| serde_json::from_str(c).ok()) + .unwrap_or_default(); + psink.lock().unwrap().push(( + req.header.method.clone(), + req.header.uri.clone(), + body.clone(), + )); + if req.header.uri.contains("/gone/") { + return SbiResponse::with_status(404); + } + if body[0]["path"] == "/reportingOptions/expiry" { + return SbiResponse::with_status(400); + } + SbiResponse::with_status(204) + } + }) + .await; + let uri = format!("http://127.0.0.1:{}", paddr.port()); + set_producers(None, Some(&uri)); + let now = now_unix_secs(); + let leg = |api: NorthboundApi, scs: &str, path: &str| { + let mut sub = NefMonitoringSubscription::new( + scs, + "LOSS_OF_CONNECTIVITY", + format!("http://127.0.0.1:{}/af-2154", af_addr.port()), + "{}", + ) + .with_northbound_api(api); + sub.southbound = Some(SouthboundRef { + producer: SouthboundProducer::Udm, + subscription_id: path.rsplit('/').next().unwrap().to_string(), + delete_path: path.to_string(), + producer_base: Some(uri.clone()), + }); + sub.southbound_expiry = Some(now + 10); + let id = sub.id.clone(); + nef_self() + .read() + .unwrap() + .subscription_insert(sub) + .expect("insert"); + id + }; + let nnef_gone = leg(NorthboundApi::NnefEventExposure, "notif-2154", "/gone/n"); + let t8_gone = leg(NorthboundApi::T8MonitoringEvent, "af1", "/gone/t"); + let old = leg(NorthboundApi::T8MonitoringEvent, "af1", "/old/o"); + + let report = refresh_live_subscriptions_at(now).await; + assert_eq!((report.lapsed, report.refreshed), (2, 1), "{report:?}"); + let want_t8 = serde_json::json!({ + "subscription": format!("/3gpp-monitoring-event/v1/af1/subscriptions/{t8_gone}"), + "cancelInd": true, + }); + let mut t8_told = false; + for _ in 0..200 { + if af_seen + .lock() + .unwrap() + .iter() + .any(|(_, _, b)| *b == want_t8) + { + t8_told = true; + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + assert!(t8_told, "the T8 consumer gets its cancelInd"); + tokio::time::sleep(Duration::from_millis(100)).await; + // One guard at a time: the message must not lock it again. + let delivered = af_seen.lock().unwrap().clone(); + assert_eq!( + delivered.len(), + 1, + "nothing for the Nnef consumer ({nnef_gone}): {delivered:?}" + ); + let old_patches: Vec = patches + .lock() + .unwrap() + .iter() + .filter(|(m, u, _)| m == "PATCH" && u.contains("/old/")) + .map(|(_, _, b)| b[0]["path"].clone()) + .collect(); + assert_eq!( + old_patches, + [ + serde_json::json!("/reportingOptions/expiry"), + serde_json::json!("/reportingOptions") + ], + "the member form, then the whole object once" + ); + assert!( + nef_self() + .read() + .unwrap() + .subscription_find(&old) + .and_then(|s| s.southbound_expiry) + .is_some_and(|e| e > now + 10), + "the old UDM's leg is refreshed" + ); + af.stop().await.ok(); + producer.stop().await.ok(); + }); + southbound_reconcile::clear_for_test(); + reset_context(); + } + /// #2133 (R42 on #2128): the UDM refresh names `reportingOptions/expiry` /// for a leg made with reportingOptions, so RFC 6902 `add` does not /// replace the object's other members; a leg made before #2102 (none @@ -5256,6 +5493,16 @@ mod tests { // The subscription really exists and is DELETE-able through the same API. let sub_id = location.rsplit('/').next().unwrap().to_string(); + // #2154: and it is recorded as made on Nnef_EventExposure, which decides + // the shape of its notifications. + assert_eq!( + nef_self() + .read() + .unwrap() + .subscription_find(&sub_id) + .map(|s| s.northbound_api), + Some(NorthboundApi::NnefEventExposure) + ); let delete = SbiRequest::delete(format!("/nnef-eventexposure/v1/subscriptions/{sub_id}")); assert_eq!(block_on(nef_sbi_request_handler(delete)).status, 204); @@ -5749,6 +5996,62 @@ mod tests { ); } + /// **#2154** (R42 on #2152): a Nnef_EventExposure subscriber is notified in + /// the TS 29.591 NefEventExposureNotif shape (notifId, eventNotifs[{event, + /// timeStamp}]), with the eventId it subscribed to, not the T8 + /// MonitoringNotification; a T8 subscriber still gets T8. + /// + /// REVERT-VERIFIED: with the API branch removed, the Nnef subscriber gets the + /// T8 body and the `notifId` assertion fails. + #[test] + fn a_nnef_subscriber_is_notified_in_the_ts_29_591_shape_2154() { + let producer_body = serde_json::json!({ + "reportList": [{ + "type": "REACHABILITY_REPORT", + "reachability": "REACHABLE", + "timeStamp": "2026-11-20T03:00:00Z", + }] + }); + let nnef = NefMonitoringSubscription::new( + "consumer-notif-2154", + "UE_REACHABILITY", + "http://consumer.example.com/cb", + "{}", + ) + .with_northbound_api(NorthboundApi::NnefEventExposure); + assert_eq!( + build_monitoring_notification(&nnef, &producer_body), + Some(serde_json::json!({ + "notifId": "consumer-notif-2154", + "eventNotifs": [{ "event": "UE_REACHABILITY", "timeStamp": "2026-11-20T03:00:00Z" }], + })) + ); + let t8 = NefMonitoringSubscription::new( + "af1", + "UE_REACHABILITY", + "http://af.example.com/cb", + "{}", + ); + let body = build_monitoring_notification(&t8, &producer_body).expect("T8"); + assert_eq!( + body["monitoringEventReports"][0]["monitoringType"], + "UE_REACHABILITY" + ); + assert!(body.get("notifId").is_none()); + // LOCATION_REPORTING maps back to the eventId LOCATION_REPORT. + let loc = NefMonitoringSubscription { + monitoring_type: "LOCATION_REPORTING".to_string(), + ..nnef.clone() + }; + let body = build_monitoring_notification( + &loc, + &serde_json::json!({ "reportList": [{ "type": "LOCATION_REPORT", "location": {} }] }), + ) + .expect("a location report"); + assert_eq!(body["eventNotifs"][0]["event"], "LOCATION_REPORT"); + assert!(body["eventNotifs"][0]["timeStamp"].is_string(), "{body}"); + } + #[test] fn build_monitoring_notification_location_report() { let sub = NefMonitoringSubscription::new( diff --git a/src/bins/nextgcore-udmd/src/app.rs b/src/bins/nextgcore-udmd/src/app.rs index 4292c4f8..ea8e8482 100644 --- a/src/bins/nextgcore-udmd/src/app.rs +++ b/src/bins/nextgcore-udmd/src/app.rs @@ -2996,7 +2996,7 @@ fn apply_ee_patch(stored: &mut serde_json::Value, patch: &serde_json::Value) -> for t in parents { parent = match parent { serde_json::Value::Object(m) => m.get_mut(t), - serde_json::Value::Array(a) => t.parse::().ok().and_then(|i| a.get_mut(i)), + serde_json::Value::Array(a) => json_pointer_index(t).and_then(|i| a.get_mut(i)), _ => None, } .ok_or_else(|| format!("path {path:?}: no member {t:?} to descend into"))?; @@ -3025,17 +3025,14 @@ fn apply_ee_patch(stored: &mut serde_json::Value, patch: &serde_json::Value) -> let at = if last == "-" { a.len() } else { - last.parse::() - .ok() + json_pointer_index(last) .filter(|i| *i <= a.len()) .ok_or_else(|| format!("path {path:?}: index out of range"))? }; a.insert(at, value()?); } ("replace" | "remove", serde_json::Value::Array(a)) => { - let at = last - .parse::() - .ok() + let at = json_pointer_index(last) .filter(|i| *i < a.len()) .ok_or_else(|| format!("path {path:?}: index out of range"))?; if op_name == "replace" { @@ -3056,6 +3053,19 @@ fn apply_ee_patch(stored: &mut serde_json::Value, patch: &serde_json::Value) -> Ok(()) } +/// #2154 (R42 on #2152): a JSON Pointer array index, RFC 6901 §4: `0`, or a +/// digit 1-9 followed by digits. A leading zero (`01`) or a sign (`+1`, which +/// `str::parse` would take) is no index. +fn json_pointer_index(token: &str) -> Option { + let well_formed = token == "0" + || (token + .as_bytes() + .first() + .is_some_and(|b| (b'1'..=b'9').contains(b)) + && token.bytes().all(|b| b.is_ascii_digit())); + well_formed.then(|| token.parse().ok()).flatten() +} + /// The reference tokens of RFC 6901 JSON Pointer `path`, unescaped (`~1` is /// `/`, `~0` is `~`). The whole document (`""`) is refused: a patch replaces /// members, not the subscription. @@ -6361,6 +6371,39 @@ mod tests { } } + /// **#2154** (R42 on #2152): an array index is `0` or has no leading zero + /// (RFC 6901 §4), and has no sign: `01`, `00` and `+1` are refused and + /// change nothing; `0` and `10` are indices. + /// + /// REVERT-VERIFIED: with the index parsed by `str::parse` again, `/list/01` + /// and `/list/+1` apply and the `is_err` assertion fails. + #[test] + fn ee_patch_refuses_an_array_index_with_a_leading_zero_2154() { + let mut stored = serde_json::json!({ "list": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }); + let before = stored.clone(); + for bad in ["/list/01", "/list/00", "/list/+1", "/list/1a"] { + assert!( + apply_ee_patch( + &mut stored, + &serde_json::json!([{ "op": "replace", "path": bad, "value": "x" }]) + ) + .is_err(), + "{bad}" + ); + assert_eq!(stored, before, "{bad}: nothing applied"); + } + apply_ee_patch( + &mut stored, + &serde_json::json!([ + { "op": "replace", "path": "/list/0", "value": "zero" }, + { "op": "replace", "path": "/list/10", "value": "ten" } + ]), + ) + .expect("well-formed indices"); + assert_eq!(stored["list"][0], "zero"); + assert_eq!(stored["list"][10], "ten"); + } + #[test] fn ee_patch_accepts_the_merge_patch_form_and_refuses_nonsense() { let mut stored = serde_json::json!({ "callbackReference": "a", "keepMe": 1 });