Skip to content

Commit 3f12b3a

Browse files
committed
Update to fix breaking change in ipfs v0.5.0
(use POST instead of GET)
1 parent b5f6b90 commit 3f12b3a

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

lib/cid.jar

-68 Bytes
Binary file not shown.

lib/multibase.jar

18.2 KB
Binary file not shown.

lib/multihash.jar

3.86 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.ipfs</groupId>
66
<artifactId>java-ipfs-http-client</artifactId>
7-
<version>v1.2.3</version>
7+
<version>v1.3.0</version>
88
<packaging>jar</packaging>
99

1010
<name>java-ipfs-http-client</name>

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,35 @@ private byte[] retrieve(String path) throws IOException {
676676
}
677677

678678
private static byte[] get(URL target, int timeout) throws IOException {
679-
HttpURLConnection conn = configureConnection(target, "GET", timeout);
679+
HttpURLConnection conn = configureConnection(target, "POST", timeout);
680+
conn.setDoOutput(true);
681+
/* See IFFS commit for why this is a POST and not a GET https://github.com/ipfs/go-ipfs/pull/7097
682+
This commit upgrades go-ipfs-cmds and configures the commands HTTP API Handler
683+
to only allow POST/OPTIONS, disallowing GET and others in the handling of
684+
command requests in the IPFS HTTP API (where before every type of request
685+
method was handled, with GET/POST/PUT/PATCH being equivalent).
686+
687+
The Read-Only commands that the HTTP API attaches to the gateway endpoint will
688+
additional handled GET as they did before (but stop handling PUT,DELETEs).
689+
690+
By limiting the request types we address the possibility that a website
691+
accessed by a browser abuses the IPFS API by issuing GET requests to it which
692+
have no Origin or Referrer set, and are thus bypass CORS and CSRF protections.
693+
694+
This is a breaking change for clients that relay on GET requests against the
695+
HTTP endpoint (usually :5001). Applications integrating on top of the
696+
gateway-read-only API should still work (including cross-domain access).
697+
*/
698+
conn.setRequestMethod("POST");
699+
conn.setRequestProperty("Content-Type", "application/json");
700+
conn.setConnectTimeout(10_000);
701+
conn.setReadTimeout(60_000);
680702

681703
try {
704+
OutputStream out = conn.getOutputStream();
705+
out.write(new byte[0]);
706+
out.flush();
707+
out.close();
682708
InputStream in = conn.getInputStream();
683709
ByteArrayOutputStream resp = new ByteArrayOutputStream();
684710

@@ -689,13 +715,10 @@ private static byte[] get(URL target, int timeout) throws IOException {
689715
return resp.toByteArray();
690716
} catch (ConnectException e) {
691717
throw new RuntimeException("Couldn't connect to IPFS daemon at "+target+"\n Is IPFS running?");
692-
} catch (SocketTimeoutException e) {
693-
throw new RuntimeException(String.format("timeout (%d ms) has been exceeded", timeout));
694718
} catch (IOException e) {
695-
String err = Optional.ofNullable(conn.getErrorStream())
696-
.map(s->new String(readFully(s)))
697-
.orElse(e.getMessage());
698-
throw new RuntimeException("IOException contacting IPFS daemon.\nTrailer: " + conn.getHeaderFields().get("Trailer") + " " + err, e);
719+
InputStream errorStream = conn.getErrorStream();
720+
String err = errorStream == null ? e.getMessage() : new String(readFully(errorStream));
721+
throw new RuntimeException("IOException contacting IPFS daemon.\n"+err+"\nTrailer: " + conn.getHeaderFields().get("Trailer"), e);
699722
}
700723
}
701724

src/main/java/io/ipfs/api/IpldNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ default Cid cid() {
3030
MessageDigest md = MessageDigest.getInstance("SHA-256");
3131
md.update(raw);
3232
byte[] digest = md.digest();
33-
Multihash h = new Multihash(Multihash.Type.sha2_256, digest);
34-
return new Cid(1, Cid.Codec.DagCbor, h);
33+
return new Cid(1, Cid.Codec.DagCbor, Multihash.Type.sha2_256, digest);
3534
} catch (NoSuchAlgorithmException e) {
3635
throw new RuntimeException(e.getMessage(), e);
3736
}

0 commit comments

Comments
 (0)