Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import { afterEach, beforeEach, describe, expect, it } from 'vitest';import app from '../src/app';import { CONFIG } from '../src/config';
// Workspace package installs reject shell metacharacters and flag-like names.
describe('POST /workspace/install package validation', () => { const originalWorkspace = CONFIG.WORKSPACE.ENABLED; const originalContainers = CONFIG.CONTAINERS.ENABLED;
beforeEach(() => { CONFIG.WORKSPACE.ENABLED = true; CONFIG.CONTAINERS.ENABLED = true; });
afterEach(() => { CONFIG.WORKSPACE.ENABLED = originalWorkspace; CONFIG.CONTAINERS.ENABLED = originalContainers; });
async function install(packages: any[], manager = 'npm') { return app.fetch(new Request('http://substrate.local/workspace/install', { method: 'POST', headers: { 'x-substrate-key': CONFIG.API_KEY, 'content-type': 'application/json', }, body: JSON.stringify({ packages, manager }), })); }
it('rejects newline-embedded package names', async () => { const res = await install(['pkg1\nrm -rf /']); expect(res.status).toBe(400); await expect(res.json()).resolves.toMatchObject({ error: expect.stringContaining('Invalid package') }); });
it('rejects semicolon-injection attempts', async () => { const res = await install(['pkg;whoami']); expect(res.status).toBe(400); });
it('rejects spaces embedded in package names', async () => { const res = await install(['pkg other']); expect(res.status).toBe(400); });
it('rejects path traversal attempts (apk)', async () => { const res = await install(['../etc/passwd'], 'apk'); expect(res.status).toBe(400); });
it('rejects leading-dash flag-like names (npm)', async () => { const res = await install(['--registry=http://evil'], 'npm'); expect(res.status).toBe(400); });
it('rejects leading-dash flag-like names (pip)', async () => { const res = await install(['--index-url=http://evil'], 'pip'); expect(res.status).toBe(400); });
it('accepts a scoped npm package with a version range', async () => { // Validation should pass; the exec may fail without a running container. const res = await install(['@scope/foo@^1.2.3'], 'npm'); expect([200, 500, 502, 503]).toContain(res.status); });
it('accepts a normal pip version pin', async () => { const res = await install(['requests==2.31.0'], 'pip'); expect([200, 500, 502, 503]).toContain(res.status); });});