This is an automated email from the ASF dual-hosted git repository. reiern70 pushed a commit to branch websocket-page-expired-1899 in repository https://gitbox.apache.org/repos/asf/wicket.git
commit b988e9da4e051ad1b2d3f6890a3d7fd98b1a3c7b Author: reiern70 <[email protected]> AuthorDate: Fri Nov 13 22:09:21 2026 -0500 Close a WebSocket connection whose page has expired When a client opened a native WebSocket for a page id that the page manager of its session could no longer find, AbstractWebSocketProcessor accepted the handshake, registered the connection and then did nothing: no onConnect, no close, only a debug log line. Every later frame and every push took the same branch, so the client held an open socket that never received anything and the registry held a connection nobody could use. This happens whenever the session outlives its page store - an application restart or redeployment with a persistent session store, or plain page store eviction - and the browser reconnects with the page id it was rendered with. A client waiting for a first message cannot tell it apart from a slow server; it times out, reconnects with the same page id and gets the same silent connection. WICKET-6267 added the null check to stop a push to an expired page from throwing in the constructor of WebSocketRequestHandler, but left the connection open. The neighbouring outcomes of the same lookup already close: an ended session with 1008 "Session ended", a refused WebSocketResource with 1008 "Not authorized". A missing page now does the same as an ended session: the connection is removed from the registry and, unless the message is a ClosedMessage, ErrorMessage or AbortedMessage, closed with 1008 (policy violation) and the reason "Page expired". The reason is distinct from "Session ended" so that a client can tell that reloading the page is what helps. The user guide documents both reasons. An application sees a client whose page has expired get a close frame instead of a silent connection. No behavior of the missing page was reached before and none is reached now, so server-side code is unaffected; a client that listens for the close (Wicket's own script publishes it on /websocket/closed with the code and the reason) can now react to it, for example by offering a reload. GitHub issue #1899 --- .../ws/api/AbstractWebSocketProcessor.java | 26 ++- .../ws/tester/WebSocketPageExpiredTest.java | 260 +++++++++++++++++++++ .../nativewebsockets/nativewebsockets_5.adoc | 5 +- 3 files changed, 289 insertions(+), 2 deletions(-) diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/AbstractWebSocketProcessor.java b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/AbstractWebSocketProcessor.java index f7d86befda..a91367442d 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/AbstractWebSocketProcessor.java +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/AbstractWebSocketProcessor.java @@ -101,6 +101,14 @@ public abstract class AbstractWebSocketProcessor implements IWebSocketProcessor */ static final String SESSION_ENDED = "Session ended"; + /** + * The reason given to a client whose session is still there but whose page is not: the page + * store no longer holds the page the connection was opened for. It is sent with close code + * {@value WebSocketConnectionOriginFilter#POLICY_VIOLATION_ERROR_CODE}, like + * {@link #SESSION_ENDED}, and tells a client that reloading the page is what helps. + */ + static final String PAGE_EXPIRED = "Page expired"; + private final WebRequest webRequest; private final int pageId; private final String context; @@ -265,6 +273,16 @@ public abstract class AbstractWebSocketProcessor implements IWebSocketProcessor * AbortedMessage is still dropped silently in that case, because closing a connection that * is already going away says nothing. * </p> + * <p> + * The page is looked up again for every message, too. When the session lives on but its + * page store no longer holds the page - the page was evicted, or the session outlived a + * restart that the store did not - the connection is removed from the registry and closed + * with {@value WebSocketConnectionOriginFilter#POLICY_VIOLATION_ERROR_CODE} and the reason + * "Page expired", which is distinct from the "Session ended" of an ended session so that a + * client can tell that reloading the page is what helps. No behavior of the page is reached, not + * even with the ConnectedMessage. A ClosedMessage, ErrorMessage or AbortedMessage is + * dropped silently, as for an ended session. + * </p> * * @param message * the message to broadcast @@ -344,7 +362,13 @@ public abstract class AbstractWebSocketProcessor implements IWebSocketProcessor } else { - LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId); + connectionRegistry.removeConnection(application, sessionId, connection.getKey()); + LOG.debug("Page with id '{}' has expired. Closing the connection with key '{}'.", + pageId, connection.getKey()); + if (isSpecialMessage(message) == false) + { + connection.close(WebSocketConnectionOriginFilter.POLICY_VIOLATION_ERROR_CODE, PAGE_EXPIRED); + } } } catch (Exception x) diff --git a/wicket-native-websocket/wicket-native-websocket-tester/src/test/java/org/apache/wicket/protocol/ws/tester/WebSocketPageExpiredTest.java b/wicket-native-websocket/wicket-native-websocket-tester/src/test/java/org/apache/wicket/protocol/ws/tester/WebSocketPageExpiredTest.java new file mode 100644 index 0000000000..44ef13c9e0 --- /dev/null +++ b/wicket-native-websocket/wicket-native-websocket-tester/src/test/java/org/apache/wicket/protocol/ws/tester/WebSocketPageExpiredTest.java @@ -0,0 +1,260 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.protocol.ws.tester; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.wicket.MarkupContainer; +import org.apache.wicket.markup.IMarkupResourceStreamProvider; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.protocol.ws.WebSocketSettings; +import org.apache.wicket.protocol.ws.api.IWebSocketConnection; +import org.apache.wicket.protocol.ws.api.WebSocketBehavior; +import org.apache.wicket.protocol.ws.api.WebSocketConnectionOriginFilter; +import org.apache.wicket.protocol.ws.api.WebSocketRequestHandler; +import org.apache.wicket.protocol.ws.api.message.ClosedMessage; +import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; +import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage; +import org.apache.wicket.protocol.ws.api.message.TextMessage; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * A connection whose session lives on but whose page is no longer in the page store reaches no + * behavior of that page, and is closed rather than left open delivering nothing. + */ +class WebSocketPageExpiredTest +{ + private static final AtomicInteger CONNECTS = new AtomicInteger(); + + private static final AtomicInteger MESSAGES = new AtomicInteger(); + + private static final AtomicInteger PUSHES = new AtomicInteger(); + + private static final AtomicInteger CLOSES = new AtomicInteger(); + + private WicketTester tester; + + /** + * The page the connection is opened for. + */ + public static class WebSocketPage extends WebPage implements IMarkupResourceStreamProvider + { + public WebSocketPage() + { + add(new WebSocketBehavior() + { + @Override + protected void onConnect(ConnectedMessage message) + { + CONNECTS.incrementAndGet(); + } + + @Override + protected void onMessage(WebSocketRequestHandler handler, TextMessage message) + { + MESSAGES.incrementAndGet(); + } + + @Override + protected void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) + { + PUSHES.incrementAndGet(); + } + + @Override + protected void onClose(ClosedMessage message) + { + CLOSES.incrementAndGet(); + } + }); + } + + @Override + public IResourceStream getMarkupResourceStream(MarkupContainer container, + Class<?> containerClass) + { + return new StringResourceStream("<html><head></head><body></body></html>"); + } + } + + private static class PushMessage implements IWebSocketPushMessage + { + } + + @BeforeEach + void before() + { + CONNECTS.set(0); + MESSAGES.set(0); + PUSHES.set(0); + CLOSES.set(0); + + tester = new WicketTester(); + } + + @AfterEach + void after() + { + tester.destroy(); + } + + private Collection<IWebSocketConnection> connections() + { + WebApplication application = tester.getApplication(); + return WebSocketSettings.Holder.get(application) + .getConnectionRegistry() + .getConnections(application, tester.getHttpSession().getId()); + } + + private void expirePages() + { + tester.getSession().getPageManager().clear(); + } + + private void assertClosedAsExpired(WebSocketTester webSocketTester) + { + assertFalse(webSocketTester.isConnectionOpen(), "the connection was left open"); + assertEquals(WebSocketConnectionOriginFilter.POLICY_VIOLATION_ERROR_CODE, + webSocketTester.getCloseCode()); + assertEquals("Page expired", webSocketTester.getCloseReason()); + assertTrue(connections().isEmpty(), "the connection was left in the registry"); + } + + @Test + void aConnectionToALivePageStaysOpen() + { + WebSocketPage page = tester.startPage(WebSocketPage.class); + tester.getSession().bind(); + + WebSocketTester webSocketTester = new WebSocketTester(tester, page); + try + { + assertEquals(1, CONNECTS.get()); + assertTrue(webSocketTester.isConnectionOpen()); + assertEquals(1, connections().size()); + + webSocketTester.sendMessage("hello"); + assertEquals(1, MESSAGES.get()); + assertTrue(webSocketTester.isConnectionOpen()); + } + finally + { + webSocketTester.destroy(); + } + } + + /** + * The session survived but the page store did not - a restart, or an eviction - and the client + * reconnects with the page id it was rendered with. + */ + @Test + void aConnectionToAnExpiredPageIsClosedAtTheHandshake() + { + WebSocketPage page = tester.startPage(WebSocketPage.class); + tester.getSession().bind(); + + expirePages(); + + WebSocketTester webSocketTester = new WebSocketTester(tester, page); + try + { + assertEquals(0, CONNECTS.get(), "onConnect ran for a page that is not in the store"); + assertClosedAsExpired(webSocketTester); + } + finally + { + webSocketTester.destroy(); + } + } + + @Test + void aConnectionIsClosedByTheFirstMessageAfterItsPageExpired() + { + WebSocketPage page = tester.startPage(WebSocketPage.class); + tester.getSession().bind(); + + WebSocketTester webSocketTester = new WebSocketTester(tester, page); + try + { + assertEquals(1, CONNECTS.get(), "the page must be reachable to start with"); + + expirePages(); + webSocketTester.sendMessage("after the page expired"); + + assertEquals(0, MESSAGES.get()); + assertClosedAsExpired(webSocketTester); + } + finally + { + webSocketTester.destroy(); + } + } + + @Test + void aConnectionIsClosedByAPushAfterItsPageExpired() + { + WebSocketPage page = tester.startPage(WebSocketPage.class); + tester.getSession().bind(); + + WebSocketTester webSocketTester = new WebSocketTester(tester, page); + try + { + expirePages(); + webSocketTester.broadcastAll(tester.getApplication(), new PushMessage()); + + assertEquals(0, PUSHES.get()); + assertClosedAsExpired(webSocketTester); + } + finally + { + webSocketTester.destroy(); + } + } + + /** + * A connection that is going away anyway is not closed a second time, but it does not stay in + * the registry either. + */ + @Test + void aClosingConnectionToAnExpiredPageIsNotClosedAgain() + { + WebSocketPage page = tester.startPage(WebSocketPage.class); + tester.getSession().bind(); + + WebSocketTester webSocketTester = new WebSocketTester(tester, page); + + expirePages(); + webSocketTester.destroy(); + + assertEquals(0, CLOSES.get()); + assertEquals(-1, webSocketTester.getCloseCode(), "the server closed the connection again"); + assertNull(webSocketTester.getCloseReason()); + assertTrue(connections().isEmpty(), "the connection was left in the registry"); + } +} diff --git a/wicket-user-guide/src/main/asciidoc/nativewebsockets/nativewebsockets_5.adoc b/wicket-user-guide/src/main/asciidoc/nativewebsockets/nativewebsockets_5.adoc index 577c17ac0b..c85af68bc5 100644 --- a/wicket-user-guide/src/main/asciidoc/nativewebsockets/nativewebsockets_5.adoc +++ b/wicket-user-guide/src/main/asciidoc/nativewebsockets/nativewebsockets_5.adoc @@ -19,4 +19,7 @@ The Web Socket communication is not processed by Servlet Filters and Listeners and thus the Dependency Injection libraries have no chance to export the request and session bean proxies. 2. A connection lasts no longer than the HTTP session it was opened on. -Every message resolves its Wicket `Session` out of that HTTP session, so once the session ends - whether the application invalidated it, a filter or a container-managed logout did, or it simply timed out - the connection is closed with code 1008 and no further message reaches the page or the resource. What a callback writes to the session goes to that same HTTP session, so a sign out performed over a connection ends the session of the client as one performed over HTTP does. +Every message resolves its Wicket `Session` out of that HTTP session, so once the session ends - whether the application invalidated it, a filter or a container-managed logout did, or it simply timed out - the connection is closed with code 1008 and the reason `Session ended`, and no further message reaches the page or the resource. What a callback writes to the session goes to that same HTTP session, so a sign out performed over a connection ends the session of the client as one perform [...] + +3. A connection lasts no longer than the page it was opened for. +Every message looks the page up again in the page store of the session. When the session lives on but the page does not - the store evicted it, or the session survived a restart that the store did not - the connection is closed with code 1008 and the reason `Page expired`, and none of the behaviors of the page is reached, not even `onConnect`. A client can tell the two reasons apart: after `Page expired` reloading the page gives it a working connection again, while reconnecting from the [...]
