From c3cc8eff283dc74c1e99c7d964ae53d77cf7c873 Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Fri, 30 Oct 2026 20:08:23 +0800 Subject: [PATCH] Verify authenticator codes against RFC 6238 test vectors --- lib/totp.ts | 12 ++++++++++++ pages/api/editor/login.ts | 13 ++----------- scripts/test-editor.cjs | 7 +++++++ 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 lib/totp.ts diff --git a/lib/totp.ts b/lib/totp.ts new file mode 100644 index 0000000..dd23edc --- /dev/null +++ b/lib/totp.ts @@ -0,0 +1,12 @@ +import { createHmac } from 'crypto'; + +export function totp(secret: string, step: number) { + let bits = ''; for (const c of secret.toUpperCase().replace(/=|\s/g, '')) { + const n = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(c); if (n < 0) throw new Error('Invalid TOTP secret'); + bits += n.toString(2).padStart(5, '0'); + } + const key = Buffer.from(bits.match(/.{8}/g)!.map(b => parseInt(b, 2))); + const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(step)); + const hash = createHmac('sha1', key).update(counter).digest(); const offset = hash[19] & 15; + return String((hash.readUInt32BE(offset) & 0x7fffffff) % 1000000).padStart(6, '0'); +} diff --git a/pages/api/editor/login.ts b/pages/api/editor/login.ts index 3fa0cf1..cfb6b89 100644 --- a/pages/api/editor/login.ts +++ b/pages/api/editor/login.ts @@ -1,18 +1,9 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { createHmac, randomBytes, timingSafeEqual } from 'crypto'; +import { randomBytes, timingSafeEqual } from 'crypto'; +import { totp } from '../../../lib/totp'; import { query } from '../../../lib/db'; import { COOKIE, signSession } from '../../../lib/editor-session'; -function totp(secret: string, step: number) { - let bits = ''; for (const c of secret.toUpperCase().replace(/=|\s/g, '')) { - const n = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(c); if (n < 0) throw new Error('Invalid TOTP secret'); - bits += n.toString(2).padStart(5, '0'); - } - const key = Buffer.from(bits.match(/.{8}/g)!.map(b => parseInt(b, 2))); - const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(step)); - const hash = createHmac('sha1', key).update(counter).digest(); const offset = hash[19] & 15; - return String((hash.readUInt32BE(offset) & 0x7fffffff) % 1000000).padStart(6, '0'); -} export default async function login(req: NextApiRequest, res: NextApiResponse) { res.setHeader('Cache-Control', 'no-store'); if (req.method !== 'POST') return res.status(405).end(); diff --git a/scripts/test-editor.cjs b/scripts/test-editor.cjs index 2141118..6f8b04f 100644 --- a/scripts/test-editor.cjs +++ b/scripts/test-editor.cjs @@ -20,5 +20,12 @@ vm.runInNewContext(ts.transpileModule(fs.readFileSync('lib/editor-session.ts','u assert.equal(await validSession(token),false); context.process.env.EDITOR_SESSION_SECRET = ''; assert.equal(await validSession(token),false); + const totpContext = {exports:{},require,Buffer}; + vm.runInNewContext(ts.transpileModule(fs.readFileSync('lib/totp.ts','utf8'), {compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2020}}).outputText, totpContext); + const rfcSecret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; + for (const [time,expected] of [[59,'287082'],[1111111109,'081804'],[1111111111,'050471'],[1234567890,'005924'],[2000000000,'279037'],[20000000000,'353130']]) { + assert.equal(totpContext.exports.totp(rfcSecret,Math.floor(time/30)),expected); + } + console.log('RFC 6238 TOTP vectors: passed'); console.log('Editor session validation: passed'); })().catch(error => { console.error(error); process.exitCode=1; });