@bun-security-scanner/osv

中文文档
1.0.0发布于 2025/11/06MITTypeScriptES Module构建来源已验证近 30 天下载 2.2万

OSV vulnerability scanner for Bun projects

原始使用文档
文档目录30 节
  1. What is OSV.dev?
  2. Features
  3. Installation
  4. Configuration
  5. 1. Enable the Scanner
  6. 2. Optional: Configuration Options
  7. How It Works
  8. Security Scanning Process
  9. Advisory Levels
  10. Error Handling Philosophy
  11. Usage Examples
  12. Basic Usage
  13. Development Usage
  14. Configuration Examples
  15. Architecture
  16. Key Design Principles
  17. Testing
  18. Test Coverage
  19. Development
  20. Building from Source
  21. Contributing
  22. API Reference
  23. OSV.dev Integration
  24. Configuration Reference
  25. Troubleshooting
  26. Common Issues
  27. Debug Mode
  28. License
  29. Acknowledgments
  30. Related Projects
Bun OSV Scanner

Bun OSV Scanner

A production-grade security scanner for Bun that integrates with OSV.dev (Open Source Vulnerabilities) to detect known vulnerabilities in npm packages during installation.

npm version npm downloads License: MIT Built with Claude Checked with Biome Secured with Lefthook

What is OSV.dev?

OSV.dev is Google's open source vulnerability database that aggregates and distributes vulnerability information for open source projects. It provides:

  • Comprehensive Coverage: Vulnerabilities from multiple sources (npm, PyPI, Go, Rust, etc.)
  • Structured Data: Machine-readable vulnerability information with precise version ranges
  • Real-time Updates: Continuously updated with the latest security advisories
  • Authoritative Source: Maintained by Google and the open source community

Features

  • Real-time Scanning: Checks packages against OSV.dev during installation
  • High Performance: Efficient batch queries with smart deduplication
  • Fail-safe: Never blocks installations due to scanner errors
  • Structured Logging: Configurable logging levels with contextual information
  • Precise Matching: Accurate vulnerability-to-package version matching
  • Configurable: Environment variable configuration for all settings
  • Well Tested: Comprehensive test suite with edge case coverage

Installation

No API keys or registration required - completely free to use with zero setup beyond installation.

# Install as a dev dependency
bun add -d @bun-security-scanner/osv

Configuration

1. Enable the Scanner

Add to your bunfig.toml:

[install.security]
scanner = "@bun-security-scanner/osv"

2. Optional: Configuration Options

The scanner can be configured via environment variables:

# Logging level (debug, info, warn, error)
export OSV_LOG_LEVEL=info

# Custom OSV API base URL (optional)
export OSV_API_BASE_URL=https://api.osv.dev/v1

# Request timeout in milliseconds (default: 30000)
export OSV_TIMEOUT_MS=30000

# Disable batch queries (default: false)
export OSV_DISABLE_BATCH=false

How It Works

Security Scanning Process

  1. Package Detection: Bun provides package information during installation
  2. Smart Deduplication: Eliminates duplicate package@version queries
  3. Batch Querying: Uses OSV.dev's efficient /querybatch endpoint
  4. Vulnerability Matching: Precisely matches vulnerabilities to installed versions
  5. Severity Assessment: Analyzes CVSS scores and database-specific severity
  6. Advisory Generation: Creates actionable security advisories

Advisory Levels

The scanner generates two types of security advisories:

Fatal (Installation Blocked)

  • CVSS Score: ≥ 7.0 (High/Critical)
  • Database Severity: CRITICAL or HIGH
  • Action: Installation is immediately blocked
  • Examples: Remote code execution, privilege escalation, data exposure

Warning (User Prompted)

  • CVSS Score: < 7.0 (Medium/Low)
  • Database Severity: MEDIUM, LOW, or unspecified
  • Action: User is prompted to continue or cancel
  • TTY: Interactive choice presented
  • Non-TTY: Installation automatically cancelled
  • Examples: Denial of service, information disclosure, deprecation warnings

Error Handling Philosophy

The scanner follows a fail-safe approach:

  • Network errors don't block installations
  • Malformed responses are logged but don't halt the process
  • Scanner crashes return empty advisory arrays (allows installation)
  • Only genuine security threats should prevent package installation

Usage Examples

Basic Usage

# Scanner runs automatically during installation
bun install express
# -> Checks express and all dependencies for vulnerabilities

bun add [email protected]
# -> May warn about known lodash vulnerabilities in older versions

Development Usage

# Enable debug logging to see detailed scanning information
OSV_LOG_LEVEL=debug bun install

