Skip to content

Commit

Permalink
Don't await a StackOverflowError on a redirection loop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thahnen committed Nov 26, 2024
1 parent b4e2f86 commit 9fa7feb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ protected boolean removeEldestEntry(final Map.Entry<File, CacheLine> 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 ...
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 9fa7feb

Please sign in to comment.