-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: Do not allow PLAIN on insecure connection
Also add connection.isSecure() method Fixes #1040
- Loading branch information
Showing
11 changed files
with
107 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
const ANONYMOUS = "ANONYMOUS"; | ||
const PLAIN = "PLAIN"; | ||
|
||
export default function createOnAuthenticate(credentials, userAgent) { | ||
return async function onAuthenticate(authenticate, mechanisms, fast) { | ||
return async function onAuthenticate(authenticate, mechanisms, fast, entity) { | ||
if (typeof credentials === "function") { | ||
await credentials(authenticate, mechanisms, fast); | ||
return; | ||
} | ||
|
||
if ( | ||
!credentials?.username && | ||
!credentials?.password && | ||
mechanisms.includes(ANONYMOUS) | ||
) { | ||
await authenticate(credentials, ANONYMOUS, userAgent); | ||
return; | ||
} | ||
credentials.token ??= await fast?.fetch(); | ||
|
||
credentials.token = await fast?.fetch?.(); | ||
|
||
await authenticate(credentials, mechanisms[0], userAgent); | ||
const mechanism = getMechanism({ mechanisms, entity, credentials }); | ||
await authenticate(credentials, mechanism, userAgent); | ||
}; | ||
} | ||
|
||
export function getMechanism({ mechanisms, entity, credentials }) { | ||
if ( | ||
!credentials?.username && | ||
!credentials?.password && | ||
!credentials?.token && | ||
mechanisms.includes(ANONYMOUS) | ||
) { | ||
return ANONYMOUS; | ||
} | ||
|
||
if (entity.isSecure()) return mechanisms[0]; | ||
|
||
return mechanisms.find((mechanism) => mechanism !== PLAIN); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { getMechanism } from "../lib/createOnAuthenticate.js"; | ||
|
||
it("returns ANONYMOUS if available and there are no credentials", () => { | ||
expect( | ||
getMechanism({ | ||
credentials: {}, | ||
mechanisms: ["PLAIN", "ANONYMOUS"], | ||
}), | ||
).toBe("ANONYMOUS"); | ||
}); | ||
|
||
it("returns the first mechanism if the connection is secure", () => { | ||
expect( | ||
getMechanism({ | ||
credentials: { username: "foo", password: "bar" }, | ||
mechanisms: ["PLAIN", "SCRAM-SHA-1"], | ||
entity: { isSecure: () => true }, | ||
}), | ||
).toBe("PLAIN"); | ||
}); | ||
|
||
it("does not return PLAIN if the connection is not secure", () => { | ||
expect( | ||
getMechanism({ | ||
credentials: { username: "foo", password: "bar" }, | ||
mechanisms: ["PLAIN", "SCRAM-SHA-1"], | ||
entity: { isSecure: () => false }, | ||
}), | ||
).toBe("SCRAM-SHA-1"); | ||
|
||
expect( | ||
getMechanism({ | ||
credentials: { username: "foo", password: "bar" }, | ||
mechanisms: ["PLAIN"], | ||
entity: { isSecure: () => false }, | ||
}), | ||
).toBe(undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Connection from "../index.js"; | ||
|
||
test("isSecure()", () => { | ||
const conn = new Connection(); | ||
|
||
conn.socket = null; | ||
expect(conn.isSecure()).toBe(false); | ||
|
||
conn.socket = { isSecure: () => false }; | ||
expect(conn.isSecure()).toBe(false); | ||
|
||
conn.socket = { isSecure: () => true }; | ||
expect(conn.isSecure()).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters