Repository navigation
Conversation
… its own round trip
mail.Address.Address is the addr-spec with quoting removed. For a quoted local
part ("a b"@example.com, "a@b"@example.com, "a,b"@example.com) the stored
string is no longer an addr-spec: MarshalJSON returns ErrValidationEmail for a
value Unmarshal had accepted. For a quoted local part with a leading space the
value is accepted, stored with the space, and parses back as a different
address. Accept only a value whose addr-spec parses back to itself.
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.
Email.UnmarshalJSON(types/email.go, since v1.2.0, same logic in v1.9.1 and on main) parses the input withmail.ParseAddressand storesm.Address.mail.Address.Addressis the addr-spec with quoting removed, so for a quoted local part the stored string is no longer an addr-spec.Observed on v1.9.1, Go 1.27.4 darwin/arm64:
"a b"@example.com,"a@b"@example.com,"a,b"@example.com: Unmarshal succeeds, thenjson.Marshalof the same value fails withemail: failed to pass regex validation." x"@example.com: Unmarshal succeeds and stores" x@example.com"(leading space); marshalling it and unmarshalling again yieldsx@example.com, a different address.Expected: a value that
UnmarshalJSONaccepts is accepted byMarshalJSONand parses back to the same value.Change
UnmarshalJSONrejects a parsed address whoseAddressdoes not parse back to itself (4 lines intypes/email.go). The new table testTestEmail_UnmarshalJSON_OnlyAcceptsValuesThatRoundTripcovers a plain address, a dropped display name, and quoted local parts with a space,@, a comma, a leading space, a backslash and angle brackets. It fails on the six quoted cases without the change;go test ./...passes.This makes
UnmarshalJSONreject valid RFC 5322 addresses with such a quoted local part; today it accepts them and produces a valueMarshalJSONcannot write. If you would rather keep them valid, the alternative is to store the quoted form, which needs quoting code; I can rework the PR that way.Reproducer
Output on v1.9.1 (
go.mod:require github.com/oapi-codegen/runtime v1.9.1,GOFLAGS=-mod=mod go run .; deterministic):Scope
Low severity, a hardening fix: no memory or resource effect. A service that accepts such an address and later marshals the same struct gets
ErrValidationEmail(typically a 500), and the leading-space case silently becomes a different address after one more parse. I searched issues and PRs for this and found no existing report.We found it by fuzzing the type (unmarshal, marshal, unmarshal must give the same value).
Would you rather take this as a PR, or prefer the stored-quoted-form alternative?