Skip to content

fix(types): Email.UnmarshalJSON rejects a value that does not survive its own round trip - #200

Open
aibeshkov wants to merge 1 commit into
oapi-codegen:mainfrom
aibeshkov:email-roundtrip
Open

aibeshkov wants to merge 1 commit into
oapi-codegen:mainfrom
aibeshkov:email-roundtrip

Conversation

@aibeshkov

Copy link
Copy Markdown

Email.UnmarshalJSON (types/email.go, since v1.2.0, same logic in v1.9.1 and on main) parses the input with mail.ParseAddress and stores m.Address. mail.Address.Address is 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, then json.Marshal of the same value fails with email: failed to pass regex validation.
  • " x"@example.com: Unmarshal succeeds and stores " x@example.com" (leading space); marshalling it and unmarshalling again yields x@example.com, a different address.

Expected: a value that UnmarshalJSON accepts is accepted by MarshalJSON and parses back to the same value.

Change

UnmarshalJSON rejects a parsed address whose Address does not parse back to itself (4 lines in types/email.go). The new table test TestEmail_UnmarshalJSON_OnlyAcceptsValuesThatRoundTrip covers 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 UnmarshalJSON reject valid RFC 5322 addresses with such a quoted local part; today it accepts them and produces a value MarshalJSON cannot 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

package main

import (
	"encoding/json"
	"fmt"

	"github.com/oapi-codegen/runtime/types"
)

func main() {
	for _, in := range []string{
		`"\" x\"@example.test"`,
		`"\"a b\"@example.test"`,
		`"\"a@b\"@example.test"`,
		`"\"a,b\"@example.test"`,
		`"user@example.test"`,
	} {
		var e types.Email
		err1 := json.Unmarshal([]byte(in), &e)
		out, err2 := json.Marshal(e)
		var e2 types.Email
		err3 := json.Unmarshal(out, &e2)
		fmt.Printf("in=%-26s stored=%-18q unmarshal=%v | marshal=%s err=%v | reunmarshal=%q err=%v idempotent=%v\n",
			in, string(e), err1, out, err2, string(e2), err3, err1 == nil && err3 == nil && e == e2)
	}
}

Output on v1.9.1 (go.mod: require github.com/oapi-codegen/runtime v1.9.1, GOFLAGS=-mod=mod go run .; deterministic):

in="\" x\"@example.test"      stored=" x@example.test"  unmarshal=<nil> | marshal="x@example.test" err=<nil> | reunmarshal="x@example.test" err=<nil> idempotent=false
in="\"a b\"@example.test"     stored="a b@example.test" unmarshal=<nil> | marshal= err=json: error calling MarshalJSON for type *types.Email: email: failed to pass regex validation | reunmarshal="" idempotent=false
in="\"a@b\"@example.test"     stored="a@b@example.test" unmarshal=<nil> | marshal= err=json: error calling MarshalJSON for type *types.Email: email: failed to pass regex validation | reunmarshal="" idempotent=false
in="\"a,b\"@example.test"     stored="a,b@example.test" unmarshal=<nil> | marshal= err=json: error calling MarshalJSON for type *types.Email: email: failed to pass regex validation | reunmarshal="" idempotent=false
in="user@example.test"        stored="user@example.test" unmarshal=<nil> | marshal="user@example.test" err=<nil> | reunmarshal="user@example.test" err=<nil> idempotent=true

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?

… 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.
@aibeshkov
aibeshkov requested a review from a team as a code owner December 2, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant