add TrailingSlashAliasChecker to fix tests

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2024-01-30 11:17:44 +11:00
parent c1e2d47968
commit a1c49c6322
3 changed files with 44 additions and 3 deletions

View File

@ -24,9 +24,8 @@ import org.slf4j.LoggerFactory;
/**
* An extension of {@link AllowedResourceAliasChecker} which will allow symlinks alias to arbitrary
* targets, so long as the symlink file itself is an allowed resource. Unlike {@link AllowedResourceAliasChecker}
* this will only not approve any alias which resolves to an allowed resource, it must contain an allowed symlink or
* the alias will not be allowed.
* targets, so long as the symlink file itself is an allowed resource. Non symlinked paths are never
* allowed by this {@link AliasCheck}.
*/
public class SymlinkAllowedResourceAliasChecker extends AllowedResourceAliasChecker
{

View File

@ -0,0 +1,39 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.server;
import java.util.Objects;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.resource.Resource;
/**
* <p>This will approve an alias where the only difference is a trailing slash.</p>
*/
public class TrailingSlashAliasChecker extends AbstractLifeCycle implements AliasCheck
{
@Override
public boolean checkAlias(String pathInContext, Resource resource)
{
String uri = resource.getURI().toString();
if (uri.isEmpty())
return false;
String realUri = resource.getRealURI().toString();
if (uri.endsWith("/") && !realUri.endsWith("/"))
return Objects.equals(uri.substring(0, uri.length() - 1), realUri);
return false;
}
}

View File

@ -55,6 +55,7 @@ import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.ResourceService;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.TrailingSlashAliasChecker;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
@ -3132,6 +3133,7 @@ public class ResourceHandlerTest
@Test
public void testRelativeRedirect() throws Exception
{
_contextHandler.addAliasCheck(new TrailingSlashAliasChecker());
Path dir = docRoot.resolve("dir");
FS.ensureDirExists(dir);
Path index = dir.resolve("index.html");
@ -3180,6 +3182,7 @@ public class ResourceHandlerTest
@Test
public void testResourceRedirect() throws Exception
{
_contextHandler.addAliasCheck(new TrailingSlashAliasChecker());
setupSimpleText(docRoot);
HttpTester.Response response = HttpTester.parseResponse(