EmailTextField renders a pattern attribute that browsers reject as an invalid regular expression
维护者通常 1 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- java, javascript
调研方向
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
PatternValidatorthat its pattern is sent to the browser, and must also be valid as a JavaScript regular expression invmode. - Let an application keep a
PatternValidatoron 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/wicket 的其他 Issue
-
PropertyResolver fails on JDK classes with a non-public field named like the property可能已有人在做 @vaibhavgupta443 今天认领。 未关闭good first issue
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
Stop the autocomplete from accumulating request parameters while typing可能已有人在做 @aryankalantri 今天认领。 未关闭good first issue
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
Validate the report-uri value given to CSPHeaderConfiguration#reportBack可能已有人在做 @AzizOuertani 于 3 天前认领。 未关闭good first issue
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 70/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
jenkinsci/user-guide-ai-chatbot-plugin#132 ·
维护者通常 6 天内回复
-
account assessment course feature lecture
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
bug db/dameng user-priority/P2
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
app:learninggoalhub enhancement good first issue
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100