This is an automated email from the ASF dual-hosted git repository. quantranhong1999 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 008f474aff007c6f58a8ba190fedb1bb50d8e5dd Author: Quan Tran <[email protected]> AuthorDate: Thu Aug 13 09:43:22 2026 +0700 JAMES-4210 Close ManageSieve SASL exchanges on transport termination Release active exchanges when clients disconnect, protocol processing fails, or an oversized SASL response is discarded. Reset unusable authentication state before processing further input. --- .../netty/ManageSieveChannelUpstreamHandler.java | 50 +++++++++++++++++----- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/server/protocols/protocols-managesieve/src/main/java/org/apache/james/managesieveserver/netty/ManageSieveChannelUpstreamHandler.java b/server/protocols/protocols-managesieve/src/main/java/org/apache/james/managesieveserver/netty/ManageSieveChannelUpstreamHandler.java index e0fec73e86..13e53203d9 100644 --- a/server/protocols/protocols-managesieve/src/main/java/org/apache/james/managesieveserver/netty/ManageSieveChannelUpstreamHandler.java +++ b/server/protocols/protocols-managesieve/src/main/java/org/apache/james/managesieveserver/netty/ManageSieveChannelUpstreamHandler.java @@ -80,7 +80,8 @@ public class ManageSieveChannelUpstreamHandler extends ChannelInboundHandlerAdap return; } if (request.length() > maxLineLength) { - throw new TooLongFrameException(); + handleTooLongFrame(ctx); + return; } Session manageSieveSession = ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).get(); @@ -125,25 +126,30 @@ public class ManageSieveChannelUpstreamHandler extends ChannelInboundHandlerAdap public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { try (Closeable closeable = ManageSieveMDCContext.from(ctx)) { if (cause instanceof TooLongFrameException) { - // Max line length exceeded - // See also JAMES-1190 - ctx.channel().attr(NettyConstants.RESPONSE_WRITER_ATTRIBUTE_KEY).get().write("NO Maximum command line length exceeded"); + handleTooLongFrame(ctx); } else if (cause instanceof SessionTerminatedException) { ctx.channel().attr(NettyConstants.RESPONSE_WRITER_ATTRIBUTE_KEY).get().write("OK channel is closing"); logout(ctx); } else { LOGGER.warn("Error while processing ManageSieve request", cause); + logout(ctx); } } } private void logout(ChannelHandlerContext ctx) { // logout on error not sure if that is the best way to handle it - ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).getAndSet(null); - // Make sure we close the channel after all the buffers were flushed out - Channel channel = ctx.channel(); - if (channel.isActive()) { - channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + Session session = ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).getAndSet(null); + try { + if (session != null) { + manageSieveProcessor.close(session); + } + } finally { + // Make sure we close the channel after all the buffers were flushed out + Channel channel = ctx.channel(); + if (channel.isActive()) { + channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + } } } @@ -170,8 +176,14 @@ public class ManageSieveChannelUpstreamHandler extends ChannelInboundHandlerAdap try (Closeable closeable = ManageSieveMDCContext.from(ctx)) { InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress(); LOGGER.info("Connection closed for {}", address.getAddress().getHostAddress()); - ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).getAndSet(null); - super.channelInactive(ctx); + Session session = ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).getAndSet(null); + try { + if (session != null) { + manageSieveProcessor.close(session); + } + } finally { + super.channelInactive(ctx); + } } } @@ -182,4 +194,20 @@ public class ManageSieveChannelUpstreamHandler extends ChannelInboundHandlerAdap channel.config().setAutoRead(true); } } + + private boolean supportsStartTLS() { + return secure != null && secure.supportsEncryption() && secure.isStartTLS(); + } + + private void handleTooLongFrame(ChannelHandlerContext ctx) { + ChannelManageSieveResponseWriter responseWriter = ctx.channel().attr(NettyConstants.RESPONSE_WRITER_ATTRIBUTE_KEY).get(); + responseWriter.resetCumulation(); + Session session = ctx.channel().attr(NettyConstants.SESSION_ATTRIBUTE_KEY).get(); + if (session != null && session.getState() == Session.State.AUTHENTICATION_IN_PROGRESS) { + // A discarded SASL response cannot be resumed; release the exchange before accepting another command. + manageSieveProcessor.close(session); + } + // Max line length exceeded. See also JAMES-1190. + responseWriter.write("NO Maximum command line length exceeded\r\n"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
