Skip to content

Commit 43d0932

Browse files
committed
Update roster push handling code
- Remove misleading comment and fix conditional logic - Check that there's only one <item> element. updates #1106
1 parent ec609a3 commit 43d0932

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/converse-roster.js

+32-26
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@
342342
/* Register a handler for roster IQ "set" stanzas, which update
343343
* roster contacts.
344344
*/
345-
_converse.connection.addHandler(
346-
_converse.roster.onRosterPush.bind(_converse.roster),
347-
Strophe.NS.ROSTER, 'iq', "set"
348-
);
345+
_converse.connection.addHandler((iq) => {
346+
_converse.roster.onRosterPush(iq);
347+
return true;
348+
}, Strophe.NS.ROSTER, 'iq', "set");
349349
},
350350

351351
registerRosterXHandler () {
@@ -506,30 +506,36 @@
506506

507507
onRosterPush (iq) {
508508
/* Handle roster updates from the XMPP server.
509-
* See: https://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push
510-
*
511-
* Parameters:
512-
* (XMLElement) IQ - The IQ stanza received from the XMPP server.
513-
*/
509+
* See: https://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push
510+
*
511+
* Parameters:
512+
* (XMLElement) IQ - The IQ stanza received from the XMPP server.
513+
*/
514514
const id = iq.getAttribute('id');
515515
const from = iq.getAttribute('from');
516-
if (from && from !== "" && Strophe.getBareJidFromJid(from) !== _converse.bare_jid) {
517-
// Receiving client MUST ignore stanza unless it has no from or from = user's bare JID.
518-
// XXX: Some naughty servers apparently send from a full
519-
// JID so we need to explicitly compare bare jids here.
520-
// https://github.com/jcbrand/converse.js/issues/493
521-
_converse.connection.send(
522-
$iq({type: 'error', id, from: _converse.connection.jid})
523-
.c('error', {'type': 'cancel'})
524-
.c('service-unavailable', {'xmlns': Strophe.NS.ROSTER })
525-
);
526-
return true;
516+
if (from && from !== _converse.connection.jid) {
517+
// https://tools.ietf.org/html/rfc6121#page-15
518+
//
519+
// A receiving client MUST ignore the stanza unless it has no 'from'
520+
// attribute (i.e., implicitly from the bare JID of the user's
521+
// account) or it has a 'from' attribute whose value matches the
522+
// user's bare JID <user@domainpart>.
523+
return;
527524
}
528525
_converse.connection.send($iq({type: 'result', id, from: _converse.connection.jid}));
529526
const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
530-
_.each(items, this.updateContact.bind(this));
527+
if (items.length > 1) {
528+
_converse.log(iq, Strophe.LogLevel.ERROR);
529+
throw new Error('Roster push query may not contain more than one "item" element.');
530+
}
531+
if (items.length === 0) {
532+
_converse.log(iq, Strophe.LogLevel.WARN);
533+
_converse.log('Received a roster push stanza without an "item" element.', Strophe.LogLevel.WARN);
534+
return;
535+
}
536+
this.updateContact(items.pop());
531537
_converse.emit('rosterPush', iq);
532-
return true;
538+
return;
533539
},
534540

535541
fetchFromServer () {
@@ -552,17 +558,17 @@
552558

553559
onReceivedFromServer (iq) {
554560
/* An IQ stanza containing the roster has been received from
555-
* the XMPP server.
556-
*/
561+
* the XMPP server.
562+
*/
557563
const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
558564
_.each(items, this.updateContact.bind(this));
559565
_converse.emit('roster', iq);
560566
},
561567

562568
updateContact (item) {
563569
/* Update or create RosterContact models based on items
564-
* received in the IQ from the server.
565-
*/
570+
* received in the IQ from the server.
571+
*/
566572
const jid = item.getAttribute('jid');
567573
if (this.isSelf(jid)) { return; }
568574

0 commit comments

Comments
 (0)