EmailTextField renders a pattern attribute that browsers reject as an invalid regular expression

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

维护者通常 1 天内回复

@reiern70 已经在做这个了。

开始于 2026年11月12日。

  • #1887 来自 @reiern70 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java, javascript
领域
backend, frontend

调研方向

Start in EmailAddressValidator, where the pattern string with unescaped hyphens in its character classes lives, and in AbstractTextComponent#onComponentTag, which copies the pattern into the pattern attribute. Check open pull request #1887 first, since it already targets this fix. Done means the rendered pattern compiles with the v flag in the browser and the existing email validation tests still pass.

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

描述

Affects: 11.0.0; also current master (a7a4a4018d).
Component: wicket-core, AbstractTextComponent, EmailAddressValidator

What happens

A page with an EmailTextField renders:

<input type="email" ... pattern="^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*((\.[A-Za-z]{2,}){1}$)">

Chrome logs this error on the page, and again on each form submit:

Pattern attribute value ^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@... is not a valid regular expression:
Uncaught SyntaxError: Invalid regular expression: /^[_A-Za-z0-9-]+(...)$/v: Invalid character class

The browser then ignores the pattern attribute. The field still works, because type="email" makes the browser check the address, and EmailAddressValidator checks it again on the server. The result is console noise on every page with an email field, plus a pattern attribute that does nothing.

Cause

AbstractTextComponent#onComponentTag copies the Java regular expression of every PatternValidator into the HTML pattern attribute (tag.put("pattern", ((PatternValidator)validator).getPattern().toString())).

The HTML standard compiles pattern with the v flag (unicodeSets mode), and current browsers follow it. In v mode, a - that is not escaped inside a character class is a syntax error. That makes [_A-Za-z0-9-] invalid there, although Java accepts it.

Fix proposed

Escape the hyphens in EmailAddressValidator. The pattern below is valid in Java, and valid in the browser in v mode. It matches the same addresses (checked in Java and in Node with new RegExp(..., "v")):

"^[_A-Za-z0-9\\-]+(\\.[_A-Za-z0-9\\-]+)*@[A-Za-z0-9\\-]+(\\.[A-Za-z0-9\\-]+)*((\\.[A-Za-z]{2,}){1}$)"

Two broader options:

  • Note in the javadoc of PatternValidator that its pattern is sent to the browser, and must also be valid as a JavaScript regular expression in v mode.
  • Let an application keep a PatternValidator on the server without the attribute, for patterns that Java and JavaScript do not share.

Workaround

Add a behavior to the field after the validator, which removes the attribute:

email.add(new Behavior() {
    @Override
    public void onComponentTag(Component component, ComponentTag tag) {
        tag.remove("pattern");
    }
});
主要语言
Java
星标
800
派生
437
平均合并
21 小时 36 分钟
30 天内合并 PR
167

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

apache/wicket 的其他 Issue

查看 apache/wicket 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

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