From c9e6d05fa0db1810f16980bffa661fa7ae5fd10d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 26 Jan 2017 18:39:25 +0100 Subject: [PATCH] use base64 encoding for file names uploaded with http --- .../conversations/generator/IqGenerator.java | 22 ++++++++++++++++++- .../conversations/xmpp/XmppConnection.java | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java index cb9ffd96f..8393a0115 100644 --- a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java @@ -10,12 +10,15 @@ import org.whispersystems.libaxolotl.ecc.ECPublicKey; import org.whispersystems.libaxolotl.state.PreKeyRecord; import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; +import java.math.BigInteger; +import java.nio.ByteBuffer; import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TimeZone; +import java.util.UUID; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; @@ -315,7 +318,7 @@ public class IqGenerator extends AbstractGenerator { IqPacket packet = new IqPacket(IqPacket.TYPE.GET); packet.setTo(host); Element request = packet.addChild("request", Xmlns.HTTP_UPLOAD); - request.addChild("filename").setContent(file.getName()); + request.addChild("filename").setContent(convertFilename(file.getName())); request.addChild("size").setContent(String.valueOf(file.getExpectedSize())); if (mime != null) { request.addChild("content-type").setContent(mime); @@ -323,6 +326,23 @@ public class IqGenerator extends AbstractGenerator { return packet; } + private static String convertFilename(String name) { + int pos = name.indexOf('.'); + if (pos != -1) { + try { + UUID uuid = UUID.fromString(name.substring(0, pos)); + ByteBuffer bb = ByteBuffer.wrap(new byte[16]); + bb.putLong(uuid.getMostSignificantBits()); + bb.putLong(uuid.getLeastSignificantBits()); + return Base64.encodeToString(bb.array(), Base64.URL_SAFE) + name.substring(pos, name.length()); + } catch (Exception e) { + return name; + } + } else { + return name; + } + } + public IqPacket generateCreateAccountWithCaptcha(Account account, String id, Data data) { final IqPacket register = new IqPacket(IqPacket.TYPE.SET); register.setFrom(account.getJid().toBareJid()); diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index ef5ff854c..d80f44f47 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1255,7 +1255,7 @@ public class XmppConnection implements Runnable { } private String nextRandomId() { - return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32); + return new BigInteger(50, mXmppConnectionService.getRNG()).toString(36); } public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {