MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / checkSemantics

Function checkSemantics

source code/utils/bash/ast.ts:2215–2681  ·  view source on GitHub ↗
(commands: SimpleCommand[])

Source from the content-addressed store, hash-verified

2213 * content. Returns the first failure or {ok: true}.
2214 */
2215export function checkSemantics(commands: SimpleCommand[]): SemanticCheckResult {
2216 for (const cmd of commands) {
2217 // Strip safe wrapper commands (nohup, time, timeout N, nice -n N) so
2218 // `nohup eval "..."` and `timeout 5 jq 'system(...)'` are checked
2219 // against the wrapped command, not the wrapper. Inlined here to avoid
2220 // circular import with bashPermissions.ts.
2221 let a = cmd.argv
2222 for (;;) {
2223 if (a[0] === 'time' || a[0] === 'nohup') {
2224 a = a.slice(1)
2225 } else if (a[0] === 'timeout') {
2226 // `timeout 5`, `timeout 5s`, `timeout 5.5`, plus optional GNU flags
2227 // preceding the duration. Long: --foreground, --kill-after=N,
2228 // --signal=SIG, --preserve-status. Short: -k DUR, -s SIG, -v (also
2229 // fused: -k5, -sTERM).
2230 // SECURITY (SAST Mar 2026): the previous loop only skipped `--long`
2231 // flags, so `timeout -k 5 10 eval ...` broke out with name='timeout'
2232 // and the wrapped eval was never checked. Now handle known short
2233 // flags AND fail closed on any unrecognized flag — an unknown flag
2234 // means we can't locate the wrapped command, so we must not silently
2235 // fall through to name='timeout'.
2236 let i = 1
2237 while (i < a.length) {
2238 const arg = a[i]!
2239 if (
2240 arg === '--foreground' ||
2241 arg === '--preserve-status' ||
2242 arg === '--verbose'
2243 ) {
2244 i++ // known no-value long flags
2245 } else if (/^--(?:kill-after|signal)=[A-Za-z0-9_.+-]+$/.test(arg)) {
2246 i++ // --kill-after=5, --signal=TERM (value fused with =)
2247 } else if (
2248 (arg === '--kill-after' || arg === '--signal') &&
2249 a[i + 1] &&
2250 /^[A-Za-z0-9_.+-]+$/.test(a[i + 1]!)
2251 ) {
2252 i += 2 // --kill-after 5, --signal TERM (space-separated)
2253 } else if (arg.startsWith('--')) {
2254 // Unknown long flag, OR --kill-after/--signal with non-allowlisted
2255 // value (e.g. placeholder from $() substitution). Fail closed.
2256 return {
2257 ok: false,
2258 reason: `timeout with ${arg} flag cannot be statically analyzed`,
2259 }
2260 } else if (arg === '-v') {
2261 i++ // --verbose, no argument
2262 } else if (
2263 (arg === '-k' || arg === '-s') &&
2264 a[i + 1] &&
2265 /^[A-Za-z0-9_.+-]+$/.test(a[i + 1]!)
2266 ) {
2267 i += 2 // -k DURATION / -s SIGNAL — separate value
2268 } else if (/^-[ks][A-Za-z0-9_.+-]+$/.test(arg)) {
2269 i++ // fused: -k5, -sTERM
2270 } else if (arg.startsWith('-')) {
2271 // Unknown flag OR -k/-s with non-allowlisted value — can't locate
2272 // wrapped cmd. Reject, don't fall through to name='timeout'.

Callers 1

bashToolHasPermissionFunction · 0.85

Calls 1

hasMethod · 0.45

Tested by

no test coverage detected