From 9fa7febd74af8c6856503b0854d1fc93f50453ab Mon Sep 17 00:00:00 2001 From: Tobias Hahnen Date: Tue, 26 Nov 2024 11:09:23 +0100 Subject: [PATCH] Don't await a StackOverflowError on a redirection loop In case of a redirection loop for URI of a p2 repository, fail fast on a redirection loop. In this case provide a warning to the user and let the rest be handled by Tycho. Otherwise there might be a StackOverflowError due to the recursion used and no check for whether the base URI is the same as the redirected URI. This links to #4451 but does not fix it, only raising visibility for such cases. --- .../tycho/p2maven/transport/HttpCache.java | 6 ++--- .../transport/RedirectionLoopException.java | 25 +++++++++++++++++++ .../transport/SharedHttpCacheStorage.java | 11 ++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RedirectionLoopException.java diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/HttpCache.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/HttpCache.java index 409186ee1d..b0367d6ada 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/HttpCache.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/HttpCache.java @@ -24,9 +24,9 @@ public interface HttpCache { * * @param uri * @return - * @throws FileNotFoundException - * if the URI is know to be not found + * @throws FileNotFoundException if the URI is know to be not found + * @throws RedirectionLoopException if the URI redirects to itself */ - CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundException; + CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundException, RedirectionLoopException; } \ No newline at end of file diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RedirectionLoopException.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RedirectionLoopException.java new file mode 100644 index 0000000000..0f8f441949 --- /dev/null +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RedirectionLoopException.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2024 Tobias Hahnen and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Tobias Hahnen - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.p2maven.transport; + +import java.io.IOException; + +/** + * Exception thrown when a URI would be redirecting to itself. This will cause a + * loop and therefore might lead to a StackOverflowError. + */ +public class RedirectionLoopException extends IOException { + public RedirectionLoopException(String uri) { + super(uri); + } +} diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java index b39b3c556c..fedffbc2a5 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java @@ -77,10 +77,11 @@ protected boolean removeEldestEntry(final Map.Entry eldest) { * * @param uri * @return - * @throws FileNotFoundException if the URI is know to be not found + * @throws FileNotFoundException if the URI is know to be not found + * @throws RedirectionLoopException if the URI redirects to itself */ @Override - public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundException { + public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundException, RedirectionLoopException { URI normalized = uri.normalize(); CacheLine cacheLine = getCacheLine(normalized); if (!cacheConfig.isUpdate()) { // if not updates are forced ... @@ -89,6 +90,12 @@ public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundExcep throw new FileNotFoundException(normalized.toASCIIString()); } if (code == HttpURLConnection.HTTP_MOVED_PERM) { + URI redirect = cacheLine.getRedirect(normalized); + if (normalized.equals(redirect.normalize())) { + logger.warn("Request to " + normalized + " would redirect to itself, this would cause a loop"); + throw new RedirectionLoopException(normalized.toASCIIString()); + } + return getCacheEntry(cacheLine.getRedirect(normalized), logger); } }