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

Configurable transforms #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
21 changes: 19 additions & 2 deletions src/evaluators.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
} from './commons';
import { getOptimizableGlobals } from './optimizer';
import { createScopeHandler } from './scopeHandler';
import { rejectDangerousSourcesTransform } from './sourceParser';
import {
rejectImportExpressionsTransform,
rejectHtmlCommentsTransform,
rejectSomeDirectEvalExpressionsTransform
} from './sourceParser';
import { assert, throwTantrum } from './utilities';

function buildOptimizer(constants) {
Expand Down Expand Up @@ -87,7 +91,20 @@ export function createSafeEvaluatorFactory(
const localTransforms = options.transforms || [];
const realmTransforms = transforms || [];

const mandatoryTransforms = [rejectDangerousSourcesTransform];
const mandatoryTransforms = [
{ rejectImportExpressions: rejectImportExpressionsTransform },
{ rejectHtmlComments: rejectHtmlCommentsTransform },
{
rejectSomeDirectEvalExpressions: rejectSomeDirectEvalExpressionsTransform
}
].reduce((acc, v) => {
const prop = Object.keys(v)[0];
if (options[prop] !== false) {
acc.push(v[prop]);
}
return acc;
}, []);

const allTransforms = [
...localTransforms,
...realmTransforms,
Expand Down
24 changes: 24 additions & 0 deletions src/sourceParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ function rejectHtmlComments(s) {
}
}

// Export a rewriter transform.
export const rejectHtmlCommentsTransform = {
rewrite(rs) {
rejectHtmlComments(rs.src);
return rs;
}
};

// The proposed dynamic import expression is the only syntax currently
// proposed, that can appear in non-module JavaScript code, that
// enables direct access to the outside world that cannot be
Expand Down Expand Up @@ -62,6 +70,14 @@ function rejectImportExpressions(s) {
}
}

// Export a rewriter transform.
export const rejectImportExpressionsTransform = {
rewrite(rs) {
rejectImportExpressions(rs.src);
return rs;
}
};

// The shim cannot correctly emulate a direct eval as explained at
// https://github.com/Agoric/realms-shim/issues/12
// Without rejecting apparent direct eval syntax, we would
Expand Down Expand Up @@ -91,6 +107,14 @@ function rejectSomeDirectEvalExpressions(s) {
}
}

// Export a rewriter transform.
export const rejectSomeDirectEvalExpressionsTransform = {
rewrite(rs) {
rejectSomeDirectEvalExpressions(rs.src);
return rs;
}
};

export function rejectDangerousSources(s) {
rejectHtmlComments(s);
rejectImportExpressions(s);
Expand Down