Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
fix: sanitize language version before passing it to acorn
Browse files Browse the repository at this point in the history
  • Loading branch information
lachrist committed Jun 7, 2022
1 parent ddbb914 commit 2595030
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
3 changes: 2 additions & 1 deletion components/instrumentation/default/.test.list
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- visit.mjs
- * visit.mjs
* version.mjs
- index.mjs
7 changes: 3 additions & 4 deletions components/instrumentation/default/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Escodegen from "escodegen";
import * as Acorn from "acorn";
import Version from "./version.mjs";

const { generate: generateEstree } = Escodegen;
const { parse: parseEstree } = Acorn;
Expand All @@ -18,6 +19,7 @@ export default (dependencies) => {
source: { getSources },
} = dependencies;
const { visit } = Visit(dependencies);
const { getEcmaVersion } = Version(dependencies);
const getHead = generateGet("head");
const getBody = generateGet("body");
const getURL = generateGet("url");
Expand Down Expand Up @@ -84,10 +86,7 @@ export default (dependencies) => {
allowHashBang: true,
sourceType: type,
allowAwaitOutsideFunction: type === "module",
ecmaVersion:
configuration.language.version === null
? "latest"
: parseInt(configuration.language.version),
ecmaVersion: getEcmaVersion(configuration.language),
locations: true,
}),
"failed to parse file %j >> %O",
Expand Down
50 changes: 50 additions & 0 deletions components/instrumentation/default/version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export default (dependencies) => {
const {
log: { logGuardWarning, logWarning },
} = dependencies;
const names = ["ecmascript", "javascript", "js"];
const versions = [
"3",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
];
return {
getEcmaVersion: ({ name, version }) => {
if (names.includes(name)) {
if (versions.includes(version)) {
return parseInt(version);
} else {
logGuardWarning(
version !== null && version !== "latest",
"Expected language version to be one of %j, but got: %j.",
versions,
version,
);
return "latest";
}
} else {
logWarning(
"Expected language name to be one of %j, but got: %j.",
names,
name,
);
return "latest";
}
},
};
};
14 changes: 14 additions & 0 deletions components/instrumentation/default/version.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assertEqual } from "../../__fixture__.mjs";
import { buildTestDependenciesAsync } from "../../build.mjs";
import Version from "./version.mjs";

Error.stackTraceLimit = Infinity;

const dependencies = await buildTestDependenciesAsync(import.meta.url);
const { getEcmaVersion } = Version(dependencies);

assertEqual(getEcmaVersion({ name: "foo", version: "2020" }), "latest");

assertEqual(getEcmaVersion({ name: "ecmascript", version: "2020" }), 2020);

assertEqual(getEcmaVersion({ name: "javascript", version: "foo" }), "latest");

0 comments on commit 2595030

Please sign in to comment.