-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathexternal-plugin-validation.mjs
More file actions
401 lines (331 loc) · 11.7 KB
/
external-plugin-validation.mjs
File metadata and controls
401 lines (331 loc) · 11.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import fs from "fs";
import path from "path";
import { ROOT_FOLDER } from "./constants.mjs";
export const EXTERNAL_PLUGINS_FILE = path.join(ROOT_FOLDER, "plugins", "external.json");
export const EXTERNAL_PLUGIN_POLICIES = Object.freeze({
marketplace: Object.freeze({
allowedSourceTypes: ["github"],
requireAuthor: true,
requireRepository: true,
requireKeywords: true,
requireLicense: false,
requireImmutableRef: false,
}),
publicSubmission: Object.freeze({
allowedSourceTypes: ["github"],
requireAuthor: true,
requireRepository: true,
requireKeywords: true,
requireLicense: true,
requireImmutableRef: true,
}),
});
const EXTERNAL_PLUGIN_ROOT_MANIFEST_PATHS = Object.freeze([
"plugin.json",
".github/plugin/plugin.json",
".plugin/plugin.json",
]);
function resolvePolicy(policy) {
if (!policy) {
return EXTERNAL_PLUGIN_POLICIES.marketplace;
}
if (typeof policy === "string") {
const resolved = EXTERNAL_PLUGIN_POLICIES[policy];
if (!resolved) {
throw new Error(`Unknown external plugin validation policy "${policy}"`);
}
return resolved;
}
return {
...EXTERNAL_PLUGIN_POLICIES.marketplace,
...policy,
};
}
function isNonEmptyString(value) {
return typeof value === "string" && value.trim().length > 0;
}
function validatePluginName(name, prefix, errors) {
if (!isNonEmptyString(name)) {
errors.push(`${prefix}: "name" is required and must be a non-empty string`);
return;
}
if (name.length > 50) {
errors.push(`${prefix}: "name" must be 50 characters or fewer`);
}
if (!/^[a-z0-9-]+$/.test(name)) {
errors.push(`${prefix}: "name" must contain only lowercase letters, numbers, and hyphens`);
}
}
function validateDescription(description, prefix, errors) {
if (!isNonEmptyString(description)) {
errors.push(`${prefix}: "description" is required and must be a non-empty string`);
return;
}
if (description.length > 500) {
errors.push(`${prefix}: "description" must be 500 characters or fewer`);
}
}
function validateVersion(version, prefix, errors) {
if (!isNonEmptyString(version)) {
errors.push(`${prefix}: "version" is required and must be a non-empty string`);
return;
}
if (version.length > 100) {
errors.push(`${prefix}: "version" must be 100 characters or fewer`);
}
}
function validateKeywords(keywords, prefix, errors, warnings, required) {
if (keywords === undefined) {
if (required) {
errors.push(`${prefix}: "keywords" is required and must be an array of lowercase tags`);
}
return;
}
if (!Array.isArray(keywords)) {
errors.push(`${prefix}: "keywords" must be an array`);
return;
}
if (keywords.length > 10) {
errors.push(`${prefix}: "keywords" must contain no more than 10 entries`);
}
for (let i = 0; i < keywords.length; i++) {
const keyword = keywords[i];
if (!isNonEmptyString(keyword)) {
errors.push(`${prefix}: "keywords[${i}]" must be a non-empty string`);
continue;
}
if (!/^[a-z0-9-]+$/.test(keyword)) {
errors.push(`${prefix}: "keywords[${i}]" must contain only lowercase letters, numbers, and hyphens`);
}
if (keyword.length > 30) {
errors.push(`${prefix}: "keywords[${i}]" must be 30 characters or fewer`);
}
}
if (keywords.length === 0) {
if (required) {
errors.push(`${prefix}: "keywords" must contain at least one entry`);
} else {
warnings.push(`${prefix}: "keywords" is empty; at least one keyword is recommended for discovery`);
}
}
}
function validateHttpsUrl(value, fieldName, prefix, errors, options = {}) {
if (!isNonEmptyString(value)) {
errors.push(`${prefix}: "${fieldName}" must be a non-empty string`);
return;
}
let parsed;
try {
parsed = new URL(value);
} catch {
errors.push(`${prefix}: "${fieldName}" must be a valid URL`);
return;
}
if (parsed.protocol !== "https:") {
errors.push(`${prefix}: "${fieldName}" must use https`);
}
if (options.githubOnly && parsed.hostname !== "github.com") {
errors.push(`${prefix}: "${fieldName}" must point to https://github.com/...`);
}
}
function validateAuthor(author, prefix, errors, required) {
if (author === undefined) {
if (required) {
errors.push(`${prefix}: "author" is required`);
}
return;
}
if (!author || typeof author !== "object" || Array.isArray(author)) {
errors.push(`${prefix}: "author" must be an object`);
return;
}
if (!isNonEmptyString(author.name)) {
errors.push(`${prefix}: "author.name" is required and must be a non-empty string`);
}
if (author.url !== undefined) {
validateHttpsUrl(author.url, "author.url", prefix, errors);
}
}
function validateLicense(license, prefix, errors, required) {
if (license === undefined) {
if (required) {
errors.push(`${prefix}: "license" is required`);
}
return;
}
if (!isNonEmptyString(license)) {
errors.push(`${prefix}: "license" must be a non-empty string`);
}
}
function validateRepository(repository, prefix, errors, required) {
if (repository === undefined) {
if (required) {
errors.push(`${prefix}: "repository" is required`);
}
return;
}
validateHttpsUrl(repository, "repository", prefix, errors, { githubOnly: true });
}
function validateHomepage(homepage, prefix, errors) {
if (homepage === undefined) {
return;
}
validateHttpsUrl(homepage, "homepage", prefix, errors);
}
function formatExpectedPluginRootMessage() {
return EXTERNAL_PLUGIN_ROOT_MANIFEST_PATHS.map((manifestPath) => `"${manifestPath}"`).join(", ");
}
function validateRelativePath(pathValue, prefix, errors) {
if (!isNonEmptyString(pathValue)) {
errors.push(`${prefix}: "source.path" must be a non-empty string when provided`);
return;
}
if (pathValue === "/") {
return;
}
const normalized = path.posix.normalize(pathValue);
const segments = pathValue.split("/");
if (pathValue.startsWith("/") || pathValue.startsWith("../") || normalized !== pathValue || segments.includes("..")) {
errors.push(`${prefix}: "source.path" must be a safe relative path inside the repository`);
}
if (pathValue.includes("\\")) {
errors.push(`${prefix}: "source.path" must use forward slashes`);
}
if (normalized === ".") {
errors.push(`${prefix}: "source.path" must be "/" for the repository root or a plugin root directory relative to the repository root`);
}
if (path.posix.basename(normalized) === "plugin.json") {
errors.push(
`${prefix}: "source.path" must point to the plugin root directory, not the manifest file; relative to "source.path", expected one of ${formatExpectedPluginRootMessage()}`
);
}
}
function validateImmutableRef(ref, prefix, errors) {
if (!isNonEmptyString(ref)) {
errors.push(`${prefix}: "source.ref" must be a non-empty string when provided`);
return;
}
if (ref.startsWith("refs/heads/")) {
errors.push(`${prefix}: "source.ref" must be a tag or commit SHA, not a branch ref`);
return;
}
if (["main", "master", "develop", "development", "dev", "trunk"].includes(ref)) {
errors.push(`${prefix}: "source.ref" must be a tag or commit SHA, not a branch name`);
}
if (ref.startsWith("refs/") && !ref.startsWith("refs/tags/")) {
errors.push(`${prefix}: "source.ref" must be a tag ref or commit SHA`);
}
}
function validateGitHubSource(source, prefix, errors, requireImmutableRef) {
if (!source || typeof source !== "object" || Array.isArray(source)) {
errors.push(`${prefix}: "source" must be an object`);
return;
}
if (source.source !== "github") {
errors.push(`${prefix}: "source.source" must be "github"`);
}
if (!isNonEmptyString(source.repo)) {
errors.push(`${prefix}: "source.repo" is required and must be a non-empty string`);
} else if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(source.repo)) {
errors.push(`${prefix}: "source.repo" must be in "owner/repo" format`);
}
if (source.path !== undefined) {
validateRelativePath(source.path, prefix, errors);
}
if (source.ref !== undefined) {
validateImmutableRef(source.ref, prefix, errors);
} else if (requireImmutableRef) {
errors.push(`${prefix}: "source.ref" is required for public external plugin submissions`);
}
}
export function validateExternalPlugin(plugin, index, options = {}) {
const policy = resolvePolicy(options.policy ?? options);
const errors = [];
const warnings = [];
const prefix = `external.json[${index}]`;
if (!plugin || typeof plugin !== "object" || Array.isArray(plugin)) {
return {
errors: [`${prefix}: entry must be an object`],
warnings,
};
}
validatePluginName(plugin.name, prefix, errors);
validateDescription(plugin.description, prefix, errors);
validateVersion(plugin.version, prefix, errors);
validateAuthor(plugin.author, prefix, errors, policy.requireAuthor);
validateRepository(plugin.repository, prefix, errors, policy.requireRepository);
validateHomepage(plugin.homepage, prefix, errors);
validateLicense(plugin.license, prefix, errors, policy.requireLicense);
validateKeywords(plugin.keywords ?? plugin.tags, prefix, errors, warnings, policy.requireKeywords);
if (plugin.tags !== undefined && plugin.keywords === undefined) {
warnings.push(`${prefix}: prefer "keywords" over legacy "tags"`);
}
if (!plugin.source) {
errors.push(`${prefix}: "source" is required`);
} else if (typeof plugin.source === "string") {
errors.push(`${prefix}: "source" must be an object (local file paths are not allowed for external plugins)`);
} else if (!policy.allowedSourceTypes.includes(plugin.source.source)) {
errors.push(`${prefix}: "source.source" must be one of: ${policy.allowedSourceTypes.join(", ")}`);
} else if (plugin.source.source === "github") {
validateGitHubSource(plugin.source, prefix, errors, policy.requireImmutableRef);
}
return { errors, warnings };
}
export function validateExternalPlugins(plugins, options = {}) {
const policy = resolvePolicy(options.policy ?? options);
const errors = [];
const warnings = [];
const localNames = new Map(
(options.localPluginNames ?? []).map((name) => [String(name).toLowerCase(), String(name)])
);
const seenExternalNames = new Map();
if (!Array.isArray(plugins)) {
return {
errors: ["external.json must contain an array"],
warnings,
};
}
plugins.forEach((plugin, index) => {
const result = validateExternalPlugin(plugin, index, { policy });
errors.push(...result.errors);
warnings.push(...result.warnings);
if (!isNonEmptyString(plugin?.name)) {
return;
}
const normalizedName = plugin.name.toLowerCase();
const duplicateIndex = seenExternalNames.get(normalizedName);
if (duplicateIndex !== undefined) {
errors.push(`external.json[${index}]: duplicate plugin name "${plugin.name}" already used by external.json[${duplicateIndex}]`);
} else {
seenExternalNames.set(normalizedName, index);
}
const localDuplicate = localNames.get(normalizedName);
if (localDuplicate) {
errors.push(`external.json[${index}]: plugin name "${plugin.name}" conflicts with local plugin "${localDuplicate}"`);
}
});
return { errors, warnings };
}
export function readExternalPlugins(options = {}) {
const filePath = options.filePath ?? EXTERNAL_PLUGINS_FILE;
if (!fs.existsSync(filePath)) {
return {
plugins: [],
errors: [],
warnings: [],
};
}
let plugins;
try {
const content = fs.readFileSync(filePath, "utf8");
plugins = JSON.parse(content);
} catch (error) {
return {
plugins: [],
errors: [`Error reading ${path.basename(filePath)}: ${error.message}`],
warnings: [],
};
}
const { errors, warnings } = validateExternalPlugins(plugins, options);
return { plugins, errors, warnings };
}