Skip to content

Commit

Permalink
Use getChildElements to fix bugs in SASL and XMPPError (#966)
Browse files Browse the repository at this point in the history
Fixes #952
  • Loading branch information
sonnyp authored Dec 1, 2022
1 parent 1f2ecb8 commit 7f8bfc4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
"@babel/plugin-transform-react-jsx",
{
pragma: "xml",
throwIfNamespace: false,
},
],
[
Expand Down
2 changes: 1 addition & 1 deletion packages/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class XMPPError extends Error {
}

static fromElement(element) {
const [condition, second, third] = element.children;
const [condition, second, third] = element.getChildElements();
let text;
let application;

Expand Down
55 changes: 55 additions & 0 deletions packages/error/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

const test = require("ava");
const XMPPError = require(".");
// eslint-disable-next-line node/no-extraneous-require
const parse = require("@xmpp/xml/lib/parse.js");

test("fromElement", (t) => {
const application_element = (
<escape-your-data xmlns="http://example.org/ns" />
);

const nonza = (
<stream:error>
<some-condition xmlns="urn:ietf:params:xml:ns:xmpp-streams" />
<text xmlns="urn:ietf:params:xml:ns:xmpp-streams" xml:lang="langcode">
foo
</text>
{application_element}
</stream:error>
);

const error = XMPPError.fromElement(nonza);

t.is(error instanceof Error, true);
t.is(error.name, "XMPPError");
t.is(error.condition, "some-condition");
t.is(error.text, "foo");
t.is(error.application, application_element);
});

test("fromElement - whitespaces", (t) => {
const nonza = parse(
`
<stream:error>
<some-condition xmlns="urn:ietf:params:xml:ns:xmpp-streams" />
<text xmlns="urn:ietf:params:xml:ns:xmpp-streams" xml:lang="langcode">
foo
</text>
<escape-your-data xmlns='http://example.org/ns'/>
</stream:error>
`.trim(),
);

const error = XMPPError.fromElement(nonza);

t.is(error instanceof Error, true);
t.is(error.name, "XMPPError");
t.is(error.condition, "some-condition");
t.is(error.text, "\n foo\n ");
t.is(
error.application.toString(),
`<escape-your-data xmlns="http://example.org/ns"/>`,
);
});
5 changes: 4 additions & 1 deletion packages/sasl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const SASLFactory = require("saslmechanisms");
const NS = "urn:ietf:params:xml:ns:xmpp-sasl";

function getMechanismNames(features) {
return features.getChild("mechanisms", NS).children.map((el) => el.text());
return features
.getChild("mechanisms", NS)
.getChildElements()
.map((el) => el.text());
}

async function authenticate(SASL, entity, mechname, credentials) {
Expand Down
22 changes: 22 additions & 0 deletions packages/sasl/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const test = require("ava");
const { mockClient, promise } = require("@xmpp/test");
const parse = require("@xmpp/xml/lib/parse.js");

const username = "foo";
const password = "bar";
Expand Down Expand Up @@ -150,3 +151,24 @@ test("use ANONYMOUS if username and password are not provided", async (t) => {
const result = await promise(entity, "send");
t.deepEqual(result.attrs.mechanism, "ANONYMOUS");
});

test("with whitespaces", async (t) => {
const { entity } = mockClient();

entity.mockInput(
parse(
`
<features xmlns="http://etherx.jabber.org/streams">
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
<mechanism>ANONYMOUS</mechanism>
<mechanism>PLAIN</mechanism>
<mechanism>SCRAM-SHA-1</mechanism>
</mechanisms>
</features>
`.trim(),
),
);

const result = await promise(entity, "send");
t.deepEqual(result.attrs.mechanism, "ANONYMOUS");
});

0 comments on commit 7f8bfc4

Please sign in to comment.