Skip to content

Commit a261746

Browse files
committed
test(compartment-map): add failing test showing failure to validate archive sources during bundling
1 parent c60a84f commit a261746

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

packages/compartment-mapper/test/test-bundle.js

+67
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import fs from 'fs';
33
import url from 'url';
44
import test from 'ava';
55
import vm from 'vm';
6+
import Buffer from 'buffer';
7+
import { writeZip } from '@endo/zip';
68
import {
79
makeBundle,
810
makeSecureBundle,
911
makeSecureBundleFromArchive,
1012
makeArchive,
1113
parseArchive,
1214
} from '../index.js';
15+
import { addSourcesToArchive } from '../src/archive.js';
1316
import { makeReadPowers } from '../node-powers.js';
1417
import { getVmEvalKitUnderLockdown } from './run-in-context.js';
1518

@@ -18,6 +21,7 @@ const fixture = new URL(
1821
import.meta.url,
1922
).toString();
2023

24+
const textEncoder = new TextEncoder();
2125
const { read } = makeReadPowers({ fs, url });
2226

2327
const expectedLog = [
@@ -183,3 +187,66 @@ test('secure bundler safely sandboxes modules', async t => {
183187
t.falsy(vmGlobalThis.pollution);
184188
}
185189
});
190+
191+
test.failing('ensure bundling from archive validates sources', async t => {
192+
const compartmentMapDescriptor = {
193+
entry: {
194+
module: 'main.js',
195+
compartment: 'xyz',
196+
},
197+
compartments: {
198+
xyz: {
199+
name: 'xyz',
200+
label: 'xyz',
201+
location: 'xyz',
202+
modules: {
203+
'main.js': {
204+
location: 'main.js',
205+
parser: 'pre-cjs-json',
206+
},
207+
},
208+
},
209+
},
210+
};
211+
212+
const moduleSource = `} // invalid js`;
213+
const moduleBytes = textEncoder.encode(
214+
JSON.stringify({
215+
imports: [],
216+
exports: [],
217+
reexports: [],
218+
source: moduleSource,
219+
}),
220+
);
221+
222+
const archiveSources = {
223+
xyz: {
224+
'main.js': {
225+
location: 'main.js',
226+
bytes: moduleBytes,
227+
},
228+
},
229+
};
230+
231+
const archive = writeZip();
232+
const compartmentMapBytes = Buffer.from(
233+
JSON.stringify(compartmentMapDescriptor),
234+
'utf8',
235+
);
236+
await archive.write('compartment-map.json', compartmentMapBytes);
237+
await addSourcesToArchive(archive, archiveSources);
238+
const archiveBytes = await archive.snapshot();
239+
240+
const fakeArchiveLocation = new URL('app.agar', import.meta.url).toString();
241+
const readWithArchive = async path => {
242+
if (path === fakeArchiveLocation) {
243+
return archiveBytes;
244+
}
245+
throw new Error(`unexpected read: ${path}`);
246+
};
247+
248+
// should validate that the module source is valid
249+
await t.throwsAsync(async () => {
250+
await makeSecureBundleFromArchive(readWithArchive, fakeArchiveLocation);
251+
});
252+
});

0 commit comments

Comments
 (0)