lsps-types: echo valid_until verbatim and send the LSPS0 datetime format
What changed, and why it matters
This commit fixes a protocol-compatibility bug in Core Lightning's LSPS (Lightning Service Provider Specification) support. When a client received an opening-fee offer from a service provider, it was accidentally changing the exact timestamp text before echoing it back. Some strict providers would then reject the client's reply because the timestamp no longer matched. The fix makes the client preserve the original timestamp text exactly, and makes the service send timestamps in the precise format the spec requires. There is no direct theft-of-funds or remote-code-execution vulnerability here, but the bug could cause legitimate JIT-channel requests to fail.
Treat as a recommended bug-fix patch for anyone running the LSPS plugin or LSPS2 client. It improves interoperability and prevents spurious JIT-channel rejections. No emergency response is warranted; review and include in the next maintenance release.
Security signals we found
Protocol compliance fix for LSPS0 datetime serialization
Client-side echo of opening_fee_params now verbatim per bLIP-52
Service-side promise computed over truncated spec-format datetime
Potential denial-of-service/interoperability failure for JIT channel opening
No memory safety, cryptographic bypass, or authorization bypass observed
Evidence from the diff
The LSPS0 DateTime wrapper previously delegated serde to chrono, which normalizes ISO 8601 strings (e.g., dropping trailing .000 or expanding .5112 to .511200). bLIP-52 requires clients to copy opening_fee_params verbatim, including valid_until, and bLIP-50 mandates the YYYY-MM-DDThh:mm:ss.uuuZ format. The patch changes DateTime to retain the deserialized wire string and re-emit it unchanged on serialization, while locally constructed values are truncated to milliseconds and formatted per LSPS0. The service rebuilds valid_via chrono before computing the HMAC promise, so the promise verifies against the exact string the client echoes. This resolves interoperability failures with LSPs that compare the echoed string.
Changed components
lsps-types/src/lsps0.rslsps-types/src/lsps2/params.rsplugins/lsps-plugin/src/proto/lsps2.rsInspect captured patch +171 / −20
### lsps-types/src/lsps0.rs
@@ -285,34 +285,106 @@ impl<'de> Deserialize<'de> for ShortChannelId {
}
}
-/// Represents a datetime as defined in LSPS0.datetime. Uses ISO8601 in UTC
-/// timezone.
-#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
-#[serde(transparent)]
-pub struct DateTime(chrono::DateTime<chrono::Utc>);
+/// Represents a datetime as defined in LSPS0.datetime:
+///
+/// > Particular points of time in the modern era (a "datetime") MUST be
+/// > encoded as a JSON string containing the [ISO 8601] format
+/// > `"YYYY-MM-DDThh:mm:ss.uuuZ"`.
+/// > These are always in the UTC timezone.
+///
+/// A datetime built locally (from a [`chrono::DateTime`]) is truncated to
+/// milliseconds and serialized in exactly that format. A datetime
+/// deserialized from JSON keeps the string it was parsed from and
+/// serializes back to that string unchanged, so a client can echo
+/// `opening_fee_params` verbatim. Parsing accepts any RFC 3339 datetime.
+///
+/// Equality, ordering and hashing only consider the point in time, not the
+/// string it came from.
+///
+/// [ISO 8601]: https://www.iso.org/iso-8601-date-and-time-format.html
+#[derive(Clone, Debug)]
+pub struct DateTime {
+ value: chrono::DateTime<chrono::Utc>,
+ /// The string this datetime was deserialized from, if any.
+ wire: Option<String>,
+}
impl DateTime {
/// Returns the datetime as a [`chrono::DateTime`].
pub fn as_chrono(&self) -> chrono::DateTime<chrono::Utc> {
- self.0
+ self.value
}
}
impl From<chrono::DateTime<chrono::Utc>> for DateTime {
fn from(dt: chrono::DateTime<chrono::Utc>) -> Self {
- DateTime(dt)
+ use chrono::SubsecRound as _;
+ DateTime {
+ value: dt.trunc_subsecs(3),
+ wire: None,
+ }
}
}
impl From<DateTime> for chrono::DateTime<chrono::Utc> {
fn from(dt: DateTime) -> Self {
- dt.0
+ dt.value
+ }
+}
+
+impl PartialEq for DateTime {
+ fn eq(&self, other: &Self) -> bool {
+ self.value == other.value
+ }
+}
+
+impl Eq for DateTime {}
+
+impl PartialOrd for DateTime {
+ fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl Ord for DateTime {
+ fn cmp(&self, other: &Self) -> core::cmp::Ordering {
+ self.value.cmp(&other.value)
+ }
+}
+
+impl core::hash::Hash for DateTime {
+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
+ self.value.hash(state)
}
}
impl core::fmt::Display for DateTime {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
- core::fmt::Display::fmt(&self.0, f)
+ core::fmt::Display::fmt(&self.value, f)
+ }
+}
+
+impl Serialize for DateTime {
+ fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
+ match &self.wire {
+ Some(wire) => serializer.serialize_str(wire),
+ None => serializer.collect_str(&self.value.format("%Y-%m-%dT%H:%M:%S%.3fZ")),
+ }
+ }
+}
+
+impl<'de> Deserialize<'de> for DateTime {
+ fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
+ let wire = String::deserialize(deserializer)?;
+ // Same parsing as chrono's serde implementation.
+ let value = wire
+ .parse::<chrono::DateTime<chrono::FixedOffset>>()
+ .map_err(de::Error::custom)?
+ .with_timezone(&chrono::Utc);
+ Ok(DateTime {
+ value,
+ wire: Some(wire),
+ })
}
}
@@ -488,4 +560,59 @@ mod tests {
let dt: DateTime = serde_json::from_str(json).unwrap();
assert_eq!(serde_json::to_string(&dt).unwrap(), json);
}
+
+ #[test]
+ fn test_datetime_echoes_received_string() {
+ // chrono's own serde would turn the first two into "...30Z" and
+ // "...30.511Z".
+ for json in [
+ r#""2023-02-23T08:47:30.000Z""#,
+ r#""2023-02-23T08:47:30.511+00:00""#,
+ r#""2023-02-23T08:47:30Z""#,
+ r#""2023-02-23T08:47:30.5112Z""#,
+ r#""2023-02-23T10:47:30.511+02:00""#,
+ ] {
+ let dt: DateTime = serde_json::from_str(json).unwrap();
+ assert_eq!(serde_json::to_string(&dt).unwrap(), json);
+ }
+ }
+
+ #[test]
+ fn test_datetime_equality_ignores_the_string() {
+ let a: DateTime = serde_json::from_str(r#""2023-02-23T08:47:30.511Z""#).unwrap();
+ let b: DateTime = serde_json::from_str(r#""2023-02-23T10:47:30.511+02:00""#).unwrap();
+ assert_eq!(a, b);
+ assert_eq!(a.cmp(&b), core::cmp::Ordering::Equal);
+ assert_eq!(DateTime::from(a.as_chrono()), a);
+ }
+
+ #[test]
+ fn test_datetime_local_values_use_the_spec_format() {
+ use chrono::TimeZone;
+ let whole = chrono::Utc
+ .with_ymd_and_hms(2023, 2, 23, 8, 47, 30)
+ .unwrap();
+ assert_eq!(
+ serde_json::to_string(&DateTime::from(whole)).unwrap(),
+ r#""2023-02-23T08:47:30.000Z""#
+ );
+
+ // Sub-millisecond precision is dropped when the value is built, so
+ // the value matches what a peer parses back.
+ let precise = whole + chrono::Duration::nanoseconds(511_200_300);
+ let dt = DateTime::from(precise);
+ assert_eq!(
+ serde_json::to_string(&dt).unwrap(),
+ r#""2023-02-23T08:47:30.511Z""#
+ );
+ let parsed: DateTime = serde_json::from_str(&serde_json::to_string(&dt).unwrap()).unwrap();
+ assert_eq!(parsed.as_chrono(), dt.as_chrono());
+ }
+
+ #[test]
+ fn test_datetime_rejects_garbage() {
+ for json in [r#""""#, r#""2023-02-23""#, r#""yesterday""#, "1677142050"] {
+ assert!(serde_json::from_str::<DateTime>(json).is_err(), "{json}");
+ }
+ }
}
### lsps-types/src/lsps2/params.rs
@@ -143,7 +143,7 @@ impl OpeningFeeParams {
) -> Result<Option<Msat>, ValidationError> {
if self.is_expired_at(now) {
return Err(ValidationError::Expired {
- valid_until: self.valid_until,
+ valid_until: self.valid_until.clone(),
});
}
payment_size
### plugins/lsps-plugin/src/proto/lsps2.rs
@@ -90,16 +90,23 @@ impl PolicyOpeningFeeParams {
}
pub fn with_promise(&self, secret: &[u8; 32]) -> OpeningFeeParams {
+ // Rebuild valid_until so it is sent in the LSPS0 datetime format,
+ // and compute the promise over exactly the value the client echoes.
+ let policy = PolicyOpeningFeeParams {
+ valid_until: self.valid_until.as_chrono().into(),
+ ..self.clone()
+ };
+ let promise =
+ Promise::try_from(policy.get_hmac_hex(secret)).expect("hex HMAC is a valid promise");
OpeningFeeParams {
- min_fee_msat: self.min_fee_msat,
- proportional: self.proportional,
- valid_until: self.valid_until,
- min_lifetime: self.min_lifetime,
- max_client_to_self_delay: self.max_client_to_self_delay,
- min_payment_size_msat: self.min_payment_size_msat,
- max_payment_size_msat: self.max_payment_size_msat,
- promise: Promise::try_from(self.get_hmac_hex(secret))
- .expect("hex HMAC is a valid promise"),
+ min_fee_msat: policy.min_fee_msat,
+ proportional: policy.proportional,
+ valid_until: policy.valid_until,
+ min_lifetime: policy.min_lifetime,
+ max_client_to_self_delay: policy.max_client_to_self_delay,
+ min_payment_size_msat: policy.min_payment_size_msat,
+ max_payment_size_msat: policy.max_payment_size_msat,
+ promise,
}
}
}
@@ -109,7 +116,7 @@ impl From<&OpeningFeeParams> for PolicyOpeningFeeParams {
PolicyOpeningFeeParams {
min_fee_msat: params.min_fee_msat,
proportional: params.proportional,
- valid_until: params.valid_until,
+ valid_until: params.valid_until.clone(),
min_lifetime: params.min_lifetime,
max_client_to_self_delay: params.max_client_to_self_delay,
min_payment_size_msat: params.min_payment_size_msat,
@@ -175,6 +182,23 @@ mod tests {
));
}
+ #[test]
+ fn test_with_promise_sends_spec_datetime() {
+ // A policy plugin may hand out any RFC 3339 string with any
+ // precision; the LSP sends the LSPS0 format and its promise must
+ // still verify on the echoed params.
+ let mut json = serde_json::to_value(policy()).unwrap();
+ json["valid_until"] = "2100-01-01T02:00:00.123456789+02:00".into();
+ let policy: PolicyOpeningFeeParams = serde_json::from_value(json).unwrap();
+
+ let params = policy.with_promise(SECRET);
+ let sent = serde_json::to_value(¶ms).unwrap();
+ assert_eq!(sent["valid_until"], "2100-01-01T00:00:00.123Z");
+
+ let echoed: OpeningFeeParams = serde_json::from_value(sent).unwrap();
+ assert!(verify_promise(&echoed, SECRET));
+ }
+
#[test]
fn test_verify_promise_survives_serde() {
let params = policy().with_promise(SECRET);Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.