deny files with unknown file types

This commit is contained in:
Daniel Gultsch 2014-04-23 21:19:56 +02:00
parent 9a8e55acd1
commit 18c183a767
4 changed files with 76 additions and 21 deletions

View file

@ -72,6 +72,7 @@ public class FileBackend {
public JingleFile copyImageToPrivateStorage(Message message, Uri image) { public JingleFile copyImageToPrivateStorage(Message message, Uri image) {
try { try {
Log.d("xmppService","copying file: "+image.toString()+ " to internal storage");
InputStream is = context.getContentResolver() InputStream is = context.getContentResolver()
.openInputStream(image); .openInputStream(image);
JingleFile file = getJingleFile(message); JingleFile file = getJingleFile(message);

View file

@ -423,6 +423,7 @@ public class XmppConnectionService extends Service {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} }
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
databaseBackend.createMessage(message);
message.setStatus(Message.STATUS_OFFERED); message.setStatus(Message.STATUS_OFFERED);
if (convChangedListener!=null) { if (convChangedListener!=null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.xmpp.jingle; package eu.siacs.conversations.xmpp.jingle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -21,6 +22,9 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JingleConnection { public class JingleConnection {
private final String[] extensions = {"webp","jpeg","jpg","png"};
private final String[] cryptoExtensions = {"pgp","gpg"};
private JingleConnectionManager mJingleConnectionManager; private JingleConnectionManager mJingleConnectionManager;
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
@ -74,9 +78,9 @@ public class JingleConnection {
if (acceptedAutomatically) { if (acceptedAutomatically) {
message.markUnread(); message.markUnread();
} }
mXmppConnectionService.databaseBackend.createMessage(message);
mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVED); mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVED);
} }
mXmppConnectionService.databaseBackend.createMessage(message);
Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum()); Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum());
} }
}; };
@ -123,13 +127,13 @@ public class JingleConnection {
Reason reason = packet.getReason(); Reason reason = packet.getReason();
if (reason!=null) { if (reason!=null) {
if (reason.hasChild("cancel")) { if (reason.hasChild("cancel")) {
this.cancel(); this.receiveCancel();
} else if (reason.hasChild("success")) { } else if (reason.hasChild("success")) {
this.finish(); this.receiveSuccess();
} }
} else { } else {
Log.d("xmppService","remote terminated for no reason"); Log.d("xmppService","remote terminated for no reason");
this.cancel(); this.receiveCancel();
} }
} else if (packet.isAction("session-accept")) { } else if (packet.isAction("session-accept")) {
receiveAccept(packet); receiveAccept(packet);
@ -210,22 +214,42 @@ public class JingleConnection {
if (fileOffer!=null) { if (fileOffer!=null) {
this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message); this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message);
Element fileSize = fileOffer.findChild("size"); Element fileSize = fileOffer.findChild("size");
Element fileName = fileOffer.findChild("name"); Element fileNameElement = fileOffer.findChild("name");
this.file.setExpectedSize(Long.parseLong(fileSize.getContent())); if (fileNameElement!=null) {
conversation.getMessages().add(message); boolean supportedFile = false;
if (this.file.getExpectedSize()<=this.mJingleConnectionManager.getAutoAcceptFileSize()) { String[] filename = fileNameElement.getContent().toLowerCase().split("\\.");
Log.d("xmppService","auto accepting file from "+packet.getFrom()); if (Arrays.asList(this.extensions).contains(filename[filename.length - 1])) {
this.acceptedAutomatically = true; supportedFile = true;
this.sendAccept(); } else if (Arrays.asList(this.cryptoExtensions).contains(filename[filename.length - 1])) {
} else { if (filename.length == 3) {
message.markUnread(); if (Arrays.asList(this.extensions).contains(filename[filename.length -2])) {
Log.d("xmppService","not auto accepting new file offer with size: "+this.file.getExpectedSize()+" allowed size:"+this.mJingleConnectionManager.getAutoAcceptFileSize()); supportedFile = true;
if (this.mXmppConnectionService.convChangedListener!=null) { this.message.setEncryption(Message.ENCRYPTION_PGP);
this.mXmppConnectionService.convChangedListener.onConversationListChanged(); }
}
} }
if (supportedFile) {
this.file.setExpectedSize(Long.parseLong(fileSize.getContent()));
conversation.getMessages().add(message);
if (this.file.getExpectedSize()<=this.mJingleConnectionManager.getAutoAcceptFileSize()) {
Log.d("xmppService","auto accepting file from "+packet.getFrom());
this.acceptedAutomatically = true;
this.sendAccept();
} else {
message.markUnread();
Log.d("xmppService","not auto accepting new file offer with size: "+this.file.getExpectedSize()+" allowed size:"+this.mJingleConnectionManager.getAutoAcceptFileSize());
if (this.mXmppConnectionService.convChangedListener!=null) {
this.mXmppConnectionService.convChangedListener.onConversationListChanged();
}
}
} else {
this.sendCancel();
}
} else {
this.sendCancel();
} }
} else { } else {
Log.d("xmppService","no file offer was attached. aborting"); this.sendCancel();
} }
} }
@ -331,7 +355,19 @@ public class JingleConnection {
Content content = packet.getJingleContent(); Content content = packet.getJingleContent();
if (content.hasSocks5Transport()) { if (content.hasSocks5Transport()) {
if (content.socks5transport().hasChild("activated")) { if (content.socks5transport().hasChild("activated")) {
onProxyActivated.success(); if ((this.transport!=null)&&(this.transport instanceof JingleSocks5Transport)) {
onProxyActivated.success();
} else {
String cid = content.socks5transport().findChild("activated").getAttribute("cid");
Log.d("xmppService","received proxy activated ("+cid+")prior to choosing our own transport");
JingleSocks5Transport connection = this.connections.get(cid);
if (connection!=null) {
connection.setActivated(true);
} else {
Log.d("xmppService","activated connection not found");
this.sendCancel();
}
}
} else if (content.socks5transport().hasChild("activated")) { } else if (content.socks5transport().hasChild("activated")) {
onProxyActivated.failed(); onProxyActivated.failed();
} else if (content.socks5transport().hasChild("candidate-error")) { } else if (content.socks5transport().hasChild("candidate-error")) {
@ -375,7 +411,7 @@ public class JingleConnection {
} }
} else { } else {
this.status = STATUS_TRANSMITTING; this.status = STATUS_TRANSMITTING;
if (connection.isProxy()) { if (connection.needsActivation()) {
if (connection.getCandidate().isOurs()) { if (connection.getCandidate().isOurs()) {
Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy and needs activation"); Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy and needs activation");
IqPacket activation = new IqPacket(IqPacket.TYPE_SET); IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
@ -507,18 +543,26 @@ public class JingleConnection {
} }
} }
private void finish() { private void receiveSuccess() {
this.status = STATUS_FINISHED; this.status = STATUS_FINISHED;
this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND); this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND);
this.disconnect(); this.disconnect();
} }
public void cancel() { private void receiveCancel() {
this.disconnect(); this.disconnect();
this.status = STATUS_CANCELED; this.status = STATUS_CANCELED;
this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_REJECTED); this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_REJECTED);
} }
private void sendCancel() {
JinglePacket packet = bootstrapPacket("session-terminate");
Reason reason = new Reason();
reason.addChild("cancel");
packet.setReason(reason);
this.sendJinglePacket(packet);
}
private void connectNextCandidate() { private void connectNextCandidate() {
for(JingleCandidate candidate : this.candidates) { for(JingleCandidate candidate : this.candidates) {
if ((!connections.containsKey(candidate.getCid())&&(!candidate.isOurs()))) { if ((!connections.containsKey(candidate.getCid())&&(!candidate.isOurs()))) {

View file

@ -24,6 +24,7 @@ public class JingleSocks5Transport extends JingleTransport {
private OutputStream outputStream; private OutputStream outputStream;
private InputStream inputStream; private InputStream inputStream;
private boolean isEstablished = false; private boolean isEstablished = false;
private boolean activated = false;
protected Socket socket; protected Socket socket;
public JingleSocks5Transport(JingleConnection jingleConnection, JingleCandidate candidate) { public JingleSocks5Transport(JingleConnection jingleConnection, JingleCandidate candidate) {
@ -185,6 +186,10 @@ public class JingleSocks5Transport extends JingleTransport {
return this.candidate.getType() == JingleCandidate.TYPE_PROXY; return this.candidate.getType() == JingleCandidate.TYPE_PROXY;
} }
public boolean needsActivation() {
return (this.isProxy() && !this.activated);
}
public void disconnect() { public void disconnect() {
if (this.socket!=null) { if (this.socket!=null) {
try { try {
@ -203,4 +208,8 @@ public class JingleSocks5Transport extends JingleTransport {
public JingleCandidate getCandidate() { public JingleCandidate getCandidate() {
return this.candidate; return this.candidate;
} }
public void setActivated(boolean activated) {
this.activated = activated;
}
} }