Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: reject incomplete Base32 groups in TOTP keys

Afloat65f33fccb3ef
diff --git a/server/totp.test.ts b/server/totp.test.ts
index 3fd7fc434c..b2b707aaa9 100644
--- a/server/totp.test.ts
+++ b/server/totp.test.ts
@@ -69,6 +69,25 @@ describe("2FA setup keys", () => {
expect(parseTotpKey(" jbsw y3dp ehpk 3pxp ")).toEqual({ secret: "JBSWY3DPEHPK3PXP", algorithm: "SHA1", digits: 6, period: 30 });
});
+ it.each(["A", "AAA", "AAAAAA"])("refuses a setup key ending in an incomplete base32 group (%s)", (suffix) => {
+ const secret = `JBSWY3DPEHPK3PXP${suffix}`;
+ expect(decodeBase32(secret)).toBeNull();
+ expect(parseTotpKey(secret)).toBeNull();
+ expect(parseTotpKey(`otpauth://totp/Example?secret=${secret}`)).toBeNull();
+ });
+
+ it.each([
+ ["MY", "f"],
+ ["MZXQ", "fo"],
+ ["MZXW6", "foo"],
+ ["MZXW6YQ", "foob"],
+ ["MZXW6YTB", "fooba"],
+ ["MZXW6YTBOI", "foobar"],
+ ])("decodes the complete base32 encoding %s with and without padding", (encoded, decoded) => {
+ expect(decodeBase32(encoded)?.toString("ascii")).toBe(decoded);
+ expect(decodeBase32(encoded.padEnd(Math.ceil(encoded.length / 8) * 8, "="))?.toString("ascii")).toBe(decoded);
+ });
+
it("reads an otpauth:// address with its parameters", () => {
expect(parseTotpKey("otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example&algorithm=SHA256&digits=8&period=60"))
.toEqual({ secret: "JBSWY3DPEHPK3PXP", algorithm: "SHA256", digits: 8, period: 60 });
diff --git a/server/totp.ts b/server/totp.ts
index b8a4ccf406..4352d45f9e 100644
--- a/server/totp.ts
+++ b/server/totp.ts
@@ -27,6 +27,7 @@ export function decodeBase32(text: string): Buffer | null {
bytes.push((buffer >> bits) & 0xff);
}
}
+ if (bits >= 5) return null;
return Buffer.from(bytes);
}