[grade=A] DC-085: Replace Math.random() with crypto for security-sensitive IDs
- port-lock-manager.js: lockId uses crypto.randomBytes(8) instead of Math.random()
- openclaw.js: generateToken() uses crypto.randomBytes(24).toString('base64url') — 192 bits entropy
- Sampling uses (health-checker 5%, resource-monitor 10%) intentionally left as Math.random
Codex grade: A (21,294 tokens). All 1539 tests pass.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
const crypto = require('crypto');
|
||||||
const { ok, errorResponse, notFound, conflict } = require('../src/utils/responses');
|
const { ok, errorResponse, notFound, conflict } = require('../src/utils/responses');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -263,10 +264,5 @@ module.exports = function openClawRoutes(ctx) {
|
|||||||
// ── token generator ──────────────────────────────────────────────────────────
|
// ── token generator ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function generateToken() {
|
function generateToken() {
|
||||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
return crypto.randomBytes(24).toString('base64url');
|
||||||
let result = '';
|
|
||||||
for (let i = 0; i < 32; i++) {
|
|
||||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const crypto = require('crypto');
|
||||||
const lockfile = require('proper-lockfile');
|
const lockfile = require('proper-lockfile');
|
||||||
const platformPaths = require('../../platform-paths');
|
const platformPaths = require('../../platform-paths');
|
||||||
const { log } = require('../utils/logging');
|
const { log } = require('../utils/logging');
|
||||||
@@ -58,7 +59,7 @@ class PortLockManager {
|
|||||||
throw new Error('Ports must be a non-empty array');
|
throw new Error('Ports must be a non-empty array');
|
||||||
}
|
}
|
||||||
|
|
||||||
const lockId = `lock-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
const lockId = `lock-${Date.now()}-${crypto.randomBytes(8).toString('hex')}`;
|
||||||
const sortedPorts = [...new Set(ports)].sort((a, b) => parseInt(a) - parseInt(b));
|
const sortedPorts = [...new Set(ports)].sort((a, b) => parseInt(a) - parseInt(b));
|
||||||
const acquiredLocks = [];
|
const acquiredLocks = [];
|
||||||
const releaseFunctions = [];
|
const releaseFunctions = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user