Skip to content

fix: validate URL host and port formats - #35

Open
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:fix/33-url-hosts-and-ports
Open

nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:fix/33-url-hosts-and-ports

Conversation

@nrps9909

@nrps9909 nrps9909 commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #33

The URL rule rejected bracketed IPv6 hosts, single-digit ports and ASCII-encoded internationalized top-level domains. It also accepted bare IPv6 hosts and five-digit ports above 65535.

Require brackets around IPv6 URL hosts, accept Punycode top-level labels, and bound one- to five-digit ports to 65535. The original IPv6 address pattern still accepts its existing zone syntax. For URL hosts, a zone uses the bracketed %25 delimiter and the zone characters defined in RFC 6874 section 2. The tests cover encoded and percent-encoded identifiers, raw delimiters, missing identifiers, and malformed escapes. RFC 6874 section 2

Verification

  • The two new valid scoped-URL cases fail on the initial patch. Both pass on this revision.
  • ut test -- --runInBand passes 130 tests and 8 snapshots across 16 suites.
  • ut lint, utx tsc --noEmit, ut compile, and Prettier on both changed files pass.

Codex provided AI assistance with implementation and verification.

Summary by CodeRabbit

  • 新功能
    • URL 校验现支持国际化域名,以及带有效区域标识符的方括号 IPv6 地址。
  • 错误修复
    • 端口校验限制为 1 至 65535,并拒绝未加方括号的 IPv6 地址及无效的区域标识符。

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

URL 校验规则现在支持方括号 IPv6 主机、编码区域标识符和国际化域名。端口范围调整为 1 至 65535。新增测试覆盖有效与无效 URL。

Changes

URL 校验

Layer / File(s) Summary
URL 匹配规则与校验用例
src/rule/url.ts, tests/url.spec.ts
IPv6 地址表达式拆分为地址和可选接口标识符。URL 主机支持方括号 IPv6 地址及 %25 引入的区域标识符。顶级域名表达式新增 xn-- 标签形式。端口支持 1 至 5 位数字,且数值不超过 65535。测试覆盖有效和无效格式,并检查校验结果或错误信息。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: yiminghe, zombiej

Merge Risk: Minimal · up to db3bb

The change makes URL validation accept bracketed IPv6 hosts, one-digit ports and Punycode TLDs, and reject unbracketed IPv6 hosts and ports above 65535. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: validate URL host and port formats”概括了主要变更。变更内容包括:接受方括号 IPv6 主机,拒绝未加方括号的 IPv6 主机,接受 xn-- 形式的 TLD,端口限制为 1 至 5 位十进制数字且不超过 65535。标题使用 fix 前缀,描述简洁,与 PR 目标(修复 #33 中的 URL 校验问题)一致。
Linked Issues check Passed [#33] 要求 type: 'url' 满足四项:接受方括号 IPv6 主机,接受 1 位端口,接受 Punycode TLD,拒绝裸 IPv6 主机和大于 65535 的端口。

在 src/rule/url.ts 中,差异如下:
- ipv6 现在为 \\[${v6Address}${urlZone}\\]。裸 IPv6 主机不再匹配。主机部分不含冒号,且 auth 需要 `…
Out of Scope Changes check Passed 所有改动都与 [#33] 的 URL 主机和端口校验相关。

- v6Address 从原有 v6 中拆出。v6 的行为保持不变,ip.v6() 的裸 IPv6 匹配仍保留原有 zone 语法。
- urlZone 只作用于 URL 主机中的方括号 IPv6。该改动是裸 IPv6 改为括号形式后的必要配套。否则原来在 URL 中可接受的 %eth0 类 zone 会失效。z…
Full details: Linked Issues check

Explanation

[#33] 要求 type: 'url' 满足四项:接受方括号 IPv6 主机,接受 1 位端口,接受 Punycode TLD,拒绝裸 IPv6 主机和大于 65535 的端口。

在 src/rule/url.ts 中,差异如下:

  • ipv6 现在为 \[${v6Address}${urlZone}\]。裸 IPv6 主机不再匹配。主机部分不含冒号,且 auth 需要 @,因此 http://::1/ 被拒绝。
  • port 改为 (?::(?:\d{1,4}|[0-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?。该表达式覆盖 0 至 65535。端口后必须是路径、查询、片段或结尾,因此 6 位及以上端口被拒绝。http://example.com:99999/ 被拒绝。
  • tld 新增 xn--[a-z0-9](?:[a-z0-9-]{0,57}[a-z0-9])?。http://example.xn--p1ai/ 匹配该分支。Unicode TLD 仍由原有的 [a-z\u00a1-\uffff]{2,} 分支接受,例如 http://example.рф/。

tests/url.spec.ts 的新增用例覆盖上述有效和无效输入。作者报告两个新增有效 scoped-URL 用例在初始补丁上失败、在当前版本通过。本次未检查测试文件的具体内容,测试结果依据 PR 摘要。代码差异本身已能确定上述行为。

Full details: Out of Scope Changes check

Explanation

所有改动都与 [#33] 的 URL 主机和端口校验相关。

  • v6Address 从原有 v6 中拆出。v6 的行为保持不变,ip.v6() 的裸 IPv6 匹配仍保留原有 zone 语法。
  • urlZone 只作用于 URL 主机中的方括号 IPv6。该改动是裸 IPv6 改为括号形式后的必要配套。否则原来在 URL 中可接受的 %eth0 类 zone 会失效。zone 字符集依据 RFC 6874 第 2 节定义。
  • tld 与 port 的改动直接对应 [#33] 的 Punycode TLD 与端口范围要求。
  • 测试改动覆盖上述 URL 校验行为,属于支持性改动。

未发现与上述目标无关的代码改动。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子捧着网址跳过草丛
方括号里,星星般的 IPv6 闪亮
%25 带来一段编码区域
小小端口也能顺利通行
xn-- 域名来到月光下
兔子收起清单,开心蹦回家

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4d8d6a5f-69f5-4ca8-b64f-b5bb47d6b5de
Commits

Reviewing files that changed from the base of the PR and between 8cca839 and db3bbb6.

Files selected for processing: 2

  • src/rule/url.ts
  • tests/url.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.29%. Comparing base (8cca839) to head (db3bbb6).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #35      +/-   ##
==========================================
- Coverage   82.38%   82.29%   -0.10%     
==========================================
  Files          25       25              
  Lines         653      655       +2     
  Branches      236      234       -2     
==========================================
+ Hits          538      539       +1     
- Misses        114      115       +1     
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yoyo837

yoyo837 commented Dec 1, 2026

Copy link
Copy Markdown
Member

Thanks for fixing the cases in #33. I found two inputs that now pass type: 'url' but still fail new URL():

  • http://[fe80::1%25eth0]:8/ — RFC 9844 §3 obsoletes RFC 6874 and reverts its URI syntax change. Could we limit this patch to bracketed IPv6 addresses without zone identifiers?
  • http://example.xn--a/ — the new pattern accepts it, but it is invalid under IDNA. Could we validate the A-label and add a rejection test alongside the valid xn--p1ai case?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

type: 'url' rejects bracketed IPv6 hosts, 1-digit ports, and punycode TLDs; accepts bare IPv6 and ports above 65535

2 participants