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

Do not enforce valid URI. #16581

Merged
merged 5 commits into from
Nov 26, 2024
Merged
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 @@ -56,7 +56,7 @@ public AzureUrlProvider(final AzureSession session) {
}

@Override
public DescriptiveUrlBag toUrl(final Path file) {
public DescriptiveUrlBag toUrl(final Path file, final EnumSet<DescriptiveUrl.Type> types) {
final DescriptiveUrlBag list = new DescriptiveUrlBag();
// In one hour
list.add(this.toSignedUrl(file, (int) TimeUnit.HOURS.toSeconds(1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.URI;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.TimeZone;
Expand Down Expand Up @@ -74,7 +73,7 @@ public DescriptiveUrl toDownloadUrl(final Path file, final Sharee sharee, final
expiry.add(Calendar.SECOND, seconds);
final String token = session.getClient().getDownloadAuthorization(fileid.getVersionId(containerService.getContainer(file)),
StringUtils.EMPTY, seconds);
return new DescriptiveUrl(URI.create(String.format("%s?Authorization=%s", download, token)), DescriptiveUrl.Type.signed,
return new DescriptiveUrl(String.format("%s?Authorization=%s", download, token), DescriptiveUrl.Type.signed,
MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3"))
+ " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")",
UserDateFormatterFactory.get().getMediumFormat(expiry.getTimeInMillis()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.UrlProvider;

import java.net.URI;
import java.text.MessageFormat;
import java.util.EnumSet;
import java.util.Locale;

public class B2UrlProvider implements UrlProvider {
Expand All @@ -40,7 +40,7 @@ public B2UrlProvider(final B2Session session) {
}

@Override
public DescriptiveUrlBag toUrl(final Path file) {
public DescriptiveUrlBag toUrl(final Path file, final EnumSet<DescriptiveUrl.Type> types) {
if(file.isVolume()) {
return DescriptiveUrlBag.empty();
}
Expand All @@ -49,7 +49,7 @@ public DescriptiveUrlBag toUrl(final Path file) {
final String download = String.format("%s/file/%s/%s", session.getClient().getDownloadUrl(),
URIEncoder.encode(containerService.getContainer(file).getName()),
URIEncoder.encode(containerService.getKey(file)));
list.add(new DescriptiveUrl(URI.create(download), DescriptiveUrl.Type.http,
list.add(new DescriptiveUrl(download, DescriptiveUrl.Type.http,
MessageFormat.format(LocaleFactory.localizedString("{0} URL"), Scheme.https.name().toUpperCase(Locale.ROOT))));
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import ch.cyberduck.core.exception.UnsupportedException;
import ch.cyberduck.core.features.Share;

import java.net.URI;
import java.text.MessageFormat;

public class BoxShareFeature implements Share {
Expand Down Expand Up @@ -81,7 +80,7 @@ private DescriptiveUrl createFileSharedLink(final Path file, final PasswordCallb
new FilesFileIdaddSharedLinkBody()
.sharedLink(new FilesfileIdaddSharedLinkSharedLink().permissions(new FilesfileIdSharedLinkPermissions().canDownload(true))
.password(password)));
return new DescriptiveUrl(URI.create(link.getSharedLink().getDownloadUrl()), DescriptiveUrl.Type.signed);
return new DescriptiveUrl(link.getSharedLink().getDownloadUrl(), DescriptiveUrl.Type.signed);
}
catch(ApiException e) {
throw new BoxExceptionMappingService(fileid).map(e);
Expand All @@ -97,7 +96,7 @@ private DescriptiveUrl createFolderSharedLink(final Path file, final PasswordCal
new FoldersFolderIdaddSharedLinkBody()
.sharedLink(new FoldersfolderIdaddSharedLinkSharedLink().permissions(new FoldersfolderIdaddSharedLinkSharedLinkPermissions().canDownload(false))
.password(password)));
return new DescriptiveUrl(URI.create(link.getSharedLink().getUrl()), DescriptiveUrl.Type.signed);
return new DescriptiveUrl(link.getSharedLink().getUrl(), DescriptiveUrl.Type.signed);
}
catch(ApiException e) {
throw new BoxExceptionMappingService(fileid).map(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import org.apache.commons.lang3.StringUtils;

import java.net.URI;
import java.text.MessageFormat;
import java.util.Collections;

Expand All @@ -53,9 +52,9 @@ public DescriptiveUrl toDownloadUrl(final Path file, final Sharee sharee, final
LocaleFactory.localizedString("Passphrase", "Cryptomator"),
MessageFormat.format(LocaleFactory.localizedString("Create a passphrase required to access {0}", "Credentials"), file.getName()),
new LoginOptions().anonymous(true).keychain(false).icon(session.getHost().getProtocol().disk()));
return new DescriptiveUrl(URI.create(new BundlesApi(new BrickApiClient(session))
return new DescriptiveUrl(new BundlesApi(new BrickApiClient(session))
.postBundles(new BundlesBody().password(password.isPasswordAuthentication() ? password.getPassword() : null).paths(Collections.singletonList(
StringUtils.removeStart(file.getAbsolute(), String.valueOf(Path.DELIMITER))))).getUrl()), DescriptiveUrl.Type.signed);
StringUtils.removeStart(file.getAbsolute(), String.valueOf(Path.DELIMITER))))).getUrl(), DescriptiveUrl.Type.signed);
}
catch(ApiException e) {
throw new BrickExceptionMappingService().map(e);
Expand Down
16 changes: 4 additions & 12 deletions core/src/main/java/ch/cyberduck/core/DefaultWebUrlProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import org.apache.commons.lang3.StringUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;

public class DefaultWebUrlProvider implements WebUrlProvider {
Expand All @@ -47,15 +45,9 @@ public DescriptiveUrl toUrl(final Host bookmark) {
base = String.format("http://%s/", bookmark.getWebURL());
}
}
final URI uri;
try {
uri = new URI(base);
}
catch(URISyntaxException e) {
return DescriptiveUrl.EMPTY;
}
return new DescriptiveUrl(uri,
DescriptiveUrl.Type.http,
MessageFormat.format(LocaleFactory.localizedString("{0} URL"), StringUtils.upperCase(uri.getScheme())));
return new DescriptiveUrl(base,
DescriptiveUrl.Type.http,
MessageFormat.format(LocaleFactory.localizedString("{0} URL"),
StringUtils.upperCase(StringUtils.substringBefore(base, "://"))));
}
}
17 changes: 9 additions & 8 deletions core/src/main/java/ch/cyberduck/core/DescriptiveUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

import org.apache.commons.lang3.StringUtils;

import java.net.URI;
import java.text.MessageFormat;
import java.util.Objects;

public class DescriptiveUrl {

public static final DescriptiveUrl EMPTY = new DescriptiveUrl(URI.create(StringUtils.EMPTY));
public static final DescriptiveUrl EMPTY = new DescriptiveUrl(StringUtils.EMPTY);

private final URI url;
private final String url;
private final Type type;
private final String help;

Expand All @@ -50,15 +49,17 @@ public enum Type {
encrypted
}

public DescriptiveUrl(final URI url) {
this(url, Type.http, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), StringUtils.upperCase(url.getScheme())));
public DescriptiveUrl(final String url) {
this(url, Type.http, MessageFormat.format(LocaleFactory.localizedString("{0} URL"),
StringUtils.upperCase(StringUtils.substringBefore(url, "://"))));
}

public DescriptiveUrl(final URI url, Type type) {
this(url, type, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), StringUtils.upperCase(url.getScheme())));
public DescriptiveUrl(final String url, Type type) {
this(url, type, MessageFormat.format(LocaleFactory.localizedString("{0} URL"),
StringUtils.upperCase(StringUtils.substringBefore(url, "://"))));
}

public DescriptiveUrl(final URI url, Type type, final String help) {
public DescriptiveUrl(final String url, Type type, final String help) {
this.url = url;
this.type = type;
this.help = help;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* GNU General Public License for more details.
*/

import java.util.EnumSet;

public class DisabledUrlProvider implements UrlProvider {
@Override
public DescriptiveUrlBag toUrl(final Path file) {
public DescriptiveUrlBag toUrl(final Path file, final EnumSet<DescriptiveUrl.Type> types) {
return DescriptiveUrlBag.empty();
}
}
24 changes: 13 additions & 11 deletions core/src/main/java/ch/cyberduck/core/HostWebUrlProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
* feedback@cyberduck.io
*/

import java.net.URI;
import org.apache.commons.lang3.StringUtils;

import java.util.EnumSet;

public class HostWebUrlProvider implements UrlProvider {

Expand All @@ -29,15 +31,15 @@ public HostWebUrlProvider(final Host host) {
}

@Override
public DescriptiveUrlBag toUrl(final Path file) {
final DescriptiveUrlBag list = new DescriptiveUrlBag();
final DescriptiveUrl base = new DefaultWebUrlProvider().toUrl(host);
list.add(new DescriptiveUrl(URI.create(String.format("%s%s", base.getUrl(), URIEncoder.encode(
PathNormalizer.normalize(PathRelativizer.relativize(PathNormalizer.normalize(host.getDefaultPath(), true), file.getAbsolute()))
))).normalize(),
base.getType(),
base.getHelp())
);
return list;
public DescriptiveUrlBag toUrl(final Path file, final EnumSet<DescriptiveUrl.Type> types) {
if(types.contains(DescriptiveUrl.Type.http)) {
final DescriptiveUrlBag list = new DescriptiveUrlBag();
final DescriptiveUrl base = new DefaultWebUrlProvider().toUrl(host);
list.add(new DescriptiveUrl(String.format("%s%s", StringUtils.stripEnd(base.getUrl(), String.valueOf(Path.DELIMITER)), URIEncoder.encode(
PathNormalizer.normalize(PathRelativizer.relativize(PathNormalizer.normalize(host.getDefaultPath(), true), file.getAbsolute()))
)), base.getType(), base.getHelp()));
return list;
}
return DescriptiveUrlBag.empty();
}
}
8 changes: 3 additions & 5 deletions core/src/main/java/ch/cyberduck/core/PathNormalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public static String normalize(final String path, final boolean absolute) {
StringBuilder n = new StringBuilder();
if(normalized.startsWith("//")) {
// see #972. Omit leading delimiter
n.append(Path.DELIMITER);
n.append(Path.DELIMITER);
n.append(Path.DELIMITER).append(Path.DELIMITER);
}
else if(absolute) {
// convert to absolute path
Expand All @@ -121,8 +120,7 @@ else if(normalized.startsWith(String.valueOf(Path.DELIMITER))) {
// Keep absolute path
n.append(Path.DELIMITER);
}
// Remove duplicated Path.DELIMITERs

// Remove duplicated "/"
final String[] segments = StringUtils.split(normalized, String.valueOf(Path.DELIMITER));
for(String segment : segments) {
if(segment.equals(StringUtils.EMPTY)) {
Expand All @@ -133,7 +131,7 @@ else if(normalized.startsWith(String.valueOf(Path.DELIMITER))) {
}
normalized = n.toString();
while(normalized.endsWith(String.valueOf(Path.DELIMITER)) && normalized.length() > 1) {
//Strip any redundant delimiter at the end of the path
// Strip any redundant delimiter at the end of the path
normalized = normalized.substring(0, normalized.length() - 1);
}
// Return the normalized path that we have completed
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/ch/cyberduck/core/UrlProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
* Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
*/

import java.util.EnumSet;

public interface UrlProvider {
DescriptiveUrlBag toUrl(Path file);
default DescriptiveUrlBag toUrl(Path file) {
return this.toUrl(file, EnumSet.allOf(DescriptiveUrl.Type.class));
}

DescriptiveUrlBag toUrl(Path file, EnumSet<DescriptiveUrl.Type> types);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,58 @@
import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class DistributionUrlProvider implements UrlProvider {

private final Distribution distribution;

private final PathContainerService containerService
= new DefaultPathContainerService();
= new DefaultPathContainerService();

public DistributionUrlProvider(final Distribution distribution) {
this.distribution = distribution;
}

@Override
public DescriptiveUrlBag toUrl(final Path file) {
public DescriptiveUrlBag toUrl(final Path file, final EnumSet<DescriptiveUrl.Type> types) {
final DescriptiveUrlBag list = new DescriptiveUrlBag();
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getOrigin()), DescriptiveUrl.Type.origin,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString("Origin", "Info"))));
if(distribution.getUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getUrl()), DescriptiveUrl.Type.cdn,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"))));
if(types.contains(DescriptiveUrl.Type.origin)) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getOrigin()), DescriptiveUrl.Type.origin,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString("Origin", "Info"))));
}
if(distribution.getSslUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getSslUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (SSL)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
}
if(distribution.getStreamingUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getStreamingUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
if(types.contains(DescriptiveUrl.Type.cdn)) {
if(distribution.getUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getUrl()), DescriptiveUrl.Type.cdn,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"))));
}
if(distribution.getSslUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getSslUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (SSL)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
}
if(distribution.getStreamingUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getStreamingUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
}
if(distribution.getiOSstreamingUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getiOSstreamingUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (iOS Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
}
}
if(distribution.getiOSstreamingUrl() != null) {
list.add(new DescriptiveUrl(this.toUrl(file, distribution.getiOSstreamingUrl()), DescriptiveUrl.Type.cdn,
String.format("%s (iOS Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} {1} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))));
if(types.contains(DescriptiveUrl.Type.cname)) {
list.addAll(this.toCnameUrl(file));
}
list.addAll(this.toCnameUrl(file));
return list;
}

Expand All @@ -85,7 +92,7 @@ public DescriptiveUrlBag toUrl(final Path file) {
* @param origin Distribution URL
* @return URL to file in distribution
*/
private URI toUrl(final Path file, final URI origin) {
private String toUrl(final Path file, final URI origin) {
final StringBuilder b = new StringBuilder(String.format("%s://%s", origin.getScheme(), origin.getHost()));
if(distribution.getMethod().equals(Distribution.CUSTOM)) {
b.append(Path.DELIMITER).append(URIEncoder.encode(PathRelativizer.relativize(origin.getRawPath(), file.getAbsolute())));
Expand All @@ -98,7 +105,7 @@ private URI toUrl(final Path file, final URI origin) {
b.append(Path.DELIMITER).append(URIEncoder.encode(containerService.getKey(file)));
}
}
return URI.create(b.toString()).normalize();
return b.toString();
}

/**
Expand All @@ -113,11 +120,11 @@ private List<DescriptiveUrl> toCnameUrl(final Path file) {
if(StringUtils.isNotEmpty(containerService.getKey(file))) {
b.append(Path.DELIMITER).append(URIEncoder.encode(containerService.getKey(file)));
}
urls.add(new DescriptiveUrl(URI.create(b.toString()).normalize(), DescriptiveUrl.Type.cname,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} {2} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"),
LocaleFactory.localizedString("CNAME", "S3"))));
urls.add(new DescriptiveUrl(b.toString(), DescriptiveUrl.Type.cname,
MessageFormat.format(LocaleFactory.localizedString("{0} {1} {2} URL"),
distribution.getName(),
LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"),
LocaleFactory.localizedString("CNAME", "S3"))));
}
return urls;
}
Expand Down
Loading