Skip to content

Commit

Permalink
fix (core): Validate CID in IPFSResource using ipld/java-cid
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jan 4, 2025
1 parent 022c6c1 commit 2e9fc16
Show file tree
Hide file tree
Showing 7 changed files with 468 additions and 18 deletions.
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ enola_maven.install(
"org.jsoup:jsoup:1.18.1",
"org.apache.tika:tika-core:3.0.0-BETA2",
"org.apache.tika:tika-parsers-standard-package:3.0.0-BETA2",
"com.github.ipld:java-cid:1.3.8",
],
# NB: Never de-activate duplicate_version_warning = "error"!
# While it can be tempting to resolve a build-time problem which
Expand All @@ -139,6 +140,8 @@ enola_maven.install(
repositories = [
# Override legacy default https://repo1.maven.org/maven2/
"https://repo.maven.apache.org/maven2/",
# Used by Multiformats, IPFS & IPLD
"https://jitpack.io",
],
)
use_repo(enola_maven, "enola_maven")
4 changes: 2 additions & 2 deletions docs/use/fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ and then use `--ipfs-gateway="http://localhost:8080/ipfs/"` instead.

For initial testing you can specify one of the [public IPFS Gateways](https://ipfs.github.io/public-gateway-checker/)
(or "rent" one from a provider such as [Pinata](https://pinata.cloud/dedicated-ipfs-gateways) or [Infura](https://www.infura.io/product/ipfs) or [Cloudflare](https://www.cloudflare.com/application-services/products/web3/)).
However, we do **NOT*** recommend using these for anything else than for illustration and initial testing,
because this has the following disadvantages:
However, we do **NOT** recommend using these for anything "real",
because of the following disadvantages:

* Local Nodes make great local caches to improve Enola's performance
* Public gateways are likely to rate limit your HTTP requests and return 403 errors
Expand Down
6 changes: 5 additions & 1 deletion java/dev/enola/common/io/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ java_library(
"//java/dev/enola/common/context",
"//java/dev/enola/common/convert",
"//java/dev/enola/data",
# TODO Separate BUILD for IPFSResource
"@enola_maven//:com_github_ipld_java_cid",
"@enola_maven//:com_github_java_json_tools_uri_template",
"@enola_maven//:com_google_auto_service_auto_service_annotations",
"@enola_maven//:com_google_errorprone_error_prone_annotations",
"@enola_maven//:com_google_guava_guava",
# TODO Separate BUILD for OkHttpResource
"@enola_maven//:com_squareup_okhttp3_logging_interceptor",
"@enola_maven//:com_squareup_okhttp3_okhttp", # TODO Separate library?
"@enola_maven//:com_squareup_okhttp3_okhttp",
"@enola_maven//:org_jspecify_jspecify",
"@enola_maven//:org_slf4j_slf4j_api",
],
Expand All @@ -51,6 +54,7 @@ junit_tests(
"//java/dev/enola/common/context/testlib",
"//java/dev/enola/common/protobuf",
"//test",
"@enola_maven//:com_github_ipld_java_cid",
"@enola_maven//:com_github_java_json_tools_uri_template",
"@enola_maven//:com_google_auto_service_auto_service_annotations",
"@enola_maven//:org_jspecify_jspecify",
Expand Down
13 changes: 5 additions & 8 deletions java/dev/enola/common/io/resource/IPFSResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@
import com.google.common.io.ByteSource;
import com.google.common.net.MediaType;

import io.ipfs.cid.Cid;

import java.net.URI;

/**
* <a href="https://ipfs.tech/">IPFS</a> Resource. TODO:
*
* <ol>
* <li>Support IPLD <=> Thing API bridge...
* <li>Existing? https://github.com/ipld/java-cid and
* https://github.com/ipfs-shipyard/java-ipfs-http-client#dependencies
* com.github.ipfs:java-ipfs-api, com.github.multiformats:java-multiaddr; cid.jar,
* multibase.jar, multihash.jar ?
* <li>Get Thing and add https://cid.ipfs.tech -like technical debugging info?
* <li>Support CAR files?
* <li>Support IPLD <=> Thing API bridge; see https://github.com/enola-dev/enola/issues/777.
* <li>Support writing - via a WritableResource, or (probably) a separate API?
* <li>Support ipns://
* <li>Detect IPFS in other links, using https://github.com/ipfs-shipyard/is-ipfs's algorithm
* </ol>
*/
public class IPFSResource extends BaseResource implements ReadableResource {
Expand Down Expand Up @@ -67,6 +62,8 @@ private static void check(URI uri, String gateway) {
if (!isIPFS(uri)) throw new IllegalArgumentException(uri.toString());
if (Strings.isNullOrEmpty(gateway))
throw new IllegalStateException("IPFS HTTP Gateway is required");
// Validate CID using parser from https://github.com/ipld/java-cid
Cid.decode(uri.getAuthority());
}

private final ReadableResource httpResource;
Expand Down
19 changes: 14 additions & 5 deletions java/dev/enola/common/io/resource/IPFSResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import dev.enola.common.context.testlib.SingletonRule;
import dev.enola.common.io.mediatype.MediaTypeProviders;

import io.ipfs.cid.Cid.CidEncodingException;

import org.junit.Rule;
import org.junit.Test;

Expand All @@ -50,7 +52,7 @@ public void hello() throws IOException {
}

@Test
public void vanGough() throws IOException {
public void vanGogh() throws IOException {
var url =
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/wiki/Vincent_van_Gogh.html";
var r =
Expand All @@ -71,8 +73,15 @@ private ReadableResource resourceFromIPFS(String url) throws IOException {
return new IPFSResource(URI.create(url), httpResourceProvider, IPFS_GATEWAY);
}

// TODO Test "ipfs:QmXV7pL1CB7A8Tzk7jP2XE9kRyk8HZd145KDptdxzmNLfu"
// (without // ) and "ipfs:/QmXV7pL1CB7A8Tzk7jP2XE9kRyk8HZd145KDptdxzmNLfu" with a single
// slash;
// does it also need to support "ipfs:/ipfs/Qm...." ?
@Test(expected = IllegalArgumentException.class)
public void notIFPS() {
var url = "http://www.google.com";
new IPFSResource(URI.create(url), httpResourceProvider, IPFS_GATEWAY);
}

@Test(expected = CidEncodingException.class)
public void badCID() {
var url = "ipfs://bad";
new IPFSResource(URI.create(url), httpResourceProvider, IPFS_GATEWAY);
}
}
1 change: 1 addition & 0 deletions learn/jbang/hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//JAVA 21+
//PREVIEW
//DEPS dev.enola:enola:0.0.1-SNAPSHOT
//REPOS mavencentral,jitpack

import static java.lang.System.out;

Expand Down
Loading

0 comments on commit 2e9fc16

Please sign in to comment.