JustBash.Limit (JustBash v0.3.0)

View Source

Production resource limits for JustBash execution.

Prevents untrusted scripts from exhausting memory or CPU by enforcing hard caps on computation steps, output size, file size, regex patterns, and execution nesting depth.

Usage

# Default limits (recommended for production)
bash = JustBash.new()

# Preset profiles
bash = JustBash.new(limits: :strict)

# Custom limits (merged with defaults)
bash = JustBash.new(limits: [max_steps: 50_000])

# No limits (not recommended for untrusted input)
bash = JustBash.new(limits: false)

Summary

Functions

Check file data size before writing. Raises ExceededError if too large.

Check regex pattern size only. Use compile_regex/3 when you also need compilation.

Check regex pattern size and compile. Raises ExceededError if pattern too large.

Returns the default limits.

Build limits from a preset atom, keyword list, or false to disable.

Increment step counter. Raises ExceededError if limit is reached.

Increment exec depth and track the high-water mark. Raises ExceededError if limit is exceeded.

Track output bytes. Raises ExceededError if limit is reached.

Types

t()

@type t() :: %JustBash.Limit{
  max_exec_depth: pos_integer(),
  max_file_bytes: pos_integer(),
  max_output_bytes: pos_integer(),
  max_regex_pattern_bytes: pos_integer(),
  max_steps: pos_integer()
}

Functions

check_file_size!(map, data)

@spec check_file_size!(JustBash.t(), String.t()) :: :ok

Check file data size before writing. Raises ExceededError if too large.

check_regex_size!(limits, pattern)

@spec check_regex_size!(t() | nil, String.t()) :: :ok

Check regex pattern size only. Use compile_regex/3 when you also need compilation.

For call sites that need custom compilation logic (e.g. grep's flag handling, sed's BRE-to-ERE conversion), call this directly.

compile_regex(limits, pattern, opts \\ "")

@spec compile_regex(t() | nil, String.t(), String.t() | [atom()]) ::
  {:ok, Regex.t()} | {:error, term()}

Check regex pattern size and compile. Raises ExceededError if pattern too large.

Centralizes the check-then-compile pattern used across commands (grep, sed, awk, jq, etc.). Accepts a limits struct (not a full bash struct) so it can be called from command internals that don't carry the full struct.

defaults()

@spec defaults() :: t()

Returns the default limits.

new(opts)

@spec new(atom() | keyword() | false) :: t() | nil

Build limits from a preset atom, keyword list, or false to disable.

step!(bash)

@spec step!(JustBash.t()) :: JustBash.t()

Increment step counter. Raises ExceededError if limit is reached.

track_exec_depth!(bash)

@spec track_exec_depth!(JustBash.t()) :: JustBash.t()

Increment exec depth and track the high-water mark. Raises ExceededError if limit is exceeded.

track_output!(bash, new_bytes)

@spec track_output!(JustBash.t(), non_neg_integer()) :: JustBash.t()

Track output bytes. Raises ExceededError if limit is reached.