Files
Shadowbroker e2473e341f Merge pull request #766 from BigBodyCobain/chore/version-0.9.85
Prepare v0.9.85: version bump and what's new popup
2026-12-02 17:19:11 -07:00

164 lines
6.3 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const test = require('node:test');
const {
assertPortableWindowsBundle,
configureEmbeddedPythonPath,
shouldCopyBackendPath,
shouldCopySitePackagePath,
} = require('./build-backend-runtime.cjs');
const repoRoot = path.resolve(__dirname, '..', '..', '..');
const backendDir = path.join(repoRoot, 'backend');
test('Windows staging never copies a virtualenv or its marker', () => {
assert.equal(
shouldCopyBackendPath(path.join(backendDir, 'venv', 'pyvenv.cfg'), 'win32', 'venv'),
false,
);
assert.equal(
shouldCopyBackendPath(path.join(backendDir, '.venv', 'Scripts', 'python.exe'), 'win32'),
false,
);
assert.equal(
shouldCopyBackendPath(path.join(backendDir, '.venv-dir'), 'win32', 'custom-venv'),
false,
);
assert.equal(
shouldCopyBackendPath(path.join(backendDir, 'services', 'config.py'), 'win32', 'venv'),
true,
);
});
test('site-packages staging strips host-bound path files and editable metadata', () => {
const sitePackages = path.join('C:', 'build', 'site-packages');
assert.equal(
shouldCopySitePackagePath(sitePackages, path.join(sitePackages, 'host-path.pth')),
false,
);
assert.equal(
shouldCopySitePackagePath(
sitePackages,
path.join(sitePackages, 'backend-0.9.85.dist-info', 'METADATA'),
),
false,
);
assert.equal(
shouldCopySitePackagePath(sitePackages, path.join(sitePackages, 'fastapi', '__init__.py')),
true,
);
});
test('embedded Python search paths are relative to the packaged runtime', () => {
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'sb-python-pth-test-'));
try {
const pthPath = path.join(temp, 'python311._pth');
fs.writeFileSync(pthPath, 'C:\\Users\\developer\\Python311\\python311.zip\n', 'utf8');
configureEmbeddedPythonPath(temp);
const configured = fs.readFileSync(pthPath, 'utf8');
assert.doesNotMatch(configured, /[A-Za-z]:\\/);
assert.match(configured, /^python311\.zip$/m);
assert.match(configured, /^Lib\\site-packages$/m);
assert.match(configured, /^\.\.$/m);
assert.match(configured, /^import site$/m);
} finally {
fs.rmSync(temp, { recursive: true, force: true });
}
});
test('bundle validation rejects pyvenv.cfg and developer home paths', () => {
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'sb-python-portability-test-'));
const buildInfo = {
prefix: 'C:\\Users\\developer\\Shadowbroker\\backend\\venv',
base_prefix: 'C:\\Users\\developer\\Python311',
};
try {
fs.writeFileSync(path.join(temp, 'safe.txt'), 'portable runtime\n', 'utf8');
assert.doesNotThrow(() => assertPortableWindowsBundle(temp, buildInfo));
fs.writeFileSync(
path.join(temp, 'pyvenv.cfg'),
'home = C:\\Users\\developer\\Python311\n',
'utf8',
);
assert.throws(
() => assertPortableWindowsBundle(temp, buildInfo),
/Non-portable virtualenv metadata/,
);
fs.rmSync(path.join(temp, 'pyvenv.cfg'));
fs.writeFileSync(
path.join(temp, 'leak.txt'),
`executable = ${buildInfo.base_prefix}\\python.exe\n`,
'utf8',
);
assert.throws(
() => assertPortableWindowsBundle(temp, buildInfo),
/Build-machine path leaked/,
);
} finally {
fs.rmSync(temp, { recursive: true, force: true });
}
});
test('only git-tracked backend files and node_modules are staged', () => {
const tracked = new Set(['services', path.join('services', 'config.py'), 'main.py']);
const copies = (rel) =>
shouldCopyBackendPath(path.join(backendDir, rel), 'linux', 'venv', tracked);
assert.equal(copies('main.py'), true);
assert.equal(copies(path.join('services', 'config.py')), true);
assert.equal(copies(path.join('node_modules', 'ws', 'index.js')), true);
// Local leftovers that are not in git never ship.
assert.equal(copies(path.join('graphify-out', 'graph.json')), false);
assert.equal(copies(path.join('venv-nov13', 'pyvenv.cfg')), false);
assert.equal(copies(path.join('services', 'ais_cache.json')), false);
assert.equal(copies('notes.md'), false);
});
test('graphify output is never staged even if it were tracked', () => {
const tracked = new Set(['graphify-out', path.join('graphify-out', 'graph.json')]);
assert.equal(
shouldCopyBackendPath(path.join(backendDir, 'graphify-out', 'graph.json'), 'linux', 'venv', tracked),
false,
);
});
test('the leak check also finds escaped, binary and large-file paths', () => {
const { findBuildMachinePaths, buildMachinePaths } = require('./build-privacy.cjs');
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'sb-privacy-scan-test-'));
const home = path.win32.join('C:', 'Users', 'developer');
const needles = buildMachinePaths([], path.win32.join('D:', 'src', 'Shadowbroker'), home);
const write = (name, data) => fs.writeFileSync(path.join(temp, name), data);
try {
write('clean.txt', 'nothing here\n');
write('json.json', JSON.stringify({ path: path.win32.join(home, 'Shadowbroker', 'main.py') }));
write('slash.js', 'x("c:/users/developer/.cargo/registry/src/lib.rs")');
write('binary.dll', Buffer.concat([Buffer.from([0, 1, 2]), Buffer.from(path.win32.join(home, 'lib.rs'))]));
write('wide.exe', Buffer.from(path.win32.join(home, 'tool'), 'utf16le'));
write('big.bin', Buffer.concat([Buffer.alloc(3 * 1024 * 1024), Buffer.from(home)]));
const leaks = findBuildMachinePaths([temp], needles).map((file) => path.basename(file)).sort();
assert.deepEqual(leaks, ['big.bin', 'binary.dll', 'json.json', 'slash.js', 'wide.exe']);
} finally {
fs.rmSync(temp, { recursive: true, force: true });
}
});
test('Rust builds remap the home folder and checkout out of binaries', () => {
const { remapRustEnv } = require('./build-privacy.cjs');
const env = remapRustEnv({ RUSTFLAGS: '-C target-cpu=native' }, '/home/dev/Shadowbroker', '/home/dev');
const flags = env.CARGO_ENCODED_RUSTFLAGS.split('\x1f');
assert.equal(env.RUSTFLAGS, undefined);
assert.deepEqual(flags.slice(0, 2), ['-C', 'target-cpu=native']);
assert.ok(flags.includes(`--remap-path-prefix=${path.resolve('/home/dev')}=~`));
// The checkout is inside home, so its remap must come last to win.
assert.equal(flags.at(-1), `--remap-path-prefix=${path.resolve('/home/dev/Shadowbroker')}=shadowbroker`);
});