Skip to content

Commit

Permalink
fix: Allow to attach unescaped links to a chat #481 (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
PMitrafanau authored Sep 16, 2024
1 parent 16f18b7 commit 84662bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/main/java/com/epam/aidial/core/function/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.epam.aidial.core.ProxyContext;
import com.epam.aidial.core.security.EncryptionService;
import com.epam.aidial.core.storage.ResourceDescription;
import com.epam.aidial.core.util.UrlUtil;
import lombok.SneakyThrows;

import java.net.URI;
import java.util.function.Function;

public abstract class BaseFunction<T, R> implements Function<T, R> {
Expand All @@ -20,14 +20,11 @@ public BaseFunction(Proxy proxy, ProxyContext context) {

@SneakyThrows
public static ResourceDescription fromAnyUrl(String url, EncryptionService encryption) {
if (url == null) {
return null;
}
URI uri = new URI(url);
if (uri.isAbsolute()) {
// skip public resource
if (url == null || UrlUtil.isAbsoluteUrl(url)) {
// second check to skip public resource
return null;
}

return ResourceDescription.fromAnyUrl(url, encryption);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/epam/aidial/core/util/UrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

@UtilityClass
public class UrlUtil {

private static final PercentCodec DECODER = new PercentCodec();
private static final Escaper ENCODER = UrlEscapers.urlPathSegmentEscaper();

/**
* Universal, non case-sensitive, protocol-agnostic URL pattern
*/
private static final Pattern ABSOLUTE_URL_PATTERN = Pattern.compile("^[a-z][a-z0-9-+.]*?://", Pattern.CASE_INSENSITIVE);

@SneakyThrows
public String encodePath(String path) {
return ENCODER.escape(path);
Expand All @@ -39,4 +45,8 @@ public String decodePath(String path, boolean checkUri) {
}
return new String(DECODER.decode(path.getBytes(Charset.defaultCharset())));
}

public boolean isAbsoluteUrl(String url) {
return ABSOLUTE_URL_PATTERN.matcher(url).find();
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/epam/aidial/core/util/UrlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class UrlUtilTest {

Expand All @@ -29,4 +31,16 @@ public void testPathDecoding() {
assertThrows(IllegalArgumentException.class, () -> UrlUtil.decodePath("/folder1/file#5.txt"));
assertThrows(IllegalArgumentException.class, () -> UrlUtil.decodePath("/folder1/q?file=5.txt"));
}

@Test
public void testIsAbsoluteUrl() {
assertTrue(UrlUtil.isAbsoluteUrl("test://example.com"));
assertTrue(UrlUtil.isAbsoluteUrl("TEST://EXAMPLE.COM"));
assertTrue(UrlUtil.isAbsoluteUrl("test1+test2://example.com/item"));
assertTrue(UrlUtil.isAbsoluteUrl("test1.test2://example.com/item"));
assertTrue(UrlUtil.isAbsoluteUrl("test1-test2://example.com/item"));
assertFalse(UrlUtil.isAbsoluteUrl("//example.com/item"));
assertFalse(UrlUtil.isAbsoluteUrl("/example/file.txt"));
assertFalse(UrlUtil.isAbsoluteUrl("file"));
}
}

0 comments on commit 84662bc

Please sign in to comment.