Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/src/main/resources/docs/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ for the Eclipse EE4J Angus Mail project:

UNRELEASED CHANGES
------------------
155: SMTP Sasl authentication failed : password is too long
201: SMTP XOAUTH2 authentication fails with large access tokens
104: IMAPProtocol: Problem with searching emails when the subject contains umlauts
217: Discard stale EHLO capabilities

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.eclipse.angus.mail.smtp;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.StringTokenizer;

/**
* Handle connection with XOAUTH2 authentication, enforcing the SMTP
* command line length limit of RFC 5321.
*/
public class SMTPOAuth2Handler extends SMTPHandler {

/**
* The maximum length of a command line, without the CRLF.
*/
private static final int MAX_COMMAND_LINE_LENGTH = 510;

private final String token;

public SMTPOAuth2Handler(String token) {
this.token = token;
}

@Override
public void ehlo() throws IOException {
println("250-hello");
println("250 AUTH XOAUTH2");
}

@Override
public void auth(String line) throws IOException {
if (line.length() > MAX_COMMAND_LINE_LENGTH) {
println("500 5.5.2 Line too long");
return;
}
StringTokenizer ct = new StringTokenizer(line, " ");
ct.nextToken(); // AUTH
String mech = ct.nextToken();
if (!mech.equalsIgnoreCase("XOAUTH2")) {
println("504 unrecognized authentication type");
return;
}
String ir;
if (ct.hasMoreTokens()) {
ir = ct.nextToken();
} else {
println("334 ");
ir = readLine();
}
String expected = "user=test\001auth=Bearer " + token + "\001\001";
String actual = new String(Base64.getDecoder().decode(
ir.getBytes(StandardCharsets.US_ASCII)), StandardCharsets.UTF_8);
if (expected.equals(actual))
println("235 Authenticated");
else
println("535 authentication failed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.eclipse.angus.mail.smtp;

import jakarta.mail.Session;
import jakarta.mail.Transport;
import org.eclipse.angus.mail.test.TestServer;
import org.junit.Test;

import java.util.Properties;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Test XOAUTH2 authentication with access tokens too long to be sent as
* the initial response of the AUTH command (RFC 4954, section 4).
*/
public class SMTPOAuth2Test {

private static final String SHORT_TOKEN = "short-token";
private static final String LONG_TOKEN = "x".repeat(1500);

@Test
public void testShortToken() throws Exception {
connect(SHORT_TOKEN, false);
}

@Test
public void testLongToken() throws Exception {
connect(LONG_TOKEN, false);
}

@Test
public void testShortTokenWithSasl() throws Exception {
connect(SHORT_TOKEN, true);
}

@Test
public void testLongTokenWithSasl() throws Exception {
connect(LONG_TOKEN, true);
}

/**
* The AUTH command line, "AUTH " + mechanism + " " + initial response,
* may have at most 510 characters without the CRLF.
*/
@Test
public void testFitsAuthCommand() {
String ir = "x".repeat(510 - "AUTH XOAUTH2 ".length());
assertTrue(SMTPTransport.fitsAuthCommand("XOAUTH2", ir));
assertFalse(SMTPTransport.fitsAuthCommand("XOAUTH2", ir + "x"));
}

private static void connect(String token, boolean sasl) throws Exception {
TestServer server = new TestServer(new SMTPOAuth2Handler(token));
try {
server.start();

Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "localhost");
properties.setProperty("mail.smtp.port", String.valueOf(server.getPort()));
properties.setProperty("mail.smtp.auth.mechanisms", "XOAUTH2");
properties.setProperty("mail.smtp.auth.xoauth2.disable", "false");
if (sasl) {
properties.setProperty("mail.smtp.sasl.enable", "true");
properties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2");
}
Session session = Session.getInstance(properties);

Transport t = session.getTransport("smtp");
try {
t.connect("test", token);
} finally {
t.close();
}
} finally {
server.quit();
server.interrupt();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -151,6 +152,13 @@ public void handle(Callback[] callbacks) {
} else
ir = "=";
}
// an initial response that does not fit on the command line
// is sent in reply to the first challenge (RFC 4954)
String deferredIr = null;
if (ir != null && !SMTPTransport.fitsAuthCommand(mech, ir)) {
deferredIr = ir;
ir = null;
}
if (ir != null)
resp = pr.simpleCommand("AUTH " + mech + " " + ir);
else
Expand All @@ -168,6 +176,9 @@ public void handle(Callback[] callbacks) {
resp = pr.simpleCommand("AUTH " + mech);
}

if (resp == 334 && deferredIr != null)
resp = pr.simpleCommand(deferredIr);

if (resp == 235)
return true; // success already!

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -871,6 +872,29 @@ private boolean authenticate(String user, String passwd)
"No authentication mechanisms supported by both server and client");
}

/**
* The maximum length of an SMTP command line, without the CRLF.
*/
private static final int MAX_COMMAND_LINE_LENGTH = 510;

/**
* Is the AUTH command for the given mechanism with the given initial
* response within the command line length limit of 512 octets,
* including the CRLF (RFC 5321, section 4.5.3.1.4)? If not, RFC 4954
* requires the initial response to be sent in reply to the first
* challenge instead.
*
* @param mech the authentication mechanism
* @param ir the initial response
* @return true if the initial response can be sent with the command
*/
static boolean fitsAuthCommand(String mech, String ir) {
// "AUTH " + mech + " " + ir, compared with what is left of the
// limit so that the lengths are never added and cannot overflow
int available = MAX_COMMAND_LINE_LENGTH - "AUTH ".length() - mech.length() - 1;
return ir.length() <= available;
}

/**
* Abstract base class for SMTP authentication mechanism implementations.
*/
Expand Down Expand Up @@ -907,6 +931,13 @@ boolean authenticate(String host, String authzid,
try {
// use "initial response" capability, if supported
String ir = getInitialResponse(host, authzid, user, passwd);
// an initial response that does not fit on the command line
// is sent in reply to the first challenge (RFC 4954)
String deferredIr = null;
if (ir != null && !fitsAuthCommand(mech, ir)) {
deferredIr = ir;
ir = null;
}
if (noauthdebug && isTracing()) {
logger.fine("AUTH " + mech + " command trace suppressed");
suspendTracing();
Expand All @@ -928,6 +959,8 @@ boolean authenticate(String host, String authzid,
else
resp = simpleCommand("AUTH " + mech);
}
if (resp == 334 && deferredIr != null)
resp = simpleCommand(deferredIr);
if (resp == 334)
doAuth(host, authzid, user, passwd);
} catch (IOException ex) { // should never happen, ignore
Expand Down
Loading