nodejs / nodejs/node

url: URLPattern rejects values requiring WebIDL USVString conversion for input and baseURL

未关闭
#64,785 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

latest main branch

Platform
7.1.4-arch1-1
Subsystem

url

What steps will reproduce the bug?

  const { URL, URLPattern } = require('node:url');

  const base = 'https://example.com/';
  const numberPattern =
    new URLPattern('https://example.com/123');
  const fooPattern =
    new URLPattern('https://example.com/foo');

  const baseObject = {
    toString() {
      return base;
    },
  };

  const baseURLObject = new URL(base);

  function observe(label, callback) {
    try {
      console.log(label, callback());
    } catch (error) {
      console.log(label, error.code, error.name);
    }
  }

  // URLPatternInput conversion.
  observe(
    'constructor input:',
    () => new URLPattern(123, base).pathname,
  );

  observe(
    'test input:',
    () => numberPattern.test(123, base),
  );

  observe(
    'exec input:',
    () => numberPattern.exec(123, base) !== null,
  );

  // baseURL conversion. The three-argument constructor is intentional:
  // it unambiguously selects the baseURL overload.
  observe(
    'constructor baseURL:',
    () => new URLPattern('foo', baseObject, {}).pathname,
  );

  observe(
    'test baseURL:',
    () => fooPattern.test('foo', baseURLObject),
  );

  observe(
    'exec baseURL:',
    () => fooPattern.exec('foo', baseURLObject) !== null,
  );
How often does it reproduce? Is there a required condition?

Every

What is the expected behavior? Why is that the expected behavior?
  constructor input: /123
  test input: true
  exec input: true
  constructor baseURL: /foo
  test baseURL: true
  exec baseURL: true
What do you see instead?

Each case throws a TypeError with code === 'ERR_INVALID_ARG_TYPE'.

Additional information

The URL Pattern IDL (https://urlpattern.spec.whatwg.org/#urlpattern) defines:

typedef (USVString or URLPatternInit) URLPatternInput;

constructor(
URLPatternInput input,
USVString baseURL,
optional URLPatternOptions options = {}
);

constructor(
optional URLPatternInput input = {},
optional URLPatternOptions options = {}
);

boolean test(
optional URLPatternInput input = {},
optional USVString baseURL
);

URLPatternResult? exec(
optional URLPatternInput input = {},
optional USVString baseURL
);

WebIDL requires JavaScript values assigned to USVString to undergo USVString conversion
(https://webidl.spec.whatwg.org/#es-USVString), rather than already being JavaScript String values.

The constructor must first perform WebIDL overload resolution (https://webidl.spec.whatwg.org/#dfn-overload-resolution-algorithm).

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 node:url URLPattern 构造函数、test() 和 exec() 入口开始,然后在最新的 main 分支上运行复现。将参数处理与所引用的 WebIDL 重载解析和 USVString 规则进行比较。完成标准是:数值输入和 object baseURL 值产生预期结果,而不是 ERR_INVALID_ARG_TYPE,并为所示案例提供回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
api, backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。