Skip to content

Commit 1855184

Browse files
committed
If we're built with std, always use real time to validate DNSSEC
In cases where we're built with the `std` feature, we can assume that `SystemTime` works, so we should use it to validate that DNSSEC proofs we receive are currently valid, even if we don't have block time data.
1 parent 072675b commit 1855184

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lightning/src/onion_message/dns_resolution.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl OMNameResolver {
330330
}
331331

332332
/// Builds a new [`OMNameResolver`] which will not validate the time limits on DNSSEC proofs
333-
/// (at least until [`Self::new_best_block`] is called).
333+
/// (for builds without the "std" feature and until [`Self::new_best_block`] is called).
334334
///
335335
/// If possible, you should prefer [`Self::new`] so that providing stale proofs is not
336336
/// possible, however in no-std environments where there is some trust in the resolver used and
@@ -339,7 +339,7 @@ impl OMNameResolver {
339339
/// Note that not calling [`Self::new_best_block`] will result in requests not timing out and
340340
/// unresolved requests leaking memory. You must instead call
341341
/// [`Self::expire_pending_resolution`] as unresolved requests expire.
342-
pub fn new_without_expiry_validation() -> Self {
342+
pub fn new_without_no_std_expiry_validation() -> Self {
343343
Self {
344344
pending_resolves: Mutex::new(new_hash_map()),
345345
latest_block_time: AtomicUsize::new(0),
@@ -478,16 +478,24 @@ impl OMNameResolver {
478478
let validated_rrs =
479479
parsed_rrs.as_ref().and_then(|rrs| verify_rr_stream(rrs).map_err(|_| &()));
480480
if let Ok(validated_rrs) = validated_rrs {
481-
let block_time = self.latest_block_time.load(Ordering::Acquire) as u64;
482-
if block_time != 0 {
481+
#[allow(unused_assignments, unused_mut)]
482+
let mut time = self.latest_block_time.load(Ordering::Acquire) as u64;
483+
#[cfg(feature = "std")]
484+
{
485+
use std::time::{SystemTime, UNIX_EPOCH};
486+
let now = SystemTime::now().duration_since(UNIX_EPOCH);
487+
time = now.expect("Time must be > 1970").as_secs();
488+
}
489+
if time != 0 {
483490
// Block times may be up to two hours in the future and some time into the past
484491
// (we assume no more than two hours, though the actual limits are rather
485492
// complicated).
486493
// Thus, we have to let the proof times be rather fuzzy.
487-
if validated_rrs.valid_from > block_time + 60 * 2 {
494+
let max_time_offset = if cfg!(feature = "std") { 0 } else { 60 * 2 };
495+
if validated_rrs.valid_from > time + max_time_offset {
488496
return None;
489497
}
490-
if validated_rrs.expires < block_time - 60 * 2 {
498+
if validated_rrs.expires < time - max_time_offset {
491499
return None;
492500
}
493501
}

0 commit comments

Comments
 (0)