Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't await a StackOverflowError on a redirection loop #4452

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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
Loading