返回 MCP 目录
public公开dns本地运行

bashrs

Rash是一个双向Shell安全工具,可将Rust代码编译为安全的POSIX Shell脚本,或自动修复现有Bash脚本的安全问题,提供确定性输出和零运行时依赖。

article

README

🚀 Rash - 双向 shell 安全工具

Rash 是一款双向 shell 安全工具,它可以净化传统的 bash 脚本,还能让你用纯正的 Rust 编写 shell 脚本,并自动提供安全保障。

🚀 快速开始

安装

# 从 crates.io 安装(推荐)
cargo install bashrs

# 或者从源代码安装
git clone https://github.com/paiml/bashrs
cd bashrs
cargo install --path rash

使用 Rust 编写,生成安全的 shell 脚本

// install.rs
#[rash::main]
fn main() {
    let version = env_var_or("VERSION", "1.0.0");
    let prefix = env_var_or("PREFIX", "/usr/local");

    echo("Installing MyApp {version} to {prefix}");

    mkdir_p("{prefix}/bin");
    mkdir_p("{prefix}/share/myapp");

    if exec("cp myapp {prefix}/bin/") {
        echo("✓ Binary installed");
    } else {
        eprint("✗ Failed to install binary");
        exit(1);
    }
}

转换为安全的 POSIX shell 脚本

$ bashrs build install.rs -o install.sh

或者净化现有的 bash 脚本

净化前(杂乱的 bash 脚本):

#!/bin/bash
SESSION_ID=$RANDOM                      # 非确定性的
mkdir /app/releases/$RELEASE            # 非幂等的
rm /app/current                         # 如果文件不存在会失败

净化后(由 Rash 净化):

#!/bin/sh
session_id="session-${version}"         # ✅ 确定性的
mkdir -p "/app/releases/${release}"     # ✅ 幂等的
rm -f "/app/current"                    # ✅ 安全的删除操作

✨ 主要特性

  • 🛡️ 自动安全防护:防止 shell 注入、单词分割和通配符扩展。
  • 🔍 超越代码检查:具备完整的抽象语法树(AST)语义理解能力,不仅能警告,还能转换代码。
  • 📦 零运行时依赖:生成的脚本可在任何 POSIX 兼容的 shell 上运行。
  • 🎯 确定性输出:相同的输入始终生成相同的脚本。
  • 符合 ShellCheck 标准:所有输出都能通过严格的代码检查。

Rash 超越 ShellCheck 的地方

| ShellCheck 的功能 | Rash 的功能 | |---------------------|----------------| | ⚠️ 警告:“$RANDOM 是非确定性的” | ✅ 重写为基于版本的确定性 ID | | ⚠️ 警告:“如果目录已存在,mkdir 可能会失败” | ✅ 转换mkdir -p(幂等操作) | | ⚠️ 警告:“未加引号的变量扩展” | ✅ 自动为所有变量加引号 | | 静态模式匹配 | 完整的 AST 语义理解 | | 检测问题(只读) | 修复问题(读写转换) |

关键区别:ShellCheck 只会告诉你哪里有问题,而 Rash 能理解你代码的意图,并自动将其重写为安全、确定性和幂等的代码。

📦 安装指南

# 从 crates.io 安装(推荐)
cargo install bashrs

# 或者从源代码安装
git clone https://github.com/paiml/bashrs
cd bashrs
cargo install --path rash

💻 使用示例

基础用法

// install.rs
#[rash::main]
fn main() {
    let version = env_var_or("VERSION", "1.0.0");
    let prefix = env_var_or("PREFIX", "/usr/local");

    echo("Installing MyApp {version} to {prefix}");

    mkdir_p("{prefix}/bin");
    mkdir_p("{prefix}/share/myapp");

    if exec("cp myapp {prefix}/bin/") {
        echo("✓ Binary installed");
    } else {
        eprint("✗ Failed to install binary");
        exit(1);
    }
}

转换为安全的 POSIX shell 脚本

$ bashrs build install.rs -o install.sh

高级用法

# 状态机测试
bashrs playbook install.playbook.yaml --run

# 变异测试(目标:杀死率 >90%)
bashrs mutate script.sh --count 10

# 确定性模拟回放
bashrs simulate script.sh --seed 42 --verify

📚 详细文档

《Rash 手册》 是所有文档的权威来源:

→ 阅读《Rash 手册》

快速链接

为什么选择手册?

  • ✅ 所有示例都经过自动测试
  • ✅ 始终与最新版本保持同步
  • ✅ 全面覆盖所有功能
  • ✅ 提供真实世界的示例和教程

🔧 技术细节

核心命令

# 将 Rust 代码转换为 shell 脚本
bashrs build input.rs -o output.sh

# 净化传统的 bash 脚本
bashrs purify messy.sh -o clean.sh

# 交互式 REPL 并支持调试
bashrs repl

# 对 shell 脚本进行代码检查(包括 Dockerfile)
bashrs lint script.sh

# 测试 bash 脚本
bashrs test script.sh

# 质量评分
bashrs score script.sh

# 全面审计
bashrs audit script.sh

高级测试

Rash 集成了 Probar 以进行全面的质量保证:

# 使用剧本进行状态机测试
bashrs playbook install.playbook.yaml --run

# 变异测试(目标:杀死率 >90%)
bashrs mutate script.sh --count 10

# 确定性模拟回放
bashrs simulate script.sh --seed 42 --verify

变异操作符:Rash 应用了 10 种变异操作符,包括字符串变异、命令替换、条件反转和重定向修改,以验证测试质量。

证伪测试(波普尔方法论)

Rash 使用波普尔证伪法 —— 测试尝试证伪功能,而不是证明其有效:

# 运行 130 点的转换器证伪检查清单
cargo test -p bashrs --test transpiler_tcode_tests

# 运行 30 点的 Dockerfile 证伪检查清单
cargo test -p bashrs --test dockerfile_dcode_tests

测试通过意味着证伪尝试失败 —— 该功能正常工作。

性能

Rash 旨在实现快速转换:

  • Rust 到 shell:转换时间为 21.1µs
  • Makefile 解析:0.034 - 1.43ms(比目标快 70 - 320 倍)
  • 内存使用:大多数脚本的内存使用 <10MB

MCP 服务器

Rash 提供了一个模型上下文协议(MCP)服务器,用于 AI 辅助的 shell 脚本生成:

# 安装 MCP 服务器
cargo install rash-mcp

# 运行服务器
rash-mcp

可在官方 MCP 注册表中找到,名称为 io.github.paiml/rash

📄 许可证

本项目采用 MIT 许可证。详情请见 LICENSE


如需全面的文档,请访问 《Rash 手册》
help

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端