Files
OmniRoute/src/lib/machineToken.ts
小妍儿 ✨ 621f30a188 fix(cli): restore packaged machine-token authentication (#10468)
Obrigado por restaurar e endurecer a autenticação por machine-token no CLI empacotado.

Validação (worktree combinado a partir de origin/release/v3.8.50, merge limpo, 0 conflitos — 34 arquivos, +1078/-247):

- `npm run typecheck:core` — limpo
- `node scripts/check/check-complexity.mjs` — OK (2558 violações vs baseline 2774)
- `node scripts/check/check-cognitive-complexity.mjs` — OK (1152 violações vs baseline 1223)
- `node scripts/check/check-file-size.mjs` — OK
- `node scripts/check/check-changelog-integrity.mjs` — OK
- Testes focados (8 arquivos: cli-doctor-command, cli-machine-token, lib/machineToken, lib/managementCliToken, agentSkills-generator, api/settings-audit, check-pack-boot, next-config) — 95/95 passando

Os dois achados de segurança do maintainer-feedback original (checagem de loopback tipo SSRF, escopo de cookie/CSRF) já estavam corrigidos e cobertos por teste no commit `2b785f0068a862fbd867221294325ad921787782` desta branch.
2026-08-20 15:07:19 -03:00

65 lines
1.9 KiB
TypeScript

import { createHash, createHmac } from "node:crypto";
import { createRequire } from "node:module";
let machineIdSync: (original?: boolean) => string;
try {
// Anchor runtime resolution to the process entrypoint. Turbopack rewrites
// createRequire(import.meta.url) into an in-bundle resolver, which cannot load
// external CommonJS packages from the installed standalone node_modules tree.
const runtimeRequire = createRequire(process.argv[1] || process.cwd());
const mod = runtimeRequire("node-machine-id");
machineIdSync = mod.machineIdSync || mod.default?.machineIdSync;
} catch {
machineIdSync = () => "";
}
const BUILTIN_DEFAULT_SALT = "omniroute-cli-auth-v1";
function getActiveSalt(): string {
return process.env.OMNIROUTE_CLI_SALT || BUILTIN_DEFAULT_SALT;
}
export function deriveMachineToken(rawId: string, salt: string): string {
if (!rawId) return "";
return createHmac("sha256", rawId).update(salt).digest("hex");
}
export function deriveLegacyCliToken(machineId: string, salt: string): string {
if (!machineId) return "";
return createHash("sha256")
.update(machineId + salt)
.digest("hex")
.substring(0, 32);
}
let cached: string | null = null;
let cachedSalt: string | null = null;
export function getMachineTokenSync(salt?: string): string {
const activeSalt = salt ?? getActiveSalt();
try {
// machineIdSync(true) returns the original unhashed hardware ID.
const rawId = machineIdSync(true);
if (!rawId) return "";
if (activeSalt === cachedSalt && cached !== null) return cached;
const token = deriveMachineToken(rawId, activeSalt);
if (!salt) {
cached = token;
cachedSalt = activeSalt;
}
return token;
} catch {
return "";
}
}
export function getLegacyCliTokenSync(salt?: string): string {
const activeSalt = salt ?? getActiveSalt();
try {
const machineId = machineIdSync();
return deriveLegacyCliToken(machineId, activeSalt);
} catch {
return "";
}
}