# Test with a known vulnerable package
bun add [email protected]
# -> Should trigger security advisory

Configuration Examples

# Increase timeout for slow networks
OSV_TIMEOUT_MS=60000 bun install

# Use custom OSV instance (advanced)
OSV_API_BASE_URL=https://api.custom-osv.dev/v1 bun install

Architecture

The scanner is built with a modular, production-ready architecture:

src/
├── index.ts              # Main scanner implementation
├── client.ts             # OSV.dev API client with batch support
├── processor.ts          # Vulnerability processing and advisory generation
├── cli.ts                # CLI interface for testing
├── schema.ts             # Zod schemas for OSV API responses
├── constants.ts          # Centralized configuration management
├── logger.ts             # Structured logging with configurable levels
├── retry.ts              # Robust retry logic with exponential backoff
├── semver.ts             # OSV semver range matching
├── severity.ts           # CVSS and severity assessment
└── types.ts              # TypeScript type definitions

Key Design Principles

  1. Separation of Concerns: Each module has a single, well-defined responsibility
  2. Error Isolation: Failures in one component don't cascade to others
  3. Performance Optimization: Batch processing, deduplication, and concurrent requests
  4. Observability: Comprehensive logging for debugging and monitoring
  5. Type Safety: Full TypeScript coverage with runtime validation

Testing

# Run the test suite
bun test

# Run with coverage
bun test --coverage

# Type checking
bun run typecheck

# Linting
bun run lint

Test Coverage

  • Known vulnerable packages detection
  • Safe package verification
  • Multiple package scenarios
  • Version-specific vulnerability matching
  • Network failure handling
  • Edge cases and error conditions

Development

Building from Source

git clone https://github.com/bun-security-scanner/osv.git
cd osv
bun install
bun run build

Contributing

We do not accept pull requests as this package is actively maintained. However, we appreciate if developers report bugs or suggest features by opening an issue.

See CONTRIBUTING.md for more details.

API Reference

OSV.dev Integration

This scanner integrates with the following OSV.dev endpoints:

  • POST /v1/querybatch: Batch vulnerability queries for multiple packages
  • POST /v1/query: Individual package queries with pagination support

For complete OSV.dev API documentation, visit: https://google.github.io/osv.dev/api/

Configuration Reference

Environment VariableDefaultDescription
OSV_LOG_LEVELinfoLogging level: debug, info, warn, error
OSV_API_BASE_URLhttps://api.osv.dev/v1OSV API base URL
OSV_TIMEOUT_MS30000Request timeout in milliseconds
OSV_DISABLE_BATCHfalseDisable batch queries (use individual queries)

Troubleshooting

Common Issues

Scanner not running during installation?

  • Verify bunfig.toml configuration
  • Check that the package is installed as a dev dependency
  • Enable debug logging: OSV_LOG_LEVEL=debug bun install

Network timeouts?

  • Increase timeout: OSV_TIMEOUT_MS=60000
  • Check internet connectivity to osv.dev
  • Consider corporate firewall restrictions

Too many false positives?

  • OSV.dev data is authoritative - verify vulnerabilities manually
  • Check if you're using an outdated package version
  • Report false positives to the OSV.dev project

Debug Mode

Enable comprehensive debug output:

OSV_LOG_LEVEL=debug bun install your-package

This shows:

  • Package deduplication statistics
  • API request/response details
  • Vulnerability matching decisions
  • Performance timing information

License

MIT License - see the LICENSE file for details.

Acknowledgments

  • OSV.dev Team: For maintaining the comprehensive vulnerability database
  • Bun Team: For the innovative Security Scanner API

Last Updated: November 6, 2025 Version: 1.0.0

This documentation is a living document and will be updated as the project evolves and new features are added.

版本与发布记录

@bun-security-scanner/osv 共发布 1 个版本,首次发布于 2025/11/06;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2025/11/061 年前

关于 @bun-security-scanner/osv 的常见问题

@bun-security-scanner/osv 是什么?

OSV vulnerability scanner for Bun projects

如何安装 @bun-security-scanner/osv?

在项目目录运行 npm install @bun-security-scanner/osv,或使用 pnpm add / yarn add / bun add。

@bun-security-scanner/osv 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bun-security-scanner/osv 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bun-security-scanner/osv 可以商用吗?

@bun-security-scanner/osv 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bun-security-scanner/osv 的下载量有多少?

根据 npm 官方下载统计,@bun-security-scanner/osv 在 2026/10/09 – 2026/11/07 这 30 天内被下载 21,510 次,日均约 717 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bun-security-scanner/osv 还在维护吗?

最新版本 1.0.0 发布于 2025/11/06(1 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/11/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。