alhudz opened a new pull request, #1838:
URL: https://github.com/apache/commons-lang/pull/1838
Repro: `LocaleUtils.toLocale("en")` (U+FF45 U+FF4E, fullwidth),
`toLocale("éé")`, `toLocale("en_ÉÉ")`, `toLocale("_ÉÉ")` and
`toLocale("en_١٢٣")` (Arabic-Indic digits).
Expected: `IllegalArgumentException`. The `toLocale(String)` Javadoc says
the input is validated strictly, and `java.util.Locale` defines language as
`[a-zA-Z]{2,8}`, country as `[a-zA-Z]{2}` and area code as `[0-9]{3}`; `new
Locale.Builder().setLanguage("en")` throws `IllformedLocaleException`.
Actual: a `Locale` is returned. `toLocale("en")` renders like `en` but its
`toLanguageTag()` is `und`, and `toLocale("en_١٢٣")` carries country `١٢٣` that
`toLanguageTag()` silently drops to `en`. Since `toLocale` is the usual way to
vet a locale string taken from a request parameter or header, these values get
past the check.
Cause: `isISO639LanguageCode`, `isISO3166CountryCode`, `isNumericAreaCode`
and the `_GB` branch of `toLocale` use `StringUtils.isAllLowerCase`,
`isAllUpperCase`, `isNumeric` and `Character.isUpperCase`, which accept any
Unicode lowercase/uppercase letter or decimal digit.
Fix: check those characters with `CharUtils.isAsciiAlphaLower`,
`isAsciiAlphaUpper` and `isAsciiNumeric`. Variants are not touched, so
`th_TH_#Thai` (LANG-1823), `en_001_US_POSIX` and the special cases keep their
behaviour, and `testParseAllLocales` still round-trips every JDK locale.
`LocaleUtilsTest.testToLocaleNonAsciiCodes` fails before the change (`Should
fail language not ASCII ==> Expected java.lang.IllegalArgumentException to be
thrown, but nothing was thrown`) and passes after.
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [x] Read the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use
Artificial Intelligence (AI).
- [x] I used AI to create any part of, or all of, this pull request. Which
AI tool was used to create this pull request, and to what extent did it
contribute? Claude (Anthropic) found the defect and drafted the patch, the test
and this description; I reviewed all of it before submitting.
- [x] Run a successful build using the default
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command
line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail
if the changes to the runtime are not applied. This may not always be possible,
but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line
and body. Note that a maintainer may squash commits during the merge process.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]