Skip to content
1 change: 1 addition & 0 deletions docs/pm/cli/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ When the minimum age filter is active:
- The check searches up to 7 days past the age gate; if releases are still rapid beyond that, Bun ignores the stability check
- Exact version requests (like `package@1.1.1`) still respect the age gate but bypass the stability check
- Bun treats versions without a `time` field as passing the age check (the npm registry should always provide timestamps)
- Bun blocks a version whose `time` value is not a date string that `Date.parse` can read, because its age is unknown

For more advanced security scanning, including integration with services and custom filtering, see the [Security Scanner API](/pm/security-scanner-api).

Expand Down
30 changes: 6 additions & 24 deletions src/bun_core/wtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@
//! library code in Rust. WTF is statically linked into the binary, so
//! tier-0 callers declare the C symbol directly — no `bun_jsc` crate
//! dependency is required to reference it.
//!
//! Source of truth: `src/jsc/bindings/wtf-bindings.cpp` (`WTF__parseES5Date`),
//! which forwards to `WTF::parseES5Date` in
//! vendor/WebKit `Source/WTF/wtf/DateMath.{h,cpp}`.
//!
//! Note: WTF's `parseES5Date` sets an `isLocalTime` out-param so the JS
//! `Date` constructor can later apply the VM's tz offset. The C shim discards
//! it, so local-time inputs return their naive
//! UTC value here too.

unsafe extern "C" {
// src/jsc/bindings/wtf-bindings.cpp:
// extern "C" double WTF__parseES5Date(const Latin1Character* string, size_t length)
fn WTF__parseES5Date(bytes: *const u8, length: usize) -> f64;
}

/// Direct call to `WTF::parseES5Date`. Returns NaN for any input the WTF
/// parser rejects. `s` is treated as Latin-1.
#[inline]
pub(crate) fn parse_es5_date_raw(s: &[u8]) -> f64 {
// SAFETY: s.as_ptr() is valid for s.len() bytes.
unsafe { WTF__parseES5Date(s.as_ptr(), s.len()) }
// src/jsc/bindings/wtf-bindings.cpp
fn Bun__parseDateTimeString(bytes: *const u8, length: usize) -> f64;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -38,13 +20,13 @@ impl core::fmt::Display for InvalidDate {
}
impl core::error::Error for InvalidDate {}

/// `bun.jsc.wtf.parseES5Date` shape — `Err` on empty input or non-finite result.
/// `2000-01-01T00:00:00.000Z` → `Ok(946684800000.0)`.
pub fn parse_es5_date(buf: &[u8]) -> Result<f64, InvalidDate> {
/// JS `Date.parse(buf)` with no VM, except that a date-time with no time zone reads as UTC.
pub fn parse_date(buf: &[u8]) -> Result<f64, InvalidDate> {
if buf.is_empty() {
return Err(InvalidDate);
}
let ms = parse_es5_date_raw(buf);
// SAFETY: `buf.as_ptr()` is valid for `buf.len()` bytes.
let ms = unsafe { Bun__parseDateTimeString(buf.as_ptr(), buf.len()) };
if ms.is_finite() {
Ok(ms)
} else {
Expand Down
90 changes: 68 additions & 22 deletions src/install/PackageManager/PackageManagerEnqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,36 +958,43 @@ pub fn enqueue_dependency_with_main_and_success_fn(
}
}
return Ok(());
} else if err == crate::Error::TooRecentVersion {
} else if err == crate::Error::TooRecentVersion
|| err == crate::Error::UnreadablePublishTime
{
if dependency.behavior.is_required() {
if let Some(fail) = fail_fn {
fail(this, dependency, id, err);
} else {
let age_gate_ms =
this.options.minimum_release_age_ms.unwrap_or(0.0);
let blocked_by = AgeGateBlock {
unreadable_publish_time: err
== crate::Error::UnreadablePublishTime,
age_gate_ms,
};
if version.tag == dependency::version::Tag::DistTag {
bun_ast::add_error_pretty!(
this.log_mut(),
None,
bun_ast::Loc::EMPTY,
"Package \"{}\" with tag \"{}\" not found<r> <d>(all versions blocked by minimum-release-age: {} seconds)<r>",
"Package \"{}\" with tag \"{}\" not found<r> <d>(all versions blocked by minimum-release-age: {})<r>",
bstr::BStr::new(this.lockfile.str(&name)),
bstr::BStr::new(
this.lockfile.str(&version.dist_tag().tag)
),
age_gate_ms / MS_PER_S,
blocked_by,
);
} else {
bun_ast::add_error_pretty!(
this.log_mut(),
None,
bun_ast::Loc::EMPTY,
"No version matching \"{}\" found for specifier \"{}\"<r> <d>(blocked by minimum-release-age: {} seconds)<r>",
"No version matching \"{}\" found for specifier \"{}\"<r> <d>(blocked by minimum-release-age: {})<r>",
bstr::BStr::new(this.lockfile.str(&name)),
bstr::BStr::new(
this.lockfile.str(&version.literal)
),
age_gate_ms / MS_PER_S,
blocked_by,
);
}
}
Expand Down Expand Up @@ -1175,8 +1182,10 @@ pub fn enqueue_dependency_with_main_and_success_fn(
if version.tag == dependency::version::Tag::Npm
&& version.npm().version.is_exact()
{
if let Some(find_result) =
loaded_manifest.as_ref().unwrap().find_by_version(
if let Some(find_result) = loaded_manifest
.as_ref()
.unwrap()
.find_by_version(
version
.npm()
.version
Expand All @@ -1186,6 +1195,17 @@ pub fn enqueue_dependency_with_main_and_success_fn(
.left
.version,
)
// A cached "not a date" cannot become a date: fetch an expired manifest again.
.filter(|found| {
!(expired
&& this
.options
.minimum_release_age_ms
.is_some_and(|age| age > 0.0)
&& found
.package
.has_unreadable_publish_time())
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
if let Some(min_age_ms) =
this.options.minimum_release_age_ms
Expand All @@ -1200,19 +1220,21 @@ pub fn enqueue_dependency_with_main_and_success_fn(
find_result.package, min_age_ms,
)
{
Comment thread
robobun marked this conversation as resolved.
Resolved
let package_name = this.lockfile.str(&name);
let min_age_seconds = min_age_ms / MS_PER_S;
let _ = this.log_mut().add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"Version \"{}@{}\" was published within minimum release age of {} seconds",
bstr::BStr::new(package_name),
find_result.version.fmt(this.lockfile.buffers.string_bytes.as_slice()),
min_age_seconds,
),
// Reported by the error arm above, like a fresh manifest would be.
resolve_result_ = Err(
if find_result
.package
.has_unreadable_publish_time()
{
crate::Error::UnreadablePublishTime
} else {
crate::Error::TooRecentVersion
},
);
return Ok(());
let _ = this
.network_dedupe_map
.remove(&task_id);
continue 'retry_with_new_resolve_result;
Comment thread
robobun marked this conversation as resolved.
Resolved
}
}
// reshaped for borrowck — `find_result`
Expand Down Expand Up @@ -1834,6 +1856,22 @@ pub fn enqueue_dependency_with_main_and_success_fn(
}
}

/// Why minimum-release-age blocked a version: the text after "minimum-release-age:" in the errors.
struct AgeGateBlock {
unreadable_publish_time: bool,
age_gate_ms: f64,
}

impl core::fmt::Display for AgeGateBlock {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.unreadable_publish_time {
f.write_str("the publish time in the registry is not a valid date")
} else {
write!(f, "{} seconds", self.age_gate_ms / MS_PER_S)
}
}
}

/// Unmet peers stay unresolved instead of failing the install; see `may_stay_unresolved`.
#[cold]
#[inline(never)]
Expand Down Expand Up @@ -2799,9 +2837,17 @@ fn get_or_put_resolved_package(
break 'blk Some(result);
}
Npm::FindVersionResult::Err(err_type) => match err_type {
Npm::FindVersionError::TooRecent
| Npm::FindVersionError::AllVersionsTooRecent => {
return Err(crate::Error::TooRecentVersion);
Npm::FindVersionError::TooRecent {
unreadable_publish_time,
}
| Npm::FindVersionError::AllVersionsTooRecent {
unreadable_publish_time,
} => {
return Err(if unreadable_publish_time {
crate::Error::UnreadablePublishTime
} else {
crate::Error::TooRecentVersion
});
}
Npm::FindVersionError::NotFound => None, // Handle below with existing logic
},
Expand Down
3 changes: 3 additions & 0 deletions src/install/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum Error {
NoMatchingVersion,
#[error("TooRecentVersion")]
TooRecentVersion,
#[error("UnreadablePublishTime")]
UnreadablePublishTime,
#[error("MissingPackageJSON")]
MissingPackageJSON,
#[error("InstallFailed")]
Expand Down Expand Up @@ -270,6 +272,7 @@ impl Error {
Self::DistTagNotFound => "DistTagNotFound",
Self::NoMatchingVersion => "NoMatchingVersion",
Self::TooRecentVersion => "TooRecentVersion",
Self::UnreadablePublishTime => "UnreadablePublishTime",
Self::MissingPackageJSON => "MissingPackageJSON",
Self::InstallFailed => "InstallFailed",
Self::HTTPError => "HTTPError",
Expand Down
Loading
Loading