[
https://issues.apache.org/jira/browse/WW-5703?focusedWorklogId=1041356&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1041356
]
ASF GitHub Bot logged work on WW-5703:
--------------------------------------
Author: ASF GitHub Bot
Created on: 14/Sep/26 10:40
Start Date: 14/Sep/26 10:40
Worklog Time Spent: 10m
Work Description: lukaszlenart opened a new pull request, #1938:
URL: https://github.com/apache/struts/pull/1938
Fixes [WW-5703](https://issues.apache.org/jira/browse/WW-5703)
## Problem
`RegexFieldValidator.validateFieldValue` returns before it consults its
`trim` param whenever the value trims to empty, so a whitespace-only value
passes the server in *every* configuration — including the only one
(`trim=false`, case-sensitive, portable regex) in which
`StrutsHtmlConstraintProvider` emits a `pattern`. The browser skips `pattern`
only for the empty string, so a single space was blocked client-side and
accepted server-side: a false reject, which the feature's governing rule
forbids.
## Fix
Two cases, decided by a pre-scan of the field's validators (so the order in
`validation.xml` does not matter):
- The field also carries a **trimming `requiredstring`** (its default) —
that rejects blank input server-side, so the bare regex is emitted as before.
- Otherwise the pattern becomes **`(?:<regex>)|[\x00-\x20]*`**: the regex,
or a value made only of the characters `String.trim()` strips. `requiredstring`
with `trim=false` counts `" "` as non-empty and falls through to the regex's
skip, so it stays in this case.
The ticket's other suggestion — refuse to emit when the regex cannot match a
whitespace-only string — is undecidable in general (`^\s$` matches `" "` but
not `" "`), so it was dropped.
`addConstraints` / `addPattern` gain a `boolean rejectsBlank` parameter; the
class is unreleased (`@since 7.4.0`), so no deprecation.
## Verification
- Composed pattern compiled with node 24.2.0 under the `v` flag, as browsers
do: `""`, `" "`, `"\t"`, `" \t "`, `""` pass; NBSP-only, `"ABC1"`, `" ABC12"`,
`"abc"` (against `^[A-Z]{3}\d{2}$`) fail; `"ABC12"` passes — identical to the
server.
- `Html5ConstraintRenderingTest.testRendersPatternOnATextField` now pins the
suffix end to end, proving FreeMarker's attribute escaping leaves it intact.
- `RegexFieldValidatorTest.testBlankValueIsSkippedEvenWithoutTrim` pins the
server-side skip the mirror depends on (the ticket's asked-for test).
- `mvn test -DskipAssembly -pl core`: 3380 tests, 0 failures.
- Security review and `/code-review high` both ran clean.
Docs: apache/struts-site PR adds a paragraph to `client-side-validation.md`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Issue Time Tracking
-------------------
Worklog Id: (was: 1041356)
Remaining Estimate: 0h
Time Spent: 10m
> HTML5 pattern false-rejects whitespace-only input that the regex validator
> accepts
> ----------------------------------------------------------------------------------
>
> Key: WW-5703
> URL: https://issues.apache.org/jira/browse/WW-5703
> Project: Struts 2
> Issue Type: Bug
> Components: Core Tags
> Reporter: Lukasz Lenart
> Assignee: Lukasz Lenart
> Priority: Major
> Fix For: 7.4.0
>
> Time Spent: 10m
> Remaining Estimate: 0h
>
> Found reviewing WW-5695 (PR #1865). Violates that feature's governing rule:
> emit a constraint only when the browser cannot reject input the server would
> accept.
> h3. What happens
> {{StrutsHtmlConstraintProvider.addPattern}} refuses to emit {{pattern}} when
> the regex validator has {{trim=true}}, because the server would then match
> the trimmed value while the browser matches the raw one. That guard is
> correct as far as it goes, but it is not the only trim in
> {{RegexFieldValidator}}. {{validateFieldValue}} opens with a blank check that
> runs _unconditionally_, regardless of the {{trim}} param:
> {code:java}
> protected void validateFieldValue(Object object, String value, String
> regexToUse) {
> // string must not be empty
> String str = value.trim();
> if (str.isEmpty()) {
> LOG.debug("Value is empty, please use a required validator");
> return;
> }
> ...
> {code}
> So the exact configuration in which {{pattern}} _is_ emitted -
> {{trim=false}}, case-sensitive, portable regex - is also the configuration in
> which the server silently accepts a whitespace-only value.
> h3. Reproduction
> A regex field-validator with {{regex}} of {{^\d{5}$}} and {{trim}} of
> {{false}}, on an {{s:textfield}} named {{zip}}, html5 theme,
> {{struts.ui.html5.constraints=true}}.
> The user types a single space.
> * Server: {{" ".trim().isEmpty()}} is true, the validator returns early, no
> field error is added, the action executes.
> * Browser: the value is non-empty so {{pattern}} applies, the match fails,
> and the submit is blocked with "Please match the requested format."
> The user cannot get past a field the server would have let through. HTML
> skips {{pattern}} for the empty string, but not for whitespace-only input, so
> there is no browser-side escape.
> h3. Possible closures
> * emit {{pattern}} only when the field also carries a {{requiredstring}}
> validator, which does reject blank server-side, so the two sides agree again;
> or
> * refuse to emit when the regex cannot itself match a whitespace-only string.
> h3. Test gap
> {{StrutsHtmlConstraintProviderTest.regexEmitsPatternWhenPortableAndCaseSensitive}}
> asserts only that the attribute is produced, so nothing currently catches
> this. A case pinning server-side acceptance of a whitespace-only value should
> land with the fix.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)