Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

url: pathToFileURL duplicates root slash across platforms and misses empty extended UNC servername

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

維護者通常 1 天內回覆

@JoaoMarcos44 已經在處理了。

開始於 2026年10月28日。

  • #67092 來自 @JoaoMarcos44 —— 未關閉

評估

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

研究方向

從 issue 指出的 lib/internal/url.js 中兩處位置開始,接著閱讀周圍的 pathToFileURL() 邏輯和相關 URL 測試。使用 { windows: false } 和 { windows: true } 重現根斜線案例,並檢查現有測試如何涵蓋空的延伸 UNC 伺服器名稱。完成條件是跨平台 URL 結尾只有一個斜線,而且空伺服器名稱會擲出 ERR_INVALID_ARG_VALUE。

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

描述

Version

v22.16.0 (also reproduces on main)

Platform
Microsoft Windows NT 10.0.26100.0 x64 (and POSIX platforms when passing `{ windows: true }`)
Subsystem

url

What steps will reproduce the bug?

url.pathToFileURL() has two bugs in lib/internal/url.js:

  1. Cross-platform root path trailing slash duplication (resolved[resolved.length - 1] !== path.sep):
    When restoring trailing slashes stripped by path.resolve(), pathToFileURL() compares the last character of resolved against the host platform's path.sep instead of the target separator (windows ? '\\' : '/'):

    // On Windows (where path.sep === '\\'):
    console.log(url.pathToFileURL('/', { windows: false }).href);
    // Prints: 'file:////' (4 slashes instead of 'file:///')
    
    // On POSIX / Linux / macOS (where path.sep === '/'):
    console.log(url.pathToFileURL('C:\\', { windows: true }).href);
    // Prints: 'file:///C://' (double trailing slash instead of 'file:///C:/')
    
  2. Extended UNC path empty servername bypass (hostnameEndIndex === 2):
    For extended UNC paths (\\?\UNC\...), prefixLength is 8, but the empty servername check hardcodes if (hostnameEndIndex === 2) instead of if (hostnameEndIndex === prefixLength):

    console.log(url.pathToFileURL('\\\\?\\UNC\\\\share', { windows: true }).href);
    // Prints: 'file:///share' instead of throwing ERR_INVALID_ARG_VALUE ('Empty UNC servername')
    
How often does it reproduce? Is there a required condition?

Always (100% reproducible).

What is the expected behavior? Why is that the expected behavior?
  • url.pathToFileURL('/', { windows: false }).href should return 'file:///' on all platforms (including Windows).
  • url.pathToFileURL('C:\\', { windows: true }).href and url.pathToFileURL('C:/', { windows: true }).href should return 'file:///C:/' on all platforms (including POSIX).
  • url.pathToFileURL('\\\\?\\UNC\\\\share', { windows: true }) should throw ERR_INVALID_ARG_VALUE (Empty UNC servername), matching url.pathToFileURL('\\\\\\share', { windows: true }).
What do you see instead?
  • url.pathToFileURL('/', { windows: false }).href on Windows returns 'file:////'.
  • url.pathToFileURL('C:\\', { windows: true }).href on POSIX returns 'file:///C://'.
  • url.pathToFileURL('\\\\?\\UNC\\\\share', { windows: true }).href returns 'file:///share'.
Additional information

In lib/internal/url.js:

  • Line 1707 checks if (hostnameEndIndex === 2) instead of if (hostnameEndIndex === prefixLength).
  • Line 1722 checks resolved[resolved.length - 1] !== path.sep instead of resolved[resolved.length - 1] !== (windows ? '\\' : '/').
主要語言
JavaScript
星號
123k
分支
39.3k
平均合併
4 天 11 小時
30 天內合併 PR
237

環境準備

從這裡開始

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

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

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

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