


















@@ -6,6 +6,10 @@
66 */
7788import type { IncomingMessage, ServerResponse } from "node:http";
9+import {
10+asDateTimestampMs,
11+resolveExpiresAtMsFromDurationMs,
12+} from "openclaw/plugin-sdk/number-runtime";
913import { safeEqualSecret } from "openclaw/plugin-sdk/security-runtime";
1014import { isPrivateNetworkOptInEnabled } from "openclaw/plugin-sdk/ssrf-runtime";
1115import type { ResolvedMattermostAccount } from "../mattermost/accounts.js";
@@ -209,8 +213,14 @@ export function clearMattermostSlashCommandValidationCacheForAccount(accountId:
209213}
210214211215function sweepCommandValidationFailureCache(now = Date.now()): void {
216+const validNow = asDateTimestampMs(now);
217+if (validNow === undefined) {
218+commandValidationFailureCache.clear();
219+return;
220+}
212221for (const [key, entry] of commandValidationFailureCache) {
213-if (entry.expiresAt <= now) {
222+const expiresAt = asDateTimestampMs(entry.expiresAt);
223+if (expiresAt === undefined || expiresAt <= validNow) {
214224commandValidationFailureCache.delete(key);
215225}
216226}
@@ -225,29 +235,48 @@ function sweepCommandValidationFailureCache(now = Date.now()): void {
225235226236function hasCachedCommandValidationFailure(key: string, now = Date.now()): boolean {
227237sweepCommandValidationFailureCache(now);
238+const validNow = asDateTimestampMs(now);
239+if (validNow === undefined) {
240+return false;
241+}
228242const cached = commandValidationFailureCache.get(key);
229243if (!cached) {
230244return false;
231245}
232-if (cached.expiresAt > now) {
246+const expiresAt = asDateTimestampMs(cached.expiresAt);
247+if (expiresAt !== undefined && expiresAt > validNow) {
233248return true;
234249}
235250commandValidationFailureCache.delete(key);
236251return false;
237252}
238253239254function cacheCommandValidationFailure(key: string, accountId: string): void {
240-sweepCommandValidationFailureCache();
255+const now = Date.now();
256+sweepCommandValidationFailureCache(now);
257+const expiresAt = resolveExpiresAtMsFromDurationMs(COMMAND_VALIDATION_FAILURE_CACHE_MS, {
258+nowMs: now,
259+});
260+if (expiresAt === undefined) {
261+commandValidationFailureCache.delete(key);
262+return;
263+}
241264commandValidationFailureCache.set(key, {
242265 accountId,
243-expiresAt: Date.now() + COMMAND_VALIDATION_FAILURE_CACHE_MS,
266+ expiresAt,
244267});
245268}
246269247270function sweepCommandValidationLookupRateLimit(now = Date.now()): void {
271+const validNow = asDateTimestampMs(now);
272+if (validNow === undefined) {
273+commandValidationLookupRateLimit.clear();
274+return;
275+}
248276const staleAfterMs = COMMAND_VALIDATION_LOOKUP_REFILL_MS * COMMAND_VALIDATION_LOOKUP_BURST * 2;
249277for (const [key, entry] of commandValidationLookupRateLimit) {
250-if (now - entry.updatedAt > staleAfterMs) {
278+const updatedAt = asDateTimestampMs(entry.updatedAt);
279+if (updatedAt === undefined || validNow - updatedAt > staleAfterMs) {
251280commandValidationLookupRateLimit.delete(key);
252281}
253282}
@@ -265,7 +294,12 @@ function reserveCommandValidationLookup(params: {
265294accountId: string;
266295now?: number;
267296}): { allowed: true } | { allowed: false; shouldLog: boolean } {
268-const now = params.now ?? Date.now();
297+const rawNow = params.now ?? Date.now();
298+const now = asDateTimestampMs(rawNow);
299+if (now === undefined) {
300+commandValidationLookupRateLimit.clear();
301+return { allowed: true };
302+}
269303sweepCommandValidationLookupRateLimit(now);
270304const existing = commandValidationLookupRateLimit.get(params.key);
271305if (!existing) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。