Repository navigation
[go] Escape control characters in pattern validate tags - #25269
Merged
wing328 merged 3 commits intoOct 19, 2026
Merged
Conversation
Member
|
tested locally and no longer able to repeat the issue with this fix |
mohitduhan19
added a commit
to mohitduhan19/mohitduhan19
that referenced
this pull request
Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #25263
A
patternwith a control character (for example one that forbids U+0000 to U+001F in a text field, a common validation) was copied raw into the Govalidate:"regexp=..."struct tag. Go source can't hold a NUL or a byte order mark, so the generated model failed to compile withillegal character NUL.AbstractGoCodegennow writes each control character (Character.isISOControl, so U+0000 to U+001F and U+007F to U+009F) as a regexp escape like\\x00, and a byte order mark as\\x{feff}, in the same place where backticks and commas are already escaped (#19358, #24349).The backslash is doubled on purpose. Go unquotes the struct tag value before validator.v2 compiles the pattern, so
\\x00reaches the regexp as\x00. A single backslash (as suggested in the issue) works up to U+007F, but for U+0080 to U+009F Go's unquote turns\x9finto a raw byte that isn't valid UTF-8, and the regexp fails with "bad parameter".Generated tag for the issue's spec:
Testing
GoClientCodegenTest.testControlCharactersInPatternAreEscapedwith a spec covering C0, DEL, C1 and a byte order mark. It checks the escaped tags and that the generated model contains no control characters (other than newlines and tabs) or byte order mark.gofmtandgo veton it, and a Go test callingvalidator.Validate(gopkg.in/validator.v2): plain text passes, and strings containing NUL, U+001F, DEL, U+0085 or a byte order mark are rejected.Go*Test, 71 tests), excepttestStringNotEnumValidation, which needs network access to download Go modules and fails the same way without this change in my environment../bin/generate-samples.sh bin/configs/go-*.yaml: no sample changes, since no sample spec has a control character in a pattern.PR checklist
cc @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5 (Go technical committee)
Summary by cubic
Fixes generated Go models failing to compile when a schema
patterncontains a control character or a byte order mark, previously emitted raw into thevalidate:"regexp=..."struct tag and rejected withillegal character NUL.Bug Fixes
\\x00/\\x{feff}escapes; the backslash is doubled because Go unquotes the struct tag value before the validator compiles the pattern.Written for commit 97f608c. Summary will update on new commits.