Skip to content

Fix #155, #201: Send a long SMTP initial response after the challenge - #215

Merged
jmehrens merged 4 commits into
eclipse-ee4j:masterfrom
sharanggupta:fix-201-long-initial-response
Oct 18, 2026
Merged

jmehrens merged 4 commits into
eclipse-ee4j:masterfrom
sharanggupta:fix-201-long-initial-response

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

Fixes #155 and #201.

What

The SMTP provider always sends the initial response of a mechanism with the AUTH command. With XOAUTH2 the initial response contains the access token,, and tokens of a thousand characters or more occur, for example with Google Workspace domain-wide delegation as in #201. The command line then exceeds the SMTP limit of 512 octets including CRLF (RFC 5321, section 4.5.3.1.4), and servers reject it:

RFC 4954, section 4, covers this case: if the initial response would make the AUTH command exceed the line length limit, the client must not send it with the command. It must send AUTH <mechanism> alone and give the response in reply to the server's first, empty 334 challenge, a line that has no such limit.

How

  • SMTPTransport.fitsAuthCommand(mech, ir) checks whether AUTH <mech> <ir> fits in 510 characters, which is the 512 octets without the CRLF.
  • SMTPTransport.Authenticator.authenticate(), used by PLAIN, XOAUTH2 and the other built-in mechanisms, and SMTPSaslAuthenticator.authenticate() both defer an initial response that does not fit. They send AUTH <mech>, then the initial response in reply to the first 334. What follows is unchanged, including the 530/STARTTLS retry.
  • An initial response that fits is still sent with the command as before, so nothing changes for the usual PLAIN credentials or short tokens.

Tests

SMTPOAuth2Test uses a new SMTPOAuth2Handler. The handler rejects command lines longer than 510 characters with 500 5.5.2 Line too long, as the servers above do, and accepts the token either with the AUTH command or after a 334. It connects with a short token and with a 1500-character token, each with and without SASL. On master both long-token tests fail (AuthenticationFailedException: 500 5.5.2 Line too long, and failed to connect with SASL), and all four pass with this change.

The CI build (clean verify with the copyright check and the oss-release,staging,build-only profiles) passes locally on JDK 17.

…onse after the challenge

The AUTH command was always sent with the initial response, if the
mechanism has one. With an XOAUTH2 access token of about a thousand
characters or more the command line exceeds the SMTP limit of 512
octets (RFC 5321), and servers such as Google's reject it with "500
5.5.2 Line too long" or "555 5.5.2 Syntax error, command line too long".

RFC 4954 requires the client not to send the initial response in that
case, but to send it in reply to the first, empty, challenge. Do so
both for the built-in mechanisms and for SASL, when the AUTH command
with the initial response would be longer than the limit. Shorter
initial responses are still sent with the AUTH command.

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>

@jmehrens jmehrens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the changes.txt to include your bug number and title.

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
Subtract the fixed parts and the mechanism from the line length limit
and compare the initial response with what is left, so that no sum of
lengths can overflow. Test the limit and add eclipse-ee4j#155 and eclipse-ee4j#201 to the
changes.

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
@sharanggupta

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I merged master and added #155 and #201 to CHANGES.txt in ba3bc2a, and restructured fitsAuthCommand as described in the thread. The Angus Mail build with the copyright check passes locally.

@jmehrens

Copy link
Copy Markdown
Contributor

@bbottema let me know if it is okay to merge this given what you are working on.

@bbottema

Copy link
Copy Markdown
Contributor

@jmehrens yes, please go ahead and merge it. I'll handle any conflicts in my branches.

@jmehrens
jmehrens merged commit c93271f into eclipse-ee4j:master Oct 18, 2026
4 checks passed
@jmehrens jmehrens linked an issue Oct 20, 2026 that may be closed by this pull request
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.

SMTP XOAUTH2 authentication fails with large access tokens SMTP Sasl authentication failed : password is too long

3 participants