From de80458c9d849790def3015b953942d82fec35fd Mon Sep 17 00:00:00 2001 From: fabianzimber <31100894+fabianzimber@users.noreply.github.com> Date: Fri, 27 Nov 2026 17:32:49 +0000 Subject: [PATCH] fix: address SonarQube findings Bound the base64url padding and Basic auth header regexes, make the .env.deploy parser and comment stripper linear, mark fire-and-forget promises with void, prefer Number.parseInt and Number.NaN, drop a no-op ternary on the invitation page and install CI dependencies with --ignore-scripts. --- .github/workflows/ci.yml | 2 +- scripts/render-config.mjs | 6 +++--- src/analytics.ts | 4 ++-- src/client/app.ts | 18 +++++++++--------- src/client/mandates.ts | 4 ++-- src/index.ts | 2 +- src/mandates/verify.ts | 2 +- src/passes/rsa.ts | 4 ++-- src/sealed.ts | 2 +- src/ui/pages.ts | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e8c719..381a346 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,5 +28,5 @@ jobs: with: node-version: 26 cache: npm - - run: npm ci --prefer-offline --no-audit --no-fund + - run: npm ci --ignore-scripts --prefer-offline --no-audit --no-fund - run: npm run check diff --git a/scripts/render-config.mjs b/scripts/render-config.mjs index 4230404..b31b241 100644 --- a/scripts/render-config.mjs +++ b/scripts/render-config.mjs @@ -31,8 +31,8 @@ if (example) { Object.assign(env, process.env); if (existsSync(".env.deploy")) { for (const line of readFileSync(".env.deploy", "utf8").split("\n")) { - const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*?)\s*$/); - if (m && !(m[1] in process.env)) env[m[1]] = m[2].replace(/^["']|["']$/g, ""); + const m = /^\s*([A-Z0-9_]+)\s*=(.*)$/s.exec(line); + if (m && !(m[1] in process.env)) env[m[1]] = m[2].trim().replace(/^["']|["']$/g, ""); } } } @@ -53,7 +53,7 @@ const json = (v) => JSON.stringify(v ?? "").slice(1, -1); let config = readFileSync("wrangler.jsonc", "utf8"); for (const k of Object.keys(REQUIRED)) config = config.replaceAll(`<${k}>`, json(env[k])); config = config.replace(/<([A-Z0-9_]+)\?>/g, (_m, k) => json(env[k])); -const left = config.replace(/^\s*\/\/.*$/gm, "").match(/<[A-Z0-9_]+\??>/); +const left = config.replace(/^[ \t]*\/\/.*$/gm, "").match(/<[A-Z0-9_]+\??>/); if (left) { console.error(`Unfilled placeholder ${left[0]} in wrangler.jsonc`); process.exit(1); diff --git a/src/analytics.ts b/src/analytics.ts index 6aea007..eb48524 100644 --- a/src/analytics.ts +++ b/src/analytics.ts @@ -54,7 +54,7 @@ export function signInEvent(ctx: Ctx, now = Date.now()): [string, Props] | null const method = SIGN_IN_METHODS[ctx.path]; const user = ctx.context.newSession?.user; if (!method || !user) return null; - const created = user.createdAt ? new Date(user.createdAt).getTime() : NaN; + const created = user.createdAt ? new Date(user.createdAt).getTime() : Number.NaN; return ["signed_in", { method, first_sign_in: Number.isFinite(created) && now - created < FIRST_SIGN_IN_MS }]; } @@ -71,7 +71,7 @@ export const usageEvents = (env: Env, exec: Exec) => const c = ctx as unknown as Ctx; if (failed(c.context.returned)) return; const ev = Object.hasOwn(SIGN_IN_METHODS, c.path) ? signInEvent(c) : EVENTS[c.path](c); - if (ev) phCapture(env, exec, ev[0], ev[1]); + if (ev) void phCapture(env, exec, ev[0], ev[1]); } catch (e) { console.error("[stem] usage event failed", e); } diff --git a/src/client/app.ts b/src/client/app.ts index 5062695..77aa22d 100644 --- a/src/client/app.ts +++ b/src/client/app.ts @@ -199,7 +199,7 @@ function signIn() { ev.preventDefault(); const form = ev.currentTarget as HTMLFormElement; const btn = form.querySelector("button[type=submit]"); - busy(btn, async () => { + void busy(btn, async () => { fail(null); email = ($("email")?.value || "").trim().toLowerCase(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) { fail("That doesn't look like an email address."); return; } @@ -217,7 +217,7 @@ function signIn() { codeForm?.addEventListener("submit", (ev) => { ev.preventDefault(); const btn = codeForm.querySelector("button[type=submit]"); - busy(btn, async () => { + void busy(btn, async () => { fail(null); const otp = ($("otp")?.value || "").replace(/\D/g, ""); if (otp.length !== 6) { fail("The code has six digits."); return; } @@ -237,7 +237,7 @@ function signIn() { nameForm?.addEventListener("submit", (ev) => { ev.preventDefault(); const btn = nameForm.querySelector("button[type=submit]"); - busy(btn, async () => { + void busy(btn, async () => { fail(null); const name = ($("name")?.value || "").trim(); if (name.length < 1) { fail("Tell us a name, even a short one."); return; } @@ -300,7 +300,7 @@ async function account() { $("profile-form")?.addEventListener("submit", (ev) => { ev.preventDefault(); const btn = (ev.currentTarget as HTMLFormElement).querySelector("button"); - busy(btn, async () => { + void busy(btn, async () => { fail(null); const name = (nameInput?.value || "").trim().slice(0, 80); if (!name) { fail("Tell us a name, even a short one."); return; } @@ -324,7 +324,7 @@ async function account() { const r = await client.passkey.deletePasskey({ id: pk.id }); if (r.error) { fail(explain(r.error as ApiError)); return; } if (pk.credentialID) await forgetCredential(pk.credentialID); - loadPasskeys(); + void loadPasskeys(); }, })); } @@ -334,7 +334,7 @@ async function account() { fail(null); const res = await client.passkey.addPasskey({ name: deviceName() }); if (res?.error && !/cancel|abort|NotAllowed/i.test(res.error.message || "")) fail(explain(res.error as ApiError)); - loadPasskeys(); + void loadPasskeys(); })); async function loadSessions() { @@ -347,7 +347,7 @@ async function account() { const current = x.id === session.id; ul.append(li(`${browserOf(x.userAgent)}${current ? " (this one)" : ""}`, `Since ${fmt(x.createdAt)}${x.ipAddress ? ` ยท ${x.ipAddress}` : ""}`, current ? undefined : { label: "End", - run: async () => { const r = await client.revokeSession({ token: x.token }); if (r.error) fail(explain(r.error as ApiError)); else loadSessions(); }, + run: async () => { const r = await client.revokeSession({ token: x.token }); if (r.error) fail(explain(r.error as ApiError)); else void loadSessions(); }, })); } } @@ -383,14 +383,14 @@ async function account() { run: async () => { if (!confirm(`Remove ${a.name}'s access? It can't use your account anymore until you sign in to it again.`)) return; const r = await client.$fetch("/stem/apps/remove", { method: "POST", body: { client_id: a.client_id } }); - if (r.error) fail(explain(r.error as ApiError)); else loadApps(); + if (r.error) fail(explain(r.error as ApiError)); else void loadApps(); }, })); } $("revoke-others")?.addEventListener("click", async () => { const r = await client.revokeOtherSessions(); - if (r.error) fail(explain(r.error as ApiError)); else loadSessions(); + if (r.error) fail(explain(r.error as ApiError)); else void loadSessions(); }); $("sign-out")?.addEventListener("click", async () => { await client.signOut(); location.assign("/"); }); $("delete-account")?.addEventListener("click", async () => { diff --git a/src/client/mandates.ts b/src/client/mandates.ts index 7817880..f1ced4c 100644 --- a/src/client/mandates.ts +++ b/src/client/mandates.ts @@ -21,7 +21,7 @@ async function challengeOf(core: unknown) { const d = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(canon(core)))); let s = ""; for (const x of d) s += String.fromCharCode(x); - return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); + return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/={1,2}$/, ""); } export function approveMandate(h: Helpers) { @@ -68,7 +68,7 @@ export async function loadAgents(h: Helpers) { run: async () => { if (!confirm(`Stop "${a.purpose}"? It and every helper it started lose access on their next request.`)) return; const r = await h.$fetch("/mandates/revoke", { method: "POST", body: { id: a.id } }); - if (r.error) h.fail(r.error.message || "That didn't work."); else loadAgents(h); + if (r.error) h.fail(r.error.message || "That didn't work."); else void loadAgents(h); }, } : undefined); const more = document.createElement("button"); diff --git a/src/index.ts b/src/index.ts index ca0ce23..75332d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -245,7 +245,7 @@ const routeOf = (p: string) => p.replace(/\/[0-9a-f-]{36}(?=\/|$)/gi, "/:id").re app.notFound((c) => html(messagePage(ui(c.env), "Not here", "This page doesn't exist. Maybe the link is old or has a typo."), 404)); app.onError((e, c) => { console.error("[stem] unhandled", e); - phException(c.env, c.executionCtx as never, e, { path: routeOf(c.req.path) }); + void phException(c.env, c.executionCtx as never, e, { path: routeOf(c.req.path) }); return c.req.path.startsWith("/api/") ? c.json({ error: "server_error" }, 500) : html(messagePage(ui(c.env), "Something broke", "That's on us. Try again in a moment.", "error"), 500); }); diff --git a/src/mandates/verify.ts b/src/mandates/verify.ts index ec3cbc5..5288ce2 100644 --- a/src/mandates/verify.ts +++ b/src/mandates/verify.ts @@ -78,7 +78,7 @@ const te = new TextEncoder(); export const b64u = (b: Uint8Array) => { let s = ""; for (const x of b) s += String.fromCharCode(x); - return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); + return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/={1,2}$/, ""); }; export const unb64u = (s: string) => Uint8Array.from(atob(s.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0)); export const sha256 = async (d: Uint8Array | string) => new Uint8Array(await crypto.subtle.digest("SHA-256", typeof d === "string" ? te.encode(d) : d)); diff --git a/src/passes/rsa.ts b/src/passes/rsa.ts index 585317c..852647b 100644 --- a/src/passes/rsa.ts +++ b/src/passes/rsa.ts @@ -5,7 +5,7 @@ export const b64u = (b: Uint8Array) => { let s = ""; for (let i = 0; i < b.length; i += 0x8000) s += String.fromCharCode(...b.subarray(i, i + 0x8000)); - return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); + return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/={1,2}$/, ""); }; export const unb64u = (s: string) => { if (!/^[A-Za-z0-9_-]*$/.test(s)) throw new Error("not base64url"); @@ -21,7 +21,7 @@ export const intToBytes = (n: bigint, len: number) => { const hex = n.toString(16).padStart(len * 2, "0"); if (hex.length > len * 2) throw new Error("integer too large"); const out = new Uint8Array(len); - for (let i = 0; i < len; i++) out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16); + for (let i = 0; i < len; i++) out[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16); return out; }; export function modPow(base: bigint, exp: bigint, mod: bigint): bigint { diff --git a/src/sealed.ts b/src/sealed.ts index 0c591a4..4c55798 100644 --- a/src/sealed.ts +++ b/src/sealed.ts @@ -90,7 +90,7 @@ function clientIdOf(query: unknown, body: unknown, headers: Headers | undefined) const b = body as { client_id?: unknown } | undefined; if (typeof q?.client_id === "string" && q.client_id) return q.client_id; if (typeof b?.client_id === "string" && b.client_id) return b.client_id; - const basic = /^Basic\s+(.+)$/i.exec(headers?.get("authorization") || ""); + const basic = /^Basic\s+(\S.*)$/i.exec(headers?.get("authorization") || ""); if (!basic) return null; try { const raw = atob(basic[1].trim()); diff --git a/src/ui/pages.ts b/src/ui/pages.ts index e79e3c1..f7a5f9e 100644 --- a/src/ui/pages.ts +++ b/src/ui/pages.ts @@ -197,7 +197,7 @@ export function invitePage(ui: Ui, inv: { id: string; orgName: string; email: st
invitation

Join ${esc(inv.orgName)}.

-

${inv.inviter ? `${esc(inv.inviter)} invited` : "You've been invited with"} ${esc(inv.email)}${inv.inviter ? "" : ""}.

+

${inv.inviter ? `${esc(inv.inviter)} invited` : "You've been invited with"} ${esc(inv.email)}.

Sign in with that address to accept. If you don't have an account yet, signing in creates one.