|
1 | | -import { removeInvalidImageMarkdown, convertRequireToImport, COMMONJS_REQUIRE_REGEX, IMPORT_EXTRACTION_REGEX } from './utils' |
| 1 | +import { |
| 2 | + removeInvalidImageMarkdown, |
| 3 | + convertRequireToImport, |
| 4 | + COMMONJS_REQUIRE_REGEX, |
| 5 | + IMPORT_EXTRACTION_REGEX, |
| 6 | + executeJavaScriptCode |
| 7 | +} from './utils' |
2 | 8 |
|
3 | 9 | describe('removeInvalidImageMarkdown', () => { |
4 | 10 | describe('strips non-http/https image markdown', () => { |
@@ -229,3 +235,55 @@ describe('Import extraction regex (utils.ts line 1596 pattern)', () => { |
229 | 235 | expect(extractModules('console.log("hello")')).toEqual([]) |
230 | 236 | }) |
231 | 237 | }) |
| 238 | + |
| 239 | +// --------------------------------------------------------------------------- |
| 240 | +// NodeVM sandbox — availableDependencies allowlist |
| 241 | +// --------------------------------------------------------------------------- |
| 242 | + |
| 243 | +describe('NodeVM sandbox — availableDependencies allowlist', () => { |
| 244 | + afterEach(() => { |
| 245 | + delete process.env.ALLOW_BUILTIN_DEP |
| 246 | + delete process.env.TOOL_FUNCTION_EXTERNAL_DEP |
| 247 | + }) |
| 248 | + |
| 249 | + describe('high-risk packages are blocked even when ALLOW_BUILTIN_DEP=true', () => { |
| 250 | + beforeEach(() => { |
| 251 | + process.env.ALLOW_BUILTIN_DEP = 'true' |
| 252 | + }) |
| 253 | + |
| 254 | + const removedPackages = [ |
| 255 | + 'pg', |
| 256 | + 'mysql2', |
| 257 | + 'mongodb', |
| 258 | + 'ioredis', |
| 259 | + 'redis', |
| 260 | + 'typeorm', |
| 261 | + 'puppeteer', |
| 262 | + 'playwright', |
| 263 | + '@zilliz/milvus2-sdk-node' |
| 264 | + ] |
| 265 | + |
| 266 | + test.each(removedPackages)( |
| 267 | + "require('%s') is denied", |
| 268 | + async (pkg) => { |
| 269 | + await expect( |
| 270 | + executeJavaScriptCode(`const m = require('${pkg}'); return 'loaded'`, {}, { timeout: 10000 }) |
| 271 | + ).rejects.toThrow() |
| 272 | + }, |
| 273 | + 15000 |
| 274 | + ) |
| 275 | + }) |
| 276 | + |
| 277 | + it('packages remaining in availableDependencies are still accessible with ALLOW_BUILTIN_DEP=true', async () => { |
| 278 | + process.env.ALLOW_BUILTIN_DEP = 'true' |
| 279 | + const result = await executeJavaScriptCode(`const cheerio = require('cheerio'); return typeof cheerio.load`, {}, { timeout: 10000 }) |
| 280 | + expect(result).toBe('function') |
| 281 | + }, 15000) |
| 282 | + |
| 283 | + it('a removed package becomes accessible via TOOL_FUNCTION_EXTERNAL_DEP', async () => { |
| 284 | + process.env.ALLOW_BUILTIN_DEP = 'true' |
| 285 | + process.env.TOOL_FUNCTION_EXTERNAL_DEP = 'pg' |
| 286 | + const result = await executeJavaScriptCode(`const { Client } = require('pg'); return typeof Client`, {}, { timeout: 10000 }) |
| 287 | + expect(result).toBe('function') |
| 288 | + }, 15000) |
| 289 | +}) |
0 commit comments