refactor(security): unify command gating and blocked-key guards

This commit is contained in:
Peter Steinberger
2026-02-21 13:04:31 +01:00
parent 356d61aacf
commit 08e020881d
10 changed files with 111 additions and 58 deletions

View File

@@ -1,7 +1,11 @@
import { normalizeChannelId } from "../channels/plugins/index.js";
import type { ChannelId } from "../channels/plugins/types.js";
import { isPlainObject } from "../infra/plain-object.js";
import type { NativeCommandsSetting } from "./types.js";
import type { CommandsConfig, NativeCommandsSetting } from "./types.js";
export type CommandFlagKey = {
[K in keyof CommandsConfig]-?: Exclude<CommandsConfig[K], undefined> extends boolean ? K : never;
}[keyof CommandsConfig];
function resolveAutoDefault(providerId?: ChannelId): boolean {
const id = normalizeChannelId(providerId);
@@ -63,7 +67,10 @@ export function isNativeCommandsExplicitlyDisabled(params: {
return false;
}
function getOwnCommandFlagValue(config: { commands?: unknown } | undefined, key: string): unknown {
function getOwnCommandFlagValue(
config: { commands?: unknown } | undefined,
key: CommandFlagKey,
): unknown {
const { commands } = config ?? {};
if (!isPlainObject(commands) || !Object.hasOwn(commands, key)) {
return undefined;
@@ -73,7 +80,7 @@ function getOwnCommandFlagValue(config: { commands?: unknown } | undefined, key:
export function isCommandFlagEnabled(
config: { commands?: unknown } | undefined,
key: string,
key: CommandFlagKey,
): boolean {
return getOwnCommandFlagValue(config, key) === true;
}