forked from JannisX11/blockbench-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
216 lines (195 loc) · 5.62 KB
/
Copy pathvalidate.js
File metadata and controls
216 lines (195 loc) · 5.62 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import vm from "node:vm";
import fs from "node:fs";
import path from "node:path";
import { compareVersions } from "compare-versions";
import PLUGINS_JSON_META from '../plugins.json' with {type: "json"};
import imageSize from "image-size";
function logError(error) {
let prefix = CHANGED_FILES ? '::error::' : '';
console.error(prefix + error);
process.exitCode = 1;
}
let PLUGIN_ID = process.argv[2];
let CHANGED_FILES = process.env.CHANGED_FILES;
// Changed files
if (CHANGED_FILES) {
let changes = CHANGED_FILES.replace(/\//g, '/').split('\n');
if (!PLUGIN_ID) {
for (let path of changes) {
if (path.startsWith('plugins/')) {
PLUGIN_ID = path.split(/[/.]/)[1];
console.log("::debug::Found Plugin ID: "+PLUGIN_ID);
}
}
}
let allowed_paths = [
'plugins.json',
`plugins/${PLUGIN_ID}`,
`src/${PLUGIN_ID}`,
];
for (let path of changes) {
if (!allowed_paths.some(match => path.startsWith(match))) {
logError(`Modifying "${path}" is not permitted as an update for "${PLUGIN_ID}"`);
}
}
}
// ID
if (!PLUGIN_ID) {
logError("Plugin ID is not specified");
process.exit();
}
if (!PLUGIN_ID.match(/^[a-z][a-z0-9_]+$/)) {
logError(`Plugin ID "${PLUGIN_ID}" is not valid snake case`);
}
let json_meta = PLUGINS_JSON_META[PLUGIN_ID];
let id = '';
let source_meta = {};
if (!json_meta) {
logError(`Plugin with ID "${PLUGIN_ID} not found in plugins.json"`);
process.exit();
}
const NEW_FORMAT = (json_meta.min_version && compareVersions(json_meta.min_version, '4.8.0') != -1)
|| json_meta.new_repository_format;
const BASE_PATH = path.join(import.meta.dirname, '..', NEW_FORMAT ? 'plugins/'+PLUGIN_ID : 'plugins');
let content_js = '';
try {
content_js = fs.readFileSync(path.resolve(BASE_PATH, PLUGIN_ID + '.js'));
} catch (err) {
logError("Could not find plugin source file at " + path.resolve(BASE_PATH, PLUGIN_ID + '.js'));
process.exit();
}
// Create sandbox to run plugin without errors
const Plugin = {
register(_id, _options) {
id = _id;
source_meta = _options;
}
}
const wildcard = new Proxy(function () {}, {
get(target, prop) {
if (prop === Symbol.toPrimitive) {
return () => '';
}
if (prop === 'toString') {
return () => '';
}
if (prop === 'valueOf') {
return () => '';
}
return wildcard;
},
apply(target, thisArg, args) {
return wildcard; // calling it returns wildcard
},
construct(target, args) {
return wildcard; // new wildcard() returns wildcard
}
});
// Sandbox that pretends every global exists
const sandbox = new Proxy({
Plugin,
BBPlugin: Plugin,
}, {
has() {
return true; // "yes, this global exists"
},
get(target, prop) {
if (prop in target) return target[prop];
return wildcard;
}
});
vm.createContext(sandbox);
vm.runInContext(content_js, sandbox);
if (!source_meta) {
logError("Could not find metadata in source file");
process.exit();
}
if (id != PLUGIN_ID) {
logError(`Plugin ID "${PLUGIN_ID}" does not match value "${id}"`);
}
// Required fields check
const REQUIRED_FIELDS = ["title", "author", "icon", "description", "version"];
if (REQUIRED_FIELDS.some(key => !json_meta[key])) {
let fields = REQUIRED_FIELDS.filter(key => !json_meta[key]).map(name => `"${name}"`);
logError(`Required fields ${fields.join()} missing in plugins.json entry`);
}
// Metadata match
const KNOWN_FIELDS = [
"title",
"icon",
"author",
"description",
"about",
"tags",
"items",
"version",
"variant",
"min_version",
"deprecation_note",
"website",
"repository",
"bug_tracker",
"await_loading",
"creation_date",
];
let all_fields = new Set([...KNOWN_FIELDS, ...Object.keys(json_meta)]);
for (let key of all_fields) {
const a = JSON.stringify(json_meta[key]);
const b = JSON.stringify(source_meta[key]);
if (a != b) {
logError(`Metadata mismatch: "${key}" is set to ${a} in plugins.json and ${b} in JS file`);
}
}
// About
if (NEW_FORMAT && json_meta.about) {
logError(`About text specified in meta data. In format version 4.8 or newer, about text should be in about.md`);
}
// Changelog
if (json_meta.has_changelog && !NEW_FORMAT) {
logError("Changelog is not supported in legacy format");
}
if (json_meta.has_changelog) {let content_js = '';
let changelog_path = path.resolve(BASE_PATH, 'changelog.json');
try {
let changelog_content = fs.readFileSync(changelog_path);
JSON.parse(changelog_content)
} catch (err) {
logError("Could not load changelog: " + err);
process.exit();
}
}
// Icon validation
if (json_meta.icon && (json_meta.icon.endsWith('.png') || json_meta.icon.endsWith('.svg'))) {
let icon_path = path.resolve(BASE_PATH, json_meta.icon);
if (!fs.existsSync(icon_path)) {
logError(`Could not find icon at "${icon_path}"`);
} else {
let buffer = fs.readFileSync(icon_path);
if (buffer.length > 12_000) {
logError(`Icon is too large at ${buffer.length/1000} KB, maximum is 12 KB`);
}
if (json_meta.icon.endsWith('.png')) {
let dimensions = imageSize(buffer);
if (dimensions.width > 96 || dimensions.height > 96) {
logError(`Icon size is larger than the limit of 96x96`);
}
if (dimensions.width != dimensions.height) {
logError(`Icon is not square`);
}
}
}
}
// Semver
//const SEMVER_REGEX = /^\d+\.\d+\.\d+(-[a-z]+\.\d+)?$/;
const SEMVER_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
function validateVersion(v) {
if (!v.match(SEMVER_REGEX)) {
logError(`"${v}" is not a valid version number. See semver.org`)
}
}
validateVersion(json_meta.version);
validateVersion(source_meta.version);
// Pass?
if (!process.exitCode) {
console.log(`Plugin "${PLUGIN_ID}" passed validation with no errors!`);
}