-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniff.cjs
70 lines (60 loc) · 1.71 KB
/
sniff.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require("fs").promises;
const assert = require("assert");
main().catch(console.error);
async function main() {
const packageJson = JSON.parse((await fs.readFile("./package.json")).toString());
const viteVersion = getViteVersion(packageJson);
if (!lessOrEqual(viteVersion, [2, 9, 9])) {
console.log("Vite.js verion " + viteVersion.join(".") + " is not vulnerable.");
return;
} else {
console.log("Vite.js verion " + viteVersion.join(".") + " is vulnerable. Perform upgrade.");
const type = getUpgradeType(packageJson);
console.log("Upgrade type:", type);
}
}
function getViteVersion(packageJson) {
let vite = getDep('vite', packageJson);
if (!vite) {
return null;
}
let version = vite;
if (version[0] === '^') {
version = version.substring(1);
}
return version.split(".").map(Number);
}
function lessOrEqual(subject, target) {
assert.equal(subject.length, 3);
assert.equal(subject.length, target.length);
for (let i = 0; i < 3; i++) {
if (subject[i] < target[i]) {
return true;
} else if (subject[i] === target[i]) {
continue;
} else {
return false;
}
}
return true;
}
function getDep(dep, packageJson) {
if (packageJson.devDependencies && dep in packageJson.devDependencies) {
return packageJson.devDependencies[dep];
}
if (packageJson.dependencies && dep in packageJson.dependencies) {
return packageJson.dependencies[dep];
}
return null;
}
function getUpgradeType(packageJson) {
if (getDep('svelte', packageJson)) {
return 'svelte';
} else if (getDep("@vitejs/plugin-vue", packageJson)) {
return 'vue';
} else if (getDep('react', packageJson)) {
return 'react';
} else {
return 'vanilla';
}
}