Skip to content

Commit 2c45126

Browse files
committed
fix: reject on invalid vhost
1 parent 86303e9 commit 2c45126

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,4 +841,4 @@ const extractPublisherId = (extendedPublisherId: string) => {
841841
return parseInt(extendedPublisherId.split("@").shift() ?? "0")
842842
}
843843

844-
const getVhostOrDefault = (vhost: string) => (vhost === "" ? "/" : vhost)
844+
const getVhostOrDefault = (vhost: string) => vhost ?? "/"

src/connection.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export class Connection {
152152
mechanism: this.params.mechanism ?? "PLAIN",
153153
})
154154
const { heartbeat } = await this.tune(this.params.heartbeat ?? 0)
155-
await this.open({ virtualHost: this.params.vhost })
155+
const connectionOpened = await this.open({ virtualHost: this.params.vhost })
156+
if (!connectionOpened.ok) return rej(connectionOpened.error)
156157
if (!this.heartbeat.started) this.heartbeat.start(heartbeat)
157158
await this.exchangeCommandVersions()
158159
this.setupCompleted = true
@@ -461,12 +462,25 @@ export class Connection {
461462

462463
private async open(params: { virtualHost: string }) {
463464
this.logger.debug(`Open ...`)
465+
if (this.virtualHostIsNotValid(params.virtualHost)) {
466+
const errorMessage = `[ERROR]: VirtualHost '${params.virtualHost}' is not valid`
467+
this.logger.error(errorMessage)
468+
return { ok: false, error: new Error(errorMessage) }
469+
}
464470
const res = await this.sendAndWait<OpenResponse>(new OpenRequest(params))
465471
this.logger.debug(`Open response: ${res.ok} - '${inspect(res.properties)}'`)
466472
const advertisedHost = res.properties["advertised_host"] ?? ""
467473
const advertisedPort = parseInt(res.properties["advertised_port"] ?? "5552")
468474
this.serverEndpoint = { host: advertisedHost, port: advertisedPort }
469-
return res
475+
return { ok: true, response: res }
476+
}
477+
478+
private virtualHostIsNotValid(virtualHost: string) {
479+
if (!virtualHost || virtualHost.split("/").length !== 2) {
480+
return true
481+
}
482+
483+
return false
470484
}
471485

472486
private async tune(heartbeatInterval: number): Promise<{ heartbeat: number }> {

test/e2e/connect.test.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai"
22
import { Client, connect } from "../../src"
33
import { createClient } from "../support/fake_data"
44
import { Rabbit } from "../support/rabbit"
5-
import { eventually, username, password, getTestNodesFromEnv } from "../support/util"
5+
import { eventually, username, password, getTestNodesFromEnv, expectToThrowAsync } from "../support/util"
66
import { Version } from "../../src/versions"
77
import { randomUUID } from "node:crypto"
88
import { readFile } from "node:fs/promises"
@@ -46,21 +46,24 @@ describe("connect", () => {
4646
}, 5000)
4747
}).timeout(10000)
4848

49-
it("with default vhost as '/' ", async () => {
49+
it("throw exception if vhost is not valid", async () => {
5050
const [firstNode] = getTestNodesFromEnv()
51-
client = await connect({
52-
hostname: firstNode.host,
53-
port: firstNode.port,
54-
username,
55-
password,
56-
vhost: "",
57-
frameMax: 0,
58-
heartbeat: 0,
59-
})
6051

61-
await eventually(async () => {
62-
expect(await rabbit.getConnections()).lengthOf(1)
63-
}, 5000)
52+
await expectToThrowAsync(
53+
async () => {
54+
client = await connect({
55+
hostname: firstNode.host,
56+
port: firstNode.port,
57+
username,
58+
password,
59+
vhost: "",
60+
frameMax: 0,
61+
heartbeat: 0,
62+
})
63+
},
64+
Error,
65+
`[ERROR]: VirtualHost '' is not valid`
66+
)
6467
}).timeout(10000)
6568

6669
it("using EXTERNAL auth", async () => {

0 commit comments

Comments
 (0)