@ -9,6 +9,15 @@ import { resolveNativeSkillsEnabled } from "../config/commands.js";
import { resolveOAuthDir } from "../config/paths.js" ;
import { formatCliCommand } from "../cli/command-format.js" ;
import { resolveDefaultAgentId } from "../agents/agent-scope.js" ;
import type { AgentToolsConfig } from "../config/types.tools.js" ;
import { resolveBrowserConfig } from "../browser/config.js" ;
import { isToolAllowedByPolicies } from "../agents/pi-tools.policy.js" ;
import { resolveToolProfilePolicy } from "../agents/tool-policy.js" ;
import {
resolveSandboxConfigForAgent ,
resolveSandboxToolPolicyForAgent ,
} from "../agents/sandbox.js" ;
import type { SandboxToolPolicy } from "../agents/sandbox/types.js" ;
import { INCLUDE_KEY , MAX_INCLUDE_DEPTH } from "../config/includes.js" ;
import { normalizeAgentId } from "../routing/session-key.js" ;
import {
@ -29,6 +38,8 @@ export type SecurityAuditFinding = {
remediation? : string ;
} ;
const SMALL_MODEL_PARAM_B_MAX = 300 ;
function expandTilde ( p : string , env : NodeJS.ProcessEnv ) : string | null {
if ( ! p . startsWith ( "~" ) ) return p ;
const home = typeof env . HOME === "string" && env . HOME . trim ( ) ? env . HOME . trim ( ) : null ;
@ -266,6 +277,20 @@ const WEAK_TIER_MODEL_PATTERNS: Array<{ id: string; re: RegExp; label: string }>
{ id : "anthropic.haiku" , re : /\bhaiku\b/i , label : "Haiku tier (smaller model)" } ,
] ;
function inferParamBFromIdOrName ( text : string ) : number | null {
const raw = text . toLowerCase ( ) ;
const matches = raw . matchAll ( /(?:^|[^a-z0-9])[a-z]?(\d+(?:\.\d+)?)b(?:[^a-z0-9]|$)/g ) ;
let best : number | null = null ;
for ( const match of matches ) {
const numRaw = match [ 1 ] ;
if ( ! numRaw ) continue ;
const value = Number ( numRaw ) ;
if ( ! Number . isFinite ( value ) || value <= 0 ) continue ;
if ( best === null || value > best ) best = value ;
}
return best ;
}
function isGptModel ( id : string ) : boolean {
return /\bgpt-/i . test ( id ) ;
}
@ -363,6 +388,155 @@ export function collectModelHygieneFindings(cfg: ClawdbotConfig): SecurityAuditF
return findings ;
}
function extractAgentIdFromSource ( source : string ) : string | null {
const match = source . match ( /^agents\.list\.([^.]*)\./ ) ;
return match ? . [ 1 ] ? ? null ;
}
function pickToolPolicy ( config ? : { allow? : string [ ] ; deny? : string [ ] } ) : SandboxToolPolicy | null {
if ( ! config ) return null ;
const allow = Array . isArray ( config . allow ) ? config.allow : undefined ;
const deny = Array . isArray ( config . deny ) ? config.deny : undefined ;
if ( ! allow && ! deny ) return null ;
return { allow , deny } ;
}
function resolveToolPolicies ( params : {
cfg : ClawdbotConfig ;
agentTools? : AgentToolsConfig ;
sandboxMode ? : "off" | "non-main" | "all" ;
agentId? : string | null ;
} ) : SandboxToolPolicy [ ] {
const policies : SandboxToolPolicy [ ] = [ ] ;
const profile = params . agentTools ? . profile ? ? params . cfg . tools ? . profile ;
const profilePolicy = resolveToolProfilePolicy ( profile ) ;
if ( profilePolicy ) policies . push ( profilePolicy ) ;
const globalPolicy = pickToolPolicy ( params . cfg . tools ? ? undefined ) ;
if ( globalPolicy ) policies . push ( globalPolicy ) ;
const agentPolicy = pickToolPolicy ( params . agentTools ) ;
if ( agentPolicy ) policies . push ( agentPolicy ) ;
if ( params . sandboxMode === "all" ) {
const sandboxPolicy = resolveSandboxToolPolicyForAgent ( params . cfg , params . agentId ? ? undefined ) ;
policies . push ( sandboxPolicy ) ;
}
return policies ;
}
function hasWebSearchKey ( cfg : ClawdbotConfig , env : NodeJS.ProcessEnv ) : boolean {
const search = cfg . tools ? . web ? . search ;
return Boolean (
search ? . apiKey ||
search ? . perplexity ? . apiKey ||
env . BRAVE_API_KEY ||
env . PERPLEXITY_API_KEY ||
env . OPENROUTER_API_KEY ,
) ;
}
function isWebSearchEnabled ( cfg : ClawdbotConfig , env : NodeJS.ProcessEnv ) : boolean {
const enabled = cfg . tools ? . web ? . search ? . enabled ;
if ( enabled === false ) return false ;
if ( enabled === true ) return true ;
return hasWebSearchKey ( cfg , env ) ;
}
function isWebFetchEnabled ( cfg : ClawdbotConfig ) : boolean {
const enabled = cfg . tools ? . web ? . fetch ? . enabled ;
if ( enabled === false ) return false ;
return true ;
}
function isBrowserEnabled ( cfg : ClawdbotConfig ) : boolean {
try {
return resolveBrowserConfig ( cfg . browser ) . enabled ;
} catch {
return true ;
}
}
export function collectSmallModelRiskFindings ( params : {
cfg : ClawdbotConfig ;
env : NodeJS.ProcessEnv ;
} ) : SecurityAuditFinding [ ] {
const findings : SecurityAuditFinding [ ] = [ ] ;
const models = collectModels ( params . cfg ) . filter ( ( entry ) = > ! entry . source . includes ( "imageModel" ) ) ;
if ( models . length === 0 ) return findings ;
const smallModels = models
. map ( ( entry ) = > {
const paramB = inferParamBFromIdOrName ( entry . id ) ;
if ( ! paramB || paramB > SMALL_MODEL_PARAM_B_MAX ) return null ;
return { . . . entry , paramB } ;
} )
. filter ( ( entry ) : entry is { id : string ; source : string ; paramB : number } = > Boolean ( entry ) ) ;
if ( smallModels . length === 0 ) return findings ;
let hasUnsafe = false ;
const modelLines : string [ ] = [ ] ;
const exposureSet = new Set < string > ( ) ;
for ( const entry of smallModels ) {
const agentId = extractAgentIdFromSource ( entry . source ) ;
const sandboxMode = resolveSandboxConfigForAgent ( params . cfg , agentId ? ? undefined ) . mode ;
const agentTools =
agentId && params . cfg . agents ? . list
? params . cfg . agents . list . find ( ( agent ) = > agent ? . id === agentId ) ? . tools
: undefined ;
const policies = resolveToolPolicies ( {
cfg : params.cfg ,
agentTools ,
sandboxMode ,
agentId ,
} ) ;
const exposed : string [ ] = [ ] ;
if ( isWebSearchEnabled ( params . cfg , params . env ) ) {
if ( isToolAllowedByPolicies ( "web_search" , policies ) ) exposed . push ( "web_search" ) ;
}
if ( isWebFetchEnabled ( params . cfg ) ) {
if ( isToolAllowedByPolicies ( "web_fetch" , policies ) ) exposed . push ( "web_fetch" ) ;
}
if ( isBrowserEnabled ( params . cfg ) ) {
if ( isToolAllowedByPolicies ( "browser" , policies ) ) exposed . push ( "browser" ) ;
}
for ( const tool of exposed ) exposureSet . add ( tool ) ;
const sandboxLabel = sandboxMode === "all" ? "sandbox=all" : ` sandbox= ${ sandboxMode } ` ;
const exposureLabel = exposed . length > 0 ? ` web=[ ${ exposed . join ( ", " ) } ] ` : " web=[off]" ;
const safe = sandboxMode === "all" && exposed . length === 0 ;
if ( ! safe ) hasUnsafe = true ;
const statusLabel = safe ? "ok" : "unsafe" ;
modelLines . push (
` - ${ entry . id } ( ${ entry . paramB } B) @ ${ entry . source } ( ${ statusLabel } ; ${ sandboxLabel } ; ${ exposureLabel } ) ` ,
) ;
}
const exposureList = Array . from ( exposureSet ) ;
const exposureDetail =
exposureList . length > 0
? ` Uncontrolled input tools allowed: ${ exposureList . join ( ", " ) } . `
: "No web/browser tools detected for these models." ;
findings . push ( {
checkId : "models.small_params" ,
severity : hasUnsafe ? "critical" : "info" ,
title : "Small models require sandboxing and web tools disabled" ,
detail :
` Small models (<= ${ SMALL_MODEL_PARAM_B_MAX } B params) detected: \ n ` +
modelLines . join ( "\n" ) +
` \ n ` +
exposureDetail +
` \ n ` +
"Small models are not recommended for untrusted inputs." ,
remediation :
'If you must use small models, enable sandboxing for all sessions (agents.defaults.sandbox.mode="all") and disable web_search/web_fetch/browser (tools.deny=["group:web","browser"]).' ,
} ) ;
return findings ;
}
export async function collectPluginsTrustFindings ( params : {
cfg : ClawdbotConfig ;
stateDir : string ;