Create accurate filter for matching on from address (SMACK-71)

Smack contains two PacketFilters to filter on the from address.
FromContainsFilter simply does a substring match, which is problematic
as explained in SMACK-71. FromMatchesFilter partially fixes this
weakness, but it still uses String#startsWith to filter on bare
addresses. For example, when setup to match all JIDs with bare JID
"foo@example.co", it will still match "foo@example.com".

This commit changes FromMatchesFilter to test equality with the bare
from instead of startsWith with the full from.

Moreover, we convert all uses of FromContainsFilter to FromMatchesFilter
and remove FromContainsFilter. Additionally, the unused ToContainsFilter
(which as the same weaknesses) is removed, too.
This commit is contained in:
Lars Noschinski 2014-03-05 00:00:02 +01:00
parent 0e49b23687
commit 980047c4e1
7 changed files with 14 additions and 120 deletions

View File

@ -26,12 +26,12 @@ import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.FromMatchesFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.collections.ReferenceMap;
@ -313,7 +313,7 @@ public class ChatManager {
PacketCollector createPacketCollector(Chat chat) {
return connection.createPacketCollector(new AndFilter(new ThreadFilter(chat.getThreadID()),
new FromContainsFilter(chat.getParticipant())));
new FromMatchesFilter(chat.getParticipant())));
}
/**

View File

@ -1,51 +0,0 @@
/**
*
* Copyright 2003 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.filter;
import org.jivesoftware.smack.packet.Packet;
/**
* Filters for packets where the "from" field contains a specified value.
*
* @author Matt Tucker
*/
public class FromContainsFilter implements PacketFilter {
private String from;
/**
* Creates a "from" contains filter using the "from" field part.
*
* @param from the from field value the packet must contain.
*/
public FromContainsFilter(String from) {
if (from == null) {
throw new IllegalArgumentException("Parameter cannot be null.");
}
this.from = from.toLowerCase();
}
public boolean accept(Packet packet) {
if (packet.getFrom() == null) {
return false;
}
else {
return packet.getFrom().toLowerCase().indexOf(from) != -1;
}
}
}

View File

@ -53,17 +53,14 @@ public class FromMatchesFilter implements PacketFilter {
}
public boolean accept(Packet packet) {
if (packet.getFrom() == null) {
String from = packet.getFrom();
if (from == null) {
return false;
}
else if (matchBareJID) {
// Check if the bare JID of the sender of the packet matches the specified JID
return packet.getFrom().toLowerCase().startsWith(address);
}
else {
// Check if the full JID of the sender of the packet matches the specified JID
return address.equals(packet.getFrom().toLowerCase());
if (matchBareJID) {
from = StringUtils.parseBareAddress(from);
}
return address.equals(from.toLowerCase());
}
public String toString() {

View File

@ -1,52 +0,0 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.filter;
import org.jivesoftware.smack.packet.Packet;
/**
* Filters for packets where the "to" field contains a specified value. For example,
* the filter could be used to listen for all packets sent to a group chat nickname.
*
* @author Matt Tucker
*/
public class ToContainsFilter implements PacketFilter {
private String to;
/**
* Creates a "to" contains filter using the "to" field part.
*
* @param to the to field value the packet must contain.
*/
public ToContainsFilter(String to) {
if (to == null) {
throw new IllegalArgumentException("Parameter cannot be null.");
}
this.to = to.toLowerCase();
}
public boolean accept(Packet packet) {
if (packet.getTo() == null) {
return false;
}
else {
return packet.getTo().toLowerCase().indexOf(to) != -1;
}
}
}

View File

@ -22,7 +22,7 @@ import java.io.OutputStream;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.FromMatchesFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
@ -86,7 +86,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
*/
this.manager.ignoreBytestreamRequestOnce(streamID);
return new AndFilter(new FromContainsFilter(from), new IBBOpenSidFilter(streamID));
return new AndFilter(new FromMatchesFilter(from), new IBBOpenSidFilter(streamID));
}
public String[] getNamespaces() {

View File

@ -262,7 +262,7 @@ public class AgentSession {
presence.addExtension(new DefaultPacketExtension(AgentStatus.ELEMENT_NAME,
AgentStatus.NAMESPACE));
PacketCollector collector = this.connection.createPacketCollector(new AndFilter(new PacketTypeFilter(Presence.class), new FromContainsFilter(workgroupJID)));
PacketCollector collector = this.connection.createPacketCollector(new AndFilter(new PacketTypeFilter(Presence.class), new FromMatchesFilter(workgroupJID)));
connection.sendPacket(presence);
@ -359,7 +359,7 @@ public class AgentSession {
presence.addExtension(agentStatus);
presence.addExtension(new MetaData(this.metaData));
PacketCollector collector = this.connection.createPacketCollector(new AndFilter(new PacketTypeFilter(Presence.class), new FromContainsFilter(workgroupJID)));
PacketCollector collector = this.connection.createPacketCollector(new AndFilter(new PacketTypeFilter(Presence.class), new FromMatchesFilter(workgroupJID)));
this.connection.sendPacket(presence);
@ -402,7 +402,7 @@ public class AgentSession {
presence.addExtension(new MetaData(this.metaData));
PacketCollector collector = this.connection.createPacketCollector(new AndFilter(new PacketTypeFilter(Presence.class),
new FromContainsFilter(workgroupJID)));
new FromMatchesFilter(workgroupJID)));
this.connection.sendPacket(presence);

View File

@ -159,7 +159,7 @@ public class Workgroup {
Presence directedPresence = new Presence(Presence.Type.available);
directedPresence.setTo(workgroupJID);
PacketFilter typeFilter = new PacketTypeFilter(Presence.class);
PacketFilter fromFilter = new FromContainsFilter(workgroupJID);
PacketFilter fromFilter = new FromMatchesFilter(workgroupJID);
PacketCollector collector = connection.createPacketCollector(new AndFilter(fromFilter,
typeFilter));