Resolve XMPP host from JID domain and set TLS SNI
Make HUBOT_XMPP_HOST optional: when unset, pass the bare JID domain as the service so @xmpp/client resolves the host via DNS SRV (XEP-0368) and the XEP-0156 well-known file. HUBOT_XMPP_PORT now only applies together with HOST. xmpp.js does not set TLS SNI, and @xmpp/resolve connects to raw IPs from SRV/A lookups, so direct TLS failed certificate verification (ERR_TLS_CERT_ALTNAME_INVALID) and silently fell back to STARTTLS. Wrap the transport socketParameters to set servername to the JID domain (XEP-0368 §3 rule 6), mirroring xmppjs/xmpp.js#1128 and the sockethub workaround. Direct TLS now succeeds on the first try.
This commit is contained in:
5 files changed
+200
-9
No files matched your search
@@ -24,14 +24,26 @@ The adapter requires these environment variables:
|
||||
|
||||
Optional:
|
||||
|
||||
* `HUBOT_XMPP_HOST` – the host name to connect to if it differs from the domain
|
||||
in the username JID.
|
||||
* `HUBOT_XMPP_PORT` – the port to connect to (default `5222`).
|
||||
* `HUBOT_XMPP_HOST` – the host name to connect to. Only needed as an explicit
|
||||
override; by default the host is resolved from the JID domain.
|
||||
* `HUBOT_XMPP_PORT` – the port to connect to when `HUBOT_XMPP_HOST` is set
|
||||
(default `5222`). Ignored otherwise.
|
||||
* `HUBOT_XMPP_PM_ADD_PREFIX` – make commands work in private messages to the bot
|
||||
without the robot name/alias prefix.
|
||||
* `HUBOT_XMPP_KEEPALIVE_INTERVAL` – keep-alive (XMPP ping) interval in ms
|
||||
(default `30000`).
|
||||
|
||||
When `HUBOT_XMPP_HOST` is not set, the connection host is resolved from the JID
|
||||
domain using DNS SRV records (XEP-0368 / RFC 6120) and the XEP-0156 well-known
|
||||
file, via `@xmpp/resolve`. This means the XMPP host does not need to match the
|
||||
JID domain and does not need to be configured manually. The most secure
|
||||
endpoint is tried first (direct TLS before STARTTLS).
|
||||
|
||||
Direct TLS SNI is set to the JID domain (per XEP-0368 §3 rule 6), so the
|
||||
certificate is verified against the domain rather than the resolved IP address.
|
||||
This is a workaround for the upstream xmpp.js issue until
|
||||
[xmppjs/xmpp.js#1128](https://github.com/xmppjs/xmpp.js/pull/1128) is released.
|
||||
|
||||
Reconnection is handled by `@xmpp/client`'s built-in reconnect support.
|
||||
|
||||
## Installation
|
||||
|
||||
+22
-6
@@ -1,6 +1,8 @@
|
||||
import { Adapter, TextMessage, EnterMessage, LeaveMessage } from 'hubot'
|
||||
import { client, xml, jid } from '@xmpp/client'
|
||||
|
||||
import { setTlsServername } from './tlsServername.mjs'
|
||||
|
||||
const NS_MUC = 'http://jabber.org/protocol/muc'
|
||||
const NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
|
||||
const NS_NICK = 'http://jabber.org/protocol/nick'
|
||||
@@ -109,22 +111,36 @@ class XmppAdapter extends Adapter {
|
||||
}
|
||||
}
|
||||
|
||||
makeClient() {
|
||||
// Build the options passed to @xmpp/client.
|
||||
//
|
||||
// When HUBOT_XMPP_HOST is set, it is used verbatim as an explicit override
|
||||
// (with HUBOT_XMPP_PORT, default 5222). Otherwise the bare JID domain is
|
||||
// passed as the service, which makes @xmpp/client resolve the host via DNS
|
||||
// SRV records (XEP-0368) and the XEP-0156 well-known file.
|
||||
buildClientOptions() {
|
||||
const address = jid(this.config.username)
|
||||
const host = this.config.host || address.domain
|
||||
const port = this.config.port || DEFAULT_PORT
|
||||
const service = this.config.host
|
||||
? `xmpp://${this.config.host}:${this.config.port || DEFAULT_PORT}`
|
||||
: address.domain
|
||||
|
||||
const options = {
|
||||
service: `xmpp://${host}:${port}`,
|
||||
return {
|
||||
service,
|
||||
domain: address.domain,
|
||||
username: address.local,
|
||||
password: this.config.password,
|
||||
resource: address.resource || this.robot.name
|
||||
}
|
||||
}
|
||||
|
||||
makeClient() {
|
||||
const options = this.buildClientOptions()
|
||||
|
||||
this.robot.logger.info(`Connecting to XMPP service ${options.service} as ${this.config.username}`)
|
||||
|
||||
return client(options)
|
||||
const xmpp = client(options)
|
||||
setTlsServername(xmpp, options.domain)
|
||||
|
||||
return xmpp
|
||||
}
|
||||
|
||||
configClient() {
|
||||
|
||||
@@ -656,6 +656,44 @@ describe('XmppAdapter', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#buildClientOptions()', () => {
|
||||
it('uses the JID domain as the service when no host is configured', () => {
|
||||
const adapter = createAdapter({ config: { host: undefined, port: undefined } })
|
||||
|
||||
const options = adapter.buildClientOptions()
|
||||
|
||||
assert.equal(options.service, 'example.com')
|
||||
assert.equal(options.domain, 'example.com')
|
||||
assert.equal(options.username, 'bot')
|
||||
assert.equal(options.password, 'secret')
|
||||
assert.equal(options.resource, 'hubot')
|
||||
})
|
||||
|
||||
it('ignores the port when no host is configured', () => {
|
||||
const adapter = createAdapter({ config: { host: undefined, port: '5223' } })
|
||||
|
||||
assert.equal(adapter.buildClientOptions().service, 'example.com')
|
||||
})
|
||||
|
||||
it('builds an explicit service URI when a host is configured', () => {
|
||||
const adapter = createAdapter({ config: { host: 'xmpp.example.com', port: undefined } })
|
||||
|
||||
assert.equal(adapter.buildClientOptions().service, 'xmpp://xmpp.example.com:5222')
|
||||
})
|
||||
|
||||
it('uses a custom port together with the host', () => {
|
||||
const adapter = createAdapter({ config: { host: 'xmpp.example.com', port: '5223' } })
|
||||
|
||||
assert.equal(adapter.buildClientOptions().service, 'xmpp://xmpp.example.com:5223')
|
||||
})
|
||||
|
||||
it('falls back to the robot name when the JID has no resource', () => {
|
||||
const adapter = createAdapter({ config: { username: 'bot@example.com' } })
|
||||
|
||||
assert.equal(adapter.buildClientOptions().resource, 'bot')
|
||||
})
|
||||
})
|
||||
|
||||
describe('entrypoint', () => {
|
||||
afterEach(() => {
|
||||
delete process.env.HUBOT_XMPP_USERNAME
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { isIP } from 'node:net'
|
||||
|
||||
const ORIGINAL = Symbol('socketParameters')
|
||||
const SERVERNAME = Symbol('servername')
|
||||
|
||||
// xmpp.js does not set TLS SNI, and @xmpp/resolve hands the transport
|
||||
// IP-based URIs (so it can cycle through every resolved address), so direct
|
||||
// TLS (xmpps://) validates the server certificate against the bare IP and
|
||||
// fails with ERR_TLS_CERT_ALTNAME_INVALID, silently falling back to STARTTLS.
|
||||
//
|
||||
// Both connect paths (@xmpp/resolve's fallbackConnect and
|
||||
// @xmpp/client-core's Client#socketParameters) call
|
||||
// Transport.prototype.socketParameters(uri), so wrapping the transport
|
||||
// prototypes covers them. The transport classes are shared module-level
|
||||
// prototypes, so the wrapper is installed once per prototype and the active
|
||||
// domain is kept in prototype state, refreshed on every call.
|
||||
//
|
||||
// XEP-0368 §3 (Requirements), rule 6 requires the SNI to be the JID's domain.
|
||||
// Remove once xmppjs/xmpp.js#1128 lands upstream and @xmpp/* is bumped.
|
||||
export function setTlsServername(client, domain) {
|
||||
// SNI must be a hostname; tls.connect rejects IP literals.
|
||||
const servername = isIP(domain) ? undefined : domain
|
||||
|
||||
for (const transport of client.transports ?? []) {
|
||||
const proto = transport.prototype
|
||||
|
||||
if (!proto[ORIGINAL]) {
|
||||
const original = proto.socketParameters
|
||||
proto[ORIGINAL] = original
|
||||
proto.socketParameters = function (service) {
|
||||
const params = original.call(this, service)
|
||||
if (params && typeof params === 'object' && proto[SERVERNAME] !== undefined) {
|
||||
params.servername = proto[SERVERNAME]
|
||||
}
|
||||
return params
|
||||
}
|
||||
}
|
||||
|
||||
proto[SERVERNAME] = servername
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { setTlsServername } from './tlsServername.mjs'
|
||||
|
||||
function makeClient() {
|
||||
class Tls {
|
||||
socketParameters(service) {
|
||||
return service.startsWith('xmpps:') ? { host: '1.2.3.4', port: 5223 } : undefined
|
||||
}
|
||||
}
|
||||
|
||||
class Ws {
|
||||
socketParameters(service) {
|
||||
return service.startsWith('ws') ? service : undefined
|
||||
}
|
||||
}
|
||||
|
||||
return { client: { transports: [Tls, Ws] }, Tls, Ws }
|
||||
}
|
||||
|
||||
describe('setTlsServername()', () => {
|
||||
it('sets servername to the JID domain on object params', () => {
|
||||
const { client, Tls } = makeClient()
|
||||
|
||||
setTlsServername(client, 'example.org')
|
||||
|
||||
assert.deepEqual(Tls.prototype.socketParameters('xmpps://1.2.3.4:5223'), {
|
||||
host: '1.2.3.4',
|
||||
port: 5223,
|
||||
servername: 'example.org'
|
||||
})
|
||||
assert.equal(Tls.prototype.socketParameters('xmpp://1.2.3.4'), undefined)
|
||||
})
|
||||
|
||||
it('leaves string params (websocket) untouched', () => {
|
||||
const { client, Ws } = makeClient()
|
||||
|
||||
setTlsServername(client, 'example.org')
|
||||
|
||||
assert.equal(Ws.prototype.socketParameters('wss://example.org/ws'), 'wss://example.org/ws')
|
||||
})
|
||||
|
||||
it('is idempotent', () => {
|
||||
const { client, Tls } = makeClient()
|
||||
|
||||
setTlsServername(client, 'example.org')
|
||||
setTlsServername(client, 'example.org')
|
||||
|
||||
assert.deepEqual(Tls.prototype.socketParameters('xmpps://1.2.3.4:5223'), {
|
||||
host: '1.2.3.4',
|
||||
port: 5223,
|
||||
servername: 'example.org'
|
||||
})
|
||||
})
|
||||
|
||||
it('updates the servername when a later client uses a different domain', () => {
|
||||
const { client, Tls } = makeClient()
|
||||
|
||||
setTlsServername(client, 'first.example.org')
|
||||
setTlsServername(client, 'second.example.org')
|
||||
|
||||
assert.deepEqual(Tls.prototype.socketParameters('xmpps://1.2.3.4:5223'), {
|
||||
host: '1.2.3.4',
|
||||
port: 5223,
|
||||
servername: 'second.example.org'
|
||||
})
|
||||
})
|
||||
|
||||
it('sends no servername for IP-literal domains', () => {
|
||||
const { client, Tls } = makeClient()
|
||||
|
||||
setTlsServername(client, '10.0.0.5')
|
||||
|
||||
assert.deepEqual(Tls.prototype.socketParameters('xmpps://1.2.3.4:5223'), {
|
||||
host: '1.2.3.4',
|
||||
port: 5223
|
||||
})
|
||||
})
|
||||
|
||||
it('tolerates a client without transports', () => {
|
||||
assert.doesNotThrow(() => setTlsServername({}, 'example.org'))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user