`styleText` fast path bypasses `options` and `options.validateStream` validation

未關閉 適合新手
#67,700 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@GBra-4-669 已經在處理了。

開始於 2026年11月23日。

  • #67720 來自 @GBra-4-669 —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
javascript, nodejs
領域
backend

研究方向

問題出在 lib/util.js 中 styleText 的快速路徑,它在單一字串格式時提早返回,跳過了陣列分支會執行的 options 與 options.validateStream 檢查。先閱讀該函式中的驗證程式碼並比較兩個分支。使用 issue 中的指令重現,並在 test/parallel/test-util-style-text.js 中新增測試。兩次呼叫對非布林值的 validateStream 都拋出 ERR_INVALID_ARG_TYPE 即為完成。

由索引模型根據 Issue 內容生成。

描述

util
Version

v26.13.0

Platform
Microsoft Windows NT 10.0.19045.0 x64
Subsystem

util

What steps will reproduce the bug?
node -e "const { styleText } = require('node:util'); const opts = { validateStream: 0 }; console.log(styleText('red', 'text', opts)); styleText(['red'], 'text', opts);"
How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

both styleText('red', 'text', opts) and styleText(['red'], 'text', opts) should throw a TypeError.

according to the documentation, options must be an <Object> and options.validateStream must be a <boolean>. the first call should not bypass validation (https://nodejs.org/api/util.html#utilstyletextformat-text-options)

What do you see instead?

the first call bypasses validation and prints the styled 'text', while the second call throws ERR_INVALID_ARG_TYPE

Additional information

No response

主要語言
JavaScript
星號
124k
分支
38k
平均合併
4 天 2 小時
30 天內合併 PR
266

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。