Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundle-source): apply eval evasion for cjs modules too #2007

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions packages/bundle-source/src/zip-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,35 @@ export async function bundleZipBase64(
};
}

/**
* @param {import('@endo/compartment-mapper/src/types.js').Language} parser
* @param {Uint8Array} sourceBytes
* @param {string} specifier
* @param {string} location
* @param {string|import('source-map').RawSourceMap} sourceMap
*/
const transformModuleSource = async (
parser,
sourceBytes,
specifier,
location,
sourceMap,
) => {
if (!['mjs', 'cjs'].includes(parser)) {
throw Error(`Parser ${parser} not supported in evadeEvalCensor`);
}
const babelSourceType = parser === 'mjs' ? 'module' : 'script';
kumavis marked this conversation as resolved.
Show resolved Hide resolved
const source = textDecoder.decode(sourceBytes);
let object;
({ code: object, map: sourceMap } = await evadeCensor(source, {
sourceType: babelSourceType,
sourceMap,
sourceMapUrl: new URL(specifier, location).href,
}));
const objectBytes = textEncoder.encode(object);
return { bytes: objectBytes, parser, sourceMap };
};

const { bytes, sha512 } = await makeAndHashArchive(powers, entry, {
dev,
moduleTransforms: {
Expand All @@ -118,15 +147,28 @@ export async function bundleZipBase64(
_packageLocation,
{ sourceMap },
) {
const source = textDecoder.decode(sourceBytes);
let object;
({ code: object, map: sourceMap } = await evadeCensor(source, {
return transformModuleSource(
'mjs',
sourceBytes,
specifier,
location,
sourceMap,
);
},
async cjs(
sourceBytes,
specifier,
location,
_packageLocation,
{ sourceMap },
) {
return transformModuleSource(
'cjs',
sourceBytes,
specifier,
location,
sourceMap,
sourceUrl: new URL(specifier, location).href,
sourceType: 'module',
}));
const objectBytes = textEncoder.encode(object);
return { bytes: objectBytes, parser: 'mjs', sourceMap };
);
},
},
sourceMapHook(sourceMap, sourceDescriptor) {
Expand Down
Loading