bun-scan 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 rawtoast
4
+ Copyright (c) 2025 maloma7
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,338 @@
1
+ <!--
2
+ Copyright (c) 2026 rawtoast. All rights reserved.
3
+ Copyright (c) 2025 maloma7. All rights reserved.
4
+ SPDX-License-Identifier: MIT
5
+ -->
6
+
7
+ # Bun-Scan
8
+
9
+ A production-grade security scanner for [Bun](https://bun.sh/) that integrates with [OSV.dev](https://osv.dev/) (Open Source Vulnerabilities) to detect known vulnerabilities in packages during installation.
10
+
11
+ [![npm version](https://img.shields.io/npm/v/bun-scan?color=dc2626)](https://npmjs.com/package/bun-scan)
12
+ [![npm downloads](https://img.shields.io/npm/dm/bun-scan?color=dc2626)](https://npmjs.com/package/bun-scan)
13
+ [![License: MIT](https://img.shields.io/badge/License-MIT-dc2626)](LICENSE)
14
+
15
+ ## What is OSV.dev?
16
+
17
+ [OSV.dev](https://osv.dev/) is Google's open source vulnerability database that aggregates and distributes vulnerability information for open source projects. It provides:
18
+
19
+ - **Comprehensive Coverage**: Vulnerabilities from multiple sources (npm, PyPI, Go, Rust, etc.)
20
+ - **Structured Data**: Machine-readable vulnerability information with precise version ranges
21
+ - **Real-time Updates**: Continuously updated with the latest security advisories
22
+ - **Authoritative Source**: Maintained by Google and the open source community
23
+
24
+ ## Features
25
+
26
+ - **Real-time Scanning**: Checks packages against OSV.dev during installation
27
+ - **High Performance**: Efficient batch queries with smart deduplication
28
+ - **Fail-safe**: Never blocks installations due to scanner errors
29
+ - **Structured Logging**: Configurable logging levels with contextual information
30
+ - **Precise Matching**: Accurate vulnerability-to-package version matching
31
+ - **Configurable**: Environment variable configuration for all settings
32
+ - **Well Tested**: Comprehensive test suite with edge case coverage
33
+
34
+ ## Installation
35
+
36
+ **No API keys or registration required** - completely free to use with zero setup beyond installation.
37
+
38
+ ```bash
39
+ # Install as a dev dependency
40
+ bun add -d bun-scan
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ ### 1. Enable the Scanner
46
+
47
+ Add to your `bunfig.toml`:
48
+
49
+ ```toml
50
+ [install.security]
51
+ scanner = "bun-scan"
52
+ ```
53
+
54
+ ### 2. Optional: Ignore Specific Vulnerabilities
55
+
56
+ Create a `.bun-scan.json` file in your project root to ignore specific vulnerabilities:
57
+
58
+ ```json
59
+ {
60
+ "$schema": "https://raw.githubusercontent.com/rawtoast/bun-scan/master/schema/bun-scan.schema.json",
61
+ "packages": {
62
+ "hono": {
63
+ "vulnerabilities": ["CVE-2026-22818"],
64
+ "reason": "Project does not use JWT from hono"
65
+ }
66
+ }
67
+ }
68
+ ```
69
+
70
+ The scanner looks for `.osvignore.json` or `osv.config.json` in your project root.
71
+
72
+ #### Ignore Configuration Options
73
+
74
+ | Field | Description |
75
+ | --------------------------------- | ------------------------------------------------------------------------- |
76
+ | `ignore` | Array of vulnerability IDs to ignore globally (e.g., `["CVE-2024-1234"]`) |
77
+ | `packages.<name>.vulnerabilities` | Vulnerability IDs to ignore for a specific package |
78
+ | `packages.<name>.until` | Expiration date (ISO 8601) for temporary ignores |
79
+ | `packages.<name>.reason` | Documentation of why the vulnerability is ignored |
80
+
81
+ **Example: Global ignore**
82
+
83
+ ```json
84
+ {
85
+ "$schema": "https://raw.githubusercontent.com/rawtoast/bun-scan/master/schema/bun-scan.schema.json",
86
+ "ignore": ["CVE-2024-1234", "GHSA-xxxx-xxxx-xxxx"]
87
+ }
88
+ ```
89
+
90
+ **Example: Temporary ignore with expiration**
91
+
92
+ ```json
93
+ {
94
+ "$schema": "https://raw.githubusercontent.com/rawtoast/bun-scan/master/schema/bun-scan.schema.json",
95
+ "packages": {
96
+ "lodash": {
97
+ "vulnerabilities": ["CVE-2021-23337"],
98
+ "until": "2024-06-01",
99
+ "reason": "Temporary ignore while migration is in progress"
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ ### 3. Optional: Environment Variables
106
+
107
+ The scanner can be configured via environment variables:
108
+
109
+ ```bash
110
+ # Logging level (debug, info, warn, error)
111
+ export OSV_LOG_LEVEL=info
112
+
113
+ # Custom OSV API base URL (optional)
114
+ export OSV_API_BASE_URL=https://api.osv.dev/v1
115
+
116
+ # Request timeout in milliseconds (default: 30000)
117
+ export OSV_TIMEOUT_MS=30000
118
+
119
+ # Disable batch queries (default: false)
120
+ export OSV_DISABLE_BATCH=false
121
+ ```
122
+
123
+ ## How It Works
124
+
125
+ ### Security Scanning Process
126
+
127
+ 1. **Package Detection**: Bun provides package information during installation
128
+ 2. **Smart Deduplication**: Eliminates duplicate package@version queries
129
+ 3. **Batch Querying**: Uses OSV.dev's efficient `/querybatch` endpoint
130
+ 4. **Vulnerability Matching**: Precisely matches vulnerabilities to installed versions
131
+ 5. **Severity Assessment**: Analyzes CVSS scores and database-specific severity
132
+ 6. **Advisory Generation**: Creates actionable security advisories
133
+
134
+ ### Advisory Levels
135
+
136
+ The scanner generates two types of security advisories:
137
+
138
+ #### Fatal (Installation Blocked)
139
+
140
+ - **CVSS Score**: ≥ 7.0 (High/Critical)
141
+ - **Database Severity**: CRITICAL or HIGH
142
+ - **Action**: Installation is immediately blocked
143
+ - **Examples**: Remote code execution, privilege escalation, data exposure
144
+
145
+ #### Warning (User Prompted)
146
+
147
+ - **CVSS Score**: < 7.0 (Medium/Low)
148
+ - **Database Severity**: MEDIUM, LOW, or unspecified
149
+ - **Action**: User is prompted to continue or cancel
150
+ - **TTY**: Interactive choice presented
151
+ - **Non-TTY**: Installation automatically cancelled
152
+ - **Examples**: Denial of service, information disclosure, deprecation warnings
153
+
154
+ ### Error Handling Philosophy
155
+
156
+ The scanner follows a **fail-safe** approach:
157
+
158
+ - Network errors don't block installations
159
+ - Malformed responses are logged but don't halt the process
160
+ - Scanner crashes return empty advisory arrays (allows installation)
161
+ - Only genuine security threats should prevent package installation
162
+
163
+ ## Usage Examples
164
+
165
+ ### Basic Usage
166
+
167
+ ```bash
168
+ # Scanner runs automatically during installation
169
+ bun install express
170
+ # -> Checks express and all dependencies for vulnerabilities
171
+
172
+ bun add lodash@4.17.20
173
+ # -> May warn about known lodash vulnerabilities in older versions
174
+ ```
175
+
176
+ ### Development Usage
177
+
178
+ ```bash
179
+ # Enable debug logging to see detailed scanning information
180
+ OSV_LOG_LEVEL=debug bun install
181
+
182
+ # Test with a known vulnerable package
183
+ bun add event-stream@3.3.6
184
+ # -> Should trigger security advisory
185
+ ```
186
+
187
+ ### Configuration Examples
188
+
189
+ ```bash
190
+ # Increase timeout for slow networks
191
+ OSV_TIMEOUT_MS=60000 bun install
192
+
193
+ # Use custom OSV instance (advanced)
194
+ OSV_API_BASE_URL=https://api.custom-osv.dev/v1 bun install
195
+ ```
196
+
197
+ ## Architecture
198
+
199
+ The scanner is built with a modular, production-ready architecture:
200
+
201
+ ```
202
+ src/
203
+ ├── index.ts # Main scanner implementation
204
+ ├── client.ts # OSV.dev API client with batch support
205
+ ├── processor.ts # Vulnerability processing and advisory generation
206
+ ├── config.ts # Ignore configuration loading and validation
207
+ ├── cli.ts # CLI interface for testing
208
+ ├── schema.ts # Zod schemas for OSV API responses
209
+ ├── constants.ts # Centralized configuration management
210
+ ├── logger.ts # Structured logging with configurable levels
211
+ ├── retry.ts # Robust retry logic with exponential backoff
212
+ ├── semver.ts # OSV semver range matching
213
+ ├── severity.ts # CVSS and severity assessment
214
+ └── types.ts # TypeScript type definitions
215
+ ```
216
+
217
+ ### Key Design Principles
218
+
219
+ 1. **Separation of Concerns**: Each module has a single, well-defined responsibility
220
+ 2. **Error Isolation**: Failures in one component don't cascade to others
221
+ 3. **Performance Optimization**: Batch processing, deduplication, and concurrent requests
222
+ 4. **Observability**: Comprehensive logging for debugging and monitoring
223
+ 5. **Type Safety**: Full TypeScript coverage with runtime validation
224
+
225
+ ## Testing
226
+
227
+ ```bash
228
+ # Run the test suite
229
+ bun test
230
+
231
+ # Run with coverage
232
+ bun test --coverage
233
+
234
+ # Type checking
235
+ bun run typecheck
236
+
237
+ # Linting
238
+ bun run lint
239
+ ```
240
+
241
+ ### Test Coverage
242
+
243
+ - Known vulnerable packages detection
244
+ - Safe package verification
245
+ - Multiple package scenarios
246
+ - Version-specific vulnerability matching
247
+ - Network failure handling
248
+ - Edge cases and error conditions
249
+
250
+ ## Development
251
+
252
+ ### Building from Source
253
+
254
+ ```bash
255
+ git clone https://github.com/rawtoast/bun-scan.git
256
+ cd bun-scan
257
+ bun install
258
+ bun run build
259
+ ```
260
+
261
+ ## API Reference
262
+
263
+ ### OSV.dev Integration
264
+
265
+ This scanner integrates with the following OSV.dev endpoints:
266
+
267
+ - **POST /v1/querybatch**: Batch vulnerability queries for multiple packages
268
+ - **POST /v1/query**: Individual package queries with pagination support
269
+
270
+ For complete OSV.dev API documentation, visit: https://google.github.io/osv.dev/api/
271
+
272
+ ### Configuration Reference
273
+
274
+ | Environment Variable | Default | Description |
275
+ | -------------------- | ------------------------ | ---------------------------------------------- |
276
+ | `OSV_LOG_LEVEL` | `info` | Logging level: debug, info, warn, error |
277
+ | `OSV_API_BASE_URL` | `https://api.osv.dev/v1` | OSV API base URL |
278
+ | `OSV_TIMEOUT_MS` | `30000` | Request timeout in milliseconds |
279
+ | `OSV_DISABLE_BATCH` | `false` | Disable batch queries (use individual queries) |
280
+
281
+ ## Troubleshooting
282
+
283
+ ### Common Issues
284
+
285
+ **Scanner not running during installation?**
286
+
287
+ - Verify `bunfig.toml` configuration
288
+ - Check that the package is installed as a dev dependency
289
+ - Enable debug logging: `OSV_LOG_LEVEL=debug bun install`
290
+
291
+ **Network timeouts?**
292
+
293
+ - Increase timeout: `OSV_TIMEOUT_MS=60000`
294
+ - Check internet connectivity to osv.dev
295
+ - Consider corporate firewall restrictions
296
+
297
+ **Too many false positives?**
298
+
299
+ - OSV.dev data is authoritative - verify vulnerabilities manually
300
+ - Check if you're using an outdated package version
301
+ - Use `.osvignore.json` to ignore vulnerabilities that don't apply to your project
302
+ - Report false positives to the OSV.dev project
303
+
304
+ ### Debug Mode
305
+
306
+ Enable comprehensive debug output:
307
+
308
+ ```bash
309
+ OSV_LOG_LEVEL=debug bun install your-package
310
+ ```
311
+
312
+ This shows:
313
+
314
+ - Package deduplication statistics
315
+ - API request/response details
316
+ - Vulnerability matching decisions
317
+ - Performance timing information
318
+
319
+ ## License
320
+
321
+ MIT License - see the [LICENSE](LICENSE) file for details.
322
+
323
+ ## Acknowledgments
324
+
325
+ - **OSV.dev Team**: For maintaining the comprehensive vulnerability database
326
+ - **Bun Team**: For the innovative Security Scanner API
327
+ - **maloma7**: For the original implementation of the Bun OSV Scanner
328
+
329
+ ## Related Projects
330
+
331
+ - [Bun Security Scanner API](https://bun.com/docs/install/security-scanner-api)
332
+ - [OSV.dev](https://osv.dev/) - Open Source Vulnerabilities database
333
+
334
+ ---
335
+
336
+ **Last Updated**: January 15, 2026
337
+
338
+ _This documentation is a living document and will be updated as the project evolves and new features are added._