Repository navigation
Conversation
console.log / Bun.inspect formatted Date values by calling
JSON.stringify(value) and trimming the quotes. JSON.stringify dispatches
through the value's own toJSON -> toISOString, so any Date carrying an
own or subclass hook (ORM wrappers, Object.assign(new Date(), {toJSON}))
ran user code inside the formatter:
- a throwing toISOString/toJSON threw out of console.log
- a non-callable toISOString threw a TypeError
- a spoofing toJSON controlled the printed text
- a toJSON returning an object produced corrupted output after the
quote-trim sliced a non-string
- a throw mid-array left partial output on stdout
Node and bun's own util.inspect read the internal date value and never
call these hooks.
Switch both formatters (ConsoleObject.rs and the test runner's
pretty_format.rs) to value.to_iso_string(), which reads
DateInstance::internalNumber() via Bun::toISOString and never executes
JS. NaN yields 'Invalid Date' as before.
|
Status: Diff is ready. New tests fail on the released binary and pass on the debug build. All review threads resolved; two LGTM bot reviews. Reproduced with: CI runs #78715 and #78730 show failures only in unrelated lanes ( |
WalkthroughChangesDate formatting in console and test-runner output now uses the Date’s internal timestamp through Date formatting behavior
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/bun/util/inspect.test.js`:
- Around line 556-561: Update the subprocess assertions around Promise.all to
compare one combined result object containing stdout, stderr, and exitCode.
Preserve the existing newline normalization for stdout and expected values while
replacing the separate expect calls with the suite’s combined-object assertion
pattern.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 9b3faf13-a577-430a-a8aa-d66ead40ee7c
📒 Files selected for processing (3)
src/jsc/ConsoleObject.rssrc/runtime/test_runner/pretty_format.rstest/js/bun/util/inspect.test.js
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
|
Not a duplicate of #35045. That PR changes the |
There was a problem hiding this comment.
LGTM — replaces the json_stringify → quote-trim path for JSDate with the existing to_iso_string() helper, which reads DateInstance::internalNumber() directly and never dispatches to user JS.
What was reviewed:
- Verified
JSC__JSValue__toISOString(bindings.cpp:5794) does adynamicDowncast<DateInstance>+internalNumber()and returns -1 for non-Date/NaN →unwrap_or(b"Invalid Date")preserves the existing Invalid Date output. - Buffer widened 36→64 to match the C++
char[64]signature (also used byTOMLObject.rs); expanded-year form fits. - Non-Date
Tag::JSONpath is unchanged —json_stringifyjust moved below the early return;add_for_new_linenow gets the actual printed length in the Date branch. - Both sibling formatters (
ConsoleObject.rs,pretty_format.rs) fixed identically; tests cover throwing/spoofing/non-function hooks, subclass, NaN, mid-array, and the subprocessconsole.logpath.
Extended reasoning...
Overview
Two Rust formatters (src/jsc/ConsoleObject.rs and src/runtime/test_runner/pretty_format.rs) had a Tag::JSON arm that special-cased JSType::JSDate by calling json_stringify, rendering into a 36-byte buffer, and stripping the surrounding quotes. Because JSON.stringify on a Date dispatches through user-visible toJSON/toISOString, an own or subclass hook could throw out of console.log or spoof/corrupt the output. The fix replaces that with the pre-existing JSValue::to_iso_string helper, which calls JSC__JSValue__toISOString → Bun::toISOString — pure C++ over DateInstance::internalNumber() and vm.dateCache. The non-Date Tag::JSON fallthrough (json_stringify + print) is preserved verbatim, just relocated after the early return. Tests are added to test/js/bun/util/inspect.test.js alongside the existing new Date(..) coverage.
Security risks
None introduced; if anything the change reduces surface by no longer executing arbitrary user JS from inside the console formatter. to_iso_string writes into a caller-owned [u8; 64] and returns a bounded slice; the C++ side's max output (~28 bytes for expanded-year ISO) is well under 64.
Level of scrutiny
Low-medium. This is a targeted output-formatting fix in a well-understood path, reusing an existing helper already exercised by TOMLObject.rs and BunProcess.cpp. No new FFI, no allocation/ownership changes, no control-flow beyond an early return that was already there. The only behavioral delta for the non-Date arm is that json_stringify now runs after the Date check instead of before — semantically identical since the Date branch returned early anyway.
Other factors
- Both parallel formatters were updated (satisfies "fix the whole class").
- Existing
new Date(..)test continues to cover valid ISO output andInvalid Date; new tests add the hostile-override matrix (throwing, non-function, spoofing string, spoofing object, subclass, NaN) plus a spawned subprocess exercising the realconsole.logwriter. - CodeRabbit's combined-object-assertion nit was applied in a24ca95 and the thread is resolved; no other outstanding comments.
add_for_new_linein the Date branch now receives the printed length rather than the quoted JSON length — a minor accuracy improvement, not a regression.
There was a problem hiding this comment.
LGTM — targeted fix that swaps the JSON.stringify → quote-trim dance for the existing to_iso_string helper, so Date formatting no longer invokes user JS.
What was reviewed:
- Verified
JSC__JSValue__toISOStringreadsDateInstance::internalNumber()viadynamicDowncastand never enters JS;None/NaN →Invalid Datepreserved. - Both parallel formatters (
ConsoleObject.rs,pretty_format.rs) updated symmetrically; non-DateTag::JSONpath is unchanged (just moved after the early return). - Buffer size matches the Rust signature's required
[u8; 64];add_for_new_linenow gets the actual output length instead of the JSON string's. - Tests cover throwing/spoofing/non-function
toJSON/toISOString, subclass, NaN, mid-array, and a subprocessconsole.logcase; combined-object assertion applied per review feedback.
Extended reasoning...
Overview
The PR replaces the JSDate arm in both native formatters (src/jsc/ConsoleObject.rs and src/runtime/test_runner/pretty_format.rs). Previously, both called value.json_stringify(...) — which dispatches through the value's own toJSON → toISOString — then rendered the result into a 36-byte stack buffer and stripped the surrounding quotes. Now they call value.to_iso_string(global, &mut [0u8; 64]), which wraps JSC__JSValue__toISOString in bindings.cpp: that helper does a dynamicDowncast<DateInstance>, reads internalNumber(), and formats via Bun::toISOString (a copy of JSC's own DatePrototype ISO formatter). No JS is entered. None maps to b"Invalid Date", matching the old ConsoleObject.rs behavior and fixing pretty_format.rs (which previously would quote-trim null into ul for NaN dates). The non-Date json_stringify fallback is preserved unchanged, just relocated after the Date early-return.
Security risks
None. This removes a path where user-controlled toJSON/toISOString could execute inside the formatter, which is strictly a robustness improvement — console.log can no longer throw or be spoofed by a hostile Date. No new attack surface is introduced.
Level of scrutiny
Low-to-medium. The change is small (~40 lines of native code across two mirrored sites), uses an existing, already-exercised in-tree helper (to_iso_string / Bun::toISOString, also used by process.report), and the buffer size exactly matches the Rust signature's &mut [u8; 64]. The test-runner formatter is lower-stakes (diff output). Node compat is the reference behavior here, and Node's util.inspect reads the internal slot the same way.
Other factors
- Test coverage is thorough for this class: throwing
toISOString, non-callabletoISOString, string-returningtoJSON, object-returningtoJSON(the old quote-trim corruption case), throwingtoJSONmid-array, NaN date with spoofedtoJSON, and aDatesubclass. A subprocess test verifiesconsole.logitself doesn't throw. Existingnew Date(..)tests (includingInvalid Date) still pass, confirming no regression on the happy path. - The PR's gate evidence shows the new tests fail on the unfixed ASAN build and pass with the fix.
- CodeRabbit's one comment (combined-object subprocess assertion) was applied in a24ca95 and marked resolved.
- The duplicate-PR bot flagged #35045; robobun correctly explained they touch different arms (
JSAsJSONTypevsJSDate) and don't conflict.
|
Updated 10:59 AM PT - Jul 23rd, 2026
❌ @robobun, your commit 1b04c4f has 1 failures in
🧪 To try this PR locally: bunx bun-pr 35287That installs a local version of the PR into your bun-35287 --bun |
|
Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-23, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main. |
What
console.log/Bun.inspectformattedDatevalues by callingJSON.stringify(value)and trimming the surrounding quotes.JSON.stringifydispatches through the value's owntoJSON→toISOString, so anyDatecarrying an own/subclass hook ran user code inside the formatter.Repro
Node reads the internal date value and never calls user
toJSON/toISOString. Bun's own portedutil.inspect(date)is also correct; only the native console arm dispatches.Realistic trigger: any Date carrying an own/subclass
toJSON/toISOString(ORM wrappers, moment-style subclasses,Object.assign(new Date(), {toJSON})). Logging it kills the logging call.Fix
Switch both formatters (
ConsoleObject.rsand the test runner'spretty_format.rs) tovalue.to_iso_string(), which readsDateInstance::internalNumber()viaBun::toISOStringand never executes JS.None(NaN time value) yieldsInvalid Dateas before. This also drops thejson_stringify→ fixed-buffer render → quote-trim dance.Verification
[review] gate passed · iteration 1 · 3 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 2 passed · 0 rejected · iteration 1
evidence per changed